-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path41.js
More file actions
312 lines (276 loc) · 7.84 KB
/
41.js
File metadata and controls
312 lines (276 loc) · 7.84 KB
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
/**
在开发前端框架、模版引擎的时候,经常会需要我们在特定的上下文中,动态分析、执行特定的表达式。例如:在 { x: 1, y: 2, z: 3 } 的上下文中执行表达式 x + y 那么就会得到 3,执行 z - x 就会得到 2。
请你完成 execute 函数,接受一个字符串和对象作为参数,它可以在特定的上下文中执行任意的表达式,例如:
execute(`'My name is ' + name`, { name: 'Jerry' }) // => My name is Jerry
execute('monkeys.length + 1', { monkeys: [1, 2, 3] }) // => 4
execute('user.name + user.age', { user: { name: 'Jerry', age: 12 } }) // => Jerry12
execute('run()', { run: () => 'Good Night' }) // => Good Night
...
(你能否想出不使用 with 的方案?)
*/
// with
const execute = (exp, data) => eval(`with(data){${exp}}`);
// function
const execute = (exp, data) => new Function(...Object.keys(data), `return ${exp}`)(...Object.values(data));
// vm + complier
class Stream {
constructor(tokens) {
this.tokens = tokens
this.pos = 0
}
get next() {
return this.tokens[this.pos++] || ''
}
cancel() {
this.pos--
}
get done() {
return this.pos >= this.tokens.length
}
get rest() {
return this.tokens.slice(this.pos)
}
transaction(fn) {
const rpos = this.pos
const ret = fn()
if (ret === failure) {
this.pos = rpos
return failure
}
return ret
}
}
function preventRec(fn) {
const stack = []
return stream => {
if (stack.includes(stream.pos)) {
return ignore
}
stack.push(stream.pos)
const ret = fn(stream)
stack.pop()
return ret
}
}
const failure = Symbol('failure')
const ignore = Symbol('ignore')
const eq = str => stream => stream.transaction(() => stream.next == str ? ignore : failure)
const re = regex => fn => stream => stream.transaction(() => {
const ret = stream.next.match(regex)
if (!ret) return failure
return fn(ret)
})
const assembly = (...parsers) => stream => stream.transaction(() => {
console.log('assembly')
let result = ignore
for (const parser of parsers) {
result = parser(result)(stream)
if (result == failure)
return failure
}
return result
})
const chain = (...parsers) => fn => stream => stream.transaction(() => {
const arr = []
for (const parser of parsers) {
const result = parser(stream)
if (result == failure)
return failure
if (result != ignore)
arr.push(result)
}
return fn(...arr)
})
const choice = (...parsers) => stream => {
for (const parser of parsers) {
const result = parser(stream)
if (result != failure)
return result
}
return failure
}
const repeatRange = parser => (low, high = Number.MAX_SAFE_INTEGER) => stream => stream.transaction(() => {
const arr = []
let i = 0
for (; i < high; i++) {
const result = parser(stream)
if (result == failure)
break
if (result != ignore)
arr.push(result)
}
if (i < low || i > high) return failure
return arr
})
const repeat = parser => number => repeatRange(parser)(number, number)
const many = parser => repeatRange(parser)(1)
const zeroOrMany = parser => repeatRange(parser)(0)
const maybe = parser => repeatRange(parser)(0, 1)
const must = parser => repeatRange(parser)(1, 1)
const packer = parser => fn => stream => {
const ret = parser(stream)
if (ret === failure)
return failure
return fn(ret)
}
const $log = (...param) => stream => {
//console.log(...param, stream.rest)
return ignore
}
const $lazy = fn => a => b => fn(a)(b)
let Parser
Parser = (() => {
const number = re(/^\d+(\.\d+)?$/)(x => ({op: 'immi', value: parseFloat(x[0])}))
const string = re(/^'((?:\\'|[^'])+)'$/)(x => ({op: 'imms', value: JSON.parse(`"${x[1].replace('"', '\\"')}"`)}))
const name = re(/^\w+$/)(x => x[0])
let expression
const variable = chain(
name,
zeroOrMany(chain(
eq('.'),
name
)(x => x))
)((x, xs) => ({op: 'arg', path: [x, ...xs]}))
const func_call = chain(
preventRec(s => expression(s)),
eq('('),
maybe(
chain(
s => expression(s),
zeroOrMany(
chain(
eq(','),
s => expression(s)
)(x => x)
)
)((x, xs) => [x, ...xs])
),
eq(')')
)((func, params) => ({op: 'call', func, params: params[0] || []}))
const factor = choice(
number,
string,
chain(
eq('('),
s => expression(s),
eq(')')
)(ex => ex),
func_call,
variable
)
const term = choice(
chain(
factor,
many(
chain(
re(/^[*/%]$/g)(x => x[0]),
factor
)((...x) => x)
)
)((x, xs) => xs.reduce((a, [op, b]) => ({op, a, b}), x)),
factor
)
expression = choice(
chain(
term,
many(
chain(
re(/^[+-]$/g)(x => x[0]),
term
)((...x) => x)
)
)((x, xs) => xs.reduce((a, [op, b]) => ({op, a, b}), x)),
term
)
return expression
})()
class Compiler {
compile(code) {
return this.pass3(this.pass2(this.pass1(code)))
}
tokenizer(str) {
return str.match(/(?:'(?:\\'|[^'])+'|[\+\-\*\/\.\(\),]|\w+)/g)
}
pass1(code) {
return Parser(new Stream(this.tokenizer(code)))
}
pass2(ast) {
const opmap = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b,
}
let simp
simp = snode => {
const node = Object.assign({}, snode)
if (node.op in opmap) {
node.a = simp(node.a)
node.b = simp(node.b)
if (node.a.op == 'immi' && node.b.op == 'immi')
return {op: 'immi', value: opmap[node.op](node.a.value, node.b.value)}
} else if (node.op == 'call') {
node.params = node.params.map(param => simp(param))
}
return node
}
return simp(ast)
}
pass3(ast) {
console.log(JSON.stringify(ast))
const opmap = {
'+': node => load => [...load(node.a), 'PUSH', ...load(node.b), 'SWAP', 'POP_', 'ADD_'],
'-': node => load => [...load(node.a), 'PUSH', ...load(node.b), 'SWAP', 'POP_', 'SUB_'],
'*': node => load => [...load(node.a), 'PUSH', ...load(node.b), 'SWAP', 'POP_', 'MUL_'],
'/': node => load => [...load(node.a), 'PUSH', ...load(node.b), 'SWAP', 'POP_', 'DIV_'],
'immi': node => load => [`IMMI ${node.value}`],
'imms': node => load => [`IMMS ${node.value}`],
'arg': node => load => [`ARG_ ${node.path[0]}`, ...node.path.slice(1).map(x => `PROP ${x}`)],
'call': node => load => [...node.params.map(param => [...load(param), 'PUSH']).reduce((p, c) => [...p, ...c], []), `IMMI ${node.params.length}`, 'SWAP', ...load(node.func), 'CALL']
}
let walk
walk = node => opmap[node.op](node)(walk)
return walk(ast)
}
}
class VM {
load(code) {
this.code = code
return this
}
execute(variable) {
let r0, r1
const stack = []
const opmap = {
'ADD_': rest => r0 = r0 + r1,
'SUB_': rest => r0 = r0 - r1,
'MUL_': rest => r0 = r0 * r1,
'DIV_': rest => r0 = r0 / r1,
'IMMI': rest => r0 = parseFloat(rest),
'IMMS': rest => r0 = rest,
'ARG_': rest => r0 = variable[rest],
'PROP': rest => r0 = r0[rest],
'CALL': rest => r0 = r0(...[...Array(parseInt(r1))].map(() => stack.pop()).reverse()),
'PUSH': rest => stack.push(r0),
'POP_': rest => r0 = stack.pop(),
'SWAP': rest => {
const tmp = r0
r0 = r1
r1 = tmp
}
}
for (const item of this.code) {
const instruction = item.substring(0, 4).toUpperCase()
const rest = item.substring(5)
console.log(instruction, rest, JSON.stringify({r0, r1, stack}))
opmap[instruction](rest)
}
return r0
}
}
const execute = (code, variable) => {
const compiler = new Compiler()
const vm = new VM()
console.log(compiler.compile(code));
return vm.load(compiler.compile(code)).execute(variable)
}