All core rules now compile into credible-looking code.
This commit is contained in:
parent
ef3ec6cf18
commit
e5b74a4a68
3 changed files with 241 additions and 8 deletions
51
src/mw_parser/bulk.clj
Normal file
51
src/mw_parser/bulk.clj
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
;; parse multiple rules from a stream, possibly a file - although the real
|
||||
;; objective is to parse rules out of a block of text from a textarea
|
||||
|
||||
(ns mw-parser.bulk
|
||||
(:use mw-parser.core
|
||||
mw-engine.utils
|
||||
clojure.java.io
|
||||
[clojure.string :only [split trim]])
|
||||
(:import (java.io BufferedReader StringReader)))
|
||||
|
||||
(defn comment?
|
||||
"Is this `line` a comment?"
|
||||
[line]
|
||||
(or (empty? (trim line)) (member? (first line) '(nil \# \;))))
|
||||
|
||||
(defn parse-string
|
||||
"Parse rules from successive lines in this `string`, assumed to have multiple
|
||||
lines delimited by the new-line character. Return a list of S-expressions."
|
||||
[string]
|
||||
;; TODO: tried to do this using with-open, but couldn't make it work.
|
||||
(map parse-rule (remove comment? (split string #"\n"))))
|
||||
|
||||
(defn parse-file
|
||||
"Parse rules from successive lines in the file loaded from this `filename`.
|
||||
Return a list of S-expressions."
|
||||
[filename]
|
||||
(parse-string (slurp filename)))
|
||||
|
||||
(defn compile-string
|
||||
"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"))))
|
||||
|
||||
(defn compile-file
|
||||
"Compile each non-comment line of the file indicated by this `filename` into
|
||||
an executable anonymous function, and return the sequence of such functions."
|
||||
[filename]
|
||||
(compile-string (slurp filename)))
|
||||
|
||||
|
||||
|
||||
|
||||
;; (let [lines
|
||||
;; (doall (with-open [rdr (reader filename)] (line-seq rdr)))]
|
||||
;; (map parse-line lines)))
|
||||
|
||||
;;(defn parse-string
|
||||
;; "Parse rules from successive lines in this `string`"
|
||||
;; [string]
|
||||
;; (parse-from-reader (BufferedReader. (StringReader. string))))
|
||||
|
|
@ -188,11 +188,11 @@
|
|||
(= have-or-are "have")
|
||||
(let [[property comp1 comp2 value & remainder] rest]
|
||||
(cond (and (= comp1 "equal") (= comp2 "to"))
|
||||
(gen-neighbours-condition comparator quantity property value remainder =)
|
||||
(gen-neighbours-condition comparator quantity property value remainder '=)
|
||||
(and (= comp1 "more") (= comp2 "than"))
|
||||
(gen-neighbours-condition '> quantity property value remainder >)
|
||||
(gen-neighbours-condition '> quantity property value remainder '>)
|
||||
(and (= comp1 "less") (= comp2 "than"))
|
||||
(gen-neighbours-condition '< quantity property value remainder <)
|
||||
(gen-neighbours-condition '< quantity property value remainder '<)
|
||||
))))))
|
||||
|
||||
(defn parse-some-neighbours-condition
|
||||
|
|
@ -202,7 +202,7 @@
|
|||
(parse-comparator-neighbours-condition (concat '("more" "than" "0" "neighbours") rest))))
|
||||
|
||||
(defn parse-simple-neighbours-condition
|
||||
"Parse conditions of the form '...6 neighbours are condition'"
|
||||
"Parse conditions of the form '...6 neighbours are [condition]'"
|
||||
[[n NEIGHBOURS have-or-are & rest]]
|
||||
(let [quantity (first (parse-numeric-value (list n)))]
|
||||
(cond
|
||||
|
|
@ -215,10 +215,10 @@
|
|||
(let [[property comp1 comp2 value & remainder] rest]
|
||||
(cond (and (= comp1 "equal") (= comp2 "to"))
|
||||
(gen-neighbours-condition '= quantity property value remainder)
|
||||
;; (and (= comp1 "more") (= comp2 "than"))
|
||||
;; (gen-neighbours-condition '> quantity property value remainder)
|
||||
;; (and (= comp1 "less") (= comp2 "than"))
|
||||
;; (gen-neighbours-condition '< quantity property value remainder)
|
||||
(and (= comp1 "more") (= comp2 "than"))
|
||||
(gen-neighbours-condition '> quantity property value remainder '>)
|
||||
(and (= comp1 "less") (= comp2 "than"))
|
||||
(gen-neighbours-condition '< quantity property value remainder '<)
|
||||
))))))
|
||||
|
||||
(defn parse-neighbours-condition
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue