-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
46 lines (33 loc) · 1.06 KB
/
parser.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
import { add, minus, times, pow } from './functions'
import { number } from './stack'
const toFn = (params, functions) => line => stacks => {
const fn = line[0]
if (stacks[fn]) { return stacks[fn].push(resolve(params, line[1])) }
if (functions[fn]) { return functions[fn](stacks, ...line.slice(1).map(e => resolve(params, e))) }
}
const resolve = (params, value) => {
if (params[value]) { return params[value] }
if (parseInt(value)) { return parseInt(value) }
return value
}
export const createParser = (functions, stacks) => (code, params) => {
const lines = code
.map(toFn(params, functions))
.forEach(fn => fn(stacks))
}
export const createEvaluator = (functions, stacks, ret) => code => params => {
const parse = createParser(functions, stacks)
parse(code, params)
return ret(stacks)
}
export const evaluate = code => params => {
const num = number(['x'])
const parse = createParser({
add: add(num),
minus: minus(num),
times: times(num),
pow: pow(num),
}, { number: num })
parse(code, params)
return num.pop()
}