-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisp.dart
394 lines (345 loc) · 8.33 KB
/
lisp.dart
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
import 'dart:async';
import 'dart:io';
import 'dart:convert';
String kLPar = '(';
String kRPar = ')';
String kQuote = "'";
class Nil {
const Nil();
}
final kNil = const Nil();
safeCar(obj) => obj is Cons ? obj.car : kNil;
safeCdr(obj) => obj is Cons ? obj.cdr : kNil;
class Error {
final data;
Error(this.data);
}
makeError(String str) => new Error(str);
Map<String, Object> sym_table = {};
class Sym {
final data;
Sym(this.data);
}
makeSym(String str) {
if (str == 'nil') {
return kNil;
} else if (!sym_table.containsKey(str)) {
sym_table[str] = new Sym(str);
}
return sym_table[str];
}
final sym_t = makeSym('t');
final sym_quote = makeSym('quote');
final sym_if = makeSym('if');
final sym_lambda = makeSym('lambda');
final sym_defun = makeSym('defun');
final sym_setq = makeSym('setq');
final sym_loop = makeSym('loop');
final sym_return = makeSym('return');
var loop_val = kNil;
class Num {
final data;
Num(this.data);
}
makeNum(num n) => new Num(n);
class Cons {
var car;
var cdr;
Cons(this.car, this.cdr);
}
makeCons(a, d) => new Cons(a, d);
class Subr {
final data;
Subr(this.data);
}
makeSubr(fn) => new Subr(fn);
class Expr {
final args;
final body;
final env;
Expr(this.args, this.body, this.env);
}
makeExpr(args, env) => new Expr(safeCar(args), safeCdr(args), env);
nreverse(lst) {
var ret = kNil;
while (lst is Cons) {
var tmp = lst.cdr;
lst.cdr = ret;
ret = lst;
lst = tmp;
}
return ret;
}
pairlis(lst1, lst2) {
var ret = kNil;
while (lst1 is Cons && lst2 is Cons) {
ret = makeCons(makeCons(lst1.car, lst2.car), ret);
lst1 = lst1.cdr;
lst2 = lst2.cdr;
}
return nreverse(ret);
}
isDelimiter(String c) => c == kLPar || c == kRPar || c == kQuote || new RegExp(
r'\s+').hasMatch(c);
skipSpaces(String str) => str.replaceAll(new RegExp(r'^\s+'), '');
makeNumOrSym(String str) {
try {
return makeNum(int.parse(str));
} catch (e) {
return makeSym(str);
}
}
readAtom(String str) {
String next = '';
for (var i = 0; i < str.length; i++) {
if (isDelimiter(str[i])) {
next = str.substring(i);
str = str.substring(0, i);
break;
}
}
return [makeNumOrSym(str), next];
}
read(String str) {
str = skipSpaces(str);
if (str.length == 0) {
return [makeError('empty input'), ''];
} else if (str[0] == kRPar) {
return [makeError('invalid syntax: ${str}'), ''];
} else if (str[0] == kLPar) {
return readList(str.substring(1));
} else if (str[0] == kQuote) {
var tmp = read(str.substring(1));
return [makeCons(sym_quote, makeCons(tmp[0], kNil)), tmp[1]];
}
return readAtom(str);
}
readList(String str) {
var ret = kNil;
while (true) {
str = skipSpaces(str);
if (str.length == 0) {
return [makeError('unfinished parenthesis'), ''];
} else if (str[0] == kRPar) {
break;
}
var tmp = read(str);
var elm = tmp[0];
var next = tmp[1];
if (elm is Error) {
return [elm, ''];
}
ret = makeCons(elm, ret);
str = next;
}
return [nreverse(ret), str.substring(1)];
}
printObj(obj) {
if (obj is Num || obj is Sym || obj is Nil) {
return obj.data.toString();
} else if (obj is Error) {
return '<error: ${obj.data}>';
} else if (obj is Cons) {
return printList(obj);
} else if (obj is Subr) {
return '<subr>';
} else if (obj is Expr) {
return '<expr>';
}
return '<unknown>';
}
printList(obj) {
String ret = '';
bool first = true;
while (obj is Cons) {
if (first) {
first = false;
} else {
ret += ' ';
}
ret += printObj(obj.car);
obj = obj.cdr;
}
if (obj == kNil) {
return '(${ret})';
}
return '(${ret} . ${printObj(obj)})';
}
findVar(sym, env) {
while (env is Cons) {
var alist = env.car;
while (alist is Cons) {
if (alist.car.car == sym) {
return alist.car;
}
alist = alist.cdr;
}
env = env.cdr;
}
return kNil;
}
var g_env = makeCons(kNil, kNil);
addToEnv(sym, val, env) {
env.car = makeCons(makeCons(sym, val), env.car);
}
eval(obj, env) {
if (obj is Nil || obj is Num || obj is Error) {
return obj;
} else if (obj is Sym) {
var bind = findVar(obj, env);
if (bind == kNil) {
return makeError('${obj.data} has no value');
}
return bind.cdr;
}
var op = safeCar(obj);
var args = safeCdr(obj);
if (op == sym_quote) {
return safeCar(args);
} else if (op == sym_if) {
var 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) {
var expr = makeExpr(safeCdr(args), env);
var sym = safeCar(args);
addToEnv(sym, expr, g_env);
return sym;
} else if (op == sym_setq) {
var val = eval(safeCar(safeCdr(args)), env);
if (val is Error) {
return val;
}
var sym = safeCar(args);
var 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 makeError('');
}
return apply(eval(op, env), evlis(args, env), env);
}
evlis(lst, env) {
var ret = kNil;
while (lst is Cons) {
var elm = eval(lst.car, env);
if (elm is Error) {
return elm;
}
ret = makeCons(elm, ret);
lst = lst.cdr;
}
return nreverse(ret);
}
progn(body, env) {
var ret = kNil;
while (body is Cons) {
ret = eval(body.car, env);
if (ret is Error) {
return ret;
}
body = body.cdr;
}
return ret;
}
loop(body, env) {
while (true) {
var ret = progn(body, env);
if (ret is Error) {
if (ret.data == '') {
return loop_val;
}
return ret;
}
}
}
apply(fn, args, env) {
if (fn is Error) {
return fn;
} else if (args is Error) {
return args;
} else if (fn is Subr) {
return fn.data(args);
} else if (fn is Expr) {
return progn(fn.body, makeCons(pairlis(fn.args, args), fn.env));
}
return makeError('noimpl');
}
subrCar(args) => safeCar(safeCar(args));
subrCdr(args) => safeCdr(safeCar(args));
subrCons(args) => makeCons(safeCar(args), safeCar(safeCdr(args)));
subrEq(args) {
var x = safeCar(args);
var 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;
}
subrAtom(args) => (safeCar(args) is Cons) ? kNil : sym_t;
subrNumberp(args) => (safeCar(args) is Num) ? sym_t : kNil;
subrSymbolp(args) => (safeCar(args) is Sym) ? sym_t : kNil;
subrAddOrMul(fn, init_val) => (args) {
var ret = init_val;
while (args is Cons) {
if (args.car is! Num) {
return makeError('wrong type');
}
ret = fn(ret, args.car.data);
args = args.cdr;
}
return makeNum(ret);
};
var subrAdd = subrAddOrMul((x, y) => x + y, 0);
var subrMul = subrAddOrMul((x, y) => x * y, 1);
subrSubOrDivOrMod(fn) => (args) {
var x = safeCar(args);
var y = safeCar(safeCdr(args));
if (x is! Num || y is! Num) {
return makeError('wrong type');
}
return makeNum(fn(x.data, y.data));
};
var subrSub = subrSubOrDivOrMod((x, y) => x - y);
var subrDiv = subrSubOrDivOrMod((x, y) => x / y);
var subrMod = subrSubOrDivOrMod((x, y) => x % y);
Stream readLine() => stdin.transform(UTF8.decoder).transform(new LineSplitter()
);
void main() {
addToEnv(makeSym('car'), makeSubr(subrCar), g_env);
addToEnv(makeSym('cdr'), makeSubr(subrCdr), g_env);
addToEnv(makeSym('cons'), makeSubr(subrCons), g_env);
addToEnv(makeSym('eq'), makeSubr(subrEq), g_env);
addToEnv(makeSym('atom'), makeSubr(subrAtom), g_env);
addToEnv(makeSym('numberp'), makeSubr(subrNumberp), g_env);
addToEnv(makeSym('symbolp'), makeSubr(subrSymbolp), g_env);
addToEnv(makeSym('+'), makeSubr(subrAdd), g_env);
addToEnv(makeSym('*'), makeSubr(subrMul), g_env);
addToEnv(makeSym('-'), makeSubr(subrSub), g_env);
addToEnv(makeSym('/'), makeSubr(subrDiv), g_env);
addToEnv(makeSym('mod'), makeSubr(subrMod), g_env);
addToEnv(sym_t, sym_t, g_env);
stdout.write('> ');
readLine().listen((String line) {
print(printObj(eval(read(line)[0], g_env)));
stdout.write('> ');
});
}