-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcc.js
123 lines (111 loc) · 1.9 KB
/
subcc.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
function makeLocation(_$) {
return {
start: {
line: _$.first_line,
column: _$.first_column
},
end: {
line: _$.last_line,
column: _$.last_column
}
};
}
if (typeof Date.now === 'undefined') {
Date.now = function now() {
return new Date().getTime();
};
}
function subcc(code, base_env, onerror) {
function errorMessage(e) {
var msg;
if (e.message)
msg = e.message;
else
msg = e.toString();
if (!e.location)
msg = 'Internal error. This isn\'t your fault, but please let us know about this\n' + msg;
return { message: msg, location: e.location };
}
var parse_tree;
try
{
subc.trace = onerror;
parse_tree = subc.parse(code);
}
catch(e)
{
if (console && console.log)
{
console.log(e);
if (e.stack)
console.log(e.stack);
}
if (onerror)
onerror(errorMessage(e));
throw e;
}
//console.log(parse_tree);
var vm = {
//run: process,
stop: function() {
if (timeoutID !== null) {
window.clearTimeout(timeoutID);
timeoutID = null;
vm.onstop(true);
}
},
onstop: function(aborted) {},
onstep: function(pos) {}
};
var vm_step;
try
{
vm_step = interpreter(parse_tree, base_env, function(node) {
if (vm)
vm.onstep(node.loc);
});
}
catch(e)
{
if (console && console.log)
{
console.log(e);
if (e.stack)
console.log(e.stack);
}
if (onerror)
onerror(errorMessage(e));
throw e;
}
var timeoutID = null;
function process() {
var start = Date.now();
while(Date.now() - start < 10)
{
try
{
if (vm_step())
continue;
}
catch(e)
{
if (console && console.log)
{
console.log(e);
if (e.stack)
console.log(e.stack);
}
if (onerror)
onerror(errorMessage(e));
timeoutID = null;
throw e;
}
vm.onstop(false);
timeoutID = null;
return;
}
timeoutID = window.setTimeout(process, 15);
}
vm.run = process;
return vm;
}