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
|
var scheme = {
obarray : {},
primitives : {},
utils : {},
env : {},
cache: [],
builtins: [],
dynstack : [],
// TODO: placeholders
FALSE : false,
TRUE : true,
NIL : false,
EMPTY : [],
UNSPECIFIED : [],
// FIXME: wingo says not to leak undefined to users
UNDEFINED: undefined
};
function not_implemented_yet() {
throw "not implemented yet";
};
function coerce_bool(obj) {
return obj ? scheme.TRUE : scheme.FALSE;
};
// Numbers
scheme.primitives.add = function (x, y) {
return x + y;
};
scheme.primitives.add1 = function (x) {
return x + 1;
};
scheme.primitives["add/immediate"] = function (x, y) {
return x + y;
};
scheme.primitives.sub = function (x, y) {
return x - y;
};
scheme.primitives.sub1 = function (x) {
return x - 1;
};
scheme.primitives["sub/immediate"] = function (x, y) {
return x - y;
};
scheme.primitives.mul = function (x, y) {
return x * y;
};
scheme.primitives.div = function (x, y) {
return x / y;
};
scheme.primitives["="] = function (x, y) {
return coerce_bool(x == y);
};
scheme.primitives["<"] = function (x, y) {
return coerce_bool(x < y);
};
scheme.primitives["<="] = function (x, y) {
return coerce_bool(x <= y);
};
scheme.primitives[">"] = function (x, y) {
return coerce_bool(x > y);
};
scheme.primitives[">="] = function (x, y) {
return coerce_bool(x >= y);
};
scheme.primitives.quo = not_implemented_yet;
scheme.primitives.rem = not_implemented_yet;
scheme.primitives.mod = not_implemented_yet;
// Unboxed Numbers
scheme.primitives["load-u64"] = function(x) {
return x;
};
scheme.primitives["u64-=-scm"] = function(x, y) {
// i.e. br-if-u64-=-scm
return coerce_bool(x === y);
};
scheme.primitives["u64-<=-scm"] = function(x, y) {
return coerce_bool(x <= y);
};
scheme.primitives["u64-<-scm"] = function(x, y) {
return coerce_bool(x < y);
};
scheme.primitives["u64->-scm"] = function(x, y) {
return coerce_bool(x > y);
};
scheme.primitives["u64->=-scm"] = function(x, y) {
return coerce_bool(x >= y);
};
// Boxes
scheme.Box = function (x) {
this.x = x;
return this;
};
scheme.primitives["box"] = function(x) {
return new scheme.Box(x);
};
scheme.primitives["box-ref"] = function (box) {
return box.x;
};
scheme.primitives["box-set!"] = function (box, val) {
box.x = val;
};
// Lists
scheme.Pair = function (car, cdr) {
this.car = car;
this.cdr = cdr;
return this;
};
scheme.primitives["pair?"] = function (obj) {
return coerce_bool(obj instanceof scheme.Pair);
};
scheme.primitives.cons = function (car, cdr) {
return new scheme.Pair(car,cdr);
};
scheme.primitives.car = function (obj) {
return obj.car;
};
scheme.primitives.cdr = function (obj) {
return obj.cdr;
};
scheme.primitives["set-car!"] = function (pair, obj) {
obj.car = obj;
};
scheme.primitives["set-cdr!"] = function (pair, obj) {
obj.cdr = obj;
};
scheme.list = function () {
var l = scheme.EMPTY;
for (var i = arguments.length - 1; i >= 0; i--){
l = scheme.primitives.cons(arguments[i],l);
};
return l;
};
scheme.primitives["null?"] = function(obj) {
return coerce_bool(scheme.EMPTY == obj);
};
// Symbols
scheme.Symbol = function(s) {
if (scheme.obarray[s]) {
return scheme.obarray[s];
} else {
this.name = s;
scheme.obarray[s] = this;
return this;
};
};
scheme.primitives["symbol?"] = function (obj) {
return coerce_bool(obj instanceof scheme.Symbol);
};
// Keywords
scheme.Keyword = function(s) {
this.name = s;
return this;
};
scheme.primitives["keyword?"] = function (obj) {
return coerce_bool(obj instanceof scheme.Keyword);
};
scheme.utils.keyword_ref = function(kw, args, start, dflt) {
var l = args.length;
if ((l - start) % 2 == 1) {
// FIXME: should error
return undefined;
}
// Need to loop in reverse because last matching keyword wins
for (var i = l - 2; i >= start; i -= 2) {
if (!(args[i] instanceof scheme.Keyword)) {
return undefined;
}
if (args[i].name === kw.name) {
return args[i + 1];
}
}
return dflt;
};
// Vectors
scheme.Vector = function () {
this.array = Array.prototype.slice.call(arguments);
return this;
};
scheme.primitives["vector-ref"] = function (vec, idx) {
return vec.array[idx];
};
scheme.primitives["vector-set!"] = function (vec, idx, obj) {
return vec.array[idx] = obj;
};
scheme.primitives["vector-length"] = function (vec) {
return vec.array.length;
};
scheme.primitives["vector?"] = function (obj) {
return coerce_bool(obj instanceof scheme.Vector);
};
scheme.primitives["make-vector/immediate"] = not_implemented_yet;
scheme.primitives["vector-set!/immediate"] = not_implemented_yet;
scheme.primitives["vector-ref/immediate"] = not_implemented_yet;
// Bytevectors
// Booleans
scheme.primitives["boolean?"] = not_implemented_yet;
// Chars
scheme.Char = function(c) {
this.c = c;
return this;
};
scheme.primitives["char?"] = function (obj) {
return coerce_bool(obj instanceof scheme.Char);
};
// Strings
scheme.String = function(s) {
this.s = s;
return this;
};
scheme.primitives["string?"] = function (obj) {
return coerce_bool(obj instanceof scheme.String);
};
scheme.primitives["string-length"] = function (str) {
return str.s.length;
};
scheme.primitives["string-ref"] = function (str, idx) {
return new scheme.Char(str.s[idx]);
};
// Closures
scheme.Closure = function(f, size) {
this.fun = f;
this.freevars = new Array(size);
return this;
};
scheme.primitives["free-set!"] = function (closure, idx, obj) {
closure.freevars[idx] = obj;
};
scheme.primitives["free-ref"] = function (closure, idx) {
return closure.freevars[idx];
};
scheme.primitives["builtin-ref"] = function (idx) {
return scheme.builtins[idx];
};
// Modules
scheme.primitives["define!"] = function(sym, obj) {
var b = new scheme.Box(obj);
scheme.env[sym.name] = b;
return b;
};
scheme.primitives["cache-current-module!"] = function (module, scope) {
scheme.cache[scope] = module;
};
scheme.primitives["cached-toplevel-box"] = function (scope, sym, is_bound) {
return scheme.cache[scope][sym.name];
};
scheme.primitives["cached-module-box"] = not_implemented_yet;
scheme.primitives["current-module"] = function () {
return scheme.env;
};
scheme.primitives["resolve"] = function (sym, is_bound) {
return scheme.env[sym.name];
};
// bleh
scheme.initial_cont = function (x) { return x; };
scheme.primitives.return = function (x) { return x; };
scheme.is_true = function (obj) {
return !(obj === scheme.FALSE || obj === scheme.NIL);
};
// Builtins
var apply = function(self, k, f) {
var args = Array.prototype.slice.call(arguments, 3);
var tail = args.pop();
while (scheme.is_true(scheme.primitives["pair?"](tail))) {
args.push(tail.car);
tail = tail.cdr;
};
return f.fun.apply(f.fun, [f,k].concat(args));
};
var values = function(self, k) {
var args = Array.prototype.slice.call(arguments, 2);
return k.apply(k,args);
};
var abort_to_prompt = function(self, k, prompt) {
var args = Array.prototype.slice.call(arguments, 3);
var idx = find_prompt(prompt);
var frame = scheme.dynstack[idx];
var kont = undefined; // actual value doesn't matter
if (!scheme.is_true(frame.escape_only)) {
var f = function (self, k2) {
var args = Array.prototype.slice.call(arguments, 2);
return k.apply(k,args);
};
kont = new scheme.Closure(f, 0);
};
unwind(idx);
var handler = frame.handler;
args.unshift(kont);
return handler.apply(handler, args);
};
var call_with_values = function (self, k, producer, consumer) {
var k2 = function () {
var args = Array.prototype.slice.call(arguments);
return consumer.fun.apply(consumer.fun, [consumer, k].concat(args));
};
return producer.fun(producer, k2);
};
var callcc = function (self, k, closure) {
var f = function (self, k2) {
var args = Array.prototype.slice.call(arguments, 2);
return k.apply(k,args);
};
return closure.fun(closure, k, new scheme.Closure(f, 0));
};
scheme.builtins[0] = new scheme.Closure(apply, 0);
scheme.builtins[1] = new scheme.Closure(values, 0);
scheme.builtins[2] = new scheme.Closure(abort_to_prompt, 0);
scheme.builtins[3] = new scheme.Closure(call_with_values, 0);
scheme.builtins[4] = new scheme.Closure(callcc, 0);
// Structs
scheme.primitives["struct?"] = not_implemented_yet;
scheme.primitives["struct-set!/immediate"] = not_implemented_yet;
scheme.primitives["struct-vtable"] = not_implemented_yet;
scheme.primitives["struct-ref/immediate"] = not_implemented_yet;
scheme.primitives["struct-ref"] = not_implemented_yet;
scheme.primitives["struct-set!"] = not_implemented_yet;
scheme.primitives["allocate-struct/immediate"] = not_implemented_yet;
// Equality
scheme.primitives["eq?"] = function(x, y) {
return coerce_bool(x === y);
};
scheme.primitives["eqv?"] = function(x, y) {
return coerce_bool(x === y);
};
scheme.primitives["equal?"] = not_implemented_yet;
// Fluids
scheme.Fluid = function (x) {
this.value = x;
return this;
};
scheme.primitives["pop-fluid"] = function () {
var frame = scheme.dynstack.shift();
if (frame instanceof scheme.frame.Fluid) {
frame.fluid.value = frame.fluid.old_value;
return;
} else {
throw "not a frame";
};
};
scheme.primitives["push-fluid"] = function (fluid, new_value) {
var old_value = fluid.value;
fluid.value = new_value;
var frame = new scheme.frame.Fluid(fluid, old_value);
scheme.dynstack.unshift(frame);
return;
};
scheme.primitives["fluid-ref"] = function (fluid) {
// TODO: check fluid type
return fluid.value;
};
// Variables
scheme.primitives["variable?"] = not_implemented_yet;
// Dynamic Wind
scheme.primitives["wind"] = not_implemented_yet;
scheme.primitives["unwind"] = not_implemented_yet;
// Misc
scheme.primitives["prompt"] = function(escape_only, tag, handler){
var frame = new scheme.frame.Prompt(tag, escape_only, handler);
scheme.dynstack.unshift(frame);
};
var unwind = function (idx) {
// TODO: call winders
scheme.dynstack = scheme.dynstack.slice(idx+1);
};
var find_prompt = function(prompt) {
var eq = scheme.primitives["eq?"];
function test(x){
return scheme.is_true(eq(x,prompt)) || scheme.is_true(eq(x,scheme.TRUE));
};
for (idx in scheme.dynstack) {
var frame = scheme.dynstack[idx];
if (frame instanceof scheme.frame.Prompt && test(frame.tag)) {
return idx;
};
};
// FIXME: should error
return undefined;
};
scheme.primitives["handle-interrupts"] = function () {
// TODO: implement
return;
};
// Dynstack frames
scheme.frame = {};
scheme.frame.Prompt = function(tag, escape_only, handler){
this.tag = tag;
this.escape_only = escape_only;
this.handler = handler;
};
scheme.frame.Fluid = function(fluid, old_value) {
this.fluid = fluid;
this.old_value = old_value;
};
|