-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
250 lines (214 loc) · 6.72 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
var TokenType = require("./token-types")
var SyntaxTreeTypes = require("./syntax-tree-types");
/*
Grammar
Exp -> identifier | string | F M A
NumOrStr -> string | NUM
M-> * NumOrStr M | / EXP M | epsilon
F -> T
T->NUM A
A-> + Exp A | - Exp A | episilon
ASSIGNMENT-> save EXP as identifier
NUM -> iden | numTerminal
*/
const BinaryOperator = {
MULT: "*",
DIV: "/",
PLUS: "+",
MINUS: "-"
}
class Parser {
constructor() {
this.inputPos = 0;
this.tokens = null;
}
enforceTokensNotNull(){
if(this.currentPosition >= this.tokens.length){
throw new Error("Reached end of input during parse");
}
}
eat(token, tokenType) {
this.enforceTokensNotNull();
if (token.type == tokenType) {
this.inputPos++;
return token;
} else {
throw new Error("Did not match expected syntax");
}
}
Num(tokens){
if(tokens[this.inputPos].type == TokenType.IDENTIFIER){
return this.eat(tokens[this.inputPos], TokenType.IDENTIFIER);
}
return this.eat(tokens[this.inputPos], TokenType.NUMBER)
}
F(tokens){
return this.T(tokens)
}
T(tokens){
var left = this.Num(tokens);
return this.A(left, tokens);
}
A(left, tokens){
var token;
if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.PLUS){
token = this.eat(tokens[this.inputPos], TokenType.PLUS);
var right = this.Expression(tokens);
//var right = this.A(tokens);
return this.A({
type: SyntaxTreeTypes.BinaryExpression,
operator: BinaryOperator.PLUS,
left: left,
right: right
}, tokens);
}
if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.MINUS){
token = this.eat(tokens[this.inputPos], TokenType.MINUS);
var right = this.Expression(tokens);
return this.A({
type: SyntaxTreeTypes.BinaryExpression,
operator: BinaryOperator.MINUS,
left: left,
right: right
}, tokens);
}
return left;
}
Str(tokens){
return this.eat(tokens[this.inputPos], TokenType.STRING);
}
NumOrStr(tokens){
if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.NUMBER){
return this.Num(tokens);
}
else if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.STRING){
return this.Str(tokens);
}
else{
throw new Error("Expected number or string");
}
}
M(left, tokens){
var token;
if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.MULT){
token = this.eat(tokens[this.inputPos], TokenType.MULT);
var right = this.NumOrStr(tokens);
return this.M({
type: SyntaxTreeTypes.BinaryExpression,
operator: BinaryOperator.MULT,
left: left,
right: right
}, tokens);
}
if(tokens[this.inputPos] && tokens[this.inputPos].type == TokenType.DIV){
token = this.eat(tokens[this.inputPos], TokenType.DIV);
var right = this.Num(tokens);
return this.M({
type: SyntaxTreeTypes.BinaryExpression,
operator: BinaryOperator.DIV,
left: left,
right: right
}, tokens);
}
return left;
}
Expression(tokens){
var token;
var left;
if(tokens[this.inputPos].type == TokenType.STRING){
left = this.Str(tokens);
}
else{
left = this.F(tokens);
}
var node = this.M(left, tokens);
if(process.env.debug){
console.log(`\nThis is the
right returned from expression`
+ JSON.stringify(node));
}
var node2 = this.A(node, tokens);
if(process.env.debug){
console.log(`\nThis is the
right returned from expression`
+ JSON.stringify(node));
}
return node2;
}
Print(tokens) {
this.eat(tokens[this.inputPos], TokenType.PRINT);
this.eat(tokens[this.inputPos], TokenType.OP_PAREN);
var value = this.Expression(tokens);
if(process.env.debug){
console.log(`\n\nThis is the value of the expression inside print: ${JSON.stringify(value)}`);
}
this.eat(tokens[this.inputPos], TokenType.CL_PAREN);
return {
type: SyntaxTreeTypes.Print,
value: value
}
}
ForLoop(tokens) {
this.eat(tokens[this.inputPos], TokenType.FOR);
this.eat(tokens[this.inputPos], TokenType.OP_PAREN);
var iterationCount = this.Expression(tokens);
this.eat(tokens[this.inputPos], TokenType.CL_PAREN);
this.eat(tokens[this.inputPos], TokenType.OP_CURLY);
var body = []
while(tokens[this.inputPos].type != TokenType.CL_CURLY){
body.push(this.Start(tokens));
}
this.eat(tokens[this.inputPos], TokenType.CL_CURLY);
return {
type: SyntaxTreeTypes.FOR,
iterationCount: iterationCount,
body: {
type: SyntaxTreeTypes.Block,
body: body
}
}
}
Assignment(tokens){
this.eat(tokens[this.inputPos], TokenType.SAVE);
var val = this.Expression(tokens)
this.eat(tokens[this.inputPos], TokenType.AS);
var id = this.eat(tokens[this.inputPos], TokenType.IDENTIFIER);
return {
type: SyntaxTreeTypes.Assignment,
identifier: id.value,
value: val
}
}
Start(tokens){
var AST;
if(tokens[this.inputPos].type == TokenType.FOR){
AST = this.ForLoop(tokens);
}
else if(tokens[this.inputPos].type == TokenType.PRINT){
AST = this.Print(tokens);
}
else if(tokens[this.inputPos].type == TokenType.SAVE){
AST = this.Assignment(tokens);
}
else{
AST = this.Expression(tokens);
}
return AST;
}
parseTokens(tokens) {
this.tokens = tokens;
var treeList = [];
while(this.inputPos < tokens.length){
treeList.push(this.Start(tokens));
}
if(process.env.debug){
console.log("\n\n")
console.log(JSON.stringify(treeList));
}
return {
type: SyntaxTreeTypes.Program,
body: treeList
};
}
}
module.exports = Parser;