-
Notifications
You must be signed in to change notification settings - Fork 2
/
goto.js
112 lines (91 loc) · 1.86 KB
/
goto.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
/*
gotojs
======
A delightfully evil abomination of eval() with goto(); all in global space
by Dave Balmer.
See the README.md for more info.
*/
// start execution
function run(l) {
linenumber = [];
// object properties are not returned in order
for (var k in program)
linenumber.push(k * 1);
// so we sort them out
linenumber = linenumber.sort(function(a, b) {
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
});
// prevent runaway scripts
var max = 0;
running = 1;
line = l || 0;
// execution engine, powered by globals and eval()
for (; line < linenumber.length; line++) {
if (debug)
console.log(linenumber[line] + " " + program[linenumber[line]]);
// no more than 10000 lines executed; who needs more than that, really?
if (++max > 10000)
return;
// eval() is eval
eval(program[linenumber[line]]);
}
running = 0;
}
// dumping things to the DOM; couldn't bring myself to use document.write()
function print(s) {
var o = document.createElement("p");
if (s)
o.innerHTML = s;
else
o.innerHTML = " ";
document.body.appendChild(o);
}
function print_source() {
linenumber.forEach(function(n) {
print('' + n + ': ' + program[n]);
});
}
// clear the screen
function clear() {
document.body.innerHTML = "";
}
// display a button
function button(text, l) {
var b = document.createElement("button");
b.innerHTML = text;
b.setAttribute("onclick", "goto(" + l + ")");
document.body.appendChild(b);
}
debug = 0;
running = 0;
// turn debugging on
function tron() {
debug = true;
}
// turn it off
function troff() {
debug = false;
}
// move execution to another line number
function goto(l) {
var test = linenumber.indexOf(l);
if (!running) {
run(test);
}
else {
if (line >= 0)
line = test - 1;
else
line = test
}
}
// stop execution
function end() {
line = linenumber.length;
running = 0;
}