mirror of
https://github.com/richardharrington/robotwar.git
synced 2024-08-04 05:29:00 +00:00
got working the compiling into command-arg pairs
This commit is contained in:
parent
c4643b285d
commit
b01e03ba33
@ -75,21 +75,33 @@
|
|||||||
[]
|
[]
|
||||||
tokens))
|
tokens))
|
||||||
|
|
||||||
(def value-type? #{:number :register})
|
|
||||||
|
|
||||||
(defn disambiguate-minus-signs
|
(defn disambiguate-minus-signs
|
||||||
[initial-tokens]
|
[initial-tokens]
|
||||||
(loop [tokens initial-tokens
|
(loop [tokens initial-tokens
|
||||||
acc []]
|
results []]
|
||||||
(let [{prev-type :type} (last acc)
|
(let [{prev-type :type} (last results)
|
||||||
{current-val :val, current-pos :pos :as current-token} (first tokens)
|
{current-val :val, current-pos :pos :as current-token} (first tokens)
|
||||||
{next-val :val, next-type :type :as next-token} (second tokens)]
|
{next-val :val, next-type :type :as next-token} (second tokens)]
|
||||||
(cond
|
(cond
|
||||||
(empty? tokens) acc
|
(empty? tokens) results
|
||||||
(and (not (value-type? prev-type)) (= current-val "-") (= next-type :number))
|
(and (not (#{:number :register} prev-type)) (= current-val "-") (= next-type :number))
|
||||||
(recur (rest (rest tokens))
|
(recur (rest (rest tokens))
|
||||||
(conj acc {:val (- next-val), :pos current-pos, :type :number}))
|
(conj results {:val (- next-val), :pos current-pos, :type :number}))
|
||||||
:otherwise (recur (rest tokens) (conj acc current-token))))))
|
:otherwise (recur (rest tokens) (conj results current-token))))))
|
||||||
|
|
||||||
|
(defn rw-compile
|
||||||
|
"Compiles the tokens into token-pairs. Commands consume the next token.
|
||||||
|
Values form the special token-pair comma-value
|
||||||
|
(meaning push the value into the accumulator)"
|
||||||
|
[initial-tokens]
|
||||||
|
(loop [[token & tail :as tokens] initial-tokens
|
||||||
|
result []]
|
||||||
|
(if (empty? tokens)
|
||||||
|
result
|
||||||
|
(case (:type token)
|
||||||
|
:command (recur (rest tail) (conj result [token (first tail)]))
|
||||||
|
(:number :register) (recur tail (conj result [{:val ",", :type :command, :pos (:pos token)} token]))
|
||||||
|
:label (recur tail (conj result [token nil]))))))
|
||||||
|
|
||||||
(defn pretty-print-tokens [token-seq]
|
(defn pretty-print-tokens [token-seq]
|
||||||
(join
|
(join
|
||||||
|
Loading…
Reference in New Issue
Block a user