-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkotlinlisp.kt
455 lines (413 loc) · 9.32 KB
/
kotlinlisp.kt
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package lisp
val kLPar = '('
val kRPar = ')'
val kQuote = '\''
class Nil {
}
val kNil = Nil()
class Num(n : Int) {
val data = n
}
class Sym(s : String) {
val data = s
}
var sym_table : MutableMap<String, Any> = hashMapOf()
fun makeSym(s : String) : Any {
if (s == "nil") {
return kNil
}
val sym : Any? = sym_table.get(s)
if (sym != null) {
return sym
}
val new_sym = Sym(s)
sym_table.put(s, new_sym)
return new_sym
}
val sym_t = makeSym("t")
val sym_quote = makeSym("quote")
val sym_if = makeSym("if")
val sym_lambda = makeSym("lambda")
val sym_defun = makeSym("defun")
val sym_setq = makeSym("setq")
class Error(s : String) {
val data = s
}
class Cons(a : Any, d : Any) {
var car = a
var cdr = d
}
class Subr(f : (Any) -> Any) {
val fn = f
}
class Expr(a : Any, b : Any, e : Any) {
val args = a
val body = b
val env = e
}
fun safeCar(obj : Any) : Any {
return when (obj) {
is Cons -> obj.car
else -> kNil
}
}
fun safeCdr(obj : Any) : Any {
return when (obj) {
is Cons -> obj.cdr
else -> kNil
}
}
fun nreverse(l : Any) : Any {
var lst = l
var ret : Any = kNil
while (true) {
val elm = lst
if (elm is Cons) {
val tmp = elm.cdr
elm.cdr = ret
ret = lst
lst = tmp
} else {
break
}
}
return ret
}
fun pairlis(l1 : Any, l2 : Any) : Any {
var lst1 = l1
var lst2 = l2
var ret : Any = kNil
while (true) {
val elm1 = lst1
val elm2 = lst2
if (elm1 is Cons && elm2 is Cons) {
ret = Cons(Cons(elm1.car, elm2.car), ret)
lst1 = elm1.cdr
lst2 = elm2.cdr
} else {
break
}
}
return nreverse(ret)
}
fun makeExpr(a : Any, e : Any) : Any = Expr(safeCar(a), safeCdr(a), e)
fun isSpace(c : Char) = c == '\t' || c == '\r' || c == '\n' || c == ' '
fun isDelimiter(c: Char) = c == kLPar || c == kRPar || c == kQuote || isSpace(c)
fun skipSpaces(str : String) : String {
for (i in 0..str.length-1) {
if (!isSpace(str[i])) {
return str.substring(i)
}
}
return ""
}
fun makeNumOrSym(str : String) : Any {
try {
return Num(str.toInt())
} catch (e : java.lang.NumberFormatException) {
return makeSym(str)
}
}
class ParseState(o : Any, s : String) {
val obj = o
val next = s
}
fun readAtom(s : String) : ParseState {
var str = s
var next = ""
for (i in 0..str.length-1) {
if (isDelimiter(str[i])) {
next = str.substring(i)
str = str.substring(0, i)
break
}
}
return ParseState(makeNumOrSym(str), next)
}
fun parseError(s : String) = ParseState(Error(s), "")
fun read(s : String) : ParseState {
val str = skipSpaces(s)
if (str == "") {
return parseError("empty input")
} else if (str[0] == kRPar) {
return parseError("invalid syntax: " + str);
} else if (str[0] == kLPar) {
return readList(str.substring(1))
} else if (str[0] == kQuote) {
val tmp = read(str.substring(1))
return ParseState(Cons(sym_quote, Cons(tmp.obj, kNil)), tmp.next)
}
return readAtom(str)
}
fun readList(s : String) : ParseState {
var str = s
var ret : Any = kNil
while (true) {
str = skipSpaces(str)
if (str == "") {
parseError("unfinished parenthesis")
} else if (str[0] == kRPar) {
break
}
val tmp = read(str)
if (tmp.obj is Error) {
return tmp
}
ret = Cons(tmp.obj, ret)
str = tmp.next
}
return ParseState(nreverse(ret), str.substring(1))
}
fun printObj(obj : Any) : String {
return when (obj) {
is Nil -> "nil"
is Num -> obj.data.toString()
is Sym -> obj.data
is Error -> "<error: " + obj.data + ">"
is Cons -> printList(obj)
is Subr -> "<subr>"
is Expr -> "<expr>"
else -> "<unknown>"
}
}
fun printList(o : Any) : String {
var obj = o
var ret = ""
var first = true
while (true) {
val elm = obj
when (elm) {
is Cons -> {
if (first) {
first = false
} else {
ret += " "
}
ret += printObj(elm.car)
obj = elm.cdr
}
else -> break
}
}
if (obj is Nil) {
return "(" + ret + ")"
}
return "(" + ret + " . " + printObj(obj) + ")"
}
fun findVar(sym : Any, e : Any) : Any {
var env = e
while (true) {
val ev = env
if (ev is Cons) {
var alist = ev.car
while (true) {
val al = alist
if (al is Cons) {
if (safeCar(al.car) == sym) {
return al.car
}
alist = al.cdr
} else {
break
}
}
env = ev.cdr
} else {
break
}
}
return kNil
}
var g_env = Cons(kNil, kNil)
fun addToEnv(sym : Any, value : Any, env : Any) {
if (env is Cons) {
env.car = Cons(Cons(sym, value), env.car)
}
}
fun eval(obj : Any, env : Any) : Any {
if (obj is Nil || obj is Num || obj is Error) {
return obj
} else if (obj is Sym) {
val bind = findVar(obj, env)
if (bind is Cons) {
return bind.cdr
}
return Error(obj.data + " has no value")
}
val op = safeCar(obj)
val args = safeCdr(obj)
if (op == sym_quote) {
return safeCar(args)
} else if (op == sym_if) {
val c = eval(safeCar(args), env)
if (c is Error) {
return c
} else if (c == kNil) {
return eval(safeCar(safeCdr(safeCdr(args))), env)
}
return eval(safeCar(safeCdr(args)), env)
} else if (op == sym_lambda) {
return makeExpr(args, env)
} else if (op == sym_defun) {
val expr = makeExpr(safeCdr(args), env)
val sym = safeCar(args)
addToEnv(sym, expr, g_env)
return sym
} else if (op == sym_setq) {
val value = eval(safeCar(safeCdr(args)), env)
val sym = safeCar(args)
val bind = findVar(sym, env)
if (bind is Cons) {
bind.cdr = value
} else {
addToEnv(sym, value, g_env)
}
return value
}
return apply(eval(op, env), evlis(args, env))
}
fun evlis(l : Any, env : Any) : Any {
var lst = l
var ret : Any = kNil
while (true) {
val elm = lst
when (elm) {
is Cons -> {
val obj = eval(elm.car, env)
if (obj is Error) {
return obj
}
ret = Cons(obj, ret)
lst = elm.cdr
}
else -> break
}
}
return nreverse(ret)
}
fun progn(b : Any, env : Any) : Any {
var body = b
var ret : Any = kNil
while (true) {
val elm = body
if (elm is Cons) {
ret = eval(elm.car, env)
body = elm.cdr
} else {
break
}
}
return ret
}
fun apply(fn : Any, args : Any) : Any {
if (fn is Error) {
return fn
} else if (args is Error) {
return args
} else if (fn is Subr) {
return fn.fn(args)
} else if (fn is Expr) {
return progn(fn.body, Cons(pairlis(fn.args, args), fn.env))
}
return Error(printObj(fn) + " is not function")
}
val subrCar = {(args : Any) : Any ->
safeCar(safeCar(args))
}
val subrCdr = {(args : Any) : Any ->
safeCdr(safeCar(args))
}
val subrCons = {(args : Any) : Any ->
Cons(safeCar(args), safeCar(safeCdr(args)))
}
fun subrEqImpl(args : Any) : Any {
val x = safeCar(args)
val y = safeCar(safeCdr(args))
if (x is Num && y is Num) {
if (x.data == y.data) {
return sym_t
}
return kNil
} else if (x == y) {
return sym_t
}
return kNil
}
val subrEq = {(args : Any) : Any -> subrEqImpl(args)}
val subrAtom = {(args : Any) : Any ->
if (safeCar(args) is Cons) kNil
else sym_t
}
val subrNumberp = {(args : Any) : Any ->
if (safeCar(args) is Num) sym_t
else kNil
}
val subrSymbolp = {(args : Any) : Any ->
if (safeCar(args) is Sym) sym_t
else kNil
}
fun subrAddOrMul(fn : (Int, Int) -> Int, init_val : Int, a : Any) : Any {
var args = a
var ret = init_val
while (true) {
val elm = args
if (elm is Cons) {
val num = elm.car
if (num is Num) {
ret = fn(ret, num.data)
args = elm.cdr
} else {
return Error("wrong type")
}
} else {
break
}
}
return Num(ret)
}
val subrAdd = {(args : Any) : Any ->
subrAddOrMul({(x : Int, y : Int) : Int -> x + y}, 0, args)
}
val subrMul = {(args : Any) : Any ->
subrAddOrMul({(x : Int, y : Int) : Int -> x * y}, 1, args)
}
fun subrSubOrDivOrMod(fn : (Int, Int) -> Int, args : Any) : Any {
val x = safeCar(args)
val y = safeCar(safeCdr(args))
if (x is Num && y is Num) {
return Num(fn(x.data, y.data))
}
return Error("wrong type")
}
val subrSub = {(args : Any) : Any ->
subrSubOrDivOrMod({(x : Int, y : Int) : Int -> x - y}, args)
}
val subrDiv = {(args : Any) : Any ->
subrSubOrDivOrMod({(x : Int, y : Int) : Int -> x / y}, args)
}
val subrMod = {(args : Any) : Any ->
subrSubOrDivOrMod({(x : Int, y : Int) : Int -> x % y}, args)
}
fun main(args: Array<String>): Unit {
addToEnv(makeSym("car"), Subr(subrCar), g_env)
addToEnv(makeSym("cdr"), Subr(subrCdr), g_env)
addToEnv(makeSym("cons"), Subr(subrCons), g_env)
addToEnv(makeSym("eq"), Subr(subrEq), g_env)
addToEnv(makeSym("atom"), Subr(subrAtom), g_env)
addToEnv(makeSym("numberp"), Subr(subrNumberp), g_env)
addToEnv(makeSym("symbolp"), Subr(subrSymbolp), g_env)
addToEnv(makeSym("+"), Subr(subrAdd), g_env)
addToEnv(makeSym("*"), Subr(subrMul), g_env)
addToEnv(makeSym("-"), Subr(subrSub), g_env)
addToEnv(makeSym("/"), Subr(subrDiv), g_env)
addToEnv(makeSym("mod"), Subr(subrMod), g_env)
addToEnv(sym_t, sym_t, g_env)
while(true) {
print("> ")
val line = readLine()
if (line == null) {
break
}
println(printObj(eval(read(line).obj, g_env)))
}
}