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