Improved debugging, and lots more tests. Ultimately I intend the

enhanced debugging to be optional, because it will have a performance hit
This commit is contained in:
Simon Brooke 2014-07-19 13:24:04 +01:00
parent 4acb2617be
commit a61ace1694
2 changed files with 16 additions and 8 deletions

View file

@ -30,7 +30,7 @@
"Compile each non-comment line of this `string` into an executable anonymous "Compile each non-comment line of this `string` into an executable anonymous
function, and return the sequence of such functions." function, and return the sequence of such functions."
[string] [string]
(map compile-rule (remove comment? (split string #"\n")))) (map #(compile-rule % true) (remove comment? (split string #"\n"))))
(defn compile-file (defn compile-file
"Compile each non-comment line of the file indicated by this `filename` into "Compile each non-comment line of the file indicated by this `filename` into

View file

@ -394,19 +394,27 @@
true true
(let [[left remainder] (parse-left-hand-side line) (let [[left remainder] (parse-left-hand-side line)
[right junk] (parse-right-hand-side remainder)] [right junk] (parse-right-hand-side remainder)]
;; TODO: there shouldn't be any junk (should be null)
(cond (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)))))) (list 'fn ['cell 'world] (list 'if left right))))))
(defn compile-rule (defn compile-rule
"Parse this `rule-text`, a string conforming to the grammar of MicroWorld rules, "Parse this `rule-text`, a string conforming to the grammar of MicroWorld rules,
into Clojure source, and then compile it into an anonymous into Clojure source, and then compile it into an anonymous
function object, getting round the problem of binding mw-engine.utils in 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." Throws an exception if parsing fails."
[rule-text] ([rule-text return-tuple?]
(do (do
(use 'mw-engine.utils) (use 'mw-engine.utils)
(eval (parse-rule rule-text)))) (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)))