-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp.js
744 lines (650 loc) · 17.3 KB
/
lisp.js
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* bootstrap a lisp to javascript compiler that works on Function
* in some ways it is tied to the semantics of javascript
* - cons are actually lists, no dotted pairs
* - has to handle exceptions
* - numeric type equivalence
* - dicts a plenty
* trying to make fexprs work a la kernel
* applicatives are normal, operatives start with $
*/
export class OpSymbol {
constructor(s) {
this.name = s;
}
// [util.inspect.custom](depth, opts) {
// return '`' + this.name + '`';
// }
toString() {
return this.name;
}
}
export function q(s) {
return new OpSymbol(s);
}
export function isvau(fname) {
return fname.length > 0 && /[\$~]/.test(fname[0]);
}
export function newenv() {
let env = {
cons(a, b) {
return this.$cons(a, b);
},
$cons(a, b) {
if (Array.isArray(b)) {
return [a, ...b];
} else {
return [a, b];
}
},
'*': (a, b) => a * b,
'/': (a, b) => a / b,
'+': (a, b) => a + b,
'-': (a, b) => a - b,
car(l) {
return l[0];
},
$car(l) {
return l[0];
},
cdr(l) {
return l.slice(1);
},
$cdr(l) {
return l.slice(1);
},
$log(...args) {
let printed = args.map(a => this.$print(a));
console.log(...printed);
},
log(...args) {
this.$log(...args);
},
shoulddebug: {},
debug(item) {
this.shoulddebug[item] = true;
},
cleardebug() {
this.shoulddebug = {};
},
$debug(subject, ...args) {
if (this.shoulddebug[subject]) {
this.$log.apply(this, [subject, ...args]);
}
},
$obj(...bindings) {
let m = {};
for (let binding of bindings) {
let name = binding[0];
let value = binding[1];
m[name] = this.$eval(value);
}
return m;
},
$get(map, key) {
return map[key];
},
get(map, key) {
return map[key];
},
$mset(map, key, val) {
return this.$eval(map)[key] = val;
},
$objp(m) {
return m && typeof m === 'object';
},
$printlist(os) {
return os.map(e => this.$print(e)).join(' ');
},
$vaup(fn) {
return isvau(this.$name(fn));
},
$dbg(...args) {
let res = this.$eval(...args);
this.$log(args, res);
return res;
},
$print(o) {
if (o instanceof Error) {
return o.stack;
} else if (this.$symbolp._(o)) {
return o.name;
} else if (this.$numberp._(o)) {
return o;
} else if (this.$stringp._(o)) {
if (o.includes(' ')) {
return `"${o}"`;
} else {
return `${o}`;
}
} else if (this.$listp._(o)) {
return `(${this.$printlist._(o)})`;
} else if (this.$functionp._(o)) {
let name = this.$name._(o);
return `(${this.$vaup._(o) ? '$vau' : '$lambda'} ${name})`;
} else if (this.$objp._(o)) {
return `\n(obj ${this.$printlist._(Object.entries(o).map(([key, val]) => [new OpSymbol(key), val]))})\n`
} else if (this.$booleanp._(o)) {
return o;
}
},
$booleanp(b) {
return typeof b === 'boolean';
},
$stringp(s) {
return typeof s === 'string';
},
_fn(fn, name, wrap = false, hygenic = true) {
// this.$log('fn', fn, name, wrap, hygenic);
let pfn = new Proxy(fn, {
apply(target, thisArg, args) {
let lambdaEnv = thisArg.$childenv._(thisArg);
lambdaEnv.hygenic = hygenic;
function wrapArgs() {
if (args.length > 0 && args[0] === '$') {
return args.slice(1);
} else if (wrap) {
return args.map(arg => lambdaEnv.$eval(arg));
} else {
return args;
}
}
let wargs = wrapArgs(args);
lambdaEnv.$pushstack._([name, ...wargs]);
let res = target.apply(lambdaEnv, wargs);
lambdaEnv.$popstack._();
return res;
},
});
pfn.fname = name?.toString();
pfn._ = fn.bind(this);
return pfn;
},
$let(defs, body) {
this.$debug('let', defs, body);
let childenv = this.$childenv(this);
childenv.hygenic = true;
for (let def of defs) {
childenv[def[0]] = childenv.$eval(def[1]);
}
return childenv.$eval(body);
},
wrap(fn, name) {
if (typeof fn !== 'function') {
throw new Error(`attempted to wrap a non-fn ${fn}`);
}
this.$debug('wrap', fn, name);
let resfn = this._fn(fn, name, true, fn.hygenic);
return resfn;
},
level: 0,
hygenic: false,
functionclass() {
return this.defclass('function', {
constructor(fn) {
this.fn = fn;
},
apply(thisArg, args) {
return this.fn.apply(thisArg, args);
},
}, [])
},
// lexical scoping is this easy (without closures)
$childenv(base = this) {
return new Proxy({
parent: base,
level: base.level + 1,
}, {
get(obj, p) {
if (p in obj) {
return obj[p];
} else if (obj.parent) {
let lookup = obj.parent[p];
// TODO: debate the merits of lexical hoisting? whatever this is
// great for deeply nested recursive lookups, 3x faster for (fib 24)
obj[p] = lookup;
return lookup;
} else {
throw new Error(`undefined ${p}`);
}
},
set(obj, p, val) {
obj[p] = val;
return true;
}
});
},
gt(a, b) {
return a > b;
},
lt(a, b) {
return a < b;
},
eq(a, b) {
return this.$eq(a, b);
},
$eq(a, b) {
if (this.$symbolp(a) && this.$symbolp(b)) {
return a.name === b.name;
} else if (this.$listp(a) && this.$listp(b)) {
return a.length === 0 && b.length === 0;
} else {
return a == b;
}
},
is(arg) {
return !this.nil._(arg);
},
nil(arg) {
if (this.$listp(arg)) {
return arg.length === 0;
} else {
return !arg;
}
},
$symextend(...args) {
return this.$symbol(args.join(''));
},
$symbol(s) {
return new OpSymbol(s);
},
$caris(form, name) {
return this.$listp(form) && this.$eq(this.$car(form), new OpSymbol(name));
},
_nativefib(n) {
return (n < 2) ? (n) : (this._nativefib(n - 1) + this._nativefib(n - 2));
},
$set(symbol, value) {
if (this.parent && !this.hygenic) {
this.$debug('set', 'up', symbol, '=', value, this.hygenic, this.level);
this.parent.$set._(symbol, value);
} else {
this.$debug('set', symbol, value, this.hygenic, this.level);
this[symbol.name] = value;
}
return value;
},
$define(symbol, value) {
let val = this.$eval(value);
// this.$log('define', symbol, val);
if (typeof val === 'undefined') {
throw new Error(`attempt to define ${symbol} to undefined!`);
}
return this.$set(symbol, val);
},
$if(cond, then, elsse) {
this.$debug('if', cond, then, elsse);
if (this.$eval(cond)) {
return this.$eval(then);
} else {
return this.$eval(elsse);
}
},
$join(l, s) {
return l.join(s);
},
$mapcompile(l) {
return l.map(e => this.$compile(e));
},
$repr(form, bracket = true) {
if (this.$symbolp(form)) {
return `this.$symbol(${this.$repr(form.name)})`;
} else if (this.$listp(form)) {
const [ob, eb] = bracket ? ['[', ']'] : ['', ''];
return `${ob}${form.map(e => this.$repr(e)).join(', ')}${eb}`
} else if (this.$stringp(form)) {
return `'${form.replace(/'/g, "\\'")}'`;
} else {
return form.toString();
}
},
$logtime(form) {
let time = +new Date();
let res = this.$eval(form);
this.$log(form, res, (+new Date() - time) / 1000);
return res;
},
$jsident(s) {
return /^[a-zA-Z$_\d]+$/.test(s);
},
$compref(s) {
let n = s.toString();
if (this.$jsident._(n)) {
return `this.${n}`;
} else {
return `this['${n.replace(/'/g, "\\'")}']`;
}
},
$compile(form, trace = true) {
this.$debug('compile', form);
if (this.$listp(form) && form.length > 0) {
let op = this.$car(form);
let comp = this[op]?.comp || this.nativecomps[op.name + 'comp'];
if (comp) {
return comp.apply(this, this.$cdr(form));
} else if (op.name) {
if (!isvau(op.name)) {
// this.$log('compile', 'wrap', op);
if (trace) {
return `${this.$compref(op)}('$', ${this.$mapcompile(this.$cdr(form))})`
} else {
return `${this.$compref(op)}(${this.$mapcompile(this.$cdr(form))})`
}
} else {
return `${this.$compref(op)}(${this.$repr(this.$cdr(form), false)})`
}
}
} else if (this.$symbolp(form)) {
return this.$compref(form);
} else if (this.$numberp(form)) {
return form;
}
return `this.$eval(${this.$repr(form)})`;
},
compile(form) {
return this.$compile(form);
},
$trycompile(form) {
try {
let compiled = this.$compile(form);
this.$debug('$compile', form, compiled);
return compiled;
} catch (e) {
this.$debug('not compiled', form, e);
return null;
}
},
nativecomps: {
$ifcomp(cond, then, elsse) {
return `${this.$compile(cond)} ? ${this.$compile(then)} : ${this.$compile(elsse)}`;
},
ltcomp(a, b) {
return `${this.$compile(a)} < ${this.$compile(b)}`;
},
gtcomp(a, b) {
return `${this.$compile(a)} > ${this.$compile(b)}`;
},
eqcomp(a, b) {
return `${this.$compile(a)} === ${this.$compile(b)}`;
},
$carcomp(l) {
return `${this.$compile(l)}[0]`
},
$cdrcomp(l) {
return `${this.$compile(l)}.slice(1)`;
},
'~comp'(...args) {
return `${this.$compile(args[0])}(${this.$mapcompile(args.slice(1)).join(', ')}])`;
//return `[${this.$repr(args[0])}, ${this.$mapcompile(args)}]`;
},
'+comp': function(a, b) {
return `${this.$compile(a)} + ${this.$compile(b)}`;
},
'-comp': function(a, b) {
return `${this.$compile(a)} - ${this.$compile(b)}`;
},
'*comp': function(a, b) {
return `${this.$compile(a)} * ${this.$compile(b)}`;
},
'/comp': function(a, b) {
return `${this.$compile(a)} / ${this.$compile(b)}`;
},
},
'~'(...args) {
return this.$eval([args[0], ...this.$mapeval(args.slice(1))]);
},
concat(...args) {
return args.join('');
},
$compileargs(args) {
return args.map(arg => this.$caris(arg, 'rest') ?
'...' + this.$car(this.$cdr(arg)).name : arg.name);
},
$argname(arg) {
if (this.$symbolp._(arg)) {
return arg;
} else if (this.$caris._(arg, 'rest')) {
return this.$car._(this.$cdr._(arg));
}
},
$uvau(args, body, name = null) {
return this.$vau._(args, body, name, false);
},
$pullarg(name, args) {
this.$debug('pullarg', name, args);
for (let i = 0; i < args.length; i++) {
let arg = args[i];
if (this.$caris(arg, name)) {
args.splice(i, 1);
return this.$cdr(arg);
}
}
return [];
},
$transvau(args, body) {
// this.$log('transvau', args, body);
let target = this.$compile._(body);
function transvaudebug(ag) {
if (false) {
return `console.log('set ${ag} = ', this['${ag}'])\n`;
}
}
const vauCode = `${args.map(arg => {
let argname = this.$argname._(arg);
return `\nthis['${argname}'] = ${argname};\n${transvaudebug(argname)}`;
}).join('')}
return ${target}`;
const vauArgs = this.$compileargs._(args);
vauArgs.push(vauCode);
this.$debug('transvau', body, vauArgs);
return new Function(...vauArgs);
},
$vau(args, body, name = null, hygenic = true) {
let [doc] = this.$pullarg._('doc', args);
this.$debug('vau', name, args, body);
let target = this.$transvau._(args, body, hygenic);
let resfn = this._fn(target, name, false, hygenic);
resfn.args = args;
resfn.body = body;
if (doc) {
resfn.doc = doc;
}
return resfn;
},
$name(fn) {
return fn.fname || 'anonymous';
},
$progn(...args) {
let res;
for (let statement of args) {
res = this.eval(statement);
}
this.$debug('progn', args, res);
return res;
},
run(code) {
return this.$run(code);
},
$run(code) {
this.$debug('run', code);
return this.$progn(...this.$parseToplevel(code));
},
jsrun(code) {
this.$debug('jsrun', code);
let body = code.indexOf("return") === -1 ? `return ${code}` : code;
return new Function('', body).apply(this, []);
},
$qt(s) {
return s;
},
eval(...form) {
return this.$eval(...form);
},
$boot() {
for (let fname in this) {
const fn = this[fname];
if (typeof fn === 'function' && fname[0] !== '_') {
console.log('boot', fname);
let wrap = !isvau(fname);
this[fname] = this._fn(fn, fname, wrap, false);
if (this.nativecomps[fname]) {
this[fname].comp = this.nativecomps[fname];
}
}
}
},
stack: [],
$pushstack(f) {
this.stack.push(f);
},
$popstack() {
this.stack.pop();
},
$stack() {
return this.stack.slice().reverse();
},
$eval(...form) {
if (form.length === 1) {
form = form[0];
}
// this.$log('eval', form);
if (Array.isArray(form)) {
let operator = this.$eval._(this.$car._(form));
let args = this.$cdr._(form);
// this.$pushstack([this.$name(operator) || 'anonymous', ...args])
let res = this.$combine(operator, args)
// this.stack.pop();
// this.$debug('eval', operator, ...args, '=', res)
return res;
} else if (this.$symbolp._(form)) {
if (form.name[0] === "'") {
return new OpSymbol(form.name.slice(1));
}
let sym = this[form.name];
if (typeof sym === 'undefined') {
throw new Error(`undefined ${form}`);
}
// this.$debug('symbol', form, this[form.name]);
return this[form.name];
} else {
return form;
}
},
$combine(c, ops) {
// this.$debug('combine', c, ops);
return c.apply(this, ops);
},
$parseToplevel(s) {
let toks = this.$tokenize(s);
let p = [];
while (toks.length > 0) {
p.push(this.$readTokens(toks));
}
return p;
},
$readTokens(toks) {
if (toks.length == 0) {
throw new Error('unexpected EOF');
}
let token = toks.shift();
if (token === '(') {
let sexp = [];
while (toks[0] !== ')') {
sexp.push(this.$readTokens(toks));
}
toks.shift();
return sexp;
} else if (token === ')') {
throw new Error('unexpected )');
} else {
return token;
}
},
$call(name, ...args) {
return this.$eval._([new OpSymbol(name), ...args]);
},
map(l, fn) {
// this.$debug('mapcar', l, fn);
return this.$map._(l, fn);
},
$map(l, fn) {
return (l || []).map(e => fn.apply(this, [e]));
},
$mapeval(l) {
// this.$debug('$mapeval', l);
return this.$map._(l, this.$eval._);
},
apply(fn, args) {
// this.$debug('apply', fn, args);
return fn.apply(this, args);
},
$numberp: s => typeof s === 'number',
$symbolp(s) {
return s instanceof OpSymbol;
},
$listp(s) {
return Array.isArray(s);
},
list(...args) {
return args;
},
$(...args) {
return args;
},
$functionp(f) {
return typeof f === 'function';
},
$tokenize(s) {
let toks = [];
for (let i = 0; i < s.length; i++) {
let c = s[i];
if ('()'.includes(c)) {
toks.push(c);
} else if (c === ';') {
while (s[i] !== '\n') {
i++;
}
} else if (' \n\t'.includes(c)) {
} else if (c === '"') {
let str = '';
i++;
while (s[i] !== '"') {
str += s[i];
i++;
}
toks.push(str);
} else {
let sym = '';
while (s[i] && /[^ \n\t();"]/.test(s[i])) {
sym += s[i];
i++;
}
i--;
let float = Number.parseFloat(sym);
if (!isNaN(float)) {
toks.push(float);
} else if (sym === 'true' || sym === 'false') {
toks.push(JSON.parse(sym))
} else {
toks.push(new OpSymbol(sym));
}
}
}
return toks;
},
recover: false,
$tryrecover(e, fn) {
console.log(e);
let stack = this.$stack._();
console.log(stack.map(f => this.$print._(f)).join('\n'));
this.$popstack._();
if (this.recover) {
fn();
} else {
throw new Error('die: ' + e);
}
}
};
env.$boot();
return env;
}