-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack_machine.cljc
283 lines (202 loc) · 6.06 KB
/
stack_machine.cljc
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(ns fif.impl.stack-machine
"Main implementation of IStackMachine"
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.flags :as stack.flags]
[fif.stack-machine.stash :as stack.stash]
[fif.stack-machine.words :as stack.words]
[fif.stack-machine.variable :as stack.variable]
[fif.stack-machine.exceptions :as stack.exceptions]
[fif.stack-machine.processor :as stack.processor]
[fif.stack-machine.error-handling :as error-handling]
[fif.stack-machine.scope :as stack.scope]
[fif.utils.scope :as utils.scope]
[fif.utils.stash :as utils.stash]))
(defrecord StackMachine
[arg-stack
code-stack
ret-stack
temp-macro-stack
scope
sub-stash
mode-stash
flags
modes
step-num
step-max
system-error-handler
stack-error-handler
halt?
debug?]
fif.stack-machine/IStackMachine
;; Main Stack
(push-stack* [this arg]
(update-in this [:arg-stack] conj arg))
(pop-stack [this]
(as-> this $
(update-in $ [:arg-stack] pop)))
(get-stack [this]
(-> this :arg-stack))
(set-stack [this stack]
(assoc this :arg-stack stack))
(clear-stack [this]
(assoc this :arg-stack (empty (:arg-stack this))))
;; Return Stack
(push-ret [this x]
(update-in this [:ret-stack] conj x))
(pop-ret [this]
(update-in this [:ret-stack] pop))
(get-ret [this]
(-> this :ret-stack))
(clear-ret [this]
(assoc this :ret-stack (empty (:ret-stack this))))
;; Stack Stash
;; Deprecated, replaced
(get-stash [this]
(-> this :sub-stash))
;; Deprecated, replaced
(set-stash [this st]
(assoc this :sub-stash st))
(clear-stash [this]
(assoc this :sub-stash (empty (:sub-stash this))))
;; will replace get-stash
(get-mode-stash [this]
(-> this :mode-stash))
;; will replace set-stash
(set-mode-stash [this stash]
(assoc this :mode-stash stash))
(clear-mode-stash [this]
(stack.stash/clear-stash this))
;; Scope
(get-scope [this]
(:scope this))
(set-scope [this scope]
(assoc this :scope scope))
(clear-scope [this]
(stack.scope/clear-scope this))
;; Temp Macro
(push-temp-macro [this x]
(update-in this [:temp-macro-stack] conj x))
(pop-temp-macro [this]
(update-in this [:temp-macro-stack] pop))
(get-temp-macro [this]
(-> this :temp-macro-stack))
(set-temp-macro [this st]
(assoc this :temp-macro-stack st))
(pick-temp-macro [this]
(-> this :temp-macro-stack peek))
(clear-temp-macro [this]
(assoc this :temp-macro-stack (empty (stack/get-temp-macro this))))
;; Word Dictionary
(set-word* [this wname wbody]
(stack.words/set-global-word this wname wbody))
(get-word [this wname]
(stack.words/get-word this wname))
(get-word-listing [this]
(stack.words/get-word-listing this))
(set-variable [this wname value]
(stack.words/set-global-word-defn
this wname (stack.variable/wrap-global-variable value)
:variable? true))
;; Word Metadata
(set-word-metadata [this wname wmeta]
(stack.words/set-global-metadata this wname wmeta))
(get-word-metadata [this wname]
(stack.words/get-global-metadata this wname))
;; Mode Functions
(set-mode [this flag modefn]
(update-in this [:modes] assoc flag modefn))
(remove-mode [this flag]
(update-in this [:modes] dissoc flag))
;; Mode Flags
(push-flag [this flag]
(update-in this [:flags] conj flag))
(pop-flag [this]
(update-in this [:flags] pop))
(get-flags [this]
(-> this :flags))
(set-flags [this flags]
(assoc this :flags flags))
(clear-flags [this]
(assoc this :flags (empty (:flags this))))
;; Code Queue
(enqueue-code [this arg]
(update-in this [:code-stack] concat (list arg)))
(dequeue-code [this]
(update-in this [:code-stack] #(-> % rest vec)))
(set-code [this args]
(assoc this :code-stack args))
(get-code [this]
(-> this :code-stack))
(clear-code [this]
(assoc this :code-stack (empty (:code-stack this))))
;; Step Tracker
(get-step-max [this]
(-> this :step-max))
(set-step-max [this m]
(assoc this :step-max m))
(inc-step [this]
(update this :step-num inc))
(set-step-num [this n]
(assoc this :step-num n))
(get-step-num [this]
(-> this :step-num))
;; Debugging
(enable-debug [this]
(assoc this :debug? true))
(disable-debug [this]
(assoc this :debug? false))
(is-debug-mode? [this]
(-> this :debug?))
;; Error Handling
(get-system-error-handler [this]
(:system-error-handler this))
(set-system-error-handler [this err-handler]
(assoc this :system-error-handler err-handler))
(get-stack-error-handler [this]
(:stack-error-handler this))
(set-stack-error-handler [this err-handler]
(assoc this :stack-error-handler err-handler))
;; Execution
(halt [this]
(assoc this :halt? true))
(step [this]
(let [arg (-> this stack/get-code first)]
(if (stack.flags/has-flags? this)
(try
(-> this stack.processor/process-mode stack/inc-step)
(catch #?(:clj Exception :cljs js/Error) ex
(error-handling/handle-system-error this ex)))
(try
(-> this stack.processor/process-arg stack/inc-step)
(catch #?(:clj Exception :cljs js/Error) ex
(error-handling/handle-system-error this ex))))))
(run [this]
(loop [sm this]
(let [step-num (stack/get-step-num sm)
step-max (stack/get-step-max sm)]
(cond
(empty? (-> sm stack/get-code)) sm
(and (> step-max 0) (>= step-num step-max))
(stack.exceptions/raise-max-steps-exceeded sm)
(:halt? sm) sm
:else (recur (stack/step sm))))))
(continue [this]
(assoc this :halt? false)))
(defn new-stack-machine []
(map->StackMachine
{:arg-stack '()
:code-stack '()
:ret-stack '()
:temp-macro-stack '()
:scope (utils.scope/new-scope)
:sub-stash '()
:mode-stash (utils.stash/create-stash)
:flags []
:modes {}
:step-num 0
:step-max 0
:system-error-handler nil
:stack-error-handler nil
:halt? false
:debug? true}))