forked from frodwith/nockjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
726 lines (657 loc) · 19.3 KB
/
compiler.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
var noun = require('./noun.js'),
NounMap = require('./hamt.js').NounMap,
list = require('./list.js'),
Noun = noun.Noun,
Atom = noun.Atom.Atom,
Cell = noun.Cell,
MEMO = noun.dwim("memo"),
SLOG = noun.dwim("slog"),
FAST = noun.dwim("fast"),
SPOT = noun.dwim("spot"),
MEAN = noun.dwim("mean"),
HUNK = noun.dwim("hunk"),
LOSE = noun.dwim("lose");
function Statement() {
}
function Block() {
Statement.call(this);
this.statements = [];
}
Block.prototype = Object.create(Statement.prototype);
Block.prototype.constructor = Block;
Block.prototype.append = function(st) {
this.statements.push(st);
}
Block.prototype.toJs = function() {
var sts = this.statements;
var parts = new Array(sts.length);
for ( var i = 0; i < sts.length; ++i) {
parts[i] = sts[i].toJs();
}
return parts.join('');
};
function Assignment(name, expr) {
Statement.call(this);
this.name = name;
this.expr = expr;
}
Assignment.prototype = Object.create(Statement.prototype);
Assignment.prototype.constructor = Assignment;
Assignment.prototype.toJs = function() {
return "var " + this.name + " = " + this.expr.toJs() + ";";
}
function Expression() {
}
function Cons(head, tail) {
Expression.call(this);
this.head = head;
this.tail = tail;
}
Cons.prototype = Object.create(Expression.prototype);
Cons.prototype.constructor = Cons;
Cons.prototype.toJs = function() {
return "context.cons(" + this.head + ", " + this.tail + ")";
};
function Frag(axis, name) {
Expression.call(this);
this.axis = axis;
this.name = name;
}
Frag.prototype = Object.create(Expression.prototype);
Frag.prototype.constructor = Frag;
Frag.prototype.toJs = function() {
var parts = [this.name];
for ( var ax = this.axis; ax > 1; ax = ax.mas() ) {
parts.push( ( 2 === ax.cap().valueOf() ) ? "head" : "tail" );
}
return parts.join(".");
};
function Bail() {
Statement.call(this);
}
Bail.prototype = Object.create(Statement.prototype);
Bail.prototype.constructor = Bail;
Bail.prototype.toJs = function() {
return "throw new Error(\"Bail\")";
};
function Identity(name) {
Expression.call(this);
this.name = name;
}
Identity.prototype = Object.create(Expression.prototype);
Identity.prototype.constructor = Identity;
Identity.prototype.toJs = function() {
return this.name;
};
function Constant(index) {
Expression.call(this);
this.index = index;
}
Constant.prototype = Object.create(Expression.prototype);
Constant.prototype.constructor = Constant;
Constant.prototype.toJs = function() {
return "constants[" + this.index + "]";
};
function Nock(subject, formula, tail) {
Expression.call(this);
this.subject = subject;
this.formula = formula;
this.tail = tail;
}
Nock.prototype = Object.create(Expression.prototype);
Nock.prototype.constructor = Nock;
Nock.prototype.toJs = function() {
var f = this.formula;
var targetCode = "(" + f + ".hasOwnProperty('target') ? " + f +
".target : (" + f + ".target = context.compile(" + this.formula + ")))";
return this.tail ?
"context.trampoline(" + targetCode + ", " + this.subject + ")" :
targetCode + "(" + this.subject + ")";
};
function Deep(name) {
Expression.call(this);
this.name = name;
}
Deep.prototype = Object.create(Expression.prototype);
Deep.prototype.constructor = Deep;
Deep.prototype.toJs = function() {
return this.name +".deep ? context.yes : context.no";
};
function Bump(name) {
Expression.call(this);
this.name = name;
}
Bump.prototype = Object.create(Expression.prototype);
Bump.prototype.constructor = Bump;
Bump.prototype.toJs = function() {
return this.name + ".bump()";
};
function Same(one, two) {
Expression.call(this);
this.one = one;
this.two = two;
}
Same.prototype = Object.create(Expression.prototype);
Same.prototype.constructor = Same;
Same.prototype.toJs = function() {
return "(" + this.one + ".equals(" + this.two + ")" + " ? context.yes : context.no)";
};
function If(test, yes, no) {
Statement.call(this);
this.test = test;
this.yes = yes;
this.no = no;
}
If.prototype = Object.create(Statement.prototype);
If.prototype.constructor = If;
If.prototype.toJs = function() {
return "if(" + this.test + ".loob()){" +
this.yes.toJs() + "}else{" + this.no.toJs() + "}";
};
function Kick(axis, core, tail) {
Expression.call(this)
this.axis = axis;
this.core = core;
this.tail = tail;
}
Kick.prototype = Object.create(Expression.prototype);
Kick.prototype.constructor = Kick;
Kick.prototype.toJs = function() {
var axis = this.axis.shortCode();
return "(function (cor) {" +
"var pro, tgt, bus, arms, bat = cor.head, has = false;" +
"if ( bat.hasOwnProperty('loc') && (tgt = bat.loc.jets[" + axis + "]) && bat.loc.fine(cor) ) {" +
"return tgt(cor);" +
"}" +
"if ( bat.hasOwnProperty('arms') ) {" +
"arms = bat.arms;" +
"has = arms.hasOwnProperty('" + axis + "');" +
"}" +
"else arms = bat.arms = {};" +
"tgt = (has ? arms['" + axis + "'] : (arms['" + axis + "'] = context.compile(" +
new Frag(this.axis, "bat").toJs() + ")));" +
"bus = cor;" +
(this.tail ? "pro = context.trampoline(tgt, bus);" :
"while (true) {" +
"pro = tgt(bus);" +
"if ( context.isTrampoline(pro) ) {" +
"tgt = pro.target;" +
"bus = pro.subject;" +
"}" +
"else break;" +
"}") +
"return pro;" +
"})(" + this.core + ")";
};
function GetMemo(name) {
Expression.call(this);
this.name = name;
}
GetMemo.prototype = Object.create(Expression.prototype);
GetMemo.prototype.constructor = GetMemo;
GetMemo.prototype.toJs = function() {
return "context.getMemo(" + this.name + ")";
};
function PutMemo(key, val) {
Statement.call(this);
this.key = key;
this.val = val;
}
PutMemo.prototype = Object.create(Statement.prototype);
PutMemo.prototype.constructor = PutMemo;
function Push(name) {
Statement.call(this);
this.name = name;
}
Push.prototype = Object.create(Statement.prototype);
Push.prototype.constructor = Push;
Push.prototype.toJs = function() {
return "context.stackPush(" + this.name + ");";
};
function Pop() {
Statement.call(this);
}
Pop.prototype = Object.create(Statement.prototype);
Pop.prototype.constructor = Pop;
Pop.prototype.toJs = function() {
return "context.stackPop()";
};
function Fast(clue, core) {
Statement.call(this);
this.clue = clue;
this.core = core;
}
Fast.prototype.toJs = function() {
return "context.register(" + this.core + ", " + this.clue + ");";
}
function Slog(name) {
Statement.call(this);
this.name = name;
}
Slog.prototype.toJs = function() {
return "context.slog(" + this.name + ")";
};
function compile(formula, subject, product, fresh, constants, block, tail) {
var op, arg, one, two, odd;
if ( !(formula instanceof Cell )) {
throw new Error("invalid formula");
}
op = formula.head;
arg = formula.tail;
if ( op instanceof Cell ) {
one = fresh();
two = fresh();
compile(op, subject, one, fresh, constants, block, false);
compile(arg, subject, two, fresh, constants, block, false);
block.append(new Assignment(product, new Cons(one, two)));
}
else switch ( op.valueOf() ) {
case 0:
if ( 0 === arg ) {
block.append(new Bail());
}
else if ( 1 === arg ) {
block.append(new Identity(subject));
}
else {
block.append(new Assignment(product, new Frag(arg, subject)));
}
break;
case 1:
constants.push(arg);
block.append(new Assignment(product, new Constant(constants.length - 1)));
break;
case 2:
one = fresh();
two = fresh();
compile(arg.head, subject, one, fresh, constants, block, false);
compile(arg.tail, subject, two, fresh, constants, block, false);
block.append(new Assignment(product, new Nock(one, two, tail)));
break;
case 3:
one = fresh();
compile(arg, subject, one, fresh, constants, block, false);
block.append(new Assignment(product, new Deep(one)));
break;
case 4:
one = fresh();
compile(arg, subject, one, fresh, constants, block, false);
block.append(new Assignment(product, new Bump(one)));
break;
case 5:
one = fresh();
two = fresh();
compile(arg.head, subject, one, fresh, constants, block, false);
compile(arg.tail, subject, two, fresh, constants, block, false);
block.append(new Assignment(product, new Same(one, two)));
break;
case 6:
odd = fresh();
one = new Block();
two = new Block();
compile(arg.head, subject, odd, fresh, constants, block, false);
compile(arg.tail.head, subject, product, fresh, constants, one, tail);
compile(arg.tail.tail, subject, product, fresh, constants, two, tail);
block.append(new If(odd, one, two));
break;
case 7:
one = fresh();
compile(arg.head, subject, one, fresh, constants, block, false);
compile(arg.tail, one, product, fresh, constants, block, tail);
break;
case 8:
one = fresh();
two = fresh();
compile(arg.head, subject, one, fresh, constants, block, false);
block.append(new Assignment(two, new Cons(one, subject)));
compile(arg.tail, two, product, fresh, constants, block, tail);
break;
case 9:
odd = arg.head;
if ( 2 === odd.cap().valueOf() ) {
one = fresh();
two = odd.mas();
compile(arg.tail, subject, one, fresh, constants, block, false);
block.append(new Assignment(product, new Kick(two, one, tail)));
}
else {
compile(noun.dwim([7, arg.tail, 2, [0, 1], 0, odd]),
subject, product, fresh, constants, block, tail);
}
break;
case 10:
var hint = arg.head;
if ( !(arg.head instanceof Cell) ) {
// no recognized static hints
compile(arg.tail, subject, product, fresh, constants, block, tail);
}
else {
var zep = hint.head;
var clu = fresh();
compile(hint.tail, subject, clu, fresh, constants, block, false);
if ( zep.equals(MEMO) ) {
var key = fresh();
var got = fresh();
odd = fresh();
one = new Block();
two = new Block();
var konst = fresh();
block.append(new Assignment(konst, new Constant(hint.tail)));
block.append(new Assignment(key, new Cons(subject, konst)));
block.append(new Assignment(got, new GetMemo(two)));
block.append(new Assignment(odd, new Deep(got)));
one.append(new Assignment(product, new Frag(noun.dwim(3), got)));
compile(arg.tail, subject, product, fresh, two, false);
two.append(new PutMemo(key, product));
block.append(new If(odd, one, two));
}
else if ( zep.equals(SLOG) ) {
block.append(new Slog(clu));
compile(arg.tail, subject, product, fresh, constants, block, tail);
}
else if ( zep.equals(FAST) ) {
compile(arg.tail, subject, product, fresh, constants, block, false);
block.append(new Fast(clu, product));
}
else if ( zep.equals(SPOT) ||
zep.equals(MEAN) ||
zep.equals(HUNK) ||
zep.equals(LOSE) ) {
one = fresh();
two = fresh();
block.append(new Assignment(one, new Constant(zep)));
block.append(new Assignment(two, new Cons(one, clu)));
block.append(new Push(two));
compile(arg.tail, subject, product, fresh, constants, block, false);
block.append(new Pop());
}
else {
// unrecognized
compile(arg.tail, subject, product, fresh, constants, block, tail);
}
}
break;
case 11:
one = fresh();
two = fresh();
compile(arg.head, subject, one, fresh, constants, block, false);
compile(arg.tail, subject, two, fresh, constants, block, false);
block.append(new Assignment(product, new Esc(one, two)));
break;
default:
throw new Error("invalid opcode");
}
}
function Trampoline(target, subject) {
this.target = target;
this.subject = subject;
}
function genFine(loc) {
var constants = [], out = [], i;
for ( i = 0; !loc.isStatic; ++i ) {
out.push("if(!constants[" + i + "].equals(a.head)){return false;}");
constants.push(loc.noun);
out.push("a=" + new Frag(loc.axisToParent, "a").toJs() + ";");
loc = loc.parentLoc;
}
out.push("return constants[" + i + "].equals(a);");
constants.push(loc.noun);
var body = 'return function(a){' + out.join('') + 'return true;};';
var builder = new Function('constants', body);
return builder(constants);
}
var three = noun.dwim(3);
function Location(context, name, label, axisToParent, hooks, noun, parentLoc) {
this.name = name;
this.label = label;
this.parentLoc = parentLoc;
this.axisToParent = axisToParent;
this.fragToParent = Noun.fragmenter(axisToParent);
this.nameToAxis = hooks;
this.axisToName = {};
this.isStatic = ( null === parentLoc || (three.equals(axisToParent) && parentLoc.isStatic) );
if ( this.isStatic ) {
this.noun = noun;
}
else {
this.noun = noun.head;
this.noun.mug();
}
for ( var k in hooks ) {
if ( hooks.hasOwnProperty(k) ) {
this.axisToName[hooks[k].shortCode()] = k;
}
}
this.jets = {};
var drivers = context.drivers[label];
if ( drivers && drivers.length > 0 ) {
for ( var i = 0; i < drivers.length; ++i ) {
var d = drivers[i];
if ( d instanceof AxisArm ) {
this.jets[d.axis.mas().shortCode()] = d.fn;
}
else {
this.jets[nameToAxis[d.name].mas().shortCode()] = d.fn;
}
}
}
this.fine = genFine(this);
}
function Clue(name, parentAxis, hooks) {
this.name = name;
this.parentAxis = parentAxis;
this.hooks = hooks;
}
function JetDriver(label, fn) {
this.label = label;
this.fn = fn;
}
function AxisArm(label, axis, fn) {
JetDriver.call(this, label, fn);
this.axis = axis;
}
AxisArm.prototype = Object.create(JetDriver.prototype);
AxisArm.prototype.constructor = AxisArm;
function NamedArm(label, name, fn) {
JetDriver.call(this, label, fn);
this.name = name;
}
NamedArm.prototype = Object.create(JetDriver.prototype);
NamedArm.prototype.constructor = NamedArm;
var two = noun.dwim(2);
function collectFromCore(prefix, spec, out) {
var name = spec[0], arms = spec[1], children = spec[2],
labl = prefix + "/" + name;
if ( arms instanceof Function ) {
out[labl] = [new AxisArm(labl, two, arms)];
}
else {
var all = [];
for ( var k in arms ) {
if ( arms.hasOwnProperty(k) ) {
all.push(( 'number' === typeof(k) ) ?
new AxisArm(labl, noun.dwim(k), arms[k]) :
new NamedArm(labl, k, arms[k]));
}
}
out[labl] = all;
}
if ( children ) {
for ( var i = 0; i < children.length; ++i ) {
collectFromCore(labl, children[i], out);
}
}
}
function Context(drivers) {
this.memo = new NounMap();
this.clues = new NounMap();
this.dash = new NounMap();
this.tax = noun.Atom.yes;
this.drivers = {};
if ( drivers ) {
collectFromCore('', drivers, this.drivers);
}
}
Context.prototype.yes = noun.Atom.yes;
Context.prototype.no = noun.Atom.no;
Context.prototype.cons = function (h, t) {
return new Cell(h, t);
};
Context.prototype.trampoline = function(tgt, bus) {
return new Trampoline(tgt, bus);
};
Context.prototype.isTrampoline = function(a) {
return (a instanceof Trampoline);
};
Context.prototype.compile = function(cell) {
var i = 0;
var fresh = function() {
return "v" + ++i;
};
var body = new Block();
var constants = [];
compile(cell, "subject", "product", fresh, constants, body, true);
var text = "return function(subject){" + body.toJs() + "return product;}";
var builder = new Function("context", "constants", text);
return cell.target = builder(this, constants);
};
Context.prototype.nock = function(subject, formula) {
var product, target;
if ( !formula.hasOwnProperty("target") ) {
this.compile(formula);
}
target = formula.target;
while ( true ) {
product = target(subject);
if ( product instanceof Trampoline ) {
subject = product.subject;
target = product.target;
}
else {
return product;
}
}
};
Context.prototype.getMemo = function(key) {
return this.memo.get(key);
}
Context.prototype.putMemo = function(key, val) {
this.memo.insert(key, val);
};
Context.prototype.stackPush = function(item) {
this.tax = new Cell(item, this.tax);
};
Context.prototype.stackPop = function() {
this.tax = this.tax.tail;
}
Context.prototype.slog = function(item) {
// TODO: don't rewrite ++wash again, just call the kernel
console.log(item);
};
function chum(n) {
if ( n.deep ) {
return Atom.cordToString(n.head) + n.tail.number.shortValue().toString(10);
}
else {
return Atom.cordToString(n);
}
}
var ten = noun.dwim(10);
function skipHints(formula) {
while ( true ) {
if ( formula.deep ) {
if ( ten.equals(formula.head) ) {
formula = formula.tail.tail;
continue;
}
}
return formula;
}
}
var zero = noun.dwim(0), constant_zero = noun.dwim(1,0);
function parseParentAxis(noun) {
var f = skipHints(noun);
if ( constant_zero.equals(f) ) {
return zero;
}
else if ( !zero.equals(f.head) ) {
throw new Error("weird formula head");
}
else if ( 3 != f.tail.cap().valueOf() ) {
throw new Error("weird parent axis");
}
return f.tail;
}
var nine = noun.dwim(9), constant_frag = noun.dwim(0,1);
function parseHookAxis(nock) {
var f = skipHints(nock),
op = f.head;
if ( !op.deep ) {
if ( zero.equals(op) ) {
if ( !f.tail.deep ) {
return f.tail;
}
}
else if ( nine.equals(op) ) {
var rest = f.tail;
if ( !rest.head.deep && constant_frag.equals(rest.tail) ) {
return rest.head;
}
}
}
return null;
}
function parseHooks(noun) {
var o = {};
list.forEach(noun, function(c) {
var term = Atom.cordToString(c.head),
axis = parseHookAxis(c.tail);
if ( null != axis ) {
o[term] = axis;
}
});
return o;
}
Context.prototype.parseClue = function(raw) {
var clue = this.clues.get(raw);
if ( clue === undefined ) {
var name = chum(raw.head),
parentAxis = parseParentAxis(raw.tail.head),
hooks = parseHooks(raw.tail.tail);
clue = new Clue(name, parentAxis, hooks);
this.clues.insert(raw, clue);
}
return clue;
}
Context.prototype.register = function(core, raw) {
var bat = core.head;
var loc = this.dash.get(bat);
if ( undefined === loc ) {
try {
var clue = this.parseClue(raw);
if ( zero.equals(clue.parentAxis) ) {
loc = new Location(this, clue.name, '/' + clue.name, zero, clue.hooks, core, null);
}
else {
var parentCore = core.at(clue.parentAxis),
parentBattery = parentCore.head,
parentLoc = this.dash.get(parentBattery);
if ( undefined === parentLoc ) {
console.log('register: invalid parent for ' + clue.name);
}
else {
var label = parentLoc.label + "/" + clue.name;
loc = new Location(this, clue.name, label, clue.parentAxis, clue.hooks, core, parentLoc);
}
}
bat.loc = loc;
this.dash.insert(bat, loc);
}
catch (e) {
console.log(e);
}
}
};
module.exports = {
Context: Context,
}