refactored parse and parse-token (parse-token is back to original)

This commit is contained in:
Richard Harrington 2013-08-04 18:52:10 -04:00
parent e3c96eaa25
commit 1af74d1630
2 changed files with 13 additions and 13 deletions

View File

@ -1,5 +1,3 @@
-- change parse function back to the way it was -- see outgoing email to Kevin
-- change the robots associative structure from an array to a map, with keys like :0, :1, :2 etc., so that they can be removed during the battle without messing things up.
-- change the robots so they have access to their own id keys, the same way the registers do, so that the robots can also have a write method which will know how to access itself in the world (and will return a world), and so that other functions won't have to provide id keys when they need to reference the robot and also alter the world -- they can just provide the robot and the world.

View File

@ -55,21 +55,23 @@
(def return-err (constantly "Invalid word or symbol"))
(defn parse-token
"removes the token-str field and adds two new fields:
:val and :type, based on sending the :token-str value
through a series of parsing functions until a match is found."
[reg-names token]
"takes a vector of reg-names and a token with a token-str field and parses the token.
needs to work with the original token map by using dissoc and into
(rather than building a new one) because it contains line and column
number metadata."
[reg-names {token-str :token-str :as token}]
(let [parser-priority
[[(set reg-names) :register]
[(set robotwar.kernel-lexicon/commands) :command]
[str->int :number]
[valid-word :label]
[return-err :error]]]
(loop [[[parser token-type] & tail] parser-priority]
(if-let [token-val (parser (:token-str token))]
(some
(fn [[parser token-type]]
(when-let [token-val (parser token-str)]
(dissoc (into token {:val token-val, :type token-type})
:token-str)
(recur tail)))))
:token-str)))
parser-priority)))
(defn parse
"take the tokens and convert them to structured source code ready for compiling.
@ -80,8 +82,8 @@
parsed-tokens []]
(if (empty? tokens)
parsed-tokens
(let [parsed-token (parse-token reg-names token)]
(if (= (:type parsed-token) :error)
(let [{token-type :type :as parsed-token} (parse-token reg-names token)]
(if (= token-type :error)
parsed-token
(recur tail (conj parsed-tokens parsed-token)))))))