-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.clj
More file actions
320 lines (269 loc) · 9.72 KB
/
Copy pathbasic.clj
File metadata and controls
320 lines (269 loc) · 9.72 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
(ns basic)
(defn create-ctx []
{:tokens []
:var-count 0
:env {}
:uvm-code []
:loops {}})
(def token-patterns
[[::skip #"^ "]
[::num #"^[0-9]+"]
[::== #"^=="]
[::!= #"^!="]
[::> #"^>"]
[::< #"^<"]
[::>= #"^>="]
[::<= #"^<="]
[::+ #"^\+"]
[::left-paren #"^\("]
[::right-paren #"^\)"]
[::- #"^\-"]
[::* #"^\*"]
[::div #"^/"]
[::! #"^\!"]
[::new-line #"^\n"]
[::sym #"^[a-zA-Z]+"]])
(defn get-token [s [token-type pattern]]
(when-let [match (re-find pattern s)]
[(subs s (count match)) {:type token-type :str match}]))
(defn token-seq
([input]
(if-let [[rem token] (some (partial get-token input) token-patterns)]
(if (empty? rem) [token]
(lazy-seq (cons token (token-seq rem))))
(throw (Exception. (str "unrecognized token: " input))))))
(defn tokenize [ctx input]
(->> input
token-seq
(remove #(= ::skip (:type %)))
(assoc ctx :tokens)))
(defn get-stack-pos [ctx dst]
(if (keyword? dst)
(recur ctx (get-in ctx [:env dst :stack-pos]))
dst))
(defn emit-code [ctx & strs]
(update ctx :uvm-code conj (apply str (conj strs "\n"))))
(defn emit-push [ctx n]
(emit-code ctx "push " (str n) ";"))
(defn emit-set-local [ctx dst]
(emit-code ctx "set_local " (str (get-stack-pos ctx dst))";"))
(defn emit-get-local [ctx src]
(emit-code ctx "get_local " (str (get-stack-pos ctx src)) ";"))
(defn emit-label [ctx name]
(if (nil? name) ctx
(emit-code ctx "label_" name ":")))
(defn emit-goto [ctx dst-label]
(emit-code ctx "jmp label_" dst-label ";"))
(def binary-op->uvm-inst
{::+ "add_u64"
::- "sub_u64"
::== "eq_u64"
::!= "nq_u64"
::> "gt_i64"
::< "lt_i64"
::>= "ge_i64"
::<= "le_i64"
::* "mul_u64"
::div "div_i64"})
(defn emit-binary-op [ctx op dst lhs rhs]
(-> ctx
(emit-get-local lhs)
(emit-get-local rhs)
(emit-code (binary-op->uvm-inst op) ";")
(emit-set-local dst)))
(defn emit-call [ctx fn args]
(emit-code
(reduce emit-get-local ctx args)
"call " fn ", " (count args) ";"))
(defn emit-branch-if-not [ctx src dst-label]
(-> ctx
(emit-get-local src)
(emit-code "jz label_" dst-label ";")))
(defn emit-branch-if [ctx src dst-label]
(-> ctx
(emit-get-local src)
(emit-code "jnz label_" dst-label ";")))
(defn emit-inc [ctx dst]
(-> ctx
(emit-push 1)
(emit-get-local dst)
(emit-code "add_u64;")
(emit-set-local dst)))
(defn alloc-var [ctx]
[(update ctx :var-count inc) (:var-count ctx)])
(defn expect-num [[t & rem]]
(assert (= ::num (:type t))
(str "expected an integer but got " t))
[t rem])
(defn expect-sym
([tokens expected-val]
(let [[{actual-val :str :as cur-token} tokens] (expect-sym tokens)]
(assert (= actual-val expected-val)
(str "Expected to find " expected-val " but got " cur-token))
[cur-token tokens]))
([[t & rem]]
(assert (= ::sym (:type t))
(str "expected a sym but got " t))
[t rem]))
(defn alloc-num [ctx tokens]
(let [[ctx pos](alloc-var ctx)
[{:keys [str]} rem] (expect-num tokens)]
[pos
(-> ctx
(emit-push str)
(emit-set-local pos))
rem]))
(defn derive-list [parent l]
(doall (map #(derive % parent) l)))
(derive-list ::equality [::!= ::==])
(derive-list ::comparison [::> ::< ::>= ::<=])
(derive-list ::term [::+ ::-])
(derive-list ::factor [::div ::*])
(derive-list ::uni [::- ::!])
(derive-list ::binary-op [::equality ::comparison ::term ::factor])
(declare emit-equality)
(defn deref-sym [ctx tokens]
(let [[{:keys [str] :as cur-token} rem] (expect-sym tokens)
pos (get-in ctx [:env (keyword str) :stack-pos])]
(assert pos (str "Symbol " cur-token " is used before it was defined"))
[pos ctx rem]))
(defn emit-prim [ctx [{:keys [type] :as cur-token} & rem :as tokens]]
(condp = type
::num (alloc-num ctx tokens)
::sym (deref-sym ctx tokens)
::left-paren
(let [[pos ctx [{:keys [type] :as cur-token} & rem]] (emit-equality ctx rem)]
(assert (= type ::right-paren) (str "bracket is not closed: " cur-token))
[pos ctx rem])))
(defn parse-and-emit-binary-op [ctx cur-type higher-prec-fn tokens]
(loop [[lhs-pos ctx [{:keys [type]} & rem :as tokens]] (higher-prec-fn ctx tokens)]
(if (isa? type cur-type)
(let [[rhs-pos ctx tokens] (higher-prec-fn ctx rem)
[ctx dst-pos] (alloc-var ctx)]
(recur [dst-pos
(emit-binary-op ctx type dst-pos lhs-pos rhs-pos)
tokens]))
[lhs-pos ctx tokens])))
(defn emit-factor [ctx tokens]
(parse-and-emit-binary-op ctx ::factor emit-prim tokens))
(defn emit-term [ctx tokens]
(parse-and-emit-binary-op ctx ::term emit-factor tokens))
(defn emit-comparison [ctx tokens]
(parse-and-emit-binary-op ctx ::comparison emit-term tokens))
(defn emit-equality [ctx tokens]
(parse-and-emit-binary-op ctx ::equality emit-comparison tokens))
(defmulti emit-cmd (fn [_ cmd _] (:str cmd)))
(defmethod emit-cmd "LET" [ctx _ tokens]
(let [[sym tokens] (expect-sym tokens)
[val-pos ctx tokens] (emit-equality ctx tokens)
kw-sym (keyword (:str sym))]
[(if-let [stack-pos (get-in ctx [:env kw-sym :stack-pos])]
(-> ctx
(emit-get-local val-pos)
(emit-set-local stack-pos))
(let [[ctx stack-pos] (alloc-var ctx)
ctx (emit-get-local ctx val-pos)
ctx (emit-set-local ctx stack-pos)]
(assoc-in ctx [:env kw-sym :stack-pos] stack-pos)))
tokens]))
(defmethod emit-cmd "GOTO" [ctx _ tokens]
(let [[{dst-label :str} tokens] (expect-num tokens)]
[(emit-goto ctx dst-label) tokens]))
(defmethod emit-cmd "IF" [ctx _ tokens]
(let [else-label (str (gensym "else__"))
end-label (str (gensym "end__"))
[pred-pos ctx tokens] (emit-equality ctx tokens)
ctx (emit-branch-if-not ctx pred-pos else-label)
;; compile then
[_ tokens] (expect-sym tokens "THEN")
[cmd tokens] (expect-sym tokens)
[ctx tokens] (emit-cmd ctx cmd tokens)
ctx (emit-goto ctx end-label)
;; compile else
;; TODO make else optional
ctx (emit-label ctx else-label)
[_ tokens] (expect-sym tokens "ELSE")
[cmd tokens] (expect-sym tokens)
[ctx tokens] (emit-cmd ctx cmd tokens)
ctx (emit-label ctx end-label)]
[ctx tokens]))
(defmethod emit-cmd "PLOT" [ctx _ tokens]
(let [[x-pos ctx tokens] (emit-equality ctx tokens)
[y-pos ctx tokens] (emit-equality ctx tokens)
[color-pos ctx tokens] (emit-equality ctx tokens)]
[(emit-call ctx "draw_point" [x-pos y-pos color-pos])
tokens]))
(defn get-loop-label [ctx var-name]
(get-in ctx [:loops var-name]))
(defn get-for-start-label [ctx var-name]
(let [label (or (get-loop-label ctx var-name)
(str (gensym (str "for_label_" var-name))))
ctx (assoc-in ctx [:loops var-name] label)]
[ctx label]))
(defn get-for-end-label [ctx var-name]
(let [label (get-loop-label ctx var-name)
_ (assert (some? label)
(str "loop with variable " var-name " doesn't exist"))
label (str "end_" label)]
[ctx label]))
(defmethod emit-cmd "FOR" [ctx _ tokens]
(let [[{var-name :str} tokens] (expect-sym tokens) ;; verify
[val-pos ctx tokens] (emit-equality ctx tokens)
kw-var-name (keyword var-name)
[ctx init-pos] (if-let [pos (get-stack-pos ctx kw-var-name)]
[ctx pos]
(alloc-var ctx))
ctx (emit-get-local ctx val-pos)
ctx (emit-set-local ctx init-pos)
[_ tokens] (expect-sym tokens "TO")
[end-pos ctx tokens] (emit-equality ctx tokens)
[ctx sum-pos] (alloc-var ctx)
ctx (assoc-in ctx [:env kw-var-name :stack-pos] init-pos)
_ (assert (nil? (get-loop-label ctx var-name))
(str "Loop variable " var-name " already exist in a parent loop"))
[ctx start-label] (get-for-start-label ctx var-name)
ctx (emit-label ctx start-label)
ctx (emit-binary-op ctx ::> sum-pos init-pos end-pos)
[ctx end-label] (get-for-end-label ctx var-name)
ctx (emit-branch-if ctx sum-pos end-label)]
[ctx tokens]))
(defmethod emit-cmd "NEXT" [ctx _ tokens]
(let [[{var-name :str} _] (expect-sym tokens)
[var-pos ctx tokens] (deref-sym ctx tokens)
[ctx start-label] (get-for-start-label ctx var-name)
[ctx end-label] (get-for-end-label ctx var-name)
ctx (assoc-in ctx [:loops var-name] nil)]
[(-> ctx
(emit-inc var-pos)
(emit-goto start-label)
(emit-label end-label))
tokens]))
(defmethod emit-cmd :default [ctx cmd tokens]
(throw (Exception. (str "invalid command: " cmd))))
(defn emit [{:keys [tokens] :as ctx}]
(loop [ctx ctx
[{:keys [type] label :str} & rem :as tokens] tokens]
(if (empty? tokens)
(-> ctx
(emit-push 0)
(emit-code "ret;"))
(let [[ctx tokens]
(if (= type ::num)
[(emit-label ctx label) rem]
[ctx tokens])
[cmd tokens] (expect-sym tokens)
[ctx tokens] (emit-cmd ctx cmd tokens)
tokens (drop-while (fn [{:keys [type]}] (= type ::new-line)) tokens)]
(recur ctx tokens)))))
(defn compile-src! [source]
(let [ctx (->> source
slurp
(tokenize (create-ctx))
emit)]
(spit "out.asm"
(str (slurp "runtime.asm")
(apply str (repeat (:var-count ctx) "push 0;\n"))
(apply str (-> ctx :uvm-code))))))
(if (empty? *command-line-args*)
(print "Provide a sourcefile as an argument. e.g. clj -M basic.clj source.clj")
(compile-src! (first *command-line-args*)))