/
satriani.interpreter.js
440 lines (398 loc) · 15.8 KB
/
satriani.interpreter.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
module.exports = {
Environment: Environment,
eq: eq
}
const MYSTERIOUS = '__MYSTERIOUS__';
function Environment(parent) {
this.vars = Object.create(parent ? parent.vars : null);
this.parent = parent;
this.output = (parent && parent.output ? parent.output : console.log);
// Because nodeJS is based on asynchronous IO, there is no built-in console.readline or similar
// so by default, any input will yield an empty string.
this.input = (parent && parent.input ? parent.input : () => "")
}
Environment.prototype = {
extend: function () { return new Environment(this) },
exists: function (name) {
return (name in this.vars);
},
lookup: function (name, index) {
if (name in this.vars == false) {
throw new Error("Undefined variable " + name);
}
let variable = this.vars[name];
if (Array.isArray(variable)) {
if (typeof (index) == 'undefined' || index == null) {
return variable;
}
return variable[index];
}
if (typeof (variable) == 'string' && typeof (index) == 'number') {
return (variable[index]);
}
return variable;
},
assign: function (name, value, index, local) {
let container = (!local && typeof (value) != "function" && this.parent && name in this.parent.vars) ? this.parent.vars : this.vars;
if (typeof (index) == 'undefined' || index == null) return container[name] = value;
if (name in container) {
if (!Array.isArray(container[name])) throw new Error(`Can't assign ${name} at ${index} - ${name} is not an indexed variable.`);
} else {
container[name] = new Array();
}
return container[name][index] = value;
},
run: function (program) {
let result = evaluate(program, this);
return (result ? result.value : undefined);
},
dealias: function (expr) {
if (expr.variable.pronoun) return this.pronoun_alias;
return (expr.variable);
},
pronoun_alias: null,
}
function toScalar(value) {
if (Array.isArray(value)) {
return value.length;
}
return value;
}
function evaluate(tree, env) {
if (tree == MYSTERIOUS || typeof (tree) == 'undefined') return undefined;
if (tree == null) return null;
let list = Object.entries(tree)
for (let i = 0; i < list.length; i++) {
let node = list[i];
let type = node[0];
let expr = node[1];
switch (type) {
case "action": return (tree);
case "list":
let result = null;
for (let i = 0; i < expr.length; i++) {
let next = expr[i];
result = evaluate(next, env);
if (result && result.action) return (result);
}
return result;
case "conditional":
if (toScalar(evaluate(expr.condition, env))) {
return evaluate(expr.consequent, env);
} else if (expr.alternate) {
return evaluate(expr.alternate, env);
}
return;
case 'break':
return { 'action': 'break' };
case 'continue':
return { 'action': 'continue' };
case "return":
return { 'action': 'return', 'value': evaluate(expr.expression, env) };
case "number":
case "string":
case "constant":
return (expr);
case "output":
let printable = toScalar(evaluate(expr, env));
if (typeof (printable) == 'undefined') printable = "mysterious";
env.output(printable);
return;
case "listen":
return env.input();
case "binary":
return binary(expr, env);
case "lookup":
return lookup(expr, env);
case "assign":
return assign(expr, env);
case "pronoun":
return env.lookup(env.pronoun_alias);
case "blank":
return;
case "rounding":
return rounding(expr, env);
case "mutation":
return mutation(expr, env);
case "increment":
let increment_name = env.dealias(expr);
let old_increment_value = env.lookup(increment_name);
switch (typeof (old_increment_value)) {
case "boolean":
if (expr.multiple % 2 != 0) env.assign(increment_name, !old_increment_value);
return;
default:
env.assign(increment_name, (old_increment_value + expr.multiple));
return;
}
case "decrement":
let decrement_name = env.dealias(expr);
let old_decrement_value = env.lookup(decrement_name);
switch (typeof (old_decrement_value)) {
case "boolean":
if (expr.multiple % 2 != 0) env.assign(decrement_name, !old_decrement_value);
return;
default:
env.assign(decrement_name, (old_decrement_value - expr.multiple));
return;
}
case "while_loop":
while_outer: while (toScalar(evaluate(expr.condition, env))) {
let result = evaluate(expr.consequent, env);
if (result) switch (result.action) {
case 'continue':
continue while_outer;
case 'break':
break while_outer;
case 'return':
return (result);
}
}
return;
case "until_loop":
until_outer: while (!toScalar(evaluate(expr.condition, env))) {
let result = evaluate(expr.consequent, env);
if (result) switch (result.action) {
case 'continue':
continue until_outer;
case 'break':
break until_outer;
case 'return':
return (result);
}
}
return;
case "comparison":
let lhs = evaluate(expr.lhs, env);
let rhs = evaluate(expr.rhs, env);
switch (expr.comparator) {
case "eq":
return eq(lhs, rhs);
case "ne":
return !eq(lhs, rhs);
case "lt":
return (toScalar(lhs) < toScalar(rhs));
case "le":
return (toScalar(lhs) <= toScalar(rhs));
case "ge":
return (toScalar(lhs) >= toScalar(rhs));
case "gt":
return (toScalar(lhs) > toScalar(rhs));
default:
throw new Error(`Unknown comparison operator ${expr.comparator}`);
}
case "not":
return (!toScalar(evaluate(expr.expression, env)));
case "function":
env.assign(expr.name, make_lambda(expr, env));
return;
case "call":
let func = env.lookup(expr.name);
let func_result = func.apply(null, expr.args.map(arg => {
let value = evaluate(arg, env);
// If the arg is an array, we shallow-copy it when passing it to a function call
return (value && value.map ? value.map(e => e) : value);
}));
return (func_result ? func_result.value : undefined);
case "enlist":
return enlist(expr, env);
case "delist":
return delist(expr, env);
default:
if (Array.isArray(tree) && tree.length == 1) return (evaluate(tree[0], env));
throw new Error("Sorry - I don't know how to evaluate this: " + JSON.stringify(tree))
}
}
}
function mutation(expr, env) {
let source = evaluate(expr.source, env);
let modifier = evaluate(expr.modifier, env);
switch (expr.type) {
case "split":
return source.toString().split(modifier || "");
case "cast":
if (typeof (source) == 'string') return parseInt(source, modifier);
if (typeof (source) == 'number') return String.fromCharCode(source);
if (Array.isArray(source)) return String.fromCharCode(toScalar(source));
throw new Error(`I don't know how to cast ${source}`);
case "join":
if (Array.isArray(source)) {
let joiner = (typeof (modifier) == 'undefined' || modifier == null) ? '' : modifier;
return source.join(joiner);
}
throw new Error("I don't know how to join that.");
}
}
function lookup(expr, env) {
let lookup_name = env.dealias(expr);
let index = evaluate(expr.index, env);
return env.lookup(lookup_name, index);
}
function assign(expr, env) {
let target = expr.target;
let index = evaluate(target.index, env);
let alias = (target.variable.pronoun ? env.pronoun_alias : target.variable);
let value = evaluate(expr.expression, env);
env.assign(alias, value, index);
if (!target.variable.pronoun) env.pronoun_alias = alias;
return value;
}
function enlist(expr, env) {
let array_value;
let array_name = env.dealias(expr);
if (env.exists(array_name)) {
array_value = env.lookup(array_name, null, true);
if (!Array.isArray(array_value)) array_value = [array_value];
} else {
array_value = [];
}
if (expr.expression) {
let elements_to_enlist = (expr.expression.map ? expr.expression : [expr.expression]);
array_value = array_value.concat(elements_to_enlist.map(e => evaluate(e, env)));
}
env.assign(array_name, array_value);
return array_value;
// let alias = "";
// let target = expr.target;
// let index = evaluate(target.index, env);
// if (target.variable.pronoun) {
// alias = env.pronoun_alias;
// } else {
// alias = target.variable;
// env.pronoun_alias = alias;
// }
// let value;
// if (env.exists(alias)) {
// value = env.lookup(alias);
// if (!Array.isArray(value)) value = [value];
// } else {
// value = [];
// }
// if (expr.expression) {
// let values = (expr.expression.map ? expr.expression : [expr.expression]);
// value = value.concat(values.map(e => evaluate(e, env)));
// }
// env.assign(alias, value, index);
// return value;
}
function delist(expr, env) {
let name = env.dealias(expr);
let source = env.lookup(name, null)
let result = (source.shift && source.shift());
return result;
}
function rounding(expr, env) {
let variable_name = env.dealias(expr);
let variable_value = env.lookup(variable_name);
switch (expr.direction) {
case "up":
return env.assign(variable_name, Math.ceil(variable_value));
case "down":
return env.assign(variable_name, Math.floor(variable_value));
default:
return env.assign(variable_name, Math.round(variable_value));
}
}
function demystify(expr, env) {
let result = evaluate(expr, env);
if (typeof (result) == 'undefined') return ('mysterious');
return (result);
}
function eq(lhs, rhs) {
if (Array.isArray(lhs)) return (eq_array(lhs, rhs));
if (Array.isArray(rhs)) return (eq_array(rhs, lhs));
if (is_nothing(lhs) && is_nothing(rhs)) return (true);
// if (typeof (lhs) == 'undefined') return (typeof (rhs) == 'undefined');
// if (typeof (rhs) == 'undefined') return (typeof (lhs) == 'undefined');
if (typeof (lhs) == 'boolean') return (eq_boolean(lhs, rhs));
if (typeof (rhs) == 'boolean') return (eq_boolean(rhs, lhs));
if (typeof (lhs) == 'number') return (eq_number(lhs, rhs));
if (typeof (rhs) == 'number') return (eq_number(rhs, lhs));
if (typeof (lhs) == 'string') return (eq_string(lhs, rhs));
if (typeof (rhs) == 'string') return (eq_string(rhs, lhs));
return lhs == rhs;
}
function is_nothing(thing) {
return (
typeof (thing) == 'undefined'
||
thing === null
||
thing === ""
||
thing == 0
||
thing == false
);
}
function eq_array(array, other) {
if (Array.isArray(other)) return ((array.length == other.length) && array.every((el, ix) => eq(el, other[ix])));
if (other == null || other == 0 || other == "") return (array.length == 0);
return (false);
}
function eq_string(string, other) {
if (other == null || typeof (other) == 'undefined') return (string === "");
return (other == string);
}
function eq_number(number, other) {
if (other == null || typeof (other) == 'undefined') return (number === 0);
return (other == number);
}
function eq_boolean(bool, other) {
// false equals null in Rockstar
if (other == null) other = false;
// false equals zero in Rockstar
if (typeof (other) == 'number') other = (other !== 0);
if (typeof (other) == 'string') other = (other !== "");
return (bool == other);
}
function make_lambda(expr, env) {
function lambda() {
let names = expr.args;
if (names.length != arguments.length) throw ('Wrong number of arguments supplied to function ' + expr.name + ' (' + expr.args + ')');
let scope = env.extend();
for (let i = 0; i < names.length; ++i) scope.assign(names[i], arguments[i], null, 1)
return evaluate(expr.body, scope);
}
return lambda;
}
function binary(b, env) {
switch (b.op) {
case "and": return (toScalar(evaluate(b.lhs, env)) && toScalar(evaluate(b.rhs, env)));
case "nor": return (!toScalar(evaluate(b.lhs, env)) && !toScalar(evaluate(b.rhs, env)));
case "or": return (toScalar(evaluate(b.lhs, env)) || toScalar(evaluate(b.rhs, env)));
case '+': return add(b.lhs, b.rhs, env);
case '-': return subtract(b.lhs, b.rhs, env);
case '/': return divide(b.lhs, b.rhs, env);
case '*': return multiply(b.lhs, b.rhs, env);
}
}
function add(lhs, rhs, env) {
return (rhs.reduce ? rhs : [rhs]).reduce((acc, val) => acc += toScalar(demystify(val, env)), toScalar(demystify(lhs, env)));
}
function subtract(lhs, rhs, env) {
return (rhs.reduce ? rhs : [rhs]).reduce((acc, val) => acc -= toScalar(evaluate(val, env)), toScalar(evaluate(lhs, env)));
}
function divide(lhs, rhs, env) {
return (rhs.reduce ? rhs : [rhs]).reduce((acc, val) => acc /= toScalar(evaluate(val, env)), toScalar(evaluate(lhs, env)));
}
function multiply(lhs, rhs, env) {
return (rhs.reduce ? rhs : [rhs])
.map(expr => toScalar(evaluate(expr, env)))
.reduce(multiply_reduce, toScalar(evaluate(lhs, env)));
}
function multiply_reduce(acc, val, idx, src) {
// Null, nothing, noone, nowhere, etc. are all zero for multiplication purposes.
if (acc == null) acc = 0;
if (val == null) val = 0;
// Mu ltiplying numbers just works.
if (typeof (acc) == 'number' && typeof (val) == 'number') return (acc * val);
// Multiplying strings by numbers does repeated concatenation
if (typeof (acc) == 'string' && typeof (val) == 'number') return multiply_string(acc, val);
if (typeof (acc) == 'number' && typeof (val) == 'string') return multiply_string(val, acc);
}
function multiply_string(s, n) {
let result = Array();
while (--n >= 0) result.push(s);
return (result.join(''));
}