-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.js
392 lines (337 loc) · 10.4 KB
/
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
const { runtimeError, ReturnError } = require('./errors')
const {
Binary,
Unary,
Call,
Literal,
Logical,
Class,
Super,
Get,
Set,
Var,
This,
Grouping,
Return,
While,
Block,
LoxFunction,
ExpressionStatement,
VarStatement,
PrintStatement,
Assignment,
Condition
} = require('./types')
const Environment = require('./environment')
const tokenizer = require('./tokenizer')
const token = tokenizer.tokenEnum
const isTruthy = val => Boolean(val)
const isEqual = (a, b) => a === b
const checkNumber = (token, ...operands) => {
for (let operand of operands) {
if (isNaN(operand)) {
throw runtimeError('Operand must be a number!', token)
}
}
}
class LoxCallable {
constructor(declaration, closure) {
this.declaration = declaration
this.closure = closure
}
call(interpreter, args, isInit = false) {
const env = new Environment(this.closure)
for (let param = 0; param < this.declaration.params.length; param++) {
env.set(this.declaration.params[param], args[param])
}
try {
interpreter.interpretBlock(this.declaration.bodyStatements, env)
} catch (ret) {
if (ret instanceof ReturnError) {
return ret.value
} else {
throw ret
}
}
// Always return "this" if a function is an initializer
if (isInit) return this.closure.get({ name: { lexeme: 'this' } })
return null
}
bind(instance) {
const env = new Environment(this.closure)
env.set({ lexeme: 'this' }, instance)
return new LoxCallable(this.declaration, env)
}
toString() {
return `<${this.declaration.name.lexeme}()>`
}
}
class LoxClass extends LoxCallable {
constructor(name, methods, superclass) {
super()
this.name = name
this.methods = methods
this.superclass = superclass
}
call(interpreter, args) {
const instance = new LoxInstance(this)
const init = this.methods.get('init')
if (init) init.bind(instance).call(interpreter, args, true)
return instance
}
getMethod(name, instance) {
if (this.methods.has(name)) return this.methods.get(name).bind(instance)
if (this.superclass) return this.superclass.getMethod(name, instance)
return null
}
toString() {
return `<${this.name}>`
}
}
class LoxInstance {
constructor(klass) {
this.klass = klass
this.fields = new Map()
}
get(token) {
const name = token.lexeme
if (this.fields.has(name)) return this.fields.get(name)
const method = this.klass.getMethod(name, this)
if (method) return method
throw runtimeError(`Undefined property "${name}"`, token)
// return null
}
set(token, value) {
const name = token.lexeme
this.fields.set(name, value)
}
toString() {
return `<+${this.klass.name}>`
}
}
class Interpreter {
constructor(environment, printfunc = console.log) {
this.printfunction = printfunc
this.environment = environment || new Environment()
this.environment.setBuiltin('PI', Math.PI)
this.environment.setBuiltin('cos', (_vars, args) => Math.cos(args[0]))
this.environment.setBuiltin('mod', (_vars, args) => args[0] % args[1])
this.environment.setBuiltin('strlen', (_vars, args) => args[0].length)
this.environment.setBuiltin('charAt', (_vars, args) => args[0][args[1]])
this.environment.setBuiltin('clock', () => new Date().getTime())
}
interpret(expr) {
return this.evaluate(expr)
}
evaluate(expr) {
if (expr instanceof Block) return this.visitBlock(expr)
else if (expr instanceof LoxFunction) return this.visitFunction(expr)
else if (expr instanceof Assignment) return this.visitAssignment(expr)
else if (expr instanceof Class) return this.visitClass(expr)
else if (expr instanceof Get) return this.visitGet(expr)
else if (expr instanceof Set) return this.visitSet(expr)
else if (expr instanceof This) return this.visitThis(expr)
else if (expr instanceof Super) return this.visitSuper(expr)
else if (expr instanceof Logical) return this.visitLogical(expr)
else if (expr instanceof Call) return this.visitCall(expr)
else if (expr instanceof While) return this.visitWhile(expr)
else if (expr instanceof Condition) return this.visitCondition(expr)
else if (expr instanceof VarStatement) return this.visitVarStatement(expr)
else if (expr instanceof PrintStatement) return this.visitPrintStatement(expr)
else if (expr instanceof Return) return this.visitReturnStatement(expr)
// Doesn't need it's own, it can just evaluate like grouping
else if (expr instanceof ExpressionStatement) return this.visitGrouping(expr)
else if (expr instanceof Grouping) return this.visitGrouping(expr)
else if (expr instanceof Var) return this.visitVar(expr)
else if (expr instanceof Literal) return this.visitLiteral(expr)
else if (expr instanceof Unary) return this.visitUnary(expr)
else if (expr instanceof Binary) return this.visitBinary(expr)
}
visitLiteral(expr) {
return expr.value
}
visitGrouping(expr) {
return this.evaluate(expr.expression)
}
visitPrintStatement(expr) {
const val = this.evaluate(expr.expression)
this.printfunction(val === null ? 'nil' : val.toString())
return val
}
visitFunction(expr) {
const fn = new LoxCallable(expr, this.environment)
this.environment.set(expr.name, fn)
}
visitLogical(expr) {
const left = this.evaluate(expr.left)
if (expr.operator.type === token.OR) {
if (isTruthy(left)) return left
} else {
if (!isTruthy(left)) return left
}
return this.evaluate(expr.right)
}
visitWhile(expr) {
while (isTruthy(this.evaluate(expr.condition))) {
this.evaluate(expr.body)
}
return null
}
visitReturnStatement(stmt) {
var val = null
if (stmt.value) val = this.evaluate(stmt.value)
throw new ReturnError(val)
}
visitVar(variable) {
return this.environment.get(variable)
}
visitVarStatement(variable) {
let value = null
if (variable.initializer !== null) {
value = this.evaluate(variable.initializer)
}
this.environment.set(variable.name, value)
return null
}
visitClass(stmt) {
let superClass = null
if (stmt.superclass) {
superClass = this.evaluate(stmt.superclass)
if (!(superClass instanceof LoxClass))
throw runtimeError('Superclass must be a class', stmt.superclass.name)
}
// We set the name before initializing it so classes can self-reference
this.environment.set(stmt.name, null)
if (superClass) {
this.environment = new Environment(this.environment)
this.environment.set({ lexeme: 'super' }, superClass)
}
const methods = new Map()
for (let method of stmt.methods) {
const fun = new LoxCallable(method, this.environment)
methods.set(method.name.lexeme, fun)
}
const klass = new LoxClass(stmt.name.lexeme, methods, superClass)
// Pop "Super" off the environment after creating the class
if (superClass) this.environment = this.environment.enclosing
this.environment.assign(stmt.name, klass)
return null
}
visitGet(expr) {
const object = this.evaluate(expr.object)
if (object instanceof LoxInstance) {
return object.get(expr.name)
}
throw runtimeError('Only instances have properties', expr.name)
}
visitSet(expr) {
const object = this.evaluate(expr.object)
if (!(object instanceof LoxInstance)) {
throw runtimeError('Only instances have fields', expr.name)
}
var val = this.evaluate(expr.value)
return object.set(expr.name, val)
}
visitThis(expr) {
return this.environment.get({ name: expr.keyword })
}
visitSuper(expr) {
const methodName = expr.method.lexeme
const superclass = this.environment.get(new Var(expr.keyword))
const instance = this.environment.get(new Var({ lexeme: 'this' }))
const method = superclass.getMethod(methodName, instance)
if (!method) {
throw runtimeError(`Undefined property "${methodName}"`, expr.method)
}
return method
}
visitBlock(expr) {
this.interpretBlock(expr.statements, new Environment(this.environment))
return null
}
visitCondition(expr) {
if (isTruthy(this.evaluate(expr.condition))) {
this.evaluate(expr.thenBranch)
} else if (expr.elseBranch) {
this.evaluate(expr.elseBranch)
}
return null
}
interpretBlock(statements, env) {
const prevEnvironment = this.environment
try {
this.environment = env
for (let stmt of statements) {
this.interpret(stmt)
}
this.environment = prevEnvironment
} catch (e) {
this.environment = prevEnvironment
throw e
}
}
visitAssignment(expr) {
const value = this.evaluate(expr.value)
this.environment.assign(expr.name, value)
return value
}
visitCall(expr) {
const callee = this.evaluate(expr.callee)
let args = expr.arguments.map(arg => this.evaluate(arg))
if (!callee.call) {
throw runtimeError('Can only call functions and classes', expr.paren)
}
return callee.call(this, args)
}
visitUnary(expr) {
const right = this.evaluate(expr.right)
switch (expr.operator.type) {
case token.MINUS:
checkNumber(expr.operator, right)
return -right
case token.BANG:
return !isTruthy(right)
}
}
visitBinary(expr) {
const left = this.evaluate(expr.left)
const right = this.evaluate(expr.right)
switch (expr.operator.type) {
// Math
case token.MINUS:
checkNumber(expr.operator, right, left)
return left - right
case token.PLUS:
return left + right
case token.SLASH:
checkNumber(expr.operator, right, left)
return left / right
case token.STAR:
checkNumber(expr.operator, right, left)
return left * right
// Comparisons
case token.GREATER:
checkNumber(expr.operator, right, left)
return left > right
case token.GREATER_EQUAL:
checkNumber(expr.operator, right, left)
return left >= right
case token.LESS:
checkNumber(expr.operator, right, left)
return left < right
case token.LESS_EQUAL:
checkNumber(expr.operator, right, left)
return left <= right
// Equality
case token.EQUAL_EQUAL:
return isEqual(left, right)
case token.BANG_EQUAL:
return !isEqual(left, right)
}
}
}
module.exports = {
Interpreter,
LoxClass,
LoxInstance
}