-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.ts
41 lines (35 loc) · 1.02 KB
/
env.ts
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
import { Mal, List } from "./types"
export class Env {
private outer: Env | null
private data: {[key: string] : any}
constructor(outer: Env | null, binds: string[], exprs: Mal[]) {
this.outer = outer
this.data = {}
for (var i = 0; i < binds.length; i++) {
if (binds[i] == "&") {
this.set(binds[i+1], new List(exprs.slice(i)))
break
}
this.set(binds[i], exprs[i])
}
}
public set(key: string, value: any) {
this.data[key] = value
}
public find(key: string): any | null {
if (this.data[key]) {
return this.data[key]
} else if (this.outer != null) {
return this.outer.find(key)
} else {
return null
}
}
public get(key: string): any {
let result = this.find(key)
if (result == null)
throw "'" + key + "' not found"
else
return result
}
}