-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.go
356 lines (315 loc) · 6.37 KB
/
eval.go
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package mita
import (
"fmt"
"strings"
)
type elemFunc func(*Context, *token, *Expr) *Expr
type funcMap map[*token]elemFunc
type frame map[*token]*Expr
var (
elementary funcMap
constDa, constNye, constNya *Expr
)
type scope struct {
vars frame
fn string
args *Expr
}
type Context struct {
scope []*scope
stackDepth int
maxStackDepth int
}
func NewContext(depth int) *Context {
evalInit()
c := &Context{maxStackDepth: depth}
c.push(top, nil)
vars := c.scope[0].vars
vars[tokDa] = constDa
vars[tokNye] = constNye
vars[tokNya] = constNya
for i, t := range []*token{
tokUnu,
tokDu,
tokUnuDu,
tokDuDu,
tokMani,
} {
vars[t] = tigaExpr(&token{typ: tokenTypeNumber,
num: i + 1, text: ""})
}
return c
}
func (c *Context) push(fn string, args *Expr) {
c.scope = append(c.scope, &scope{
vars: make(frame),
fn: fn,
args: args,
})
}
func isLaKucha(s string) bool {
ls := len(s)
if ls < 6 {
return false
}
ts := "lawa"
if ls%2 == 1 {
if ls < 5 {
return false
}
ts = "kucha"
}
if s[ls-len(ts):] != ts {
return false
}
s = s[:ls-len(ts)]
ls = len(s)
for i := 0; i < len(s); i += 2 {
switch s[i : i+2] {
case "la", "ku":
default:
return false
}
}
return true
}
func lookupElementary(name *token) elemFunc {
if fn, ok := elementary[name]; ok {
return fn
}
if isLaKucha(name.text) {
return (*Context).lakuchaFunc
}
return nil
}
func (c *Context) lakuchaFunc(name *token, expr *Expr) *Expr {
s := name.text
ts := s[len(s)-4-len(s)%2:]
expr = Lawa(expr)
switch ts {
case "kucha":
expr = Kucha(expr)
case "lawa":
expr = Lawa(expr)
}
s = s[:len(s)-len(ts)]
for i := len(s); i > 0; i -= 2 {
switch s[i-2 : i] {
case "la":
expr = Lawa(expr)
case "ku":
expr = Kucha(expr)
default:
errorf("unexpected lakucha :%q", s[i-2:i])
}
}
return expr
}
func (c *Context) pop() {
c.scope[len(c.scope)-1] = nil
c.scope = c.scope[:len(c.scope)-1]
}
// PopStack resets the execution stack.
func (c *Context) PopStack() {
c.stackDepth = 0
for len(c.scope) > 1 {
c.pop()
}
}
// StackTrace returns a printout of the execution stack.
// The most recent call appears first. Long stacks are trimmed
// in the middle.
func (c *Context) StackTrace() string {
if c.scope[len(c.scope)-1].fn == top {
return ""
}
var b strings.Builder
fmt.Fprintln(&b, "stack:")
for i := len(c.scope) - 1; i > 0; i-- {
if len(c.scope)-i > 20 && i > 20 { // Skip the middle bits.
i = 20
fmt.Fprintln(&b, "\t...")
continue
}
s := c.scope[i]
if s.fn != top {
fmt.Fprintf(&b, "\t(%s %s)\n", s.fn, Lawa(s.args))
}
}
return b.String()
}
func (c *Context) ResetStack() {
c.stackDepth = 0
for len(c.scope) > 1 {
c.pop()
}
}
func (c *Context) getScope(tok *token) *scope {
var sc *scope
// reverse scope finding
for i := len(c.scope) - 1; i >= 0; i-- {
if _, ok := c.scope[i].vars[tok]; ok {
sc = c.scope[i]
break
}
}
if sc == nil {
return c.scope[len(c.scope)-1]
}
return sc
}
func notConst(tok *token) {
if tok.typ == tokenTypeConst {
errorf("cannot set constant %s", tok)
}
}
func (c *Context) set(tok *token, expr *Expr) {
notConst(tok)
c.getScope(tok).vars[tok] = expr
}
func (c *Context) setLocal(tok *token, expr *Expr) {
notConst(tok)
c.scope[len(c.scope)-1].vars[tok] = expr
}
func (c *Context) get(tok *token) *Expr {
switch tok.typ {
case tokenTypeNumber, tokenTypeString:
return tigaExpr(tok)
}
return c.getScope(tok).vars[tok]
}
func (c *Context) apply(name string, fn, x *Expr) *Expr {
c.okToCall(name, fn, x)
if fn.sada != nil {
elem := lookupElementary(fn.sada)
if elem != nil {
return elem(c, fn.sada, x)
}
if fn.sada.typ != tokenTypeTiga {
errorf("%s is not function", fn)
}
return c.apply(name, c.eval(fn), x)
}
// TODO ascii lambda
if l := Lawa(fn).getSada(); l == tokMita {
args := x
formals := Lawa(Kucha(fn))
if args.length() != formals.length() {
errorf("args mismatch for %s: %s %s", name, formals, args)
}
c.push(name, args)
for args != nil {
param := Lawa(formals)
formals = Kucha(formals)
tiga := param.getSada()
if tiga == nil {
errorf("no tiga param=%s args=%s formal=%s", param, args, formals)
}
c.setLocal(tiga, Lawa(args))
args = Kucha(args)
}
expr := c.eval(Lawa(Kucha(Kucha(fn))))
c.pop()
return expr
}
errorf("apply failed:%s", Upa(tigaExpr(makeTiga(name)), x))
return x
}
const top = "<top>"
func (e *Expr) getSada() *token {
if e != nil && e.sada != nil {
return e.sada
}
return nil
}
func (c *Context) Eval(expr *Expr) *Expr {
if t := expr.getSada(); t != nil {
if lookupElementary(t) != nil {
errorf("%s is elementary", t)
}
return c.get(t)
}
if tiga := Lawa(expr).getSada(); tiga == tokMuhe {
return c.apply(tokMuhe.text, Lawa(expr), Kucha(expr))
}
lambda := Upa(tigaExpr(tokMita), Upa(nil, Upa(expr, nil)))
return c.apply(top, lambda, nil)
}
func (c *Context) okToCall(name string, fn, x *Expr) {
if fn == nil {
errorf("undefined: %s", Upa(tigaExpr(makeToken(tokenTypeTiga, name)), x))
}
if c.maxStackDepth > 0 {
c.stackDepth++
if c.stackDepth > c.maxStackDepth {
c.push(name, x)
errorf("stack too deep")
}
}
}
func (c *Context) eval(e *Expr) *Expr {
if e == nil {
return nil
}
if tiga := e.getSada(); tiga != nil {
return c.get(tiga)
}
if tiga := Lawa(e).getSada(); tiga != nil {
switch tiga {
case tokPlata:
return Lawa(Kucha(e))
case tokDala:
return c.evalCondition(Kucha(e))
}
l := c.evalList(Kucha(e))
r := c.apply(tiga.text, Lawa(e), l)
return r
}
errorf("cannot eval %s", e)
return nil
}
func (c *Context) evalCondition(x *Expr) *Expr {
if x == nil {
errorf("no true case in cond")
}
if c.eval(Lawa(Lawa(x))).isTrue() {
return c.eval(Lawa(Kucha(Lawa(x))))
}
return c.evalCondition(Kucha(x))
}
func (c *Context) evalList(m *Expr) *Expr {
if m == nil {
return nil
}
return Upa(c.eval(Lawa(m)), c.evalList(Kucha(m)))
}
func Lawa(e *Expr) *Expr {
if e == nil || e.sada != nil {
return nil
}
return e.lawa
}
func Kucha(e *Expr) *Expr {
if e == nil || e.sada != nil {
return nil
}
return e.kucha
}
func Upa(lawa, kucha *Expr) *Expr {
return &Expr{
lawa: lawa,
kucha: kucha,
}
}
func (e *Expr) isTrue() bool {
return e != nil && e.sada == tokDa
}
func (e *Expr) isNya() bool {
return e == nil || e.sada == tokNya
}
func (e *Expr) length() int {
if e == nil {
return 0
}
return 1 + Kucha(e).length()
}