-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.js
349 lines (296 loc) · 6.19 KB
/
parser.js
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
/**
* PL/0 recursive descent parser
*
* https://en.wikipedia.org/wiki/Recursive_descent_parser
*
* program = block "." .
*
* block = [ "const" ident "=" number {"," ident "=" number} ";"]
* [ "var" ident {"," ident} ";"]
* { "procedure" ident ";" block ";" } statement .
*
* statement = [ ident ":=" expression | "call" ident
* | "?" ident | "!" expression
* | "begin" statement {";" statement } "end"
* | "if" condition "then" statement
* | "while" condition "do" statement ].
*
* condition = "odd" expression |
* expression ("="|"#"|"<"|"<="|">"|">=") expression .
*
* expression = [ "+"|"-"] term { ("+"|"-") term}.
*
* term = factor {("*"|"/") factor}.
*
* factor = ident | number | "(" expression ")".
*
*/
const ShuntingYard = require('./shuntingyard.js');
let curToken;
let curIndex;
let allTokens;
/**
* Get previous tokens without changing curToken
*/
function prevToken(n=0) {
return allTokens[curIndex - n - 2];
}
/**
* Take the next token off the front of the list
*/
function nextToken() {
curToken = allTokens[curIndex];
curIndex++;
}
/**
* Return true if the next token matches
*/
function accept(t) {
if (curToken.token == t) {
nextToken();
return true;
}
return false;
}
/**
* Return true iif next token matches, otherwise throw an exception
*/
function expect(t) {
if (!accept(t)) {
throw 'unexpected symbol: ' + curToken.strval;
}
return true;
}
/**
* Parse a condition
*
* condition =
* "odd" expression
* | expression ("="|"#"|"<"|"<="|">"|">=") expression .
*/
function parseCondition() {
let astNode;
if (accept('ODD')) {
astNode = {
type: 'Odd',
expression: parseExpression()
};
} else {
let expression1 = parseExpression();
if (['EQ', 'NE', 'LT', 'LE', 'GT', 'GE'].includes(curToken.token)) {
nextToken();
astNode = {
type: prevToken().token, // EQ, NE, etc.
expression: [
expression1,
parseExpression()
]
};
} else {
throw 'condition: invalid operator: ' + curToken.strval;
}
}
return astNode;
}
/**
* Parse a factor
*
* factor =
* ident
* | number
* | "(" expression ")" .
*/
function parseFactor(sy) {
if (accept('IDENTIFIER')) {
sy.process(prevToken());
} else if (accept('NUMBER')) {
sy.process(prevToken());
} else if (accept('LPAREN')) {
sy.process(prevToken());
parseExpression(sy);
expect('RPAREN');
sy.process(prevToken());
} else {
throw 'syntax error: ' + curToken.strval;
}
}
/**
* Parse a product term
*
* term = factor {("*"|"/") factor} .
*/
function parseTerm(sy) {
parseFactor(sy);
while (['MULT', 'DIV'].includes(curToken.token)) {
sy.process(curToken);
nextToken();
parseFactor(sy);
}
}
/**
* Parse an expression
*
* expression = ["+"|"-"] term {("+"|"-") term} .
*/
function parseExpression(shuntingYard) {
let sy;
if (shuntingYard) {
sy = shuntingYard;
} else {
sy = new ShuntingYard();
}
let astNode = {
type: 'Expression'
};
// This is the section for negation ... I THINK
if (curToken.token == 'PLUS' || curToken.token == 'MINUS') {
curToken.token = 'UNARY_' + curToken.token;
sy.process(curToken);
nextToken();
}
parseTerm(sy);
while (curToken.token == 'PLUS' || curToken.token == 'MINUS') {
sy.process(curToken);
nextToken();
parseTerm(sy);
}
if (!shuntingYard) {
// if shuntingYard is undefined, it means we created a new on in
// this stack frame and we're responsible for finishing it up
sy.complete();
astNode.rpn = sy.getRPN();
astNode.tree = sy.getAST();
}
return astNode;
}
/**
* Parse a statement
*
* statement =
* ident ":=" expression
* | "call" ident
* | "begin" statement {";" statement } "end"
* | "if" condition "then" statement
* | "while" condition "do" statement .
*/
function parseStatement() {
let astNode;
if (accept('IDENTIFIER')) {
expect('ASSIGNMENT');
astNode = {
type: 'Assignment',
identifier: prevToken(1).strval,
expression: parseExpression()
};
} else if (accept('CALL')) {
expect('IDENTIFIER');
astNode = {
type: 'Call',
identifier: prevToken().strval
};
} else if (accept('WRITE')) {
parseExpression();
astNode = {
type: 'Write',
identifier: prevToken().strval
};
} else if (accept('BEGIN')) {
astNode = {
type: 'CompoundStatement',
statements: []
};
do {
astNode.statements.push(parseStatement());
} while (accept('SEMICOLON'));
expect('END');
} else if (accept('IF')) {
astNode = {
type: 'IfThen',
};
astNode.condition = parseCondition();
expect('THEN');
astNode.statement = parseStatement();
} else if (accept('WHILE')) {
astNode = {
type: 'While'
};
astNode.condition = parseCondition();
expect('DO');
astNode.statement = parseStatement();
} else {
throw 'syntax error: ' + curToken.strval;
}
return astNode;
}
/**
* Parse a block
*
* block =
* ["const" ident "=" number {"," ident "=" number} ";"]
* ["var" ident {"," ident} ";"]
* {"procedure" ident ";" block ";"} statement .
*/
function parseBlock() {
let astNode = {
type: 'Block',
procedure: [],
statement: null,
symbol: {}
};
if (accept('CONST')) {
do {
expect('IDENTIFIER');
expect('EQ');
expect('NUMBER');
astNode.symbol[prevToken(2).strval] = {
const: true,
val: prevToken().intval
};
} while (accept('COMMA'));
expect('SEMICOLON');
}
if (accept('VAR')) {
do {
expect('IDENTIFIER');
astNode.symbol[prevToken().strval] = { const: false };
} while (accept('COMMA'));
expect('SEMICOLON');
}
while (accept('PROCEDURE')) {
expect('IDENTIFIER');
let id = prevToken().strval;
expect('SEMICOLON');
let b = parseBlock();
expect('SEMICOLON');
astNode.procedure.push({
type: 'Procedure',
name: id,
block: b
});
}
astNode.statement = parseStatement();
return astNode;
}
/**
* Parse the main program block
*
* program = block "." .
*/
function parseProgram() {
let astNode = {
type: 'Program'
};
nextToken();
astNode.block = parseBlock();
expect('PERIOD');
return astNode;
}
/**
* Main exported function
*/
function parse(tokens) {
allTokens = tokens;
curIndex = 0;
return parseProgram();
}
module.exports = parse;