-
Notifications
You must be signed in to change notification settings - Fork 52
/
PyParser.cpp
509 lines (382 loc) · 14.2 KB
/
PyParser.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* File: PyParser.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on February 11, 2013, 10:38 PM
*
* License:
* Please read the LICENSE file in this distribution for details regarding
* the licensing of this code. This code is freely available for educational
* use. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*
* Description:
* The format of programs for the CoCo virtual machine is given by the following
* grammar. This grammar is LL(1) and therefore can be implemented as a top
* down parser. The methods of the PyParser class implement this top-down
* parser. Each non-terminal of the grammar below becomes a function in the
* top-down parser. Non-terminals that appear on the rhs (right-hand side) of a
* rule correspond to function calls. Tokens that appear on the rhs of a rule
* can be retrieved from the scanner by calling getToken on the scanner. Because
* the grammar is LL(1), once in a while a token must be read to look ahead (needed
* in deciding which rule to apply in some cases). In those cases, the token may
* be put back using the putBackToken method. Only one token may be put back
* on the scanner. Calling putBackToken multiple times will only put the last token
* back once (until it is retrieved with getToken again).
*
* Here is the grammar.
* ===========================================================================
* PyAssemblyProg ::= FunctionListPart EOF
* FunctionListPart ::= FunDef FunctionList
* FunctionList ::= FunDef FunctionList | <null>
* FunDef ::= Function colon Identifier slash Integer FunctionList ConstPart LocalsPart
* FreeVarsPart CellVarsPart GlobalsPart BodyPart
* ConstPart ::= <null> | Constants colon ValueList
* ValueList ::= Value ValueRest
* ValueRest ::= comma ValueList | <null>
* Value ::= None | Integer | Float | String (* the Scanner recognizes None as an Identifier *)
* LocalsPart ::= <null> | Locals colon IdList
* FeeVarsPart ::= <null> | FreeVars colon IdList
* CellVarsPart ::= <null> | CellVars colon IdList
* IdList ::= Identifier IdRest
* IdRest ::= comma IdList | <null>
* GlobalsPart ::= <null> | Globals colon IdList
* BodyPart ::= BEGIN InstructionList END
* InstructionList ::= <null> | LabeledInstruction InstructionList
* LabeledInstruction ::= Identifier colon LabeledInstruction | Instruction | OpInstruction
* Instruction ::= STOP_CODE | NOP | POP_TOP | ROT_TWO | ROT_THREE | ...
* OpInstruction ::= OpMnemonic Integer | OpMnemonic Identifier
* OpMnemonic ::= LOAD_CONST | STORE_FAST | SETUP_LOOP | COMPARE_OP | POP_JUMP_IF_FALSE | ...
* ============================================================================
* The implementation below cheats a little bit and combines the LabeledInstruction,
* UnaryInstruction, BinaryInstruction, and BinaryMneomic non-terminals into
* one function (the LabeledInstruction function).
*
*/
#include "PyParser.h"
#include "PyObject.h"
#include "PyException.h"
#include "PyInt.h"
#include "PyFloat.h"
#include "PyStr.h"
#include "PyBool.h"
#include "PyNone.h"
#include "PyUtil.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
void badToken(PyToken* tok, string message) {
cerr << "*********************************************************" << endl;
cerr << " A Parse Exception Occurred" << endl;
cerr << "*********************************************************" << endl;
cerr << "Bad token '" << tok->getLex() << "' found at line " << tok->getLine() << " and column " << tok->getCol() << "." << endl;
cerr << "Token type was " << tok->getType() << "." << endl;
cerr << message << endl;
exit(0);
}
vector<PyCode*>* PyParser::parse() {
try {
return PyAssemblyProg();
} catch (PyException* ex) {
in->putBackToken();
PyToken* tok = in->getToken();
cerr << "*********************************************************" << endl;
cerr << " A Parse Exception Occurred" << endl;
cerr << "*********************************************************" << endl;
cerr << "The exception occurred at line " << tok->getLine() << " and column " << tok->getCol() << "." << endl;
cerr << "The exception was: " << ex->toString() << endl;
exit(0);
}
}
PyParser::PyParser(PyScanner* in) {
this->in = in;
}
PyParser::PyParser(const PyParser& orig) {
this->in = orig.in;
}
PyParser::~PyParser() {
try {
delete in;
} catch (...) {
}
}
vector<PyCode*>* PyParser::PyAssemblyProg() {
vector<PyCode*>* code = FunctionListPart();
PyToken* tok = in->getToken();
if (tok->getType() != PYEOFTOKEN)
badToken(tok, "Expected End Of File marker.");
return code;
}
vector<PyCode*>* PyParser::FunctionListPart() {
PyCode* code = FunDef();
vector<PyCode*>* vec = new vector<PyCode*>();
vec->push_back(code);
vec = FunctionList(vec);
return vec;
}
vector<PyCode*>* PyParser::FunctionList(vector<PyCode*>* vec) {
PyToken* tok = in->getToken();
in->putBackToken();
if (tok->getLex() == "Function") {
PyCode* code = FunDef();
vec->push_back(code);
vec = FunctionList(vec);
}
return vec;
}
PyCode* PyParser::FunDef() {
PyToken* tok = in->getToken();
if (tok->getLex() != "Function")
badToken(tok, "Expected Function keyword.");
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a colon.");
PyToken* funName = in->getToken();
if (funName->getType() != PYIDENTIFIERTOKEN)
badToken(funName, "Expected an identifier.");
tok = in->getToken();
if (tok->getLex() != "/")
badToken(tok, "Expected a '/'.");
PyToken* numArgsToken = in->getToken();
int numArgs;
if (numArgsToken->getType() != PYINTEGERTOKEN)
badToken(numArgsToken, "Expected an integer for the argument count.");
istringstream numArgsIn(numArgsToken->getLex(), istringstream::in);
numArgsIn.exceptions(ios_base::failbit | ios_base::badbit);
numArgsIn >> numArgs;
vector<PyCode*>* nestedFunctions = new vector<PyCode*>();
nestedFunctions = FunctionList(nestedFunctions);
vector<PyObject*>* constants = ConstPart(nestedFunctions);
vector<string>* locals = LocalsPart();
vector<string>* freevars = FreeVarsPart();
vector<string>* cellvars = CellVarsPart();
vector<string>* globals = GlobalsPart();
vector<PyByteCode*>* instructions = BodyPart();
return new PyCode(funName->getLex(), nestedFunctions, constants, locals, freevars, cellvars, globals, instructions, numArgs);
}
vector<PyObject*>* PyParser::ConstPart(vector<PyCode*>* nestedFunctions) {
vector<PyObject*>* constants = new vector<PyObject*>();
PyToken* tok = in->getToken();
if (tok->getLex() != "Constants") {
in->putBackToken();
return constants;
}
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a ':'.");
constants = ValueList(constants, nestedFunctions);
return constants;
}
vector<PyObject*>* PyParser::ValueList(vector<PyObject*>* constants, vector<PyCode*>* nestedFunctions) {
PyObject* value = Value(nestedFunctions);
constants->push_back(value);
constants = ValueRest(constants, nestedFunctions);
return constants;
}
vector<PyObject*>* PyParser::ValueRest(vector<PyObject*>* constants, vector<PyCode*>* nestedFunctions) {
PyToken* tok = in->getToken();
if (tok->getLex() != ",") {
in->putBackToken();
return constants;
}
return ValueList(constants, nestedFunctions);
}
PyObject* PyParser::Value(vector<PyCode*>* nestedFunctions) {
int iVal;
float fVal;
string sVal;
PyToken* codeId;
istringstream* tokStr;
PyToken* tok = in->getToken();
switch (tok->getType()) {
case PYINTEGERTOKEN:
tokStr = new istringstream(tok->getLex());
tokStr->exceptions(ios_base::failbit | ios_base::badbit);
(*tokStr) >> iVal;
delete tokStr;
return new PyInt(iVal);
break;
case PYFLOATTOKEN:
tokStr = new istringstream(tok->getLex());
tokStr->exceptions(ios_base::failbit | ios_base::badbit);
(*tokStr) >> fVal;
delete tokStr;
return new PyFloat(fVal);
break;
case PYSTRINGTOKEN:
sVal = tok->getLex();
return new PyStr(sVal);
break;
case PYIDENTIFIERTOKEN:
if (tok->getLex() == "None")
return new PyNone();
else if (tok->getLex() == "True")
return new PyBool(true);
else if (tok->getLex() == "False")
return new PyBool();
else if (tok->getLex() == "code") {
tok = in->getToken();
if (tok->getLex() != "(")
badToken(tok, "Expected a '('.");
codeId = in->getToken();
if (codeId->getType() != PYIDENTIFIERTOKEN)
badToken(tok, "Expected an identifier.");
tok = in->getToken();
if (tok->getLex() != ")")
badToken(tok, "Expected a ')'.");
for (int i = 0; i < nestedFunctions->size(); i++) {
PyCode* code = (*nestedFunctions)[i];
if (code->getName() == codeId->getLex()) {
return code;
}
}
cerr << "Identifier " << codeId->getLex() << " is not a nested function." << endl;
badToken(codeId, "Must be name of inner function.");
} else
badToken(tok, "Expected None, int, float, str, True, or False.");
default:
badToken(tok, "Expected None, int, float, str, True, or False.");
break;
}
return NULL; // This won't happen, but C++ OS X compiler warns without.
}
vector<string>* PyParser::LocalsPart() {
vector<string>* locals = new vector<string > ();
PyToken* tok = in->getToken();
if (tok->getLex() != "Locals") {
in->putBackToken();
return locals;
}
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a ':'.");
return IdList(locals);
}
vector<string>* PyParser::FreeVarsPart() {
vector<string>* freevars = new vector<string > ();
PyToken* tok = in->getToken();
if (tok->getLex() != "FreeVars") {
in->putBackToken();
return freevars;
}
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a ':'.");
return IdList(freevars);
}
vector<string>* PyParser::CellVarsPart() {
vector<string>* cellvars = new vector<string > ();
PyToken* tok = in->getToken();
if (tok->getLex() != "CellVars") {
in->putBackToken();
return cellvars;
}
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a ':'.");
return IdList(cellvars);
}
vector<string>* PyParser::IdList(vector<string>* lst) {
PyToken* tok = in->getToken();
if (tok->getType() != PYIDENTIFIERTOKEN)
badToken(tok, "Expected an identifier.");
lst->push_back(tok->getLex());
return IdRest(lst);
}
vector<string>* PyParser::IdRest(vector<string>* lst) {
PyToken* tok = in->getToken();
if (tok->getType() != PYCOMMATOKEN) {
in->putBackToken();
return lst;
}
return IdList(lst);
}
vector<string>* PyParser::GlobalsPart() {
vector<string>* globals = new vector<string>();
PyToken* tok = in->getToken();
if (tok->getLex() != "Globals") {
in->putBackToken();
return globals;
}
tok = in->getToken();
if (tok->getLex() != ":")
badToken(tok, "Expected a ':'.");
return IdList(globals);
}
vector<PyByteCode*>* PyParser::BodyPart() {
vector<PyByteCode*>* instructions = new vector<PyByteCode*> ();
PyToken* tok = in->getToken();
target.clear();
index = 0;
if (tok->getLex() != "BEGIN")
badToken(tok, "Expected a BEGIN keyword.");
instructions = InstructionList(instructions);
//Find the target of any labels in the byte code instructions
int i;
for (i = 0; i < instructions->size(); i++) {
PyByteCode* inst = (*instructions)[i];
string label = inst->getLabel();
if (label != "") {
string op = inst->getOpCodeName();
delete (*instructions)[i];
(*instructions)[i] = new PyByteCode(op, target[label]);
}
}
tok = in->getToken();
if (tok->getLex() != "END")
badToken(tok, "Expected a END keyword.");
//printMap<string,int>(target);
return instructions;
}
vector<PyByteCode*>* PyParser::InstructionList(vector<PyByteCode*>* instructions) {
PyToken* tok = in->getToken();
in->putBackToken();
if (tok->getLex() == "END")
return instructions;
PyByteCode* inst = LabeledInstruction();
instructions->push_back(inst);
index++;
return InstructionList(instructions);
}
PyByteCode* PyParser::LabeledInstruction() {
PyToken* tok1 = in->getToken();
string tok1Lex = tok1->getLex();
PyToken* tok2 = in->getToken();
string tok2Lex = tok2->getLex();
if (tok2Lex == ":") {
if (target.find(tok1Lex) == target.end()) {
target[tok1Lex] = index;
} else
badToken(tok1, "Duplicate label found.");
return LabeledInstruction();
}
int argCount = 0;
try {
argCount = PyByteCode::numArgs(tok1Lex);
} catch (int e) {
badToken(tok1, "Illegal Opcode.");
}
if (argCount == 0) {
in->putBackToken();
return new PyByteCode(tok1Lex);
}
if (tok2->getType() == PYIDENTIFIERTOKEN) {
// found a label to jump to.
try {
return new PyByteCode(tok1Lex, tok2Lex);
} catch (int e) {
badToken(tok2, "Illegal Opcode.");
}
}
if (tok2->getType() == PYINTEGERTOKEN) {
try {
int operand;
stringstream(tok2Lex) >> operand;
return new PyByteCode(tok1Lex, operand);
} catch (int e) {
badToken(tok1, "Illegal Opcode.");
}
}
badToken(tok1, "Instruction Syntax Error.");
return NULL; // This won't happen but C++ OS X compiler warns without.
}