-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangparse.js
359 lines (304 loc) · 8.71 KB
/
langparse.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
350
351
352
353
354
355
356
357
358
359
// Credit where credit is due. I'm following this article: http://lisperator.net/pltut/parser/
function input_stream(input) {
var pos = 0, line = 1, col = 0;
return {
next,
peek,
eof,
croak
};
function next() {
var ch = input.charAt(pos++);
if(ch == "\n") line++, col=0; else col++;
return ch;
}
function peek() {
return input.charAt(pos);
}
function eof() {
return peek() == "";
}
function croak(msg) {
throw new Error(msg + " ("+line+":"+col+")");
}
}
function token_stream(input) {
var current = null;
var keywords = " if then else lambda λ true false ";
return {
next,
peek,
eof,
croak: input.croak
};
function is_keyword(x) {
return keywords.indexOf(" "+x+" ") >= 0;
}
function is_digit(ch) {
return /[0-9]/i.test(ch);
}
function is_id_start(ch) {
return /[a-zλ_]/i.test(ch);
}
function is_id(ch) {
return is_id_start(ch) || "?!-<>=0123456789".indexOf(ch) >= 0;
}
function is_op_char(ch) {
return "+-*/%=&|<>!".indexOf(ch) >= 0;
}
function is_punc(ch) {
return ",;(){}[]".indexOf(ch) >= 0;
}
function is_whitespace(ch) {
return " \t\n".indexOf(ch) >= 0;
}
function read_while(predicate) {
var str = "";
while(!input.eof() && predicate(input.peek()))
str+=input.next();
return str;
}
function read_number() {
var has_dot = false;
var number = read_while(function(ch){
if(ch == "."){
if(has_dot) return false;
has_dot=true;
return true;
}
return is_digit(ch);
});
return {type: "num", value: has_dot ? parseFloat(number) : parseInt(number)};
}
function read_ident() {
var id = read_while(is_id);
return {
type: is_keyword(id) ? "kw" : "var",
value: id
};
}
function read_escaped(end) {
var escaped = false, str = "";
input.next();
while(!input.eof()) {
var ch = input.next();
if(escaped) {
str+=ch;
escaped=false;
}else if(ch == "\\") {
escaped = true;
}else if (ch == end) {
break;
}else{
str += ch;
}
}
return str;
}
function read_string(){
return {type: "str", value: read_escaped('"')};
}
function skip_comment() {
read_while(function (ch) { return ch != "\n"});
input.next();
}
function read_next(){
read_while(is_whitespace);
if(input.eof()) return null;
var ch = input.peek();
if(ch=="") {
skip_comment();
return read_next();
}
if(ch == '"') return read_string();
if(is_digit(ch)) return read_number();
if(is_id_start(ch)) return read_ident();
if(is_punc(ch)) return {
type: "punc",
value: input.next()
};
if(is_op_char(ch)) return {
type: "op",
value: read_while(is_op_char)
}
input.croak("Can't handle character: "+ch);
}
function peek() {
return current || (current = read_next())
}
function next() {
var tok = current;
current = null;
return tok || read_next();
}
function eof() {
return peek() == null;
}
}
var FALSE = {type: "bool", value: false};
function parse(input) {
var PRECEDENCE = {
"=": 1,
"||": 2,
"&&": 3,
"<":7, ">":7, "<=":7, ">=":7, "==":7, "!=":7,
"+":10, "-":10,
"*":20, "/":20, "%":20
};
return parse_toplevel();
function is_punc(ch) {
var tok = input.peek();
return tok && tok.type == "punc" && (!ch || tok.value == ch) && tok;
}
function is_kw(kw) {
var tok = input.peek();
return tok && tok.type == "kw" && (!kw || tok.value == kw) && tok;
}
function is_op(op) {
var tok = input.peek();
return tok && tok.type == "op" && (!op || tok.value == op) && tok;
}
function skip_punc(ch) {
if(is_punc(ch)) input.next();
else input.croak("Expecting punctuation: '"+ch+"'");
}
function skip_kw(ch) {
if(is_kw(ch)) input.next();
else input.croak("Expecting keyword: '"+ch+"'");
}
function skip_op(ch) {
if(is_op(ch)) input.next();
else input.croak("Expecting operator: '"+ch+"'");
}
function unexpected() {
input.croak("Unexpected token: "+JSON.stringify(input.peek()));
}
function delimited(start, stop, separator, parser) {
var a = [], first = true;
skip_punc(start);
while(!input.eof()) {
if(is_punc(stop)) break;
if(first) first = false; else skip_punc(separator);
if(is_punc(stop)) break;
a.push(parser());
}
skip_punc(stop);
return a;
}
function parse_varname() {
var name = input.next();
if(name.type != "var") input.croak("Expecting variable name");
return name.value;
}
function parse_lambda() {
return {
type: "lambda",
vars: delimited("(", ")", ",", parse_varname),
body: parse_expression()
}
}
function parse_bool() {
return {
type: "bool",
value: input.next().value == "true"
}
}
function parse_toplevel() {
var prog = [];
while (!input.eof()) {
prog.push(parse_expression());
if (!input.eof()) skip_punc(";")
}
return {type: "prog", prog};
}
function parse_if() {
skip_kw("if");
var cond = parse_expression();
if(!is_punc("{")) skip_kw("then")
var then = parse_expression();
var ret = {type: "if", cond, then};
if(is_kw("else")) {
input.next();
ret.else = parse_expression();
}
return ret;
}
function parse_atom() {
return maybe_call(function() {
if(is_punc("(")) {
input.next();
var exp = parse_expression();
skip_punc(")");
return exp;
}
// This is the proper place to implement unary operators.
// Following is the code for boolean negation which is present in the final version of lambda.js
// if (is_op("!")) {
// input.next();
// return {
// type: "not",
// body: parse_atom();
// }
// }
if (is_punc("{")) return parse_prog();
if (is_kw("if")) return parse_if();
if (is_kw("true") || is_kw("false")) return parse_bool();
if (is_kw("lambda") || is_kw("λ")) {
input.next();
return parse_lambda();
}
var tok = input.next();
if(tok.type == "var" || tok.type == "num" || tok.type == "str")
return tok;
unexpected();
})
}
function parse_prog() {
var prog = delimited("{", "}", ";", parse_expression);
if(prog.length == 0) return FALSE;
if(prog.length == 1) return prog[0];
return {type:"prog", prog};
}
function parse_expression() {
return maybe_call(function(){
return maybe_binary(parse_atom(), 0);
})
}
function maybe_call(expr) {
expr = expr();
return is_punc("(") ? parse_call(expr) : expr;
}
function parse_call(func) {
return {
type: "call",
func,
args: delimited("(", ")", ",", parse_expression)
}
}
function maybe_binary(left, my_prec) {
console.log("left", left);
var tok = is_op();
console.log("tok", tok);
if(tok) {
var his_prec = PRECEDENCE[tok.value];
if(his_prec > my_prec) {
input.next();
var right = maybe_binary(parse_atom(), his_prec);
var binary = {
type: tok.value == "=" ? "assign" : "binary",
operator: tok.value,
left, right
};
return maybe_binary(binary, my_prec);
}
}
console.log("return left")
return left;
}
}
var code =
`
test = if (9+3 > 8) then 4 + (4 + 3); end
`;
var ast = parse(token_stream(input_stream(code)));
console.log(JSON.stringify(ast));