From b01e03ba33ea90952da1f8546adb9d9f964c2e60 Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Wed, 17 Jul 2013 16:43:11 -0400 Subject: [PATCH] got working the compiling into command-arg pairs --- src/hs_robotwar/core.clj | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/hs_robotwar/core.clj b/src/hs_robotwar/core.clj index 6028f71..b336132 100644 --- a/src/hs_robotwar/core.clj +++ b/src/hs_robotwar/core.clj @@ -75,21 +75,33 @@ [] tokens)) -(def value-type? #{:number :register}) - (defn disambiguate-minus-signs [initial-tokens] (loop [tokens initial-tokens - acc []] - (let [{prev-type :type} (last acc) + results []] + (let [{prev-type :type} (last results) {current-val :val, current-pos :pos :as current-token} (first tokens) {next-val :val, next-type :type :as next-token} (second tokens)] (cond - (empty? tokens) acc - (and (not (value-type? prev-type)) (= current-val "-") (= next-type :number)) + (empty? tokens) results + (and (not (#{:number :register} prev-type)) (= current-val "-") (= next-type :number)) (recur (rest (rest tokens)) - (conj acc {:val (- next-val), :pos current-pos, :type :number})) - :otherwise (recur (rest tokens) (conj acc current-token)))))) + (conj results {:val (- next-val), :pos current-pos, :type :number})) + :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] (join