Rename gst and pil to gnu-smalltalk and picolisp
[jackhill/mal.git] / gnu-smalltalk / env.st
CommitLineData
4ade8121
VS
1Object subclass: Env [
2 | data outer |
3
4 Env class >> new: outerEnv [
adb2ac78
VS
5 ^self new: outerEnv binds: {} exprs: {}
6 ]
7
8 Env class >> new: outerEnv binds: binds exprs: exprs [
4ade8121
VS
9 | env |
10 env := super new.
adb2ac78 11 env init: outerEnv binds: binds exprs: exprs.
4ade8121
VS
12 ^env
13 ]
14
adb2ac78 15 init: env binds: binds exprs: exprs [
4ade8121
VS
16 data := Dictionary new.
17 outer := env.
adb2ac78
VS
18 1 to: binds size do:
19 [ :i | (binds at: i) = #& ifTrue: [
20 | rest |
fd216c19
VS
21 rest := OrderedCollection from: (exprs copyFrom: i).
22 self set: (binds at: i + 1) value: (MALList new: rest).
adb2ac78
VS
23 ^nil
24 ] ifFalse: [
25 self set: (binds at: i) value: (exprs at: i)
26 ] ]
4ade8121
VS
27 ]
28
29 set: key value: value [
30 data at: key put: value.
31 ]
32
33 find: key [
34 ^data at: key ifAbsent: [
35 outer notNil ifTrue: [
36 outer find: key
37 ] ifFalse: [
38 nil
39 ]
40 ]
41 ]
42
43 get: key [
44 | value |
45 value := self find: key.
46
47 value notNil ifTrue: [
48 ^value
49 ] ifFalse: [
50 ^MALUnknownSymbol new signal: key
51 ]
52 ]
53]