-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
385 lines (333 loc) · 10.9 KB
/
parse.js
File metadata and controls
385 lines (333 loc) · 10.9 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
// parse.js
/*
* parser: construct a parse tree according to token list
*/
function tequila_parse(token_list) {
const tokTable = {}; // ready to get referenced for all possible symbols
function getNextToken() { return token_list.shift(); }
// duplicate one from token list, deep cloning and register callbacks
function dupCurToken() {
const tok = token_list[0];
if (!tokTable[tok.node]) {
throw "Unrecognised token \"" + tok.node + "\"";
}
const newTok = Object.create(tokTable[tok.node]); // nud, lbp, led
newTok.node = tok.node;
newTok.value = tok.value;
return newTok;
}
/*
* Top-down operator precedence parsing
* LR(1): shift-reduce
*
* @param rbp right binding power
*/
function expression(rbp) {
let tok = dupCurToken();
if (!tok.nud) {
throw "Unexpected token: " + tok.node;
}
getNextToken(); // eat id/number/prefix
let lhs = tok.nud(tok); // mostly simply return itself
while (rbp < dupCurToken().lbp) {
tok = dupCurToken();
getNextToken(); // eat infix
if (!tok.led) {
throw "Unexpected token: " + tok.node;
}
lhs = tok.led(lhs);
}
return lhs;
}
/*
* @brief http://javascript.crockford.com/tdop/tdop.html
* @param id Identifier
* @param nud Null denotative
* @param led Left denotative
*/
function makeSymbol(id, nud, lbp, led) {
// register symbol table
const tok = tokTable[id] || {};
tokTable[id] = {
nud: tok.nud || nud,
lbp: tok.lbp || lbp,
led: tok.led || led,
};
}
makeSymbol("number", function (n) { return n; });
makeSymbol("string", function (n) { return n; });
makeSymbol(","); // argument separator
makeSymbol(";"); // statement separator
makeSymbol(":"); // key-value mapping
// for array
makeSymbol("[", function () {
const elems = [];
if (token_list[0].node !== "]") {
while (true) {
elems.push(expression(0));
if (token_list[0].node !== ",") {
break;
}
getNextToken(); // eat ","
}
}
if (token_list[0].node !== "]") {
throw "Expected closing bracket ']'";
}
getNextToken(); // eat "]"
return { node: "array", elements: elems };
});
makeSymbol("]");
makeSymbol("for", function () {
if ("(" !== token_list[0].node) {
throw "Expected '(' after for";
}
getNextToken(); // eat "("
const init = expression(0);
// case A: for n in nums
if (init.node === "in") {
if (")" !== token_list[0].node) {
throw "Expected ')' after for-in loop";
}
getNextToken(); // eat ")"
const body = expression(0);
return {
node: "loop_for_in",
iterator: init.lhs, // "n"
collection: init.rhs, // "nums"
body: body
};
}
// case B: for (i = 0; i < N; i = i + 1)
if (";" !== token_list[0].node) {
throw "Expected ';' or 'in' in for loop";
}
getNextToken(); // eat first ";"
const cond = expression(0);
if (";" !== token_list[0].node) {
throw "Expected ';' after condition";
}
getNextToken(); // eat second ";"
const step = expression(0);
if (")" !== token_list[0].node) {
throw "Expected ')' after step";
}
getNextToken(); // eat ")"
const body = expression(0);
return { node: "loop_for", init: init, cond: cond, step: step, body: body };
});
makeSymbol("while", function () {
const cond = expression(0);
const body = expression(0);
return { node: "loop_while", cond: cond, body: body };
});
makeSymbol("if", function () {
const cond = expression(0);
if ("then" !== token_list[0].node) {
throw "Expected 'then' clause";
}
getNextToken(); // eat "then"
const conseq = expression(0);
if ("else" !== token_list[0].node) {
return { "node": "branch", "cond": cond, "conseq": conseq };
}
getNextToken(); // eat "else"
const alt = expression(0);
return { "node": "branch", "cond": cond, "conseq": conseq, "alt": alt };
});
makeSymbol("then");
makeSymbol("else");
makeSymbol("(", function () {
const e = expression(0);
// check if it is a tuple
if (token_list[0].node === ",") {
const elems = [e];
while (token_list[0].node === ",") {
getNextToken(); // eat ","
if (token_list[0].node === ")") {
break;
}
elems.push(expression(0));
}
if (")" !== token_list[0].node) {
throw "Expected closing parenthesis ')' for tuple";
}
getNextToken(); // eat ")"
return { node: "tuple", elements: elems };
}
if (")" !== token_list[0].node) {
throw "Expected closing parenthesis ')'";
}
getNextToken(); // eat ")"
return e;
});
makeSymbol(")");
makeSymbol('{', function () {
// empty dict {}
if ("}" === token_list[0].node) {
getNextToken();
return { "node": "dict", "pairs": [] };
}
let isDict = false;
if (token_list.length > 1 && token_list[1].node === ":") {
isDict = true;
}
if (isDict) {
const pairs = [];
do {
const key = expression(0);
if (":" !== token_list[0].node)
throw "Expected ':' in dict definition";
getNextToken(); // eat ":""
const val = expression(0);
pairs.push({ key: key, val: val });
if ("," === token_list[0].node) {
getNextToken(); // eat ",""
} else {
break;
}
} while ("}" !== token_list[0].node);
if ("}" !== token_list[0].node) {
throw "Expected '}'";
}
getNextToken();
return { node: "dict", pairs: pairs };
}
const statements = [];
while ("}" !== token_list[0].node) {
if (";" === token_list[0].node) {
getNextToken();
continue;
}
statements.push(expression(0));
}
getNextToken(); // eat "}"
return { node: "block", stmts: statements };
});
makeSymbol('}');
makeSymbol("id", function (name) {
// variable reference
if ("(" !== token_list[0].node) {
return name;
}
// function call
const args = [];
if (")" === token_list[1].node) {
getNextToken(); // eat ")" since no args
} else {
do {
getNextToken(); // eat "(" and ","
args.push(expression(0));
} while ("," === token_list[0].node);
if (")" !== token_list[0].node) {
throw "Expected closing parenthesis ')'";
}
}
getNextToken(); // move to new token ready to go
return { node: "call", args: args, name: name.value };
});
makeSymbol("llm", function () {
// grammar: llm "prompt" on context
const prompt = getNextToken().value;
let context = [];
if (token_list[0].node === "on") {
getNextToken(); // eat "on"
context = expression(0);
}
return { node: "llm_call", prompt: prompt, context: context };
});
makeSymbol("llm_do", function () {
// grammar: llm_do "intent" on context
const intent = getNextToken().value;
let context = [];
if (token_list[0].node === "on") {
getNextToken(); // eat "on"
context = expression(0);
}
return { node: "llm_synth", intent: intent, context: context };
});
makeSymbol("on");
makeSymbol("EOF");
// wrappers
function prefix(id, rbp) {
makeSymbol(id, function () {
return { node: id, rhs: expression(rbp) };
});
}
function infix(id, lbp, rbp, led) {
rbp = rbp || lbp;
makeSymbol(id, null, lbp, led || function (lhs) {
return { node: id, lhs: lhs, rhs: expression(rbp) };
});
}
// install standard operators and set precedence, 1 is the lowest
infix("[", 8, 8, function (lhs) {
const index = expression(0);
if ("]" !== token_list[0].node)
throw "Expected ']'";
getNextToken(); // eat "]"
return { "node": "index", "target": lhs, "index": index };
});
prefix("+", 7);
prefix("-", 7);
infix("*", 6);
infix("/", 6);
infix("%", 6);
infix("+", 5);
infix("-", 5);
infix("<", 4);
infix(">", 4);
infix("<=", 4);
infix(">=", 4);
infix("==", 4);
infix("!=", 4);
infix("in", 4);
prefix("not", 3);
infix("and", 3);
infix("or", 3);
infix("=", 1, 2, function (lhs) {
// assignment to identifier
if ("id" === lhs.node) {
return { node: "assign", name: lhs.value, value: expression(0) };
}
if ("call" === lhs.node) {
// check whether each arg is valid
for (let i = 0; i < lhs.args.length; ++i) {
if ("id" !== lhs.args[i].node) {
throw "Invalid argument name";
}
}
return { node: "func_def", name: lhs.name, args: lhs.args, value: expression(0) };
}
throw "Invalid lvalue";
});
infix(":=", 1, 2, function (lhs) {
let name;
const params = [];
if (lhs.node === "id") { // without args
name = lhs.value;
} else if (lhs.node === "call") { // with args
name = lhs.name;
for (let i = 0; i < lhs.args.length; i++) {
if (lhs.args[i].node !== "id")
throw "Parameter name must be an identifier";
params.push(lhs.args[i].value);
}
} else {
throw "Invalid lvalue for procedure definition";
}
const body = expression(0);
return { node: "proc_def", name: name, params: params, body: body };
});
const parse_tree = [];
while ("EOF" !== token_list[0].node) {
if (";" === token_list[0].node) {
getNextToken();
continue;
}
parse_tree.push(expression(0));
}
// console.log(JSON.stringify(token_list));
// console.log(JSON.stringify(parse_tree));
return parse_tree;
}