-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
395 lines (315 loc) · 10.3 KB
/
Parser.cpp
File metadata and controls
395 lines (315 loc) · 10.3 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "Parser.h"
inline bool Parser::isskipable(char &c)
{
/* Skip whitespaces, tabs and line breaks */
return (c == ' ' || c == '\n' || c == '\t');
}
Token Parser::gettok()
{
char c;
while ( (c = input_str[input_str_index]) && isskipable(c) ) {
input_str_index++;
if (c == '\n') { // On new lines...
line++; // ...increment the line number
col = 0; // ...reset the column number
} else {
col++;
}
}
col += 2;
if (input_str_index < input_str_length) {
Token tok(input_str[input_str_index], input_str[input_str_index+1]);
input_str_index += 2;
return tok;
}
return tok_unkown;
}
Token Parser::nexttok(bool skipSkipeableCharacters)
{
int tmp_int = input_str_index;
char c;
while ( (c = input_str[tmp_int])
&& (skipSkipeableCharacters && isskipable(c)) ) { tmp_int++; }
if (input_str_index < input_str_length) {
Token tok(input_str[tmp_int], input_str[tmp_int+1]);
return tok;
}
return tok_unkown;
}
Expr /* VarExpr or NamedVarExpr */ * Parser::parseVar(int inversed)
{
string name;
Token tok;
while ( (tok = nexttok()) ) {
if (tok == tok_var_end) { // End of the variable
gettok(); // Eat var end token
break;
} else if (tok == tok_var_start && name.length() == 0) { // Named variable, like `:( :( :$ :) :)'
gettok(); // Eat var begin token
Expr *expr = parseVar();
Token tok = gettok(); // Eat the |tok_var_end| after the variable
// @TODO: Check that |tok| is |tok_var_end|
return new NamedVarExpr(expr, line, col);
} else { // Appends symbol to |name|
name += input_str[input_str_index++];
}
}
return new VarExpr(name, inversed, line, col);
}
InputExpr * Parser::parseInput()
{
int index = 0; // First input by default (index = 0), (because the first ':$' has already been found to call this method)
while (nexttok(false /* Don't skip skipable characters */) == tok_input) {
index++;
gettok(); // Eat the token
}
return new InputExpr(index, line, col);
}
CommentExpr * Parser::parseComment()
{
string str;
char c;
while ( (c = input_str[input_str_index++]) && c != '\n' ) { str += c; }
return new CommentExpr(str, line++, col);
}
InitExpr * Parser::parseInit()
{
Expr *LHS = _lastExpr;
if (!isa<AssignableExpr>(LHS)) {
/* Show an error if |LHS| is not a variable */
Assert("parseInit: LHS is not assignable (" + LHS->DebugString() + ")", LHS->line(), LHS->col());
}
Expr *RHS = parseExpr();
return new InitExpr(LHS, RHS, line, col);
}
/* Used in |parseBinaryOperation()| function to get next expression from a while loop */
Expr * Parser::parseOperand()
{
Token tok = gettok();
Expr *expr = NULL;
if /**/ (tok == tok_var_start || tok == tok_var_not_start) // Parse variable
expr = parseVar(tok == tok_var_not_start);
else if (tok == tok_input) // Parse input
expr = parseInput();
else if (tok == tok_str_length) // Length function
expr = parseLengthFunction();
return expr;
}
BinOpExpr * Parser::parseBinaryOperation(Expr *LHS, Token &op)
{
/* This uses the Shunting Yard Algorithm: http://en.wikipedia.org/wiki/Shunting_yard_algorithm */
list<Token> ops;
list<Expr *> outputRPN;
/* Build the RPN representation of tokens */
outputRPN.push_back(LHS);
ops.push_back(op);
Expr *RHS;
while ( (RHS = parseOperand()) && (op = nexttok()) && op.isOperator() ) { // Until we have comming operations
gettok(); // Eat the operator
outputRPN.push_back(RHS);
Token &lastOp = ops.back();
/* If the precedence of the last operator of the stack if lower (or equal) than the operator to push... */
if (lastOp.getPrecedence() >= op.getPrecedence()) {
/* ...pop all operator until a lower (or equal) precedence is found */
list<Token> ops_copy(ops.begin(), ops.end()); // Copy |ops| to keep sync even after a pop
for (list<Token>::reverse_iterator it = ops_copy.rbegin(); it != ops_copy.rend(); it++) {
Token &lastOp = (*it);
if (op.getPrecedence() > lastOp.getPrecedence()) break;
Expr *tkExpr = new TokenExpr(*it);
outputRPN.push_back(tkExpr);
ops.pop_back();
}
}
ops.push_back(op);
}
/* Push the remaining RHS (out of the while loop) */
outputRPN.push_back(RHS);
/* Add remaining operators at the end of the RPN list (in reversed order) */
for (list<Token>::reverse_iterator it = ops.rbegin(); it != ops.rend(); it++) {
Expr *tkExpr = new TokenExpr((*it));
outputRPN.push_back(tkExpr);
}
/* Transform RPN to binary operators */
list<Expr *> binops;
for (list<Expr *>::iterator it = outputRPN.begin(); it != outputRPN.end(); it++) {
if (!isa<TokenExpr>(*it)) { // If it's an input or variable...
binops.push_back(*it); // ... push it to stack
} else { // Else, it's an operator (as token), create a binary expression
Token tk = (cast<TokenExpr>(*it))->token();
/* Pop the two last items of the stack */
Expr *m = binops.back();
binops.pop_back();
Expr *n = binops.back();
binops.pop_back();
BinOpExpr *expr = new BinOpExpr(n, tk, m, line, col);
binops.push_back(expr);
}
}
return cast<BinOpExpr>(binops.front() /* There *should* be only one item */); // @TODO: Check that is there only one binop into the list
}
PrintExpr * Parser::parsePrint()
{
vector<Expr *> output;
Token tok;
while ( (tok = nexttok()) && tok != tok_print_end ) {
output.push_back(parseExpr());
}
gettok(); // Eat the |tok_print_end|
// @TODO: Show an error if |output| is not printable
return new PrintExpr(output, line, col);
}
HelloPrintExpr * Parser::parseHelloPrint()
{
return new HelloPrintExpr(line, col);
}
NopExpr * Parser::parseNop()
{
return new NopExpr(line, col);
}
ExitExpr * Parser::parseExit()
{
return new ExitExpr(0, line, col);
}
PushExpr * Parser::parsePush()
{
Expr *expr = parseExpr();
return new PushExpr(expr, line, col);
}
PopExpr * Parser::parsePop()
{
Expr *expr = parseExpr();
if (!isa<AssignableExpr>(expr)) {
// @TODO: Show an error if |expr| is nor assignable (variable)
Assert("parsePop: Destination expr is not assignable (" + expr->DebugString() + ")", expr->line(), expr->col());
}
return new PopExpr(expr, line, col);
}
ClearExpr * Parser::parseClear()
{
return new ClearExpr(line, col);
}
LoopExpr * Parser::parseLoop()
{
Expr *condition;
vector<Expr *> thenExprs, thelseExprs;
Token tok;
/* Get the (only) condition expression */
while ( (tok = nexttok()) && tok != tok_loop_then ) {
if (condition)
_lastExpr = condition;
condition = parseExpr();
// @TODO: Show a warning if multiple condition (that can not be appended)
}
gettok(); // Eat the |tok_loop_then| token
/* Get *then* expressions */
while ( (tok = nexttok()) && tok != tok_loop_thelse ) {
Expr *expr = parseExpr();
if (canGen(expr))
thenExprs.push_back(expr);
else
_lastExpr = expr;
}
gettok(); // Eat the |tok_loop_thelse| token
/* Get *thelse* expressions */
while ( (tok = nexttok()) && tok != tok_loop_end ) {
Expr *expr = parseExpr();
if (canGen(expr))
thelseExprs.push_back(expr);
else
_lastExpr = expr;
}
gettok(); // Eat the |tok_loop_end| token
// @TODO: Show an error if |condition| is not a condition (binary op, variable or input)
return new LoopExpr(condition, thenExprs, thelseExprs, line, col);
}
LengthFuncExpr * Parser::parseLengthFunction()
{
Expr *expr = parseExpr();
// @TODO: Show an error if [expr] is not a string
return new LengthFuncExpr(expr, line, col);
}
Expr * Parser::parseExpr()
{
Expr *expr;
Token tok = gettok();
// @TODO: Re-order depending frequency
if (tok == tok_var_start || tok == tok_var_not_start) { // Parse variable
expr = parseVar(tok == tok_var_not_start);
} else if (tok == tok_input) { // Parse input
expr = parseInput();
} else if (tok == tok_equal) { // Parse initialization
return parseInit(); // @TODO: Explain why this return now
} else if (tok == tok_comment) { // Parse comment
expr = parseComment();
} else if (tok == tok_print_start) { // Parse print
expr = parsePrint();
} else if (tok == tok_print_hello) { // Parse hello print
expr = parseHelloPrint();
} else if (tok == tok_nop) { // Parse nop
expr = parseNop();
} else if (tok == tok_exit) { // Parse exit
expr = parseExit();
} else if (tok == tok_stack_push) { // Parse push
expr = parsePush();
} else if (tok == tok_stack_pop) { // Parse pop
expr = parsePop();
} else if (tok == tok_stack_clear) { // Parse clear
expr = parseClear();
} else if (tok == tok_loop_start) { // Parse loop
expr = parseLoop();
} else if (tok == tok_str_length) { // Parse length function
expr = parseLengthFunction();
} else {
expr = new UnkownExpr(tok, line, col);
}
Token op;
if ( (op = nexttok()) && op.isOperator() ) {
gettok(); // Eat the operator
expr = parseBinaryOperation(expr, op);
}
return expr;
}
Parser::Parser(string &s)
{
input_str = s.c_str();
input_str_length = strlen(input_str);
Token tok;
if ( (tok = nexttok()) && (tok == tok_prog_start))
gettok();
while ( (tok = nexttok()) && (tok != tok_prog_end) ) {
Expr *expr = parseExpr();
if (!canGen(expr)) {
_lastExpr = expr;
}
if (isa<UnkownExpr>(expr)) {
// @TODO: Show an error if |error| is unknown
Assert(expr->DebugString(), expr->line(), expr->col());
}
if (expr) {
exprs.push_back(expr);
expr = NULL;
}
}
/*** Uncomment this block to get more debugging information ***
* // This prints all expressions found by the parser
* list<string> lines;
* string line;
* stringstream ss(s);
*
* while ( getline(ss, line, '\n') ) {
* lines.push_back(line);
* }
*
* for (vector<Expr *>::iterator it = exprs.begin(); it != exprs.end(); it++) {
* Expr *expr = *it;
* cout << expr->line() << ":" << expr->col() << ": " << expr->DebugString() << "\n";
*
* list<string>::iterator lines_it = lines.begin();
* advance(lines_it, expr->line());
* cout << *(lines_it) << "\n";
* cout.width(expr->col());
* cout << "^" << "\n";
* cout << "\n";
* }
*/
}