-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.js
38 lines (32 loc) · 1.05 KB
/
environment.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
const { runtimeError } = require('./errors')
class Environment {
constructor(enclosing) {
this.map = new Map()
this.enclosing = enclosing
}
get(varToken) {
if (this.map.has(varToken.name.lexeme)) {
return this.map.get(varToken.name.lexeme)
}
if (this.enclosing) return this.enclosing.get(varToken)
throw runtimeError(`Undefined variable "${varToken.name.lexeme}"`, varToken.name)
}
set(token, value) {
if (this.map.has(token.lexeme)) {
throw runtimeError(`Duplicate variable declaration "${token.lexeme}"`, token)
// return this.map.set(token.lexeme, value)
}
return this.map.set(token.lexeme, value)
}
setBuiltin(name, func) {
this.map.set(name, typeof func === 'function' ? { call: func } : func)
}
assign(token, value) {
if (!this.map.has(token.lexeme)) {
if (this.enclosing) return this.enclosing.assign(token, value)
throw runtimeError(`Undefined variable "${token.lexeme}"`, token)
}
return this.map.set(token.lexeme, value)
}
}
module.exports = Environment