-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.js
370 lines (351 loc) · 8.57 KB
/
expr.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
360
361
362
363
364
365
366
367
368
369
370
// Operator constants
var unaryMinus = 1;
var unaryLogicalNot = 2;
var unaryBitwiseNot = 3;
var power = 4;
var multiply = 5;
var divide = 6;
var remainder = 7;
var plus = 8;
var minus = 9;
var shl = 10;
var shr = 11;
var lessThan = 12;
var lessOrEquals = 13;
var greaterThan = 14;
var greaterOrEquals = 15;
var equals = 16;
var notEquals = 17;
var bitwiseAnd = 18;
var bitwiseXor = 19;
var bitwiseOr = 20;
var logicalAnd = 21;
var logicalOr = 22;
var assign = 23;
var comma = 24;
var ops = {
'-u': unaryMinus, '!u': unaryLogicalNot, '^u': unaryBitwiseNot,
'**': power, '*': multiply, '/': divide, '%': remainder,
'+': plus, '-': minus,
'<<': shl, '>>': shr,
'<': lessThan, '<=': lessOrEquals, '>': greaterThan, '>=': greaterOrEquals,
'==': equals, '!=': notEquals,
'&': bitwiseAnd, '^': bitwiseXor, '|': bitwiseOr,
'&&': logicalAnd, '||': logicalOr,
'=': assign, ',': comma
};
function isUnary(op) {
return op >= unaryMinus && op <= unaryBitwiseNot;
}
function isLeftAssoc(op) {
return !isUnary(op) && op != assign && op != power && op != comma;
}
function constExpr(value) {
return function() {
return value;
};
}
function varExpr(value) {
var v = function(x) {
if (x !== undefined) {
value = x;
}
return value;
};
v.variable = true;
return v;
}
function unaryExpr(op, arg) {
return function() {
if (op == unaryMinus) {
return -arg();
} else if (op == unaryBitwiseNot) {
return ~arg();
} else if (op == unaryLogicalNot) {
return !arg();
}
};
}
function binaryExpr(op, a, b) {
var f = function() {
var c;
if (op == power) {
return Math.pow(a(), b());
} else if (op == multiply) {
return a() * b();
} else if (op == divide) {
c = b();
return c ? (a() / c) : 0;
} else if (op == remainder) {
c = b();
return c ? (a() % c) : 0;
} else if (op == plus) {
return a() + b();
} else if (op == minus) {
return a() - b();
} else if (op == shl) {
return a() << b();
} else if (op == shr) {
return a() >> b();
} else if (op == lessThan ) {
return a() < b();
} else if (op == lessOrEquals) {
return a() <= b();
} else if (op == greaterThan ) {
return a() > b();
} else if (op == greaterOrEquals) {
return a() >= b();
} else if (op == equals ) {
return a() == b();
} else if (op == notEquals) {
return a() != b();
} else if (op == bitwiseAnd) {
return a() & b();
} else if (op == bitwiseXor) {
return a() ^ b();
} else if (op == bitwiseOr) {
return a() | b();
} else if (op == logicalAnd) {
return a() && b();
} else if (op == logicalOr) {
return a() || b();
} else if (op == assign) {
return a(b());
} else if (op == comma) {
a(); return b();
}
};
if (op == assign) {
if (!a.variable) {
return; // bad variable
}
} else if (op == comma) {
f.car = a;
f.cdr = b;
}
return f;
}
var tokNumber = 1 << 0;
var tokWord = 1 << 1;
var tokOp = 1 << 2;
var tokOpen = 1 << 3;
var tokClose = 1 << 4;
function isDigit(c) {
return c >= '0' && c <= '9';
}
function isLetter(c) {
return c >= 'a' && c <= 'z';
}
function isSpace(c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
function tokenize(s) {
var tokens = [];
var expected = tokOpen | tokNumber | tokWord;
for (var i = 0; i < s.length;) {
var c = s.charAt(i);
var tok = '';
if (isSpace(c)) {
i++;
continue;
}
if (isDigit(c)) {
if ((expected & tokNumber) === 0) {
return; // Unexpected number
}
expected = tokOp | tokClose;
while ((c == '.' || isDigit(c)) && i < s.length) {
tok = tok + s.charAt(i);
i++;
c = s.charAt(i);
}
} else if (c >= 'a' && c <= 'z') {
if ((expected & tokWord) === 0) {
return; // ErrUnexpectedWord
}
expected = tokOp | tokOpen | tokClose;
while ((isLetter(c) || isDigit(c) || c == '_') && i < s.length) {
tok = tok + s.charAt(i);
i++;
c = s.charAt(i);
}
} else if (c == '(' || c == ')') {
tok = tok + c;
i++;
if (c == '(' && (expected&tokOpen) !== 0) {
expected = tokNumber | tokWord | tokOpen | tokClose;
} else if (c == ')' && (expected&tokClose) !== 0) {
expected = tokOp | tokClose;
} else {
return; // Bad parens
}
} else {
if ((expected & tokOp) === 0) {
if (c != '-' && c != '^' && c != '!') {
return; // ErrOperandMissing
}
tok = tok + c + 'u';
i++;
} else {
var lastOp = '';
while (!isLetter(c) && !isDigit(c) && ! isSpace(c) &&
c != '_' && c != '(' && c != ')' && i < s.length) {
if (ops[tok + s.charAt(i)] > 0) {
tok = tok + s.charAt(i);
lastOp = tok;
} else if (!lastOp) {
tok = tok + s.charAt(i);
} else {
break;
}
i++;
c = s.charAt(i);
}
if (!lastOp) {
return; //ErrBadOp
}
}
expected = tokNumber | tokWord | tokOpen;
}
tokens.push(tok);
}
return tokens;
}
var parenAllowed = 1;
var parenExpected = 2;
var parenForbidden = 3;
function parse(s, vars, funcs) {
var os = [];
var es = [];
var as = [];
vars = vars || {};
funcs = funcs || {};
var tokens = tokenize(s);
if (!tokens) {
return;
}
var paren = parenAllowed;
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
var parenNext = parenAllowed;
if (token == '(') {
if (paren == parenExpected) {
as.push({osp: os.length, esp: es.length, args: []});
os.push('{');
} else if (paren == parenAllowed) {
os.push('(');
} else {
return; // Bad call
}
} else if (paren == parenExpected) {
return; // Bad call
} else if (token == ')') {
var minLength = (as.length > 0 ? as[as.length-1].osp : 0);
while (os.length > minLength && os[os.length-1] != '(' && os[os.length-1] != '{') {
var parenExpr = bind(os.pop(), funcs, es);
if (!parenExpr) {
return;
}
es.push(parenExpr);
}
if (os.length === 0) {
return; // Bad parens
}
if (os.pop() == '{') {
var name = os.pop();
var f = funcs[name];
var a = as.pop();
var args = a.args;
if (es.length > a.esp) {
args.push(es.pop());
}
es.push(f.bind({}, args));
}
parenNext = parenForbidden;
} else if (!isNaN(token)) {
var n = parseFloat(token);
es.push(constExpr(n));
parenNext = parenForbidden;
} else if (funcs[token]) {
os.push(token);
parenNext = parenExpected;
} else if (ops[token]) {
var op = ops[token];
var o2 = os[os.length-1];
for (;;) {
if (token == ',' && os.length > 0 && os[os.length-1] == '{') {
as[as.length-1].args.push(es.pop());
break;
}
if (!(ops[o2] !== 0 && ((isLeftAssoc(op) && op >= ops[o2]) || op > ops[o2]))) {
os.push(token);
break;
}
var expr = bind(o2, funcs, es);
if (!expr) {
return;
}
es.push(expr);
os.pop();
o2 = os[os.length-1];
}
} else if (token.match(/^\D/)) {
// Variable
if (vars[token]) {
es.push(vars[token]);
} else {
var v = varExpr(0);
vars[token] = v;
es.push(v);
}
parenNext = parenForbidden;
} else {
return; // Bad variable name, e.g. '2.3.4' or '4ever'
}
paren = parenNext;
}
if (paren == parenExpected) {
return; // Bad call
}
while (os.length > 0) {
var rest = os.pop();
if (rest == '(' || rest == ')') {
return; // Bad paren
}
var restExpr = bind(rest, funcs, es);
if (!restExpr) {
return;
}
es.push(restExpr);
}
if (es.length === 0) {
return constExpr(0);
} else {
return es.pop();
}
}
function bind(name, funcs, stack) {
if (ops[name]) {
if (isUnary(ops[name])) {
if (stack[stack.length - 1] === undefined) {
return; // Operand missing
} else {
return unaryExpr(ops[name], stack.pop());
}
} else {
var b = stack.pop();
var a = stack.pop();
if (a === undefined || b === undefined) {
return; // Binary operand missing
}
return binaryExpr(ops[name], a, b);
}
} else {
return; // Bad call
}
}
module.exports = {
parse: parse,
varExpr: varExpr,
tokenize: tokenize
};