-
Notifications
You must be signed in to change notification settings - Fork 0
/
imp-eval.mjs
221 lines (200 loc) · 6.84 KB
/
imp-eval.mjs
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
import { T, P, nil, end, TreeBuilder } from './imp-core.mjs'
import * as imp from './imp-core.mjs'
import { impShow } from './imp-show.mjs'
import { read } from './imp-read.mjs'
import * as assert from "assert"
let impWords = {
'nil': nil,
'+' : imp.jdy((x,y)=>imp.int(x[2]+y[2])),
'-' : imp.jdy((x,y)=>imp.int(x[2]-y[2])),
'*' : imp.jdy((x,y)=>imp.int(x[2]*y[2])),
'%' : imp.jdy((x,y)=>imp.int(Math.floor(x[2]/y[2]))),
'read': imp.jsf(x=>read(x), 1),
'xmls': imp.jsf(x=>imp.str(toXml(x)), 1),
'look': imp.jsf(x=>imp.str(impWords[x[2]]??"nil"), 1),
'eval': imp.jsf(x=>eval(x), 1),
'part': imp.jsf(x=>imp.str(wordClass(x)), 1),
'show': imp.jsf(x=>imp.str(impShow(x)), 1),
'echo': imp.jsf(x=>(console.log(x[2]), nil), 1),
}
function xmlTag(tag, attrs, content) {
let attrStr = Object.entries(attrs).map(([k,v])=>`${k}="${v}"`).join(' ')
if (content) return `<${tag} ${attrStr}>${content}</${tag}>`
else return `<${tag} ${attrStr}/>`
}
function toXml(impv) {
let [t, a, v] = impv
switch (t) {
case T.SEP:
case T.INT:
case T.STR:
return xmlTag('imp:'+t.toLowerCase(),{v})
case T.NIL: return '<nil/>'
case T.SYM: return xmlTag('imp:sym', {v:`${v.description}`})
case T.TOP:
case T.LST:
return xmlTag('imp:'+t.toLowerCase(), a,
'\n ' + v.map(toXml).join('\n ') + '\n')
default: }
}
function wordClass(x) {
let [xt, xa, xv] = x
switch (xt) {
case T.TOP: return P.N
case T.END: return P.E
case T.INT: return P.N
case T.STR: return P.N
case T.MLS: return P.N
case T.LST: return P.N
// -- resolved symbols:
case T.JSF: return P.V
case T.NIL: return P.N
case T.JDY: return P.O
default: throw "[wordClass] invalid argument:" + x }}
class ImpEvaluator {
words = impWords
stack = []
item = []; wc = null; pos = 0; wcs = [];
constructor(root) {
this.root = root
this.here = this.root
this.stack = []; this.pos = 0 }
enter = (xs)=> {
this.stack.push([this.here, this.pos, this.wcs])
this.pos=0; this.here=xs[2]; this.wcs=[]}
leave = (xs)=> {[this.here, this.pos, this.wcs] = this.stack.pop()}
atEnd = ()=> this.pos >= this.here.length
/// sets this.item and this.wc
nextItem = ()=> {
let x = (this.pos >= this.here.length) ? end : this.here[this.pos++]
let [t, a, v] = x
if (t === T.SYM) {
switch (v.description[0]) {
case '`': this.wc = P.Q; break // TODO: literal word
case '.': this.wc = P.M; break // TODO: method
case ':': this.wc = P.G; break // getter
default: {
if (v.description[-1] === ':') this.wc = P.S
else { // normal symbol, so look it up
let w = this.words[v.description]
if (w) x = w, this.wc = this.wordClass(w)
else throw "undefined word: " + v.description }}}}
else this.wc = this.wordClass(x)
this.wcs.push(this.wc)
return this.item = x}
peek = ()=> {
if (this.atEnd()) return {item:null, wc:null}
let [item, wc, pos] = [this.item, this.wc, this.pos]
this.nextItem()
let [peekItem, peekWC] = [this.item, this.wc]
// !! why does this give "TypeError: Cannot create property '2' on number '1'" ?!?
// [this.item, this.wc, this.pos] = [item, wc, pos]
this.item = item; this.wc = wc; this.pos = pos
let res = {'item':peekItem, 'wc':peekWC}
return res}
modifyNoun = (x)=> {
// if next token is infix operator (dyad), apply it to x and next noun
let p, res = x
while ((p = this.peek()).wc === P.O) {
let op = this.nextItem()
let arg = this.nextItem()
res = op[2].apply(this, [res, arg]) }
return res }
nextNoun = ()=> {
// read a noun, after applying chains of infix operators
let res = this.nextItem()
if (this.wc === P.N) res = this.modifyNoun(res)
else throw "expected a noun, got: " + res
// todo: collect multiple numbers or quoted symbols into a vector
// todo: if it's a symbol that starts with ., that's also infix (it's a method)
return res }
wordClass = (x)=> wordClass(x)
// keep the peeked-at item
keep = (p)=> { this.item=p.item; this.wc=p.wc; this.pos++ }
modifyVerb = (v0) => {
let p, res = v0
while ([P.V, P.A, P.P].includes((p = this.peek()).wc)) {
this.keep(p)
switch (p.wc) {
case P.V: // TODO: composition (v u)
assert.ok(res[1].arity===1, "oh no")
let u = res[2]
let v = p.item[2]
res = imp.jsf((x)=>u(v(x)), 1)
break
case P.A: // TODO: adverb (v/)
case P.P: // TODO: preposition (v -arg)
case P.C: // TODO: conjunction (v &. u)
}
}
return res
}
// evaluate a list
evalList = (xs)=> {
// walk from left to right, building up values to emit
let tmp = [], done = false, tb = new TreeBuilder()
this.enter(xs)
while (!done) {
// skip separators
do {this.nextItem() } while (this.item[0] === T.SEP && !this.atEnd())
if (this.atEnd()) done = true
let x = this.item
switch (this.wc) {
case P.V: // verb
x = this.modifyVerb(x)
let args = []
for (let i = 0; i < x[1].arity; i++) { args.push(this.nextNoun()) }
tb.emit(x[2].apply(this, args))
break
case P.N:
// process.stderr.write(`noun: ${impShow(x)}\n`)
// if x[0] === T.LST {}
x = this.eval(x)
x = this.modifyNoun(x)
tb.emit(x)
break
case P.Q:
tb.emit(x)
break
case P.E:
break
default: throw "evalList: invalid word class: " + this.wc
}}
this.leave()
return tb.root}
// evaluate a list but return last expression
lastEval = (xs)=> {
let res = this.evalList(xs)
return res.length ? res.pop() : nil }
// project a function
project = (sym, xs)=> {
let f = this.words[sym]
if (!f) throw "[project]: undefined word: " + sym
let args = [], arg = imp.lst()
for (let x of xs) {
if (x[0] === T.SEP) { args.push(arg); arg = imp.lst() }
else imp.push(arg,x)}
args.push(arg)
let res = f[2].apply(this, args.map(this.lastEval))
return res}
// evaluate an expression
eval = (x)=> {
let [t, a, v] = x
switch (t) {
case T.TOP: return this.lastEval(x)
case T.SEP: return nil
case T.NIL: return x
case T.INT: return x
case T.STR: return x
case T.MLS: return x
case T.SYM: return x
case T.LST:
let m = a.open.match(/^(.+)([[({])$/)
if (m) { let sym = m[1]; switch (m[2]) {
case '[': return this.project(sym, v)
// case '(': TODO
// case '{': TODO
default: return imp.lst(a, this.evalList(x))}}
else return imp.lst(a, this.evalList(x))
default: throw "invalid imp value:" + JSON.stringify(x) }}}
export let impEval = (x)=> new ImpEvaluator(x).eval(x)