-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.c
257 lines (236 loc) · 7.55 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* \mainpage lci Documentation
*
* \section license License
*
* lci - a LOLCODE interpreter written in C.
* Copyright (C) 2010-2014 Justin J. Meza
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* \section maintainer Maintainer
*
* - The lead maintainer for this project is Justin J. Meza
* (justin.meza@gmail.com).
*
* - For more information, check this project's webpage at
* http://icanhaslolcode.org .
*
* \section about About
*
* lci is a LOLCODE interpreter written in C and is designed to be correct,
* portable, fast, and precisely documented.
*
* - correct: Every effort has been made to test lci's conformance to the
* LOLCODE language specification. Unit tests come packaged with the lci
* source code.
*
* - portable: lci follows the widely ported ANSI C specification allowing it
* to compile on a broad range of systems.
*
* - fast: Much effort has gone into producing simple and efficient code
* whenever possible to the extent that the above points are not compromized.
*
* - precisely documented: lci uses Doxygen to generate literate code
* documentation, browsable here.
*
* \section organization Organization
*
* lci employs several different modules which each perform a specific task
* during interpretation of code:
*
* - \b lexer (lexer.c, lexer.h)- The lexer takes an array of characters and
* splits it up into individual \e lexemes. Lexemes are divided by whitespace
* and other rules of the language.
*
* - \b tokenizer (tokenizer.c, tokenizer.h) - The tokenizer takes the output
* of the lexer and converts it into individual \e tokens. Tokens are
* different from lexemes in that a single token may be made up of multiple
* lexemes. Also, the contents of some tokens are evaluated (such as integers
* and floats) for later use.
*
* - \b parser (parser.c, parser.h) - The parser takes the output of the
* tokenizer and analyzes it semantically to turn it into a parse tree.
*
* - \b interpreter (interpreter.c, interpreter.h) - The interpreter takes the
* output of the parser and executes it.
*
* Each of these modules is contained within its own C header and source code
* files of the same name.
*
* To handle the conversion of Unicode code points and normative names to bytes,
* two additional files, unicode.c and unicode.h are used.
*
* Finally, main.c ties all of these modules together and handles the initial
* loading of input data for the lexer.
*/
/**
* \page varscope Variable Scope
*
* The specification states that variables are local to the scope of the main
* block or any function they are contained within--except for temporary loop
* variables which are local to the loop they are instantiated within. This
* behavior, combined with the fact that variables must be declared before being
* used, means that variables may not be shadowed in different control scopes
* (such as loops and conditional statements) and, more importantly, programmers
* must keep track of whether variables have been previously declared within
* conditionally executed code (for example, under this scoping if a variable is
* declared in a conditional block it cannot be safely used in later code).
*
* One advantage of a flat scoping scheme is that nearly everything can be
* stored in a single structure, making lookups faster. However, I believe that
* this advantage is not worth the extra frustration transferred to the
* programmer and so scoping in lci is done in a similar manner to other
* programming languages, to wit, within
*
* - the main block of code,
* - the body of functions,
* - the body of loop statements, and
* - the bodies of conditional statements.
*
* This should alleviate any confusion which may have been caused by using a
* completely local free-for-all scope. Also, there seems to be a general
* consensus on the LOLCODE forums that this is the way to go and flat scoping
* causes too many problems for the programmer.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "lexer.h"
#include "tokenizer.h"
#include "parser.h"
#include "interpreter.h"
#include "error.h"
#define READSIZE 512
static char *program_name;
static char *shortopt = "hv";
static struct option longopt[] = {
{ "help", no_argument, NULL, (int)'h' },
{ "version", no_argument, NULL, (int)'v' },
{ 0, 0, 0, 0 }
};
static void help(void) {
fprintf(stderr, "\
Usage: %s [FILE] ... \n\
Interpret FILE(s) as LOLCODE. Let FILE be '-' for stdin.\n\
-h, --help\t\toutput this help\n\
-v, --version\t\tprogram version\n", program_name);
}
static void version (char *revision) {
fprintf(stderr, "%s %s\n", program_name, revision);
}
int main(int argc, char **argv)
{
unsigned int size = 0;
unsigned int length = 0;
char *buffer = NULL;
LexemeList *lexemes = NULL;
Token **tokens = NULL;
MainNode *node = NULL;
char *fname = NULL;
FILE *file = NULL;
int ch;
char *revision = "v0.10.5";
program_name = argv[0];
while ((ch = getopt_long(argc, argv, shortopt, longopt, NULL)) != -1) {
switch (ch) {
default:
help();
exit(EXIT_FAILURE);
case 'h':
help();
exit(EXIT_SUCCESS);
case 'v':
version(revision);
exit(EXIT_SUCCESS);
}
}
for (; optind < argc; optind++) {
size = length = 0;
buffer = fname = NULL;
lexemes = NULL;
tokens = NULL;
node = NULL;
file = NULL;
if (!strncmp(argv[optind],"-\0",2)) {
file = stdin;
fname = "stdin";
}
else {
file = fopen(argv[optind], "r");
fname = argv[optind];
}
if (!file) {
error(MN_ERROR_OPENING_FILE, argv[optind]);
return 1;
}
while (!feof(file)) {
size += READSIZE;
buffer = realloc(buffer, sizeof(char) * size);
length += fread((buffer + size) - READSIZE,
1,
READSIZE,
file);
}
if (fclose(file) != 0) {
error(MN_ERROR_CLOSING_FILE, argv[optind]);
if (buffer) free(buffer);
return 1;
}
if (!buffer) return 1;
buffer[length] = '\0';
/* Remove hash bang line if run as a standalone script */
if (buffer[0] == '#' && buffer[1] == '!') {
unsigned int n;
for (n = 0; buffer[n] != '\n' && buffer[n] != '\r'; n++)
buffer[n] = ' ';
}
/*
* Remove UTF-8 BOM if present and add it to the output stream
* (we assume here that if a BOM is present, the system will
* also expect the output to include a BOM).
*/
if (buffer[0] == (char)0xef
|| buffer[1] == (char)0xbb
|| buffer[2] == (char)0xbf) {
buffer[0] = ' ';
buffer[1] = ' ';
buffer[2] = ' ';
printf("%c%c%c", 0xef, 0xbb, 0xbf);
}
/* Begin main pipeline */
if (!(lexemes = scanBuffer(buffer, length, fname))) {
free(buffer);
return 1;
}
free(buffer);
if (!(tokens = tokenizeLexemes(lexemes))) {
deleteLexemeList(lexemes);
return 1;
}
deleteLexemeList(lexemes);
if (!(node = parseMainNode(tokens))) {
deleteTokens(tokens);
return 1;
}
deleteTokens(tokens);
if (interpretMainNode(node)) {
deleteMainNode(node);
return 1;
}
deleteMainNode(node);
/* End main pipeline */
}
return 0;
}