-
Notifications
You must be signed in to change notification settings - Fork 7
/
runtime.js
732 lines (672 loc) · 16.5 KB
/
runtime.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
// This will be a compiler when the runtime is attached to a jison parser.
var parse;
var assert = require('assert');
var inspect = require('util').inspect;
var forms = {};
exports.Form = Form;
function Form(name) {
if (forms[name]) return forms[name];
if (!(this instanceof Form)) return new Form(name);
this.name = name;
forms[name] = this;
}
Form.prototype.inspect = function () {
return "\033[34;1m@" + this.name + "\033[0m";
};
var symbols = {};
exports.Symbol = Symbol;
function Symbol(name) {
if (symbols[name]) return symbols[name];
if (!(this instanceof Symbol)) return new Symbol(name);
this.name = name;
this.depth = name.lastIndexOf(":") + 1;
symbols[name] = this;
}
Symbol.prototype.inspect = function () {
return "\033[35m:" + this.name + "\033[0m";
};
var metas = new WeakMap();
var pmetas = new Map();
function getMeta(obj) {
var type = typeof obj;
var mtab = (type === "object" || type === "function") ? metas : pmetas;
if (mtab.has(obj)) {
return mtab.get(obj);
}
var meta = {};
mtab.set(obj, meta);
if (Array.isArray(obj) || type === "string" || Buffer.isBuffer(obj)) {
meta.len = function () { return obj.length; };
}
else if (type === "function") {
meta.call = obj;
}
else {
meta.keys = function () { return Object.keys(obj); };
}
return meta;
}
function metaSet(obj, key, value) {
var meta = getMeta(obj);
if (key instanceof Form) {
return meta[key.name] = value;
}
if (key in obj) {
return obj[key] = value;
}
if (meta.set) {
return meta.set(key, value);
}
return obj[key] = value;
}
function metaGet(obj, key) {
var meta = getMeta(obj);
if (key instanceof Form) {
return meta[key.name];
}
if (obj && Object.prototype.hasOwnProperty.call(obj, key)) {
return obj[key];
}
if (meta.get) {
return meta.get(key);
}
}
function metaHas(obj, key) {
var meta = getMeta(obj);
if (key instanceof Form) {
return key.value in meta;
}
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return true;
}
if (meta.has) {
return meta.has();
}
return false;
}
function metaDelete(obj, key) {
var meta = getMeta(obj);
if (key instanceof Form) {
delete meta[key.name];
return;
}
if (Object.prototype.hasOwnProperty.call(obj, key)) {
delete obj[key];
return;
}
if (meta.delete) {
return meta.delete(key);
}
}
function Scope(parent) {
this.scope = Object.create(parent || null);
}
function getForm(array) {
return Array.isArray(array) && array[0] instanceof Form && array[0].name;
}
Scope.prototype.run = function (code) {
// Evaluate form codes
var form = getForm(code);
if (form) {
// console.log("running", code)
return this[form].apply(this, code.slice(1));
}
if (code instanceof Symbol) {
if (code.depth) {
return new Symbol(code.name.substr(1));
}
return this.lookup(code.name);
}
return code;
};
Scope.prototype.runCodes = function (codes) {
var result;
for (var i = 0, l = codes.length; i < l; i++) {
var code = codes[i];
result = this.run(code);
}
return result;
};
var hasOwn = Object.prototype.hasOwnProperty;
var slice = Array.prototype.slice;
var map = Array.prototype.map;
Scope.prototype.spawn = function () {
return new Scope(this.scope);
};
Scope.prototype.params = function () {
for (var i = 0, l = arguments.length; i < l; i++) {
this.scope[arguments[i]] = this.arguments[i];
}
};
Scope.prototype.vars = function () {
for (var i = 0, l = arguments.length; i < l; i++) {
this.scope[arguments[i]] = undefined;
}
};
Scope.prototype.fn = function () {
var scope = this.scope;
var codes = slice.call(arguments);
return jackFunction;
function jackFunction() {
var child = new Scope(scope);
child.arguments = arguments;
return child.runCodes(codes);
}
};
Scope.prototype.escape = function (expr) {
return expr;
};
Scope.prototype.exec = function (expr) {
return this.run(this.run(expr));
};
Scope.prototype.call = function (val) {
val = this.run(val);
var meta = getMeta(val);
if (!meta.call) {
return this.abort("Attempt to call non-callable value");
}
try {
return meta.call.apply(null, slice.call(arguments, 1).map(this.run, this));
}
catch (err) {
if (err.code === "RETURN") return err.value;
throw err;
}
};
Scope.prototype.le = function (a, b) {
return this.run(a) <= this.run(b);
};
Scope.prototype.lt = function (a, b) {
return this.run(a) < this.run(b);
};
Scope.prototype.eq = function (a, b) {
return this.run(a) === this.run(b);
};
Scope.prototype.neq = function (a, b) {
return this.run(a) !== this.run(b);
};
Scope.prototype.in = function (val, key) {
val = this.run(val);
key = this.run(key);
return metaHas(val, key);
};
Scope.prototype.add = function (a, b) {
return this.run(a) + this.run(b);
};
Scope.prototype.sub = function (a, b) {
return this.run(a) - this.run(b);
};
Scope.prototype.mul = function (a, b) {
return this.run(a) * this.run(b);
};
Scope.prototype.div = function (a, b) {
return Math.floor(this.run(a) / this.run(b));
};
Scope.prototype.pow = function (a, b) {
return Math.pow(this.run(a), this.run(b));
};
Scope.prototype.mod = function (a, b) {
return this.run(a) % this.run(b);
};
Scope.prototype.unm = function (a) {
return -this.run(a);
};
Scope.prototype.or = function (a, b) {
return this.run(a) || this.run(b);
};
Scope.prototype.and = function (a, b) {
return this.run(a) && this.run(b);
};
Scope.prototype.xor = function (a, b) {
return !this.run(a) !== !this.run(b);
};
Scope.prototype.not = function (a) {
return !this.run(a);
};
Scope.prototype.len = function (obj) {
obj = this.run(obj);
var meta = getMeta(obj);
if (meta.len) return meta.len();
};
Scope.prototype.set = function (obj, key, value) {
obj = this.run(obj);
key = this.run(key);
value = this.run(value);
return metaSet(obj, key, value);
};
Scope.prototype.get = function (obj, key) {
obj = this.run(obj);
key = this.run(key);
return metaGet(obj, key);
};
Scope.prototype.delete = function (obj, key) {
obj = this.run(obj);
key = this.run(key);
return metaDelete(obj, key);
};
Scope.prototype.return = function (val) {
throw {code:"RETURN", value: this.run(val)};
};
Scope.prototype.abort = function (message) {
message = this.run(message);
console.error(message);
process.exit();
// console.log("ABORT", {message:message});
// throw new Error(message);
};
Scope.prototype.assign = function (name, value) {
var scope = this.scope;
while (scope) {
if (hasOwn.call(scope, name)) return scope[name] = this.run(value);
scope = Object.getPrototypeOf(scope);
}
return this.abort("Attempt to access undefined variable '" + name + "'");
};
Scope.prototype.lookup = function (name) {
if (name in this.scope) {
return this.scope[name];
}
return this.abort("Attempt to access undefined variable '" + name + "'");
};
Scope.prototype.if = function () {
var pairs = slice.call(arguments);
for (var i = 0, l = pairs.length; i + 1 < l; i += 2) {
var cond = this.run(pairs[i]);
if (cond) {
return this.runCodes(pairs[i + 1]);
}
}
if (i < l) {
return this.runCodes(pairs[i]);
}
};
Scope.prototype.while = function (cond) {
var child = this.spawn();
var code = slice.call(arguments, 1);
var ret;
while (child.run(cond)) {
ret = child.runCodes(code);
}
return ret;
};
function iValues(list) {
var meta = getMeta(list);
if (meta.call) {
return meta.call;
}
if (meta.len) {
var i = 0, length = meta.len();
return function () {
if (i < length) {
return metaGet(list, i++);
}
};
}
if (meta.keys) {
var it = iValues(meta.keys());
return function () {
var key = it();
if (key !== undefined) {
return metaGet(list, key);
}
};
}
throw new Error("Can't iterate over value that doesn't have @call, @len, or @keys");
}
function iPairs(list) {
var meta = getMeta(list);
if (meta.keys) {
var it = iValues(meta.keys());
return function () {
var key = it();
if (key !== undefined) {
return [key, metaGet(list, key)];
}
};
}
if (meta.len) {
var i = 0, length = meta.len();
return function () {
if (i < length) {
var k = i++;
return [k, metaGet(list, k)];
}
};
}
if (meta.call) {
var k = 0;
return function () {
var next = meta.call();
if (next !== undefined) {
return [k++, next];
}
};
}
throw new Error("Can't iterate over value that doesn't have @call, @len, or @keys");
}
function iKeys(list) {
var meta = getMeta(list);
if (meta.keys) {
return iValues(meta.keys());
}
if (meta.len) {
var i = 0, length = meta.len();
return function () {
if (i < length) {
return i++;
}
};
}
if (meta.call) {
var i = 0;
return function () {
var next = meta.call();
if (next !== undefined) {
return i++;
}
};
}
throw new Error("Can't iterate over value that doesn't have @call, @len, or @keys");
}
Scope.prototype.iterate = function (list, names, filter, code, callback) {
list = this.run(list);
var child = this.spawn();
var ret;
var meta = getMeta(list);
if (meta.call) {
var i = 0;
var item;
while ((item = meta.call()) !== undefined) {
if (names.length === 2) {
child.scope[names[0]] = i++;
child.scope[names[1]] = item;
}
else {
child.scope[names[0]] = item;
}
if (filter && !child.run(filter)) continue;
ret = child.runCodes(code);
if (callback) callback(ret);
}
}
else if (meta.len) {
for (var i = 0, l = meta.len(); i < l; i++) {
var item = metaGet(list, i);
if (names.length === 2) {
child.scope[names[0]] = i;
child.scope[names[1]] = item;
}
else {
child.scope[names[0]] = item;
}
if (filter && !child.run(filter)) continue;
ret = child.runCodes(code);
if (callback) callback(ret);
}
}
else if (meta.keys) {
var keyIt = iValues(meta.keys());
var key;
while ((key = keyIt()) !== undefined) {
var item = metaGet(list, key);
if (names.length === 2) {
child.scope[names[0]] = key;
child.scope[names[1]] = item;
}
else {
child.scope[names[0]] = item;
}
if (filter && !child.run(filter)) continue;
ret = child.runCodes(code);
if (callback) callback(ret);
}
}
return ret;
};
Scope.prototype.for = function (list, names, filter) {
return this.iterate(list, names, filter, slice.call(arguments, 2));
};
Scope.prototype.map = function (list, names, filter) {
var result = [];
this.iterate(list, names, filter, slice.call(arguments, 2), result.push.bind(result));
return result;
};
Scope.prototype.list = function () {
return map.call(arguments, this.run, this);
};
function Tuple(len) {
Array.call(this, len);
this.length = len;
}
Tuple.prototype.__proto__ = Array.prototype;
Tuple.prototype.inspect = function () {
return "(" + this.map(inspect).join(", ") + ")";
};
Scope.prototype.tuple = function () {
var l = arguments.length;
var tuple = new Tuple(l);
for (var i = 0; i < l; i++) {
tuple[i] = this.run(arguments[i]);
}
return tuple;
};
Scope.prototype.object = function () {
var obj = Object.create(null);
for (var i = 0, l = arguments.length; i < l; i += 2) {
var key = this.run(arguments[i]);
var value = this.run(arguments[i + 1]);
metaSet(obj, key, value);
}
return obj;
};
var is = {
Integer: function (val) {
return val >>> 0 === val;
},
Null: function (val) {
return val === undefined;
},
Boolean: function (val) {
return val === true || val === false;
},
String: function (val) {
return typeof val === "string";
},
Buffer: function (val) {
return Buffer.isBuffer(val);
},
Function: function (val) {
return typeof val === "function"
},
Tuple: function (val) {
return val instanceof Tuple;
},
List: function (val) {
return Array.isArray(val);
},
Object: function (val) {
return val && val.__proto__ === null;
}
}
Scope.prototype.is = function (a, b) {
a = this.run(a);
var fn = is[b];
if (!fn) return this.abort("Unknown type " + b);
return fn(a);
};
Scope.prototype.eval = function (string) {
var codes = parse(string);
return this.runCodes(codes);
};
exports.eval = function (string) {
var scope = new Scope({
lines: string.split("\n"),
source: string,
parse: parse,
'read-stream': function (path) {
var stream = require('fs').createReadStream(path);
var dataQueue = [];
var readQueue = [];
stream.on('data', function (chunk) {
dataQueue.push(chunk);
check();
});
stream.on('end', function (err) {
dataQueue.push(undefined);
check();
});
stream.on("error", function (err) {
dataQueue.push(undefined);
check();
});
return function (callback) {
readQueue.push(callback);
check();
};
function check() {
while (dataQueue.length && readQueue.length) {
readQueue.pop()(dataQueue.pop());
}
}
},
'interval': function (ms, callback) {
var before = Date.now();
setInterval(function () {
var now = Date.now();
callback(now - before);
before = now;
}, ms);
},
substr: function (string, start, len) {
return string.substr(start, len);
},
parseint: function (num, base) {
return parseInt(num, base || 10);
},
write: function (string) {
process.stdout.write(string);
},
print: console.log.bind(console),
range: function range(n) {
var i = 0, v;
return function () {
v = i;
i++;
if (v < n) return v;
else i = 0;
};
},
rand: function rand(n) {
return Math.floor(Math.random() * n);
},
inspect: function (val, d) {
return inspect(val, false, d, true);
},
bind: function (fn) {
var args = slice.call(arguments, 1);
return function partial() {
return fn.apply(this, args.concat(slice.call(arguments)));
};
},
"i-values": iValues,
"i-pairs": iPairs,
"i-keys": iKeys,
"i-map": function (callback, it) {
it = iValues(it);
return function () {
var next = it();
if (next !== undefined) return callback(next);
};
},
"i-filter": function (callback, it) {
it = iValues(it);
return function () {
var next;
while ((next = it()) !== undefined && !callback(next));
return next;
};
},
"i-chunk": function (size, it) {
it = iValues(it);
return function () {
var next, chunk = [];
while (chunk.length < size && (next = it()) !== undefined) {
chunk.push(next);
}
if (chunk.length) return chunk;
};
},
"i-all?": function (callback, it) {
it = iValues(it);
var next;
while ((next = it()) !== undefined) {
if (!callback(next)) return false;
}
return true;
},
"i-any?": function (callback, it) {
it = iValues(it);
var next;
while ((next = it()) !== undefined) {
if (callback(next)) return true;
}
return false;
},
"i-collect": function (it) {
it = iValues(it);
var arr = [], next;
while ((next = it()) !== undefined) {
arr.push(next);
}
return arr;
},
"i-zip": function () {
var its = slice.call(arguments).map(iValues);
var num = its.length;
var alive = true;
return function () {
if (!alive) { return; }
var part = [];
var good = false;
for (var i = 0; i < num; i++) {
var item = part[i] = its[i]();
if (item === undefined) {
alive = false;
return;
}
}
return part;
};
},
"i-merge": function (it) {
it = iValues(it);
var obj = Object.create(null);
var pair;
while ((pair = it()) !== undefined) {
obj[metaGet(pair, 0)] = metaGet(pair, 1);
}
return obj;
},
"i-each": function (callback, it) {
var next;
var ret;
while ((next = it()) !== undefined) {
ret = callback(next);
}
return ret;
}
});
return scope.eval(string);
};
exports.attachParser = function (parser) {
parser.yy.F = Form;
parser.yy.S = Symbol;
parse = parser.parse.bind(parser);
};
Number.prototype.times = function (callback) {
var value;
for (var i = 0; i < this; i++) {
value = callback(i);
}
return value;
};