forked from mathk/gst-thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRInterpret.st
139 lines (91 loc) · 2.08 KB
/
IRInterpret.st
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
IRVisitor subclass: IRInterpret [
IRInterpret class >> on: aProgram with: anArray [
^ self new
argumentsValue: anArray;
visitProgram: aProgram;
yourself
]
IRInterpret class >> new [
<category: 'instance creation'>
^ self basicNew
initialize;
yourself
]
| argsValue memory variables |
initialize [
memory := Array new: 1000000.
variables := Dictionary new
]
argumentsValue: anArray [
argsValue := anArray
]
visitFunction: aFunction [
| i |
i := 0.
aFunction argumentDo: [ :each |
i := i + 1.
variables at: each name put: (argsValue at: i) ].
super visitFunction: aFunction
]
variableValue: aSymbol [
^ variables at: aSymbol
]
visitVariableDeclaration: aVariable [
variables at: aVariable name put: nil
]
visitInteger: anInteger [
^ anInteger
]
visitVariable: aSymbol [
^ self variableValue: aSymbol
]
visitMov: aMov [
| i |
i := aMov source accept: self.
variables at: aMov destination put: i
]
do: aSymbol on: anIRCommand [
| i j |
i := anIRCommand source accept: self.
j := anIRCommand param accept: self.
variables at: anIRCommand destination put: (i perform: aSymbol with: j)
]
visitAdd: anAdd [
self do: #+ on: anAdd
]
visitSub: aSub [
self do: #- on: aSub
]
visitMul: aMul [
self do: #* on: aMul
]
visitDiv: aDiv [
self do: #// on: aDiv
]
visitMod: aMod [
self do: #\\ on: aMod
]
visitAnd: anAnd [
self do: #bitAnd: on: anAnd
]
visitOr: anOr [
self do: #bitOr: on: anOr
]
visitXor: aXor [
self do: #bitXor: on: aXor
]
visitLsh: aLsh [
self do: #bitShift: on: aLsh
]
visitRsh: aRsh [
self do: #bitRShift: on: aRsh
]
visitLoad: aLoad [
variables
at: aLoad destination
put: (aLoad readFrom: memory at: (aLoad source accept: self))
]
visitStore: aStore [
(aStore writeFrom: memory at: (aStore destination accept: self) put: (aStore value accept: self))
]
]