Grammar more or less complete except '[more or fewer] than n neighbours'.

This commit is contained in:
Simon Brooke 2014-07-06 12:44:17 +01:00
parent 3545e6f129
commit 51388b5be4

View file

@ -6,13 +6,14 @@
;; * "if deer is more than 2 and wolves is 0 and fertility is more than 20 then deer should be deer + 2" ;; * "if deer is more than 2 and wolves is 0 and fertility is more than 20 then deer should be deer + 2"
;; * "if deer is more than 1 and wolves is more than 1 then deer should be deer - wolves" ;; * "if deer is more than 1 and wolves is more than 1 then deer should be deer - wolves"
;; * "if state is grassland and 4 neighbours have state equal to water then state should be village" ;; * "if state is grassland and 4 neighbours have state equal to water then state should be village"
;; * "if state is forest and fertility is between 55 and 75 then state should be climax"
;; * "if 6 neighbours have state equal to water then state should be fishery"
;; ;;
;; It should also but does not yet parse rules of the form: ;; It should also but does not yet parse rules of the form:
;; * "if 6 neighbours have state is water then state should be fishery"
;; * "if state is forest or state is climax and some neighbours have state is fire then 3 in 5 chance that state should be fire" ;; * "if state is forest or state is climax and some neighbours have state is fire then 3 in 5 chance that state should be fire"
;; * "if state is pasture and more than 3 neighbours have state is scrub then state should be scrub" ;; * "if state is pasture and more than 3 neighbours have state equal to scrub then state should be scrub"
;; * "if state is forest and fertility is between 55 and 75 then state should be climax" ;; * "if state is in grassland or pasture or heath and 4 neighbours are water then state should be village"
;; ;;
;; it generates rules in the form expected by mw-engine.core ;; it generates rules in the form expected by mw-engine.core
@ -76,24 +77,18 @@
(defn parse-disjunct-value (defn parse-disjunct-value
"Parse a list of values from among these `tokens`. If `expect-int` is true, return "Parse a list of values from among these `tokens`. If `expect-int` is true, return
an integer or something which will evaluate to an integer. an integer or something which will evaluate to an integer."
[[OR token & tokens] expect-int]
NOTE: contrary to the general behaviour of `parse-` functions, this one (println OR)
returns a vector [nil unconsumed-tokens] when it cannot find any further (cond (member? OR '("or" "in"))
disjuncts. TODO: doesn't work." (let [[others remainder] (parse-disjunct-value2 tokens expect-int)]
[[OR & tokens] expect-int] [(cons
(cond (cond
(member? OR '("in" "or")) expect-int (first (parse-simple-value (list token) true))
(cond expect-int true (keyword token))
(let [[member r] (parse-simple-value tokens) others)
[others remainder] (parse-disjunct-value r expect-int)] remainder])
(cond member [(cons member others) remainder]) true [nil (cons OR (cons token tokens))]))
true
(let [[member r] tokens
[others remainder] (parse-disjunct-value r expect-int)]
(cond member [(cons (keyword member) others) remainder]
true [(list member) r]))))
true [nil (cons OR tokens)]))
(defn parse-value (defn parse-value
"Parse a value from among these `tokens`. If `expect-int` is true, return "Parse a value from among these `tokens`. If `expect-int` is true, return
@ -108,7 +103,7 @@
(defn parse-member-condition (defn parse-member-condition
[[property IN & rest]] [[property IN & rest]]
(if (= IN "in") (if (= IN "in")
(let [[l & remainder] (parse-disjunct-value (cons "in" rest) false)] (let [[l remainder] (parse-disjunct-value (cons "in" rest) false)]
[(list 'member? (keyword property) l) remainder]))) [(list 'member? (keyword property) l) remainder])))
(defn parse-less-condition (defn parse-less-condition