From ca3861b50518334898095a80566325f5d799b7e0 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 10 Jul 2023 13:44:47 +0100 Subject: [PATCH] Upversioning whole system to 0.2.0, for flow feature This is definitely a point version change! --- project.clj | 4 ++-- src/mw_parser/declarative.clj | 1 + src/mw_parser/flow.clj | 4 ++-- src/mw_parser/generate.clj | 12 ++++++++-- src/mw_parser/simplify.clj | 45 ++++++++++++++++++++--------------- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/project.clj b/project.clj index 5afc9a1..77f0874 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject mw-parser "0.1.6-SNAPSHOT" +(defproject mw-parser "0.2.0-SNAPSHOT" :description "Parser for production rules for MicroWorld engine" :url "http://www.journeyman.cc/microworld" :manifest { @@ -14,5 +14,5 @@ :dependencies [[org.clojure/clojure "1.11.1"] [org.clojure/tools.trace "0.7.11"] [instaparse "1.4.12"] - [mw-engine "0.1.6-SNAPSHOT"] + [mw-engine "0.2.0-SNAPSHOT"] [trptr/java-wrapper "0.2.3"]]) diff --git a/src/mw_parser/declarative.clj b/src/mw_parser/declarative.clj index 3239c62..a0f6b39 100644 --- a/src/mw_parser/declarative.clj +++ b/src/mw_parser/declarative.clj @@ -88,6 +88,7 @@ "CHANCE-IN := 'chance in';" "EACH := 'each' | 'every' | 'all';" "EQUAL := 'equal to';" + "FIRST := 'first';" "FLOW := 'flow' | 'move';" "FROM := 'from';" "IF := 'if';" diff --git a/src/mw_parser/flow.clj b/src/mw_parser/flow.clj index 80f8b50..adaacf4 100644 --- a/src/mw_parser/flow.clj +++ b/src/mw_parser/flow.clj @@ -9,7 +9,7 @@ "Grammar for flow rules" (join "\n" ["FLOW-RULE := FLOW SPACE QUANTITY SPACE PROPERTY SPACE FROM SPACE SOURCE SPACE TO-HOW SPACE DESTINATION;" "PERCENTAGE := NUMBER #'%';" - "QUANTITY := PERCENTAGE | NUMBER;" + "QUANTITY := PERCENTAGE | NUMBER | SOME;" "SOURCE := STATE | STATE SPACE WITH SPACE CONDITIONS;" "DESTINATION := STATE | STATE SPACE WITH SPACE FLOW-CONDITIONS;" "DETERMINER := MOST | LEAST;" @@ -18,7 +18,7 @@ "STATE := SYMBOL;" "TO-HOW := TO | TO-EACH | TO-FIRST;" "TO-EACH := TO SPACE EACH | TO SPACE ALL;" - "TO-FIRST := TO SPACE EACH" + "TO-FIRST := TO SPACE FIRST" ])) (def parse-flow diff --git a/src/mw_parser/generate.clj b/src/mw_parser/generate.clj index 3c86b02..6a1d318 100644 --- a/src/mw_parser/generate.clj +++ b/src/mw_parser/generate.clj @@ -53,9 +53,9 @@ (defn generate-conjunct-condition - [tree] "From this `tree`, assumed to be a syntactically conjunct correct condition clause, generate and return the appropriate clojure fragment." + [tree] (assert-type tree :CONJUNCT-CONDITION) (cons 'and (map generate (rest tree)))) @@ -93,7 +93,7 @@ qualifier (generate (nth tree 2)) expression (generate (nth tree 3))] (generate-disjunct-property-condition tree property qualifier expression))) - ([tree property qualifier expression] + ([_tree property qualifier expression] (let [e (list 'some (list 'fn ['i] '(= i value)) (list 'quote expression))] (list 'let ['value (list property 'cell)] (if (= qualifier '=) e @@ -314,3 +314,11 @@ :WITHIN-CONDITION (generate-within-condition tree) (map generate tree)) tree)) + +;;; Flow rules. A flow rule DOES NOT return a modified world; instead, it +;;; returns a PLAN to modify the world, in the form of a sequence of `flows`. +;;; It is only when the plan is executed that the world is modified. +;;; +;;; so we're looking at something like +;;; (fn [cell world]) +;;; (if (= (:state cell) (or (:house cell) :house)) \ No newline at end of file diff --git a/src/mw_parser/simplify.clj b/src/mw_parser/simplify.clj index e203b0c..44d81bb 100644 --- a/src/mw_parser/simplify.clj +++ b/src/mw_parser/simplify.clj @@ -1,7 +1,8 @@ (ns ^{:doc "Simplify a parse tree." :author "Simon Brooke"} mw-parser.simplify - (:require [mw-engine.utils :refer [member?]])) + (:require [clojure.pprint :refer [pprint]] + [mw-engine.utils :refer [member?]])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -26,20 +27,20 @@ ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(declare simplify-rule) +(declare simplify-flow simplify-rule) -(defn simplify-qualifier - "Given that this `tree` fragment represents a qualifier, what - qualifier is that?" - [tree] - (cond - (empty? tree) nil - (and (coll? tree) - (member? (first tree) '(:EQUIVALENCE :COMPARATIVE))) tree - (coll? (first tree)) (or (simplify-qualifier (first tree)) - (simplify-qualifier (rest tree))) - (coll? tree) (simplify-qualifier (rest tree)) - :else tree)) +;; (defn simplify-qualifier +;; "Given that this `tree` fragment represents a qualifier, what +;; qualifier is that?" +;; [tree] +;; (cond +;; (empty? tree) nil +;; (and (coll? tree) +;; (#{:EQUIVALENCE :COMPARATIVE} (first tree))) tree +;; (coll? (first tree)) (or (simplify-qualifier (first tree)) +;; (simplify-qualifier (rest tree))) +;; (coll? tree) (simplify-qualifier (rest tree)) +;; :else tree)) (defn simplify-second-of-two "There are a number of possible simplifications such that if the `tree` has @@ -47,11 +48,11 @@ [tree] (if (= (count tree) 2) (simplify-rule (nth tree 1)) tree)) -(defn simplify-quantifier - "If this quantifier is a number, 'simplifiy' it into a comparative whose operator is '=' - and whose quantity is that number. This is actually more complicated but makes generation easier." - [tree] - (if (number? (second tree)) [:COMPARATIVE '= (second tree)] (simplify-rule (second tree)))) +;; (defn simplify-quantifier +;; "If this quantifier is a number, 'simplifiy' it into a comparative whose operator is '=' +;; and whose quantity is that number. This is actually more complicated but makes generation easier." +;; [tree] +;; (if (number? (second tree)) [:COMPARATIVE '= (second tree)] (simplify-rule (second tree)))) (defn simplify-rule "Simplify/canonicalise this `tree`. Opportunistically replace complex fragments with @@ -76,12 +77,18 @@ (remove nil? (map simplify-rule tree))) tree)) +(defn simplify-determiner-condition + [tree]) + (defn simplify-flow [tree] (if (coll? tree) (case (first tree) + :FLOW nil :DETERMINER (simplify-second-of-two tree) + :DETERMINER-CONDITION (simplify-determiner-condition tree) :SPACE nil + :QUANTITY (simplify-second-of-two tree) :STATE [:PROPERTY-CONDITION [:SYMBOL "state"] [:QUALIFIER