-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eva-cmts.js
472 lines (368 loc) · 12.8 KB
/
Eva-cmts.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
const Environment = require('./Environment');
const Transformer = require('./transform/Transformer');
const evaParser = require('./parser/evaParser');
const fs = require('fs');
/**
* Eva Interpreter
*/
class Eva {
/**
* Creates an Eva instance with the global environment
*/
// constructor(global = new Environment()) {
// this.global = global;
// }
constructor(global = GlobalEnvironment) {
this.global = global;
this._transformer = new Transformer();
}
/**
* Evaluates global code wrapping into a block.
*/
// evalGlobal(expressions) {
// return this._evalBlock(
// ['block', expressions],
// this.global,
// );
// }
evalGlobal(exp) {
return this._evalBody(
exp,
this.global,
);
}
/**
* Evaluates an expression in the given environment.
*/
eval(exp, env = this.global) {
// ----------------------------------------------------------------
// Self-evaluating expressions:
if (this._isNumber(exp)) {
return exp;
}
if (this._isString(exp)) {
return exp.slice(1, -1);
}
// ----------------------------------------------------------------
// Math operations:
// if (exp[0] == '+') {
// return exp[1] + exp[2];
// }
// if (exp[0] === '+') {
// return this.eval(exp[1], env) + this.eval(exp[2], env);
// }
// if (exp[0] === '*') {
// return this.eval(exp[1], env) * this.eval(exp[2], env);
// }
// ----------------------------------------------------------------
// Comparison operators:
// if (exp[0] === '>') {
// return this.eval(exp[1], env) > this.eval(exp[2], env);
// }
// if (exp[0] === '>=') {
// return this.eval(exp[1], env) >= this.eval(exp[2], env);
// }
// if (exp[0] === '<') {
// return this.eval(exp[1], env) < this.eval(exp[2], env);
// }
// if (exp[0] === '<=') {
// return this.eval(exp[1], env) <= this.eval(exp[2], env);
// }
// if (exp[0] === '=') {
// return this.eval(exp[1], env) === this.eval(exp[2], env);
// }
// ----------------------------------------------------------------
// Block: sequence of expressions
if (exp[0] === 'begin') {
const blockEnv = new Environment({}, env);
return this._evalBlock(exp, blockEnv);
}
// ----------------------------------------------------------------
// Variable declaration: (var foo 10)
if (exp[0] === 'var') {
const [_, name, value] = exp;
return env.define(name, this.eval(value, env)); // all the eval() need to be updated with value and env!
}
// ----------------------------------------------------------------
// Variable update: (set foo 100)
// if (exp[0] === 'set') {
// const [_, name, value] = exp;
// return env.assign(name, this.eval(value, env));
// }
if (exp[0] === 'set') {
const [_, ref, value] = exp;
if (ref[0] === 'prop') {
const [_tag, instanceName, propName] = ref;
const instanceEnv = this.eval(instanceName, env);
return instanceEnv.define(propName, this.eval(value, env));
}
return env.assign(ref, this.eval(value, env));
}
// ----------------------------------------------------------------
// Variable access: foo
if (this._isVariableName(exp)) {
return env.lookup(exp);
}
// ----------------------------------------------------------------
// If-expression:
if (exp[0] === 'if') {
const [_tag, condition, consequent, alternate] = exp;
if (this.eval(condition, env)) {
return this.eval(consequent, env);
}
return this.eval(alternate, env);
}
// ----------------------------------------------------------------
// while-expression:
if (exp[0] == 'while') {
const [_tag, condition, body] = exp;
let result;
while (this.eval(condition, env)) {
result = this.eval(body, env);
}
return result;
}
// --------------------------------------------
// Function declaration: (def square (x) (* x x))
//
// All the functions will be closures, just like JavaScript but not PHP
// A function which captures its definition environment
//
// Syntactic sugar for: (var square (lambda (x) (* x x)))
if (exp[0] === 'def') {
const [_tag, name, params, body] = exp;
//JIT-transpile to a variable declaration
// const varExp = ['var', name, ['lambda', params, body]];
const varExp = this._transformer.transformDefToVarLambda(exp);
return this.eval(varExp, env);
// const fn = {
// params,
// body,
// env, // Closure!
// };
// return env.define(name, fn);
}
// --------------------------------------------
// Switch-expression: (switch (cond1, block1) ... )
//
// Syntactic sugar for nested if-expressions
if (exp[0] === 'switch') {
const ifExp = this._transformer.transformSwitchToIf(exp);
return this.eval(ifExp, env);
}
// --------------------------------------------
// For-loop: (for init condition modifier body )
//
// Syntactic sugar for: (begin init (while condition (begin body modifier)))
if (exp[0] === 'for') {
const whileExp = this._transformer.transformForToWhile(exp);
return this.eval(whileExp, env);
}
// --------------------------------------------
// Increment: (++ foo)
//
// Syntactic sugar for: (set foo (+ foo 1))
if (exp[0] === '++') {
const setExp = this._transformer.transformIncToSet(exp);
return this.eval(setExp, env);
}
// --------------------------------------------
// Decrement: (-- foo)
//
// Syntactic sugar for: (set foo (- foo 1))
if (exp[0] === '--') {
const setExp = this._transformer.transformDeccToSet(exp);
return this.eval(setExp, env);
}
// --------------------------------------------
// Increment: (+= foo inc)
//
// Syntactic sugar for: (set foo (+ foo inc))
if (exp[0] === '+=') {
const setExp = this._transformer.transformIncValToSet(exp);
return this.eval(setExp, env);
}
// --------------------------------------------
// Decrement: (-= foo dec)
//
// Syntactic sugar for: (set foo (- foo dec))
if (exp[0] === '-=') {
const setExp = this._transformer.transformDecValToSet(exp);
return this.eval(setExp, env);
}
// --------------------------------------------
// Lambda function: (lambda (x) (* x x))
if (exp[0] === 'lambda') {
const [_tag, params, body] = exp;
return {
params,
body,
env, // Closure!
};
}
// --------------------------------------------
// Class declaration: (class <Name> <Parent> <Body>)
if (exp[0] === 'class') {
const [_tag, name, parent, body] = exp;
const parentEnv = this.eval(parent, env) || env;
const classEnv = new Environment({}, parentEnv);
this._evalBody(body, classEnv);
return env.define(name, classEnv);
}
// --------------------------------------------
// Class instantiation: (new <Class> <Arguments>...)
if (exp[0] === 'new') {
const classEnv = this.eval(exp[1], env);
const instanceEnv = new Environment({}, classEnv);
const args = exp
.slice(2)
.map(arg => this.eval(arg, env));
this._callUserDefinedFunction(
classEnv.lookup('constructor'),
[instanceEnv, ...args],
);
return instanceEnv;
}
// --------------------------------------------
// Property access: (prop <instanceName> <propName>)
if (exp[0] === 'prop') {
const [_tag, instanceName, propName] = exp;
const instanceEnv = this.eval(instanceName, env);
return instanceEnv.lookup(propName);
}
// --------------------------------------------
// Super expressions: (super <ClassName>)
if (exp[0] === 'super') {
const [_tag, className] = exp;
return this.eval(className, env).parent;
}
// --------------------------------------------
// Module declaration: (module <name> <body>)
if (exp[0] === 'module') {
const [_tag, name, body] = exp;
const moduleEnv = new Environment({}, env);
this._evalBody(body, moduleEnv);
return env.define(name, moduleEnv);
}
// --------------------------------------------
// Module import: (import <name>)
// (import (export1, export2, ...) <name>)
// TODO: import specific functions, export
if (exp[0] === 'import') {
const [_tag, name] = exp;
const moduleSrc = fs.readFileSync(
`${__dirname}/modules/${name}.eva`,
'utf-8',
);
const body = evaParser.parse(`(begin ${moduleSrc})`);
const moduleExp = ['module', name, body];
// NOTE: If we pass this.global as 2th argument, module will be stored in the global.
// return this.eval(moduleExp, this.global);
return this.eval(moduleExp, env);
}
// --------------------------------------------
// Function calls:
//
// (print "Hello World")
// (+ x 5)
// (> foo bar)
if (Array.isArray(exp)) {
const fn = this.eval(exp[0], env);
const args = exp
.slice(1)
.map(arg => this.eval(arg, env));
// 1. Native function:
if (typeof fn === 'function') {
return fn(...args);
}
// 2. User-defined function:
return this._callUserDefinedFunction(fn, args);
}
// throw `Unimplemented`;
throw `Unimplemented: ${JSON.stringify(exp)}`;
}
_callUserDefinedFunction(fn, args) {
const activationRecord = {};
fn.params.forEach((param, index) => {
activationRecord[param] = args[index];
});
const activationEnv = new Environment(
activationRecord,
fn.env, // static scope!
// env, // ? - dynamic scope!
);
return this._evalBody(fn.body, activationEnv);
}
_evalBody(body, env) {
if (body[0] === 'begin') {
return this._evalBlock(body, env);
}
return this.eval(body, env);
}
_evalBlock(block, env) {
let result;
const [_tag, ...expressions] = block;
expressions.forEach(exp => {
result = this.eval(exp, env);
});
return result;
}
_isNumber(exp) {
return typeof exp === 'number';
}
_isString(exp) {
return typeof exp === 'string' && exp[0] === '"' && exp.slice(-1) === '"';
}
// _isVariableName(exp) {
// return typeof exp === 'string' && /^[a-zA-Z][a-zA-Z0-9_]*$/.test(exp);
// }
_isVariableName(exp) {
return typeof exp === 'string' && /^[+\-*/<>=a-zA-Z0-9_]+$/.test(exp);
}
}
/**
* Default Global Environment.
*/
const GlobalEnvironment = new Environment({
null: null,
true: true,
false: false,
VERSION: '0.1',
// Operators:
'+'(op1, op2) {
return op1 + op2;
},
'*'(op1, op2) {
return op1 * op2;
},
'-'(op1, op2 = null) {
if (op2 == null) {
return -op1;
}
return op1 - op2;
},
'/'(op1, op2) {
return op1 / op2;
},
// Comparison:
'>'(op1, op2) {
return op1 > op2;
},
'<'(op1, op2) {
return op1 < op2;
},
'>='(op1, op2) {
return op1 >= op2;
},
'<='(op1, op2) {
return op1 <= op2;
},
'='(op1, op2) {
return op1 === op2;
},
// Console output:
print(...args) {
console.log(...args);
},
});
module.exports = Eva;