-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathEva.js
330 lines (247 loc) · 6.78 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
329
330
/**
* Eva programming language.
*
* AST interpreter.
*
* Course info: http://dmitrysoshnikov.com/courses/essentials-of-interpretation/
*
* (C) 2018-present Dmitry Soshnikov <[email protected]>
*/
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 = 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) {
// --------------------------------------------
// Self-evaluating expressions:
if (this._isNumber(exp)) {
// Implement here: see Lecture 5
}
if (this._isString(exp)) {
// Implement here: see Lecture 5
}
// --------------------------------------------
// 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') {
// Implement here: see Lecture 6
}
// --------------------------------------------
// Variable update: (set foo 10)
if (exp[0] === 'set') {
// Implement here: see Lectures 6 and 15
}
// --------------------------------------------
// Variable access: foo
if (this._isVariableName(exp)) {
// Implement here: see Lecture 6
}
// --------------------------------------------
// if-expression:
if (exp[0] === 'if') {
// Implement here: see Lecture 8
}
// --------------------------------------------
// while-expression:
if (exp[0] === 'while') {
// Implement here: see Lecture 8
}
// --------------------------------------------
// Function declaration: (def square (x) (* x x))
//
// Syntactic sugar for: (var square (lambda (x) (* x x)))
if (exp[0] === 'def') {
// Implement here: see Lectures 11, 12
}
// --------------------------------------------
// Switch-expression: (switch (cond1, block1) ... )
//
// Syntactic sugar for nested if-expressions
if (exp[0] === 'switch') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// For-loop: (for init condition modifier body )
//
// Syntactic sugar for: (begin init (while condition (begin body modifier)))
if (exp[0] === 'for') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// Increment: (++ foo)
//
// Syntactic sugar for: (set foo (+ foo 1))
if (exp[0] === '++') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// Decrement: (-- foo)
//
// Syntactic sugar for: (set foo (- foo 1))
if (exp[0] === '--') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// Increment: (+= foo inc)
//
// Syntactic sugar for: (set foo (+ foo inc))
if (exp[0] === '+=') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// Decrement: (-= foo dec)
//
// Syntactic sugar for: (set foo (- foo dec))
if (exp[0] === '-=') {
// Implement here: see Lecture 14
}
// --------------------------------------------
// Lambda function: (lambda (x) (* x x))
if (exp[0] === 'lambda') {
// Implement here: see Lecture 12
}
// --------------------------------------------
// Class declaration: (class <Name> <Parent> <Body>)
if (exp[0] === 'class') {
// Implement here: see Lecture 15
}
// --------------------------------------------
// Super expressions: (super <ClassName>)
if (exp[0] === 'super') {
// Implement here: see Lecture 16
}
// --------------------------------------------
// Class instantiation: (new <Class> <Arguments>...)
if (exp[0] === 'new') {
// Implement here: see Lecture 15
}
// --------------------------------------------
// Property access: (prop <instance> <name>)
if (exp[0] === 'prop') {
// Implement here: see Lecture 15
}
// --------------------------------------------
// Module declaration: (module <name> <body>)
if (exp[0] === 'module') {
// Implement here: see Lecture 17
}
// --------------------------------------------
// Module import: (import <name>)
// (import (export1, export2, ...) <name>)
if (exp[0] === 'import') {
// Implement here: see Lecture 17
}
// --------------------------------------------
// 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:
// See Lecture 10
if (typeof fn === 'function') {
return fn(...args);
}
// 2. User-defined function:
return this._callUserDefinedFunction(fn, args);
}
throw `Unimplemented: ${JSON.stringify(exp)}`;
}
_callUserDefinedFunction(fn, args) {
// Implement here: see Lecture 11
}
_evalBody(body, env) {
if (body[0] === 'begin') {
return this._evalBlock(body, env);
}
return this.eval(body, env);
}
_evalBlock(block, env) {
// Implement here: see Lecture 7
}
_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;