From a61ace1694a24efa129dbc40c764acbbab0c8fa8 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Sat, 19 Jul 2014 13:24:04 +0100 Subject: [PATCH] Improved debugging, and lots more tests. Ultimately I intend the enhanced debugging to be optional, because it will have a performance hit --- src/mw_parser/bulk.clj | 2 +- src/mw_parser/core.clj | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/mw_parser/bulk.clj b/src/mw_parser/bulk.clj index 753a009..657b18a 100644 --- a/src/mw_parser/bulk.clj +++ b/src/mw_parser/bulk.clj @@ -30,7 +30,7 @@ "Compile each non-comment line of this `string` into an executable anonymous function, and return the sequence of such functions." [string] - (map compile-rule (remove comment? (split string #"\n")))) + (map #(compile-rule % true) (remove comment? (split string #"\n")))) (defn compile-file "Compile each non-comment line of the file indicated by this `filename` into diff --git a/src/mw_parser/core.clj b/src/mw_parser/core.clj index 01edee7..3984da0 100644 --- a/src/mw_parser/core.clj +++ b/src/mw_parser/core.clj @@ -394,19 +394,27 @@ true (let [[left remainder] (parse-left-hand-side line) [right junk] (parse-right-hand-side remainder)] - ;; TODO: there shouldn't be any junk (should be null) (cond - (and left right (nil? junk)) + ;; there should be a valide left hand side and a valid right hand side + ;; there shouldn't be anything left over (junk should be empty) + (and left right (empty? junk)) (list 'fn ['cell 'world] (list 'if left right)))))) (defn compile-rule "Parse this `rule-text`, a string conforming to the grammar of MicroWorld rules, into Clojure source, and then compile it into an anonymous function object, getting round the problem of binding mw-engine.utils in - the compiling environment. + the compiling environment. If `return-tuple?` is present and true, return + a list comprising the anonymous function compiled, and the function from + which it was compiled. Throws an exception if parsing fails." - [rule-text] - (do - (use 'mw-engine.utils) - (eval (parse-rule rule-text)))) + ([rule-text return-tuple?] + (do + (use 'mw-engine.utils) + (let [afn (eval (parse-rule rule-text))] + (cond + (and afn return-tuple?)(list afn rule-text) + true afn)))) + ([rule-text] + (compile-rule rule-text false)))