mw-parser/README.md

4.5 KiB

mw-parser

A rule parser for MicroWorld

Usage

Main entry point is (parse-rule string), where string takes a form detailed in grammar, below. If the rule is interpretted correctly the result will be a Clojure anonymous function; if the rule is not interpretted, currently nil is returned and there's no helpful error message.

Generated function and evaluation environment

The generated function is a function of two arguments

  • cell a cell in a world as defined in mw-engine.world, q.v.;
  • world the world of which that cell forms part.

It returns a new cell, based on the cell passed.

Actions of the rule will (can only) modify properties of the cell; there are two properties which are special and SHOULD NOT be modified, namely the properties x and y. Currently there is no policing that these properties are not modified.

Grammar

Comments

  • Any line which starts with the hash character (#) is ignored;
  • Any line which starts with a semi-colon (;) is ignored.

Rules

A rule comprises:

  • if conditions then actions

Each rule must be on a single line. There should be nothing else on that line.

Conditions

In rules, conditions is one of:

  • condition
  • condition and conditions
  • condition or conditions

Note that 'and' takes precedence over or, so

conditionA and conditionB or conditionC and conditionD

is interpreted as

(conditionA and (conditionB or (conditionC and conditionD)))

A condition is one of:

  • property is value
  • property is not value
  • property is in values
  • property is not in values
  • property is more than numeric-value
  • property is less than numeric-value
  • number neighbours have property equal to value
  • number neighbours have property more than numeric-value
  • number neighbours have property less than numeric-value
  • more than number neighbours have property equal to value
  • fewer than number neighbours have property equal to value
  • some neighbours have property equal to value
  • more than number neighbours have property more than numeric-value
  • fewer than number neighbours have property more than numeric-value
  • some neighbours have property more than numeric-value
  • more than number neighbours have property less than numeric-value
  • fewer than number neighbours have property less than numeric-value
  • some neighbours have property less than numeric-value

Actions

In these rules, actions is one of:

  • action
  • action and actions

and action is:

  • property should be value
  • number chance in number property should be value

Properties

In the above, property is the name of any property of a cell. Any alpha-numeric string of characters can form the name of a property. Actions should NOT refer to the reserved properties x and y.

Values in Conditions

Values in conditions and actions are considered slightly differently. In a condition, a value is one of:

  • symbolic-value
  • numeric-value

The '...more than...' and '...less than...' conditions imply a numeric-value. Thus "if altitude is more than fertility..." is interpreted as meaning "if the value of the property of the current cell called 'altitude' is greater than the value of the property of the current cell called 'fertility'", whereas the apparently similar condition 'if altitude is fertility...' is interpreted as meaning "if the value of the property of the current cell called 'altitude' is the symbol 'fertility'".

Thus symbolic-value is any sequence of alphanumeric characters, whereas numeric-value is one of:

  • number
  • property

and number is any sequence of the decimal digits 0...9, the minus character '-' and the period character '.', provided that the minus character can only be in the first position, and the period character can only appear once.

Values in Actions

A value in an action is one of

  • symbolic-value
  • arithmetic-value
  • number

where arithmetic-value is:

  • property operator numeric-value

and operator is one of the simple arithmetic operators '+', '-', '*' and '/'.

Shorthand

Note that '...neighbours are...' is equivalent to '...neighbours have state equal to...', and 'some neighbours...' is equivalent to 'more than 0 neighbours...'

License

Copyright © 2014 Simon Brooke

Distributed under the terms of the GNU General Public License v2