-
Notifications
You must be signed in to change notification settings - Fork 237
/
evaluate.js
143 lines (136 loc) · 4.47 KB
/
evaluate.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
import { INUMBER, IOP1, IOP2, IOP3, IVAR, IVARNAME, IFUNCALL, IFUNDEF, IEXPR, IEXPREVAL, IMEMBER, IENDSTATEMENT, IARRAY } from './instruction';
export default function evaluate(tokens, expr, values) {
var nstack = [];
var n1, n2, n3;
var f, args, argCount;
if (isExpressionEvaluator(tokens)) {
return resolveExpression(tokens, values);
}
var numTokens = tokens.length;
for (var i = 0; i < numTokens; i++) {
var item = tokens[i];
var type = item.type;
if (type === INUMBER || type === IVARNAME) {
nstack.push(item.value);
} else if (type === IOP2) {
n2 = nstack.pop();
n1 = nstack.pop();
if (item.value === 'and') {
nstack.push(n1 ? !!evaluate(n2, expr, values) : false);
} else if (item.value === 'or') {
nstack.push(n1 ? true : !!evaluate(n2, expr, values));
} else if (item.value === '=') {
f = expr.binaryOps[item.value];
nstack.push(f(n1, evaluate(n2, expr, values), values));
} else {
f = expr.binaryOps[item.value];
nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values)));
}
} else if (type === IOP3) {
n3 = nstack.pop();
n2 = nstack.pop();
n1 = nstack.pop();
if (item.value === '?') {
nstack.push(evaluate(n1 ? n2 : n3, expr, values));
} else {
f = expr.ternaryOps[item.value];
nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values), resolveExpression(n3, values)));
}
} else if (type === IVAR) {
if (/^__proto__|prototype|constructor$/.test(item.value)) {
throw new Error('prototype access detected');
}
if (item.value in expr.functions) {
nstack.push(expr.functions[item.value]);
} else if (item.value in expr.unaryOps && expr.parser.isOperatorEnabled(item.value)) {
nstack.push(expr.unaryOps[item.value]);
} else {
var v = values[item.value];
if (v !== undefined) {
nstack.push(v);
} else {
throw new Error('undefined variable: ' + item.value);
}
}
} else if (type === IOP1) {
n1 = nstack.pop();
f = expr.unaryOps[item.value];
nstack.push(f(resolveExpression(n1, values)));
} else if (type === IFUNCALL) {
argCount = item.value;
args = [];
while (argCount-- > 0) {
args.unshift(resolveExpression(nstack.pop(), values));
}
f = nstack.pop();
if (f.apply && f.call) {
nstack.push(f.apply(undefined, args));
} else {
throw new Error(f + ' is not a function');
}
} else if (type === IFUNDEF) {
// Create closure to keep references to arguments and expression
nstack.push((function () {
var n2 = nstack.pop();
var args = [];
var argCount = item.value;
while (argCount-- > 0) {
args.unshift(nstack.pop());
}
var n1 = nstack.pop();
var f = function () {
var scope = Object.assign({}, values);
for (var i = 0, len = args.length; i < len; i++) {
scope[args[i]] = arguments[i];
}
return evaluate(n2, expr, scope);
};
// f.name = n1
Object.defineProperty(f, 'name', {
value: n1,
writable: false
});
values[n1] = f;
return f;
})());
} else if (type === IEXPR) {
nstack.push(createExpressionEvaluator(item, expr, values));
} else if (type === IEXPREVAL) {
nstack.push(item);
} else if (type === IMEMBER) {
n1 = nstack.pop();
nstack.push(n1[item.value]);
} else if (type === IENDSTATEMENT) {
nstack.pop();
} else if (type === IARRAY) {
argCount = item.value;
args = [];
while (argCount-- > 0) {
args.unshift(nstack.pop());
}
nstack.push(args);
} else {
throw new Error('invalid Expression');
}
}
if (nstack.length > 1) {
throw new Error('invalid Expression (parity)');
}
// Explicitly return zero to avoid test issues caused by -0
return nstack[0] === 0 ? 0 : resolveExpression(nstack[0], values);
}
function createExpressionEvaluator(token, expr, values) {
if (isExpressionEvaluator(token)) return token;
return {
type: IEXPREVAL,
value: function (scope) {
return evaluate(token.value, expr, scope);
}
};
}
function isExpressionEvaluator(n) {
return n && n.type === IEXPREVAL;
}
function resolveExpression(n, values) {
return isExpressionEvaluator(n) ? n.value(values) : n;
}