-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfantomlisp.fan
438 lines (403 loc) · 10.6 KB
/
fantomlisp.fan
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
class LObj {
Str tag
Int? num
Str? str
LObj? car
LObj? cdr
LObj? args
LObj? body
LObj? env
|LObj->LObj|? fn
new makeNil() {
this.tag = "nil"
this.str = "nil"
}
new makeError(Str s) {
this.tag = "error"
this.str = s
}
new makeNum(Int n) {
this.tag = "num"
this.num = n
}
new makeSym(Str s) {
this.tag = "sym"
this.str = s
}
new makeCons(LObj a, LObj d) {
this.tag = "cons"
this.car = a
this.cdr = d
}
new makeSubr(|LObj->LObj| fn) {
this.tag = "subr"
this.fn = fn
}
new makeExpr(LObj args, LObj env) {
this.tag = "expr"
this.args = args.car
this.body = args.cdr
this.env = env
}
}
class ParseState {
LObj obj
Str next
new make(LObj o, Str n) {
this.obj = o
this.next = n
}
}
class Lisp {
Int kLPar := '('
Int kRPar := ')'
Int kQuote := '\''
LObj kNil := LObj.makeNil()
LObj loop_val := LObj.makeNil()
LObj safeCar(LObj obj) {
if (obj.tag == "cons") return obj.car
return kNil
}
LObj safeCdr(LObj obj) {
if (obj.tag == "cons") return obj.cdr
return kNil
}
Str:LObj sym_table := [:]
LObj makeSym(Str str) {
if (str == "nil") return kNil
if (!sym_table.containsKey(str)) {
sym_table[str] = LObj.makeSym(str)
}
return sym_table[str]
}
LObj sym_t := makeSym("t")
LObj sym_quote := makeSym("quote")
LObj sym_if := makeSym("if")
LObj sym_lambda := makeSym("lambda")
LObj sym_defun := makeSym("defun")
LObj sym_setq := makeSym("setq")
LObj sym_loop := makeSym("loop")
LObj sym_return := makeSym("return")
LObj nreverse(LObj lst) {
ret := kNil
while (lst.tag == "cons") {
tmp := lst.cdr
lst.cdr = ret
ret = lst
lst = tmp
}
return ret
}
LObj pairlis(LObj lst1, LObj lst2) {
ret := kNil
while (lst1.tag == "cons" && lst2.tag == "cons") {
ret = LObj.makeCons(LObj.makeCons(lst1.car, lst2.car), ret)
lst1 = lst1.cdr
lst2 = lst2.cdr
}
return nreverse(ret)
}
Bool isSpace(Int c) {
return c == '\n' || c == '\r' || c == '\t' || c == ' '
}
Bool isDelimiter(Int c) {
return c == kLPar || c == kRPar || c == kQuote || isSpace(c)
}
Str skipSpaces(Str s) {
return Regex.fromStr("^\\s+").matcher(s).replaceFirst("")
}
LObj makeNumOrSym(Str str) {
Int? n := str.toInt(10, false)
if (n != null) {
return LObj.makeNum(n)
}
return makeSym(str)
}
ParseState readAtom(Str str) {
next := ""
for (i := 0; i < str.size(); ++i) {
if (isDelimiter(str[i])) {
next = str[i..-1]
str = str[0..i-1]
break
}
}
return ParseState.make(makeNumOrSym(str), next)
}
ParseState read(Str str) {
str = skipSpaces(str)
if (str.size() == 0) {
return ParseState.make(LObj.makeError("empty input"), "")
} else if (str[0] == kRPar) {
return ParseState.make(LObj.makeError("invalid syntax" + str), "")
} else if (str[0] == kLPar) {
return readList(str[1..-1])
} else if (str[0] == kQuote) {
tmp := read(str[1..-1])
return ParseState(LObj.makeCons(sym_quote,
LObj.makeCons(tmp.obj, kNil)),
tmp.next)
}
return readAtom(str)
}
ParseState readList(Str str) {
ret := kNil
while (true) {
str = skipSpaces(str)
if (str.size() == 0) {
return ParseState.make(LObj.makeError("unfinished parenthesis"), "")
} else if (str[0] == kRPar) {
break
}
ParseState tmp := read(str)
if (tmp.obj.tag == "error") {
return tmp
}
ret = LObj.makeCons(tmp.obj, ret)
str = tmp.next
}
return ParseState.make(nreverse(ret), str[1..-1])
}
Str printObj(LObj obj) {
if (obj.tag == "sym" || obj.tag == "nil") {
return obj.str
} else if (obj.tag == "num") {
return obj.num.toStr()
} else if (obj.tag == "error") {
return "<error: " + obj.str + ">"
} else if (obj.tag == "cons") {
return printList(obj)
} else if (obj.tag == "subr" || obj.tag == "expr") {
return "<" + obj.tag + ">"
}
return "<unknown>"
}
Str printList(LObj obj) {
ret := ""
first := true
while (obj.tag == "cons") {
if (first) {
first = false
} else {
ret += " "
}
ret += printObj(obj.car)
obj = obj.cdr
}
if (obj === kNil) {
return "(" + ret + ")"
}
return "(" + ret + " . " + printObj(obj) + ")";
}
LObj findVar(LObj sym, LObj env) {
while (env.tag == "cons") {
alist := env.car
while (alist.tag == "cons") {
if (alist.car.car === sym) {
return alist.car
}
alist = alist.cdr
}
env = env.cdr
}
return kNil
}
Void addToEnv(LObj sym, LObj val, LObj env) {
env.car = LObj.makeCons(LObj.makeCons(sym, val), env.car)
}
LObj eval(LObj obj, LObj env) {
if (obj.tag == "nil" || obj.tag == "num" || obj.tag == "error") {
return obj
} else if (obj.tag == "sym") {
bind := findVar(obj, env)
if (bind === kNil) {
return LObj.makeError(obj.str + " has no value")
}
return bind.cdr
}
op := safeCar(obj)
args := safeCdr(obj)
if (op === sym_quote) {
return safeCar(args)
} else if (op === sym_if) {
cond := eval(safeCar(args), env)
if (cond.tag == "error") {
return cond
} else if (cond === kNil) {
return eval(safeCar(safeCdr(safeCdr(args))), env)
}
return eval(safeCar(safeCdr(args)), env)
} else if (op === sym_lambda) {
return LObj.makeExpr(args, env)
} else if (op === sym_defun) {
expr := LObj.makeExpr(safeCdr(args), env)
sym := safeCar(args)
addToEnv(sym, expr, g_env)
return sym
} else if (op === sym_setq) {
val := eval(safeCar(safeCdr(args)), env)
if (val.tag == "error") {
return val
}
sym := safeCar(args)
bind := findVar(sym, env)
if (bind === kNil) {
addToEnv(sym, val, g_env)
} else {
bind.cdr = val
}
return val
} else if (op === sym_loop) {
return loop(args, env)
} else if (op === sym_return) {
loop_val = eval(safeCar(args), env)
return LObj.makeError("")
}
return apply(eval(op, env), evlis(args, env), env)
}
LObj evlis(LObj lst, LObj env) {
ret := kNil
while (lst.tag == "cons") {
elm := eval(lst.car, env)
if (elm.tag == "error") return elm
ret = LObj.makeCons(elm, ret)
lst = lst.cdr
}
return nreverse(ret)
}
LObj progn(LObj body, LObj env) {
ret := kNil
while (body.tag == "cons") {
ret = eval(body.car, env)
if (ret.tag == "error") {
return ret
}
body = body.cdr
}
return ret
}
LObj loop(LObj body, LObj env) {
while (true) {
ret := progn(body, env)
if (ret.tag == "error") {
if (ret.str == "") {
return loop_val
}
return ret
}
}
return kNil // Not reached.
}
LObj apply(LObj fn, LObj args, LObj env) {
if (fn.tag == "error") {
return fn
} else if (args.tag == "error") {
return args
} else if (fn.tag == "subr") {
return fn.fn(args)
} else if (fn.tag == "expr") {
return progn(fn.body, LObj.makeCons(pairlis(fn.args, args), fn.env))
}
return LObj.makeError(printObj(fn) + " is not function")
}
|LObj->LObj| subrCar := |LObj args -> LObj| {
return safeCar(safeCar(args))
}
|LObj->LObj| subrCdr := |LObj args -> LObj| {
return safeCdr(safeCar(args))
}
|LObj->LObj| subrCons := |LObj args -> LObj| {
return LObj.makeCons(safeCar(args), safeCar(safeCdr(args)))
}
|LObj->LObj| subrEq := |LObj args -> LObj| {
x := safeCar(args)
y := safeCar(safeCdr(args))
if (x.tag == "num" && y.tag == "num") {
if (x.num == y.num) {
return sym_t
}
return kNil
} else if (x == y) {
return sym_t
}
return kNil
}
|LObj->LObj| subrAtom := |LObj args -> LObj| {
if (safeCar(args).tag == "cons") {
return kNil
}
return sym_t
}
|LObj->LObj| subrNumberp := |LObj args -> LObj| {
if (safeCar(args).tag == "num") {
return sym_t
}
return kNil
}
|LObj->LObj| subrSymbolp := |LObj args -> LObj| {
if (safeCar(args).tag == "sym") {
return sym_t
}
return kNil
}
|LObj->LObj| subrAddOrMul(|Int,Int->Int| fn, Int init_val) {
return |LObj args -> LObj| {
ret := init_val
while (args.tag == "cons") {
if (args.car.tag != "num") return LObj.makeError("wrong type")
ret = fn(ret, args.car.num)
args = args.cdr
}
return LObj.makeNum(ret)
}
}
|LObj->LObj| subrSubOrDivOrMod(|Int,Int->Int| fn) {
return |LObj args -> LObj| {
x := safeCar(args)
y := safeCar(safeCdr(args))
if (x.tag != "num" || y.tag != "num") return LObj.makeError("wrong type")
return LObj.makeNum(fn(x.num, y.num))
}
}
|LObj->LObj| subrAdd
|LObj->LObj| subrMul
|LObj->LObj| subrSub
|LObj->LObj| subrDiv
|LObj->LObj| subrMod
LObj g_env := LObj.makeCons(kNil, kNil)
new make() {
subrAdd = subrAddOrMul(|Int x, Int y -> Int| { return x + y }, 0)
subrMul = subrAddOrMul(|Int x, Int y -> Int| { return x * y }, 1)
subrSub = subrSubOrDivOrMod(|Int x, Int y -> Int| { return x - y })
subrDiv = subrSubOrDivOrMod(|Int x, Int y -> Int| { return x / y })
subrMod = subrSubOrDivOrMod(|Int x, Int y -> Int| { return x % y })
addToEnv(makeSym("car"), LObj.makeSubr(subrCar), g_env)
addToEnv(makeSym("cdr"), LObj.makeSubr(subrCdr), g_env)
addToEnv(makeSym("cons"), LObj.makeSubr(subrCons), g_env)
addToEnv(makeSym("eq"), LObj.makeSubr(subrEq), g_env)
addToEnv(makeSym("atom"), LObj.makeSubr(subrAtom), g_env)
addToEnv(makeSym("numberp"), LObj.makeSubr(subrNumberp), g_env)
addToEnv(makeSym("symbolp"), LObj.makeSubr(subrSymbolp), g_env)
addToEnv(makeSym("+"), LObj.makeSubr(subrAdd), g_env)
addToEnv(makeSym("*"), LObj.makeSubr(subrMul), g_env)
addToEnv(makeSym("-"), LObj.makeSubr(subrSub), g_env)
addToEnv(makeSym("/"), LObj.makeSubr(subrDiv), g_env)
addToEnv(makeSym("mod"), LObj.makeSubr(subrMod), g_env)
addToEnv(sym_t, sym_t, g_env)
}
}
class Main {
static Void main() {
Lisp lisp := Lisp.make()
env := Env.cur()
env.out().print("> ")
env.out().flush()
line := env.in().readLine()
while (line != null) {
echo(lisp.printObj(lisp.eval(lisp.read(line).obj, lisp.g_env)))
env.out().print("> ")
env.out().flush()
line = env.in().readLine()
}
}
}