Genes: cond, start, else and stop
How a gene is built with cond, start, else and stop, which parts of the DNA run, and how genes are numbered.
A bot's DNA is a long list of words that is traversed in full, from left to right, once per cycle. Its order comes from four flow markers: cond, start, else and stop. With them you build genes: pieces of DNA that run only if certain conditions are met.
There's no nesting and there are no blocks: the four markers are flat, and each one changes the interpreter's state as it passes. This page covers what each one does, with examples we ran in the port.
Anatomy of a gene
The typical form is this:
' Goes forward the first 10 cycles of life and then goes back
cond
*.robage 10 <
start
10 .up store
else
10 .dn store
stopcondopens the gene and starts the condition section.startcloses the conditions and opens the body, which runs if they all came out true.elseopens a second body, which runs if any came out false.stopcloses the gene.
This bot pushes forward with .up while its age (.robage) is less than 10, and then pushes backward with .dn. Running it, the bot moves forward about 400 units, brakes by inertia around cycle 16 and retraces its steps.
The condition section
Between cond and start, everything runs except stores: numbers, memory reads, operators and comparisons. So you can compute before comparing:
' In the condition section you can compute, but not write
cond
2 3 add 5 =
7 87 store
start
1 88 store
stopHere the condition is 2 3 add 5 = (true) and the body writes 1 to address 88. The 7 87 store in the condition section writes nothing: a store only acts inside a body.
Comparisons, such as < or =, leave their result on the boolean stack (see The integer and boolean stacks). What happens to that stack at each marker is the key to everything:
condempties the boolean stack at the start.start(andelse, when it comes right after the conditions) does a logical AND of everything on the boolean stack and leaves it empty. If everything is true, the body runs.- If there was no comparison at all, the stack is empty and counts as true:
cond start … stopalways runs.
You don't need to write and between conditions: three comparisons in a row are already combined with AND. For an OR, use an explicit or.
A start without cond
A start that doesn't come after a cond opens a gene with no conditions: it runs every cycle. It's the same as cond start, and many bots begin that way. Bardus, by Moonfisher, is an entire bot made of a single cond start … stop gene that solves everything with arithmetic, without a single comparison.
The else
In this version, else works the way you'd expect:
' Gene with else: writes 1 or 2 to address 60 depending on *50
cond
*50 0 >
start
1 60 store
else
2 60 store
stopWith address 50 at 0, the bot writes 2 to 60; with 50 at 5, it writes
- The
elseuses the result of the conditions that opened the previous
start.
An else attached to the conditions, without a start, also works. Its body runs only if the conditions come out false, and there's no body for the true case:
' else without start: runs when the condition is false
cond
*50 0 >
else
9 83 store
stopWith 50 at 0 it writes 9 to 83; with 50 at 3 it does nothing.
An else that doesn't have a cond … start right before it never runs: after a start without cond, after a stop or after another else. In this bot only address 84 gets written:
' else after a start without cond, or after stop: never runs
start
1 84 store
else
2 85 store
stop
else
3 86 store
stopCode outside the genes
What's left before the first marker, or between a stop and the next marker, does not run. Not even numbers get pushed:
' What's left outside a gene does not run
5 70 store
start
1 71 store
stop
7 72 store
cond
3 73 store
start
4 74 store
stopOf the five writes, only the one to 71 and the one to 74 happen. The one to 73 doesn't, because it's in the condition section.
Markers, on the other hand, are always processed, even if the gene is being skipped, and they charge their cost every time (see Execution and costs). That's why a cond anywhere opens a new gene: it closes the previous body even if its stop is missing.
' A cond closes the previous gene even if the stop is missing
cond
1 2 >
start
1 90 store
cond
start
2 91 store
stopThe first gene doesn't run (1 isn't greater than 2) and the second does: 91 holds 2.
The DNA ends at the first end; whatever follows doesn't exist for the bot.
Several starts in a row
A second start isn't an “and also”: it opens another gene, with no conditions.
' The second start opens a new gene with no conditions
cond
*50 0 >
start
1 75 store
start
2 76 store
stopWith 50 at 0, the condition is false and 75 stays at 0, but 76 gets 2 anyway. If you want two bodies with the same condition, repeat the cond.
The boolean stack carries over from one gene to the next
Only cond clears the boolean stack: neither stop nor a start with no conditions does. That's why a condition written inside a body (see Inline conditions) can keep blocking the stores of the next gene if that gene doesn't begin with cond. The example is in The integer and boolean stacks.
If a gene has to run no matter what, begin it with cond start, or clear the stack at the end of the previous gene with clearbool or dropbool.
Gene numbering
Genes are numbered from 1, in order. A new gene is counted for:
- each
cond; - each
startorelsethat does not come right after the conditions of acond.
Watch out for the second rule: in cond … start … else … stop, the else counts as a separate gene. This bot records the number of its gene in each body:
' Each gene records its number with .thisgene
cond
start
*.thisgene 61 store
stop
start
*.thisgene 62 store
stop
cond
1 2 >
start
*.thisgene 63 store
else
*.thisgene 64 store
stopResult: 61 holds 1, 62 holds 2 and 64 holds 4 (the start for 63 is gene 3, which didn't run). .genes is 4.
| Sysvar | What it holds |
|---|---|
.genes | How many genes the DNA has. It updates itself. |
.thisgene | The number of the current gene. It changes at every marker, even if the gene is skipped, so at the end of the cycle it holds the last gene's. |
.dnalen | The length of the DNA in words. |
That same numbering is used by .delgene, which deletes a gene from the bot's own DNA, and .mkvirus, which builds a virus from a gene (see Viruses). When the engine exports a bot as text it adds comments of the style Gene: 5 Begins at position 97, with the same count (see The .txt format); many Bestiary bots include them.
Summary
| Situation | What happens |
|---|---|
cond C start A stop | A runs if all the conditions in C are true. |
cond start A stop or start A stop | A always runs. |
cond C start A else B stop | A if C is true; B if it's false. They are two genes. |
cond C else B stop | B runs if C is false. |
else after stop, after another else or after a start without cond | Never runs. |
start A start B | B is another gene, with no conditions. |
Stores between cond and start | They don't write. |
| Code outside a gene | It doesn't run. |