-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eva.js
328 lines (272 loc) · 8.85 KB
/
Eva.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
const fs = require('fs');
const Environment = require('./Environment');
const Transformer = require('./transform/Transformer');
const evaParser = require('./parser/evaParser');
// Eva Interpreter
class Eva {
// Creates an Eva instance with the global environment
constructor(global = GlobalEnvironment) {
this.global = global;
this._transformer = new Transformer();
}
// Evaluates global code wrapping into a block.
evalGlobal(exp) {
return this._evalBody(exp, this.global);
}
// Evaluates an expression in the given environment.
eval(exp, env = this.global) {
// this._logEnvChain(env);
// Self-evaluating expressions:
if (this._isNumber(exp)) {
return exp;
}
if (this._isString(exp)) {
return exp.slice(1, -1);
}
// 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 [_, 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))
if (exp[0] === 'def') {
const [_tag, name, params, body] = exp;
const varExp = this._transformer.transformDefToVarLambda(exp);
return this.eval(varExp, env);
}
// Switch-expression: (switch (cond1, block1) ... )
if (exp[0] === 'switch') {
const ifExp = this._transformer.transformSwitchToIf(exp);
return this.eval(ifExp, env);
}
// For-loop: (for init condition modifier body )
if (exp[0] === 'for') {
const whileExp = this._transformer.transformForToWhile(exp);
return this.eval(whileExp, env);
}
// Increment: (++ foo)
if (exp[0] === '++') {
const setExp = this._transformer.transformIncToSet(exp);
return this.eval(setExp, env);
}
// Decrement: (-- foo)
if (exp[0] === '--') {
const setExp = this._transformer.transformDeccToSet(exp);
return this.eval(setExp, env);
}
// Increment: (+= foo inc)
if (exp[0] === '+=') {
const setExp = this._transformer.transformIncValToSet(exp);
return this.eval(setExp, env);
}
// Decrement: (-= 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>)
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];
return this.eval(moduleExp, env);
}
// Function calls:
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)}`;
}
_logEnvChain(env) {
let env_output = env;
let res = [];
while (env_output.parent != null) {
res.push(env_output.record);
res.push('->');
env_output = env_output.parent;
}
if(res.length > 0){
res.pop();
}
console.log(res);
}
_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-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;