-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
327 lines (323 loc) · 9.51 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function InputStream(input) {
var pos = 0,
line = 1,
col = 0;
return {
next: next,
peek: peek,
eof: eof,
croak: 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 TokenStream(input) {
var current = null;
var keywords = ' if then else lambda λ true false ';
return {
next: next,
peek: peek,
eof: 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: parseFloat(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(kw) {
if (is_kw(kw)) input.next();
else input.croak('Expecting keyword: "' + kw + '"');
}
function skip_op(op) {
if (is_op(op)) input.next();
else input.croak('Expecting operator: "' + op + '"');
}
function unexpected() {
input.croak('Unexpected token: ' + JSON.stringify(input.peek()));
}
// 从左到右遍历,按符号优先级进行包装,下一个运算符比上一个binary结构的运算符大的话,对后面的进行包装新的bianry(因为解析执行时按深度优先遍历,所以会先执行,从而满足高优先级的运算符先执行)
function maybe_binary(left, my_prec) {
var tok = is_op();
if (tok) {
var his_prec = PRECEDENCE[tok.value];
if (his_prec > my_prec) {
input.next();
const right = maybe_binary(parse_atom(), his_prec);
return maybe_binary(
{
type: tok.value == '=' ? 'assign' : 'binary',
operator: tok.value,
left: left,
right: right,
},
my_prec
);
}
}
return left;
}
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_call(func) {
return {
type: 'call',
func: func,
args: delimited('(', ')', ',', parse_expression),
};
}
function parse_varname() {
var name = input.next();
if (name.type != 'var') input.croak('Expecting variable name');
return name.value;
}
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: cond,
then: then,
};
if (is_kw('else')) {
input.next();
ret.else = parse_expression();
}
return ret;
}
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 maybe_call(expr) {
expr = expr();
return is_punc('(') ? parse_call(expr) : expr;
}
function parse_atom() {
return maybe_call(function () {
if (is_punc('(')) {
input.next();
var exp = parse_expression();
skip_punc(')');
return exp;
}
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_toplevel() {
var prog = [];
while (!input.eof()) {
prog.push(parse_expression());
if (!input.eof()) skip_punc(';');
}
return { type: 'prog', prog: prog };
}
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: prog };
}
function parse_expression() {
return maybe_call(function () {
return maybe_binary(parse_atom(), 0);
});
}
}
</script>
</body>
</html>