Flesh out more examples, implement state predicate.
catseye
12 years ago
174 | 174 |
|
175 | 175 |
##### State Referents #####
|
176 | 176 |
|
|
177 |
-> Tests for functionality "Evolve ALPACA CA one generation"
|
|
178 |
|
177 | 179 |
A state referent may be:
|
178 | 180 |
|
179 | 181 |
* the name of a defined state, to refer to that state directly
|
|
181 | 183 |
* a chain of _arrows_, to refer to the state of the cell found at
|
182 | 184 |
that relative position in the playfield
|
183 | 185 |
|
184 | |
Example: a somewhat less simple ALPACA description. Here the states
|
185 | |
have transition rules that cause each cell to take on the state of the
|
186 | |
cell to the "north" (immediately above it.) The effect would be to
|
187 | |
make any form in this cellular automaton "scroll downwards":
|
188 | |
|
189 | |
| state Space
|
|
186 |
Example: a somewhat less simple ALPACA description. Here the (non-empty)
|
|
187 |
states have transition rules that cause each cell to take on the state of one
|
|
188 |
of its neighbours; `Up` cells take on the state of the cell to the "north"
|
|
189 |
(immediately above it) while `Down` cells take on the state of the cell to
|
|
190 |
the "south" (immediately below it.) The effect in this example is for the
|
|
191 |
cells to "swap places":
|
|
192 |
|
|
193 |
| state Space " ";
|
|
194 |
| state Up "U"
|
190 | 195 |
| to ^ when true;
|
191 | |
| state Thing
|
192 | |
| to ^ when true.
|
193 | |
= ok
|
|
196 |
| state Down "D"
|
|
197 |
| to v when true
|
|
198 |
| begin
|
|
199 |
| DDD
|
|
200 |
| UUU
|
|
201 |
= -----
|
|
202 |
= UUU
|
|
203 |
= DDD
|
|
204 |
= -----
|
194 | 205 |
|
195 | 206 |
An arrow is either `^` (referring to one cell "north" or "above" the
|
196 | 207 |
current cell,) `v` (one cell "south" or "below",) `<` (one cell "west"
|
|
203 | 214 |
Example: an ALPACA description of a cellular automaton where `Thing`
|
204 | 215 |
elements grow "streaks" to the northwest (diagonally up and to the left.)
|
205 | 216 |
|
206 | |
| state Space
|
|
217 |
| state Space " "
|
207 | 218 |
| to Thing when v> Thing;
|
208 | |
| state Thing.
|
209 | |
= ok
|
|
219 |
| state Thing "*"
|
|
220 |
| begin
|
|
221 |
| *
|
|
222 |
| *
|
|
223 |
= -----
|
|
224 |
= *
|
|
225 |
= **
|
|
226 |
= *
|
|
227 |
= -----
|
210 | 228 |
|
211 | 229 |
##### Boolean Expressions #####
|
|
230 |
|
|
231 |
-> Tests for functionality "Parse ALPACA Description"
|
212 | 232 |
|
213 | 233 |
The boolean expression may be:
|
214 | 234 |
|
|
0 |
state Space " "
|
|
1 |
to Thing when v> Thing;
|
|
2 |
state Thing "*"
|
|
3 |
begin
|
|
4 |
*
|
|
5 |
*
|
46 | 46 |
#print "(%d,%d) has %d neighbours that are %s" % (x, y, count, state)
|
47 | 47 |
return count >= int(ast.value)
|
48 | 48 |
elif ast.type == 'Relational':
|
49 | |
raise NotImplementedError
|
|
49 |
state0 = eval_state_ref(playfield, x, y, ast.children[0])
|
|
50 |
state1 = eval_state_ref(playfield, x, y, ast.children[1])
|
|
51 |
return state0 == state1
|
50 | 52 |
elif ast.type == 'BoolLit':
|
51 | 53 |
if ast.value == 'true':
|
52 | 54 |
return True
|
215 | 215 |
else:
|
216 | 216 |
s1 = self.state_ref()
|
217 | 217 |
self.scanner.consume('=') # optional
|
218 | |
if self.scanner.consume('is'):
|
219 | |
classid = self.scanner.consume_type('identifier')
|
220 | |
return AST('RelationalClass', [s1], value=classid)
|
|
218 |
if self.scanner.on('is'):
|
|
219 |
c = self.class_decl()
|
|
220 |
return AST('RelationalClass', [s1, c])
|
221 | 221 |
s2 = self.state_ref()
|
222 | |
return AST('Relational', [s2])
|
|
222 |
return AST('Relational', [s1, s2])
|
223 | 223 |
|
224 | 224 |
|
225 | 225 |
def resolve_arrow_chain(s):
|