forked from nlitsme/bitcoinexplainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.js
277 lines (252 loc) · 7.98 KB
/
expression.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
/* Author: Willem Hengeveld <[email protected]> */
/* https://github.com/nlitsme/bitcoinexplainer */
"use strict";
/*
* An expression parser.
* parses expressions with:
* - operators: +, -, *, /, ^, **, =
* - values: 0xabcd or 1234 numbers, or "x+y" strings
* - names: starting with a letter.
* - brackets for subexpressions: 3*(4+5)
* - function calls: sqrt(a, b)
* - bracketed lists: (1, 2)
* - four types of brackets can be used: ([{<>}])
* use the function `parseexpr` to parse a text string.
* then use the function `evaluator` to evaluate the expression tree
* given a evaluation context.
* the evaluation context must contain the following functions:
* class Context {
* add(a, b) // a+b
* sub(a, b) // a-b
* mul(a, b) // a*b
* div(a, b) // a/b
* pow(a, b) // a^b or a**b
*
* numbervalue(a) // 0x123 or 456 used to convert the string to a value
* listvalue(t, a) // (1, 2) used to convert a list to a value.
* get(name) // called when using a `name`
* set(name, value) // called when assigning to a variable: name=value
* }
* And optionally any functions called by name.
*
* TODO:
* support '.' / DOT operator for method calls, eg: pt.x or x.sqrt()
*/
class NumValue {
constructor(value) { this.value = value; }
toString() { return "NUM:["+this.value+"]"; }
};
class Name {
constructor(name) { this.name = name; }
toString() { return "NAME:["+this.name+"]"; }
};
class TextValue {
// text values can not be nested, so no escaped quotes.
constructor(value) { this.value = value.substring(1, value.length-1); }
toString() { return "TEXT:["+this.value+"]"; }
};
class Operator {
constructor(op) { this.op = op; this.args = []; }
toString() { return "OP:["+this.op+":"+this.args.join(", ")+"]"; }
precedence()
{
if (this.op == "+") return 1;
if (this.op == "-") return 1;
if (this.op == "*") return 2;
if (this.op == "/") return 2;
if (this.op == "^") return 3;
if (this.op == "**") return 3;
if (this.op == "=") return 0;
throw "unsupported operator";
}
add(item) { this.args.push(item); }
};
class Bracket {
constructor(type) { this.type = type; }
toString() { return "BRACKET:["+this.type+"]"; }
isopen() { return "[({<".includes(this.type); }
isclose() { return "])}>".includes(this.type); }
closes(t) { return ["[]", "()", "{}", "<>"].includes(t.type+this.type); }
};
class Comma {
toString() { return "COMMA"; }
};
class Whitespace {
toString() { return "SPACE"; }
};
function* tokenizer(txt)
{
var p = 0;
var patterns = [
[ NumValue, /0x[0-9a-fA-F]+\b/y ],
[ NumValue, /[0-9]+\b/y ],
[ Name, /[a-zA-Z_]\w*(?:\.\w+)*/y ],
[ Operator, /\*\*|[=+*/^]|-/y ],
[ Bracket, /[()]/y ],
[ Comma, /,/y ],
[ Whitespace, /\s+/y ],
[ TextValue, /"[^"]*"/y ],
];
while (p < txt.length)
{
var t = null;
for (var [cls, pattern] of patterns) {
pattern.lastIndex = p;
var m = pattern.exec(txt);
if (m) {
p = pattern.lastIndex;
t = new cls(m[0]);
break;
}
}
if (!t)
throw "invalid token";
yield t;
}
}
class ExpressionList {
constructor(type, values)
{
this.type = type;
this.values = values;
}
toString() { return "EXPR:["+this.type+":"+this.values.join(", ")+"]"; }
};
class Function {
constructor(name, args)
{
this.name = name;
this.args = args;
}
toString() { return "FUNC:["+this.name+":"+this.args.join(", ")+"]"; }
};
function brackets(tokens)
{
var stack = [];
var exprlist = [];
var tokensequence = [];
for (let t of tokens) {
if (t instanceof Whitespace)
continue;
if (t instanceof Bracket && t.isopen()) {
stack.push([t, exprlist, tokensequence]);
exprlist = [];
tokensequence = [];
}
else if (t instanceof Comma) {
if (stack.length==0)
throw "comma without bracket";
exprlist.push(tokensequence);
tokensequence = [];
}
else if (t instanceof Bracket && t.isclose()) {
exprlist.push(tokensequence);
let closedlist = exprlist;
var topen;
[topen, exprlist, tokensequence] = stack.pop();
if (!t.closes(topen))
throw "mismatched brackettypes";
tokensequence.push(new ExpressionList(topen.type+t.type, closedlist));
}
else {
tokensequence.push(t)
}
}
if (stack.length) {
throw "unclosed brackets";
}
if (tokensequence.length) {
exprlist.push(tokensequence);
tokensequence = undefined;
}
if (exprlist.length==0)
return [];
if (exprlist.length>1)
throw "expected toplevel exprlist to have just one token sequence";
return exprlist[0];
}
class EmptyList {
toString() { return "EMPTY[]"; }
};
function parse(tokens)
{
/* parse a token stream into an expression tree */
var stack = [];
for (var t of tokens) {
if (t instanceof ExpressionList) {
t.values = t.values.map(s=>parse(s));
if (stack.length>0 && stack[stack.length-1] instanceof Name)
t = new Function(stack.pop().name, t.values)
}
else if (t instanceof Operator) {
// [..., +, item] && + < * ==> [..., *(+(item))]
while (stack.length>=2 && stack[stack.length-2] instanceof Operator && t.precedence() <= stack[stack.length-2].precedence()) {
let rhs = stack.pop();
stack[stack.length-1].add(rhs);
}
if (stack.length==0)
throw "unary operators not allowed";
t.add(stack.pop());
}
stack.push(t);
}
while (stack.length>=2 && stack[stack.length-2] instanceof Operator) {
let rhs = stack.pop();
stack[stack.length-1].add(rhs)
}
if (stack.length>1)
throw "too many items on the stack";
if (stack.length==0)
return new EmptyList()
return stack.pop()
}
function parseexpr(txt)
{
return parse(brackets(tokenizer(txt)));
}
function evaluator(e, ctx)
{
/* evaluate the expression with the specified context. */
if (e instanceof Operator) {
var fn;
if (e.op == "+") fn = ctx.add;
else if (e.op == "-") fn = ctx.sub;
else if (e.op == "*") fn = ctx.mul;
else if (e.op == "/") fn = ctx.div;
else if (e.op == "^") fn = ctx.pow;
else if (e.op == "=") {
// '=' handles its first argument diffrently, another solution would
// be to use some kind of reference mechanism, like in c++.
let [varname, ...rest] = e.args;
ctx.set(varname.name, ...rest.map(_=>evaluator(_, ctx)));
return;
}
else {
throw "unknown operator";
}
return fn(...e.args.map(_=>evaluator(_, ctx)));
}
if (e instanceof Function) {
fn = ctx[e.name];
return fn(...e.args.map(_=>evaluator(_, ctx)));
}
if (e instanceof Name)
return ctx.get(e.name);
if (e instanceof NumValue)
return ctx.numbervalue(e.value);
if (e instanceof TextValue)
return ctx.textvalue(e.value);
if (e instanceof ExpressionList) {
if (e.values.length==1)
return evaluator(e.values[0], ctx);
// todo, I need a way to propagate a state change in the ctx
// when processing values between '()',
// example: coordinates in an elliptic curve.
return ctx.listvalue(e.type, e.values.map(_=>evaluator(_, ctx)));
}
if (e instanceof EmptyList)
return [];
if (typeof(e) == "array")
return e.map(_=>evaluator(_, ctx));
throw "unknown object";
}