From 5cbda73abae28c57da2b026ebf0525d6dc9b3c46 Mon Sep 17 00:00:00 2001 From: Richard Harrington Date: Sat, 13 Jul 2013 12:17:09 -0400 Subject: [PATCH] halfway towards converted the lexer to just use regular expressions --- src/hs_robotwar/core.clj | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/hs_robotwar/core.clj b/src/hs_robotwar/core.clj index 863cea4..ae9cbb2 100644 --- a/src/hs_robotwar/core.clj +++ b/src/hs_robotwar/core.clj @@ -4,14 +4,27 @@ (use '[clojure.set :only (union)]) (use '[clojure.string :only (split join)]) +(def operators "-+*/=#<>") -(def operators #{\= \< \> \# \+ \- \* \/}) +(def lex-re (re-pattern (str "[" operators "]|[^" operators "\\s]+"))) +(def operators-set (set (map str operators))) (def registers (union (set (map #(-> % char str) (range (int \A) (inc (int \Z))))) #{"AIM" "SHOT" "RADAR" "DAMAGE" "SPEEDX" "SPEEDY" "RANDOM" "INDEX"})) -(def commands (union (set (map str operators)) - #{"TO" "IF" "GOTO" "GOSUB" "ENDSUB"})) +(def commands (union operators-set #{"TO" "IF" "GOTO" "GOSUB" "ENDSUB"})) + +; TODO: FINISH THIS METHOD, AND CONVERT THE WHOLE LEXER TO REGEX +(defn re-seq-with-pos + [initial-s] + (let [len (count initial-s)] + (loop [s initial-s, pos 0, acc []] + (cond + (empty? s) acc + ( + + + (defn conj-with-metadata [coll s n]