-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeOut.js
420 lines (344 loc) · 14.9 KB
/
TimeOut.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
var forever = 'forever';
function TimeOut() {
var that = this;
this.handleList = new Array(); // store of all the handles, in order to clear them
// internal class Token
function Token(t, v) {
this.type = t; // what type is it? there are only two: all expressions are literal values, everything else is a command
this.value = v; // if it's a literal, the actual value
}
Token.TokenType = {
LITERAL: 0,
COMMAND: 1
};
// actually instantiated Token to be populated when start and end times are established
function SleepNode(index, to) { // to = the TimeOut object
var that = this;
this.toWait = to.programTimes[index];
this.calledTime = Date.now();
this.token;
this.index = index;
this.executed = false; // the second time we get to the while or if statement, we'll need to know we've been here before
this.CallBack = function () {
var executedTime = Date.now();
var passedTime = executedTime - that.calledTime;
to.currentTime += Math.round(passedTime / TimeOut.COMMAND_LENGTH);
that.token = to.commandList[to.currentTime % to.commandList.length];
to.Execute(that.token, that.toWait, passedTime, to.currentTime, that.executed);
this.executed = true;
clearTimeout(that.Handle);
// if this is an if or while, we'll need to add to call stack and change up the handles
if (index + 1 < to.programTimes.length)
nextNode = new SleepNode(index + 1, to);
}
this.Handle = window.setTimeout(function () {
that.CallBack();
}, this.toWait);
to.handleList.push(this.Handle);
}
this.programTimes = new Array(); // list of all the loaded times, to be executed
this.callStack = new Array(); // for loops, if statements, etc
this.commandList = new Array();
this.currentCommandIndex = 0;
this.currentTime = 0;
var concat = function(type, value) {
that.commandList.push(new Token(type, value));
}
for (var a = 0; a < 16; a++) {
concat(Token.TokenType.LITERAL, a);
} // literals
var chars =
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
" _<>,.?/:;\"'{}[]|`~!@#$%^&*()-+=\\'"; // 34
for (var a = 0; a < chars.length; a++) {
concat(Token.TokenType.LITERAL, chars[a]);
} // literals
concat(Token.TokenType.LITERAL, "NEWLINE");
// operators
concat(Token.TokenType.COMMAND, '+'); // ADDS first two items
concat(Token.TokenType.COMMAND, '-'); // SUBTRACTS second item from first
concat(Token.TokenType.COMMAND, '*');
concat(Token.TokenType.COMMAND, '/'); // DIVIDES second item from first
concat(Token.TokenType.COMMAND, '%');
// stack operations (an be run on either stack)
concat(Token.TokenType.COMMAND, "CONCAT"); // concat second into first
concat(Token.TokenType.COMMAND, "SWAP"); // swap top 2
concat(Token.TokenType.COMMAND, "DUP"); // copy TOS into NOS
concat(Token.TokenType.COMMAND, "DROP"); // remove TOS
concat(Token.TokenType.COMMAND, "ROT3"); // rotate top 3
concat(Token.TokenType.COMMAND, "PICK"); // get top item (should be a number), copy that numbered item to the top
concat(Token.TokenType.COMMAND, "ROLL"); // get top item (should be a number), roll that numbered item to top
concat(Token.TokenType.COMMAND, "DEPTH"); // push the current length of the stack
concat(Token.TokenType.COMMAND, "EMIT"); // print top item, pops it off the stack
concat(Token.TokenType.COMMAND, "READ"); // read input, push onto stack
concat(Token.TokenType.COMMAND, "IF_NONZERO"); // IF top item on data stack is non-zero
concat(Token.TokenType.COMMAND, "IF_LESSER"); // IF top item on data stack is less than second item
concat(Token.TokenType.COMMAND, "ENDIF");
concat(Token.TokenType.COMMAND, "ELSE");
concat(Token.TokenType.COMMAND, "WHILE_NONZERO"); // WHILE top item on data stack is non-zero
concat(Token.TokenType.COMMAND, "WHILE_LESSER"); // WHILE top item on data stack is less than second item
concat(Token.TokenType.COMMAND, "WHILE_GREATER"); // WHILE top item on data stack is more than second item
concat(Token.TokenType.COMMAND, "ENDWHILE");
while (this.commandList.length < TimeOut.COMMAND_COUNT)
{
concat(Token.TokenType.PLACEHOLDER, "UNASSIGNED");
}
this.Set = function (time) {
if (time == forever) {
that.sleepNodeList = new SleepNode(0, that);
}
else if (!isNaN(time)) {
// amount to sleep
that.programTimes.push(parseInt(time) * TimeOut.COMMAND_LENGTH);
}
else {
// FIXME: this should be written to output
alert("invalid time entered: " + time);
}
};
that.dataStack = new Array();
that.programList = new Array();
that.currentQuery = '';
that.newlines = function (line) {
line = line.toString(); // in case it's a number
return line.replace("\n", "<BR>");
};
this.Execute = function (token, toWait, passedTime, timeIndex) {
var console = document.getElementById('console');
// var output = document.getElementById('output');
var failed = (passedTime - toWait >= TimeOut.COMMAND_LENGTH / 2.0);
//build string for program monitor
var addString = "";
if (failed) addString += "<span class=\"bad\">";
if (Object.getOwnPropertyNames(Token.TokenType)[token.type] == 'LITERAL') {
addString += Object.getOwnPropertyNames(Token.TokenType)[token.type] + " | " + token.value;
}
else {
addString += token.value
}
addString += "| toWait: " + toWait + "| passedtime: " + passedTime + "<br/>";
if (failed) addString += "</span>";
console.innerHTML = addString + console.innerHTML;
that.currentQuery += TimeOut.Interpret(token) + '\n';
if (that.callStack.length == 0) { // if the callStack has nothing in it, we can execute what we have. Otherwise, we build up the query
eval(that.currentQuery);
that.currentQuery = '';
// populate our stack
$('#dataStack').html('');
for (var di = that.dataStack.length - 1; di >= 0; di--) {
$('#dataStack').append(that.dataStack[di] + "<br />")
}
}
}
// this translates pseudo-commands (such as "DUP") into Time Out commands in the timeOut.Set() format
// used on the translate page
this.translate = function(program)
{
var commandIdx = 0; // index of current command from the big list
for (var programIdx = 0; programIdx < program.length; programIdx++) {
var commandDiff = 0; // number of steps from last command to current command
var startingPlace = commandIdx;
var sourceValue, sourceType
var nextTimeFail = false;
do {
commandIdx++; commandDiff++;
if (program.length < programIdx && program[programIdx].trim() === "") // break on blank lines
{
programIdx++; // advance to the next line
continue;
}
var sourceParts = (program[programIdx]).split('|');
sourceType = Object.getOwnPropertyNames(Token.TokenType).indexOf(sourceParts[0]);
sourceValue = sourceParts[1];
if (sourceValue != " " && !isNaN(sourceValue))
{
sourceValue = parseInt(sourceValue);
}
if (sourceType === -1)
{
$('#console').text("COULD NOT FIND TYPE: " + sourceParts[0]);
return;
}
if (nextTimeFail) {
$('#console').text("COULD NOT FIND COMMAND: " + sourceValue);
return;
}
if (commandIdx >= that.commandList.length) // take care of looping
commandIdx = 0;
if (commandIdx == startingPlace) { // if we've looped all the way around
nextTimeFail = true;
}
if (that.commandList[commandIdx] == undefined)
{
$('#console').text("BAD COMMAND");
return;
}
} while (
sourceType != that.commandList[commandIdx].type ||
sourceValue !== that.commandList[commandIdx].value
);
$('#generatedCode').append("timeOut.Set(" + commandDiff + ");<br />");
}
$('#generatedCode').append("timeOut.Set(forever);");
}
// token = a Token object
TimeOut.Interpret = function (token)
{
switch(token.type)
{
case (Token.TokenType.LITERAL):
if (token.value == "NEWLINE")
return "that.dataStack.push('<br>');";
if (token.value == " ")
return "that.dataStack.push(' ');";
var toAdd = '';
if (isNaN(token.value))
{
toAdd = "'" + token.value + "'";
}
else
{
toAdd = token.value;
}
return "that.dataStack.push(" + toAdd + ");"; // add to the stack
break;
case (Token.TokenType.COMMAND):
that.programList.push(token.value); // show the command in the program list
switch (token.value) {
case ("EMIT"):
return "$('#output').append(that.newlines(that.dataStack.pop()));";
case ("CONCAT"):
case ("SWAP"):
case ("DUP"):
case ("ROT3"):
case ("PICK"):
case ("ROLL"):
return "that." + token.value + "();";
case ("DROP"):
that.dataStack.pop();
case ("DEPTH"):
that.dataStack.push(that.dataStack.length);
case ("-"):
case ("*"):
case ("/"):
case ("%"):
return "that.Math('" + token.value + "');"
case ("IF_NONZERO"):
that.callStack.push(token.value);
return "if(that.dataStack[that.dataStack.length - 1] != 0) {";
case ("IF_LESSER"):
that.callStack.push(token.value);
return "if(that.dataStack[that.dataStack.length - 1] < that.dataStack[that.dataStack.length - 2]) {"
case ("WHILE_NONZERO"):
that.callStack.push(token.value);
return "while(that.dataStack[that.dataStack.length - 1] != 0) {"
case ("WHILE_LESSER"):
that.callStack.push(token.value);
return "while(that.dataStack[that.dataStack.length - 1] < that.dataStack[that.dataStack.length - 2]) {"
case ("WHILE_GREATER"):
that.callStack.push(token.value);
return "while(that.dataStack[that.dataStack.length - 1] > that.dataStack[that.dataStack.length - 2]) {"
case ("ENDWHILE"):
var topOfStack = that.callStack.pop();
if (topOfStack == "WHILE_NONZERO" || topOfStack == "WHILE_LESSER" || topOfStack == "WHILE_GREATER")
{
return "}";
}
else
{
$('#console').append("ENDWHILE UNEXPECTED");
return;
}
case ("ELSE"):
var topOfStack = that.callStack[that.callStack.length - 1];
if (topOfStack == "IF_NONZERO" || topOfStack == "IF_LESSER") {
return "} else {";
}
else {
$('#console').append("ELSE UNEXPECTED");
return;
}
case ("ENDIF"):
var topOfStack = that.callStack.pop();
if (topOfStack == "IF_NONZERO" || topOfStack == "IF_LESSER") {
return "}";
}
else {
$('#console').append("ENDIF UNEXPECTED");
return;
}
}
break;
}
}
// COMMANDS
// these are the commands called by the code passed back from Interpret()
that.CONCAT = function()
{
var topItem = that.dataStack.pop().toString();
var secondItem = that.dataStack.pop().toString();
topItem += secondItem;
that.dataStack.push(topItem);
}
that.SWAP = function()
{
var top = that.dataStack.pop();
var sec = that.dataStack.pop();
that.dataStack.push(top);
that.dataStack.push(sec);
}
that.DUP = function()
{
var top = that.dataStack.pop();
var next = top;
that.dataStack.push(top);
that.dataStack.push(next);
}
that.ROT3 = function ()
{
var tos = that.dataStack.pop();
var nos = that.dataStack.pop();
var thr = that.dataStack.pop();
that.dataStack.push(thr);
that.dataStack.push(tos);
that.dataStack.push(nos);
}
that.ROLL = function () {
var idx = that.dataStack.pop();
var value = that.dataStack[that.dataStack.length - 1 - idx];
that.dataStack.splice(that.dataStack.length - 1 - idx, 1);
that.dataStack.push(value);
}
that.PICK = function () {
var idx = that.dataStack.pop();
var value = that.dataStack[that.dataStack.length - 1 - idx];
that.dataStack.push(value);
}
that.Math = function(operation)
{
var tos = that.dataStack.pop();
var nos = that.dataStack.pop();
switch(operation)
{
case "+":
that.dataStack.push(tos + nos);
break;
case "-":
that.dataStack.push(tos - nos);
break;
case "*":
that.dataStack.push(tos * nos);
break;
case "/":
that.dataStack.push(tos / nos);
break;
case "^":
that.dataStack.push(tos ^ nos);
break;
}
}
};
TimeOut.COMMAND_LENGTH = 30.0; // number of milliseconds for it to pass from one command to the next
TimeOut.COMMAND_COUNT = 200; // 3000