-
Notifications
You must be signed in to change notification settings - Fork 1
/
variable.cljc
195 lines (150 loc) · 4.97 KB
/
variable.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
(ns fif.stdlib.variable
"Includes the variable-mode, for creating mutable variables within a
stack machine."
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.words :as stack.words
:refer [set-global-word-defn
set-local-word-defn]]
[fif.stack-machine.exceptions :as exceptions]
[fif.stack-machine.mode :as mode]
[fif.stack-machine.stash :as stash]
[fif.stack-machine.variable :refer [wrap-global-variable
wrap-local-variable]]
[fif.stdlib.reserved :refer [*reserved-tokens*]]))
(def arg-global-var-token 'def)
(def arg-local-var-token 'let)
(def variable-mode-flag :variable-mode)
(defn enter-variable-mode
[sm stash]
(mode/enter-mode sm variable-mode-flag stash))
(defn exit-variable-mode
[sm]
(mode/exit-mode sm))
(defmulti variable-mode mode/mode-dispatch-fn)
;; Global Variable Implementation
(defn start-global-variable
"Puts the stack machine into variable-mode"
[sm]
(-> sm
(enter-variable-mode {:op ::global-variable :op-state ::get-symbol})
stack/dequeue-code))
(defmethod variable-mode
{:op ::global-variable :op-state ::get-symbol}
[sm]
(let [arg (-> sm stack/get-code first)]
(cond
(not (symbol? arg))
(exceptions/raise-validation-error sm 0 arg "Variable name must be a symbol")
(contains? *reserved-tokens* arg)
(exceptions/raise-reserved-word-redefinition-error sm arg)
:else
(-> sm
(stack/push-stack arg)
(mode/update-state assoc :op-state ::set)
stack/dequeue-code))))
(defmethod variable-mode
{:op ::global-variable :op-state ::set}
[sm]
(let [val (-> sm stack/get-code first)
sym (-> sm stack/get-stack peek)]
(-> sm
(stack.words/set-global-word-defn
sym (wrap-global-variable val)
:variable? :global)
stack/pop-stack
exit-variable-mode
stack/dequeue-code)))
;; Local Variable Implementation
(defn start-local-variable
"Puts the stack machine into variable-mode to create a local
variable."
[sm]
(-> sm
(enter-variable-mode {:op ::local-variable :op-state ::get-symbol})
stack/dequeue-code))
(defmethod variable-mode
{:op ::local-variable :op-state ::get-symbol}
[sm]
(let [arg (-> sm stack/get-code first)]
(cond
(not (symbol? arg))
(exceptions/raise-validation-error sm 0 arg "Variable name must be a symbol")
(contains? *reserved-tokens* arg)
(exceptions/raise-reserved-word-redefinition-error sm arg)
:else
(-> sm
(stack/push-stack arg)
(mode/update-state assoc :op-state ::set)
stack/dequeue-code))))
(defmethod variable-mode
{:op ::local-variable :op-state ::set}
[sm]
(let [val (-> sm stack/get-code first)
sym (-> sm stack/get-stack peek)]
(-> sm
(stack.words/set-local-word-defn
sym (wrap-local-variable val)
:variable? :local)
stack/pop-stack
exit-variable-mode
stack/dequeue-code)))
(defn setg
"Word function used to set a global variable to a provided value"
[sm]
(let [[val sym] (stack/get-stack sm)]
(cond
(not (symbol? sym))
(exceptions/raise-validation-error sm 1 sym "Variable name must be a symbol")
(contains? *reserved-tokens* sym)
(exceptions/raise-reserved-word-redefinition-error sm sym)
:else
(-> sm
stack/pop-stack
stack/pop-stack
(stack.words/set-global-word-defn
sym (wrap-global-variable val)
:variable? :global)
stack/dequeue-code))))
(defn setl
"Word function used to set a local variable to a provided value"
[sm]
(let [[val sym] (stack/get-stack sm)]
(cond
(not (symbol? sym))
(exceptions/raise-validation-error sm 1 sym "Variable name must be a symbol")
(contains? *reserved-tokens* sym)
(exceptions/raise-reserved-word-redefinition-error sm sym)
:else
(-> sm
stack/pop-stack
stack/pop-stack
(stack.words/set-local-word-defn
sym (wrap-local-variable val)
:variable? :local)
stack/dequeue-code))))
(defn import-stdlib-variable-mode
"Stack Machine Import for variable-mode"
[sm]
(-> sm
(set-global-word-defn
arg-global-var-token start-global-variable
:stdlib? true
:doc "def <word> <val> -- Set global variable."
:group :stdlib.variable)
(set-global-word-defn
'setg setg
:stdlib? true
:doc "( wname val -- ) Set global variable."
:group :stdlib.variable)
(set-global-word-defn
arg-local-var-token start-local-variable
:stdlib? true
:doc "let <word> <val> -- Set local variable."
:group :stdlib.variable)
(set-global-word-defn
'setl setl
:stdlib? true
:doc "( wname val -- ) Set local variable."
:group :stdlib.variable)
(stack/set-mode variable-mode-flag variable-mode)))