-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
109 lines (92 loc) · 3.09 KB
/
test.js
File metadata and controls
109 lines (92 loc) · 3.09 KB
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
'use strict';
function testRun() {
var body = document.getElementById("codearea").value;
try { alert(topLevelEval(body)); }
catch (error) { alert(error); }
}
function topLevelEval(text) {
var expr = parseProgram(text)[0];
return trampoline(expr.evaluate(globalEnv, null),
false);
}
function compiledRun(text) {
var expr = parseProgram(text)[0];
var code = expr.compile('null');
return trampoline((0, eval(code)),
false);
}
var sysBob = makeBob(rootBob, {
'$true': function(_, me, k) { return [k, true]; },
'$false': function(_, me, k) { return [k, false]; },
});
var globalEnv = {'$sys': sysBob};
// We incorporate these interpreted methods by mutating the primitive
// method tables, because:
// * We want to be able to test the system with just the core
// primitives and no library.
// * We don't want to slow down the primitive types by extending each
// 'real' primitive with a bob-wrapper that adds the library methods.
function extendInPlace(methods, overlay) {
// TODO: deep copy? Shallow is all we need for now.
Object.getOwnPropertyNames(overlay.methods).forEach(function(slot) {
if (methods[slot] !== undefined)
throw new Error("Redefining " + slot);
methods[slot] = overlay.methods[slot];
});
}
extendInPlace(mirandaMethods, topLevelEval(sys.miranda));
extendInPlace(booleanMethods, topLevelEval(sys.bool));
extendInPlace(numberMethods, topLevelEval(sys.number));
extendInPlace(stringMethods, topLevelEval(sys.string));
extendInPlace(sysBob.methods, topLevelEval(sys.sys));
document.onkeypress = function(e) {
// Intercept ctrl-Y; when you see it, call testRun().
if (e.ctrlKey && (e.charCode === 121 || e.charCode === 25)) {
testRun();
return false;
}
return true;
}
// Benchmark
var compileBench = false; // false to interpret, true to compile.
// Log to the console whether the examples work, and how long they take.
function bench() {
timeExample('fac');
timeExample('tarai');
timeExample('itersum3');
timeExample('fib');
timeExample('tak');
timeExample('freezer');
}
var badResult;
function timeExample(name) {
var expr = parseProgram(examples[name])[0];
var result, thunk;
if (compileBench) {
var code = expr.compile('null');
// console.log(code);
thunk = function() {
result = trampoline((0, eval(code)), false);
};
} else {
thunk = function() {
result = trampoline(expr.evaluate(globalEnv, null),
false);
};
}
var dt = timex(thunk);
var expected = examples[name+'.expected'];
if (expected !== undefined && expected !== ''+result) {
console.log('FAILED', dt, name, '; see badResult');
badResult = result;
} else if (expected === undefined)
console.log('Completed', dt, name, result);
else
console.log('OK', dt, name);
}
function timex(thunk) {
var start = Date.now();
var result = thunk();
var end = Date.now();
return end - start;
}