Compare commits

..

1 commit

Author SHA1 Message Date
simon 0716b517cb This again doesn't compile, with the same error - can't take nth of symbol -
as previously, and again I don't know why.
2016-03-10 08:31:04 +00:00
3 changed files with 59 additions and 24 deletions

View file

@ -1,11 +1,11 @@
(ns ^:figwheel-always mw3.core
(:use mw3.utils)
(:use-macros [dommy.template :only [node deftemplate]])
(:require-macros [cljs.core.async.macros :refer [go]])
(:require
[mw3.rulesets :as rulesets]
[dommy.core :as dommy :refer-macros [sel sel1]]
[dommy.template :as temp]
))
[dommy.template :as temp]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -101,17 +101,6 @@
[:pre {:id (str "rule-feedback-" index) :class "rule-feedback"}]
])
;; (deftemplate rule-editors
;; ;; Constructs, as a `div`, a set of rule editors for the rules in the ruleset with
;; ;; this `ruleset-name`.
;; [ruleset-name]
;; [:div
;; (vec
;; (map
;; #(rule-editor % %)
;; (rulesets/rulesets ruleset-name)
;; (range)))])
(defn load-ruleset
"Loads the ruleset with the specified `name` into a set of rule editors"
[name]

View file

@ -1,15 +1,11 @@
(ns ^:figwheel-always mw3.core
(:use [mw3.utils :only [error member?]]
[clojure.string :only [split trim triml]]
#?(:cljs
[cljs.reader :only [read-string]]))
(:require [instaparse.core :as insta])
)
(:use mw3.utils
[clojure.string :only [split trim triml]])
(:require [instaparse.core :as insta]))
;; error thrown when an attempt is made to set a reserved property
(def reserved-properties-error
"The properties 'x' and 'y' of a cell are reserved and should not be set in rule actions")
;; error thrown when a rule cannot be parsed. Slots are for
;; (1) rule text
;; (2) cursor showing where in the rule text the error occurred
@ -204,6 +200,39 @@
([comp1 quantity property-condition]
(generate-neighbours-condition comp1 quantity property-condition 1)))
;; (def s1 "if 3 neighbours have state equal to forest then state should be forest")
;; (def s2 "if some neighbours have state equal to forest then state should be forest")
;; (def s3 "if more than 3 neighbours have state equal to forest then state should be forest")
;; (def s4 "if fewer than 3 neighbours have state equal to forest then state should be forest")
;; (def s5 "if all neighbours have state equal to forest then state should be forest")
;; (def s6 "if more than 3 neighbours within 2 have state equal to forest then state should be forest")
;; (nth (simplify (parse-rule s1)) 2)
;; (second (nth (simplify (parse-rule s1)) 2))
;; (nth (simplify (parse-rule s2)) 2)
;; (map simplify (nth (simplify (parse-rule s2)) 2))
;; ;; (second (nth (simplify (parse-rule s2)) 2))
;; ;; (nth (simplify (parse-rule s3)) 2)
;; (second (nth (simplify (parse-rule s3)) 2))
;; (map simplify (second (nth (simplify (parse-rule s3)) 2)))
;; ;; (nth (simplify (parse-rule s4)) 2)
;; ;; (second (nth (simplify (parse-rule s4)) 2))
;; ;; (nth (simplify (parse-rule s5)) 2)
;; ;; (second (nth (simplify (parse-rule s5)) 2))
;; ;; (nth (simplify (parse-rule s6)) 2)
;; ;; (second (nth (simplify (parse-rule s6)) 2))
;; ;; (generate (nth (nth (simplify (parse-rule s5)) 2) 4))
;; ;; (generate (nth (simplify (parse-rule s2)) 2))
;; ;; (generate (nth (simplify (parse-rule s1)) 2))
;; (generate-neighbours-condition '= 3 '(= (:state cell) :forest) 1)
;; (generate-neighbours-condition (nth (simplify (parse-rule s3)) 2))
;; (generate-neighbours-condition (nth (simplify (parse-rule s2)) 2))
;; (generate-neighbours-condition (nth (simplify (parse-rule s1)) 2))
(defn generate
"Generate code for this (fragment of a) parse tree"
[tree]
@ -323,7 +352,5 @@
[rule]
(assert (string? rule))
(let [tree (simplify (parse-rule rule))]
(if (rule? tree) #?(:clj (eval (generate tree))
:cljs (generate tree))
(if (rule? tree) (eval (generate tree))
(throw-parse-exception tree))))

View file

@ -5,11 +5,21 @@
#?(:cljs (js/Error. message)
:clj (Exception. message)))
(defn nth
"I'm getting a compilation error saying `nth` isn't defined; so I'm defining it."
[collection index]
{:pre [(and (coll? collection) (integer? index) (or (zero? index) (pos? index)))]}
(cond
(empty? collection) nil
(zero? index) (first collection)
:true (nth (rest collection) (dec index))))
(defn abs
"Surprisingly, Clojure doesn't seem to have an abs function, or else I've
missed it. So here's one of my own. Maps natural numbers onto themselves,
and negative integers onto natural numbers. Also maps negative real numbers
onto positive real numbers.
* `n` a number, on the set of real numbers."
[n]
(if (neg? n) (- 0 n) n))
@ -33,9 +43,11 @@
[world cell]
(merge cell {:generation (get-int-or-zero cell :generation)}))
(defn in-bounds
"True if x, y are in bounds for this world (i.e., there is a cell at x, y)
else false.
* `world` a world as defined above;
* `x` a number which may or may not be a valid x coordinate within that world;
* `y` a number which may or may not be a valid y coordinate within that world."
@ -77,6 +89,7 @@
(defn get-cell
"Return the cell a x, y in this world, if any.
* `world` a world as defined above;
* `x` a number which may or may not be a valid x coordinate within that world;
* `y` a number which may or may not be a valid y coordinate within that world."
@ -86,6 +99,7 @@
(defn get-int
"Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0.
* `map` a map;
* `key` a symbol or keyword, presumed to be a key into the `map`."
[map key]
@ -99,6 +113,7 @@
"Return the population of this species in this cell. Currently a synonym for
`get-int`, but may not always be (depending whether species are later
implemented as actors)
* `cell` a map;
* `species` a keyword representing a species which may populate that cell."
[cell species]
@ -124,15 +139,18 @@
(defn get-neighbours
"Get the neighbours to distance depth of a cell in this world.
Several overloads:
* `world` a world, as described in world.clj;
* `cell` a cell within that world
Gets immediate neighbours of the specified cell.
* `world` a world, as described in world.clj;
* `cell` a cell within that world
* `depth` an integer representing the depth to search from the
`cell`
Gets neighbours within the specified distance of the cell.
* `world` a world, as described in world.clj;
* `x` an integer representing an x coordinate in that world;
* `y` an integer representing an y coordinate in that world;
@ -184,6 +202,7 @@
(defn get-neighbours-with-state
"Get the neighbours to distance depth of the cell at x, y in this world which
have this state.
* `world` a world, as described in `world.clj`;
* `cell` a cell within that world;
* `depth` an integer representing the distance from [x,y] that
@ -212,6 +231,7 @@
(get-least-cell cells property #?(:cljs 900719925474099
:clj (Integer/MAX_VALUE)))))
(defn- set-cell-property
"If this `cell`s x and y properties are equal to these `x` and `y` values,
return a cell like this cell but with the value of this `property` set to
@ -252,4 +272,3 @@
(merge %2 cell)
%2))
world))