From 1af74d1630cdae45d4a7d07387121191b7174e3a Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Sun, 4 Aug 2013 18:52:10 -0400 Subject: [PATCH] refactored parse and parse-token (parse-token is back to original) --- TODO.md | 2 -- src/robotwar/foundry.clj | 24 +++++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/TODO.md b/TODO.md index 6583d14..fe41332 100644 --- a/TODO.md +++ b/TODO.md @@ -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. diff --git a/src/robotwar/foundry.clj b/src/robotwar/foundry.clj index 8ad643e..5eed186 100644 --- a/src/robotwar/foundry.clj +++ b/src/robotwar/foundry.clj @@ -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))] - (dissoc (into token {:val token-val, :type token-type}) - :token-str) - (recur tail))))) + (some + (fn [[parser token-type]] + (when-let [token-val (parser token-str)] + (dissoc (into token {:val token-val, :type token-type}) + :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)))))))