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