Difference between revisions of "Stack"
m (→Where its used) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Overview = | = Overview = | ||
− | The principle is quite simple: values are placed in a temporary holding area that is | + | The principle is quite simple: values are placed in a temporary holding area that is last-in-first-out (LIFO): like a stack of dishes. Almost all language commands make use of some stack or other, taking values off the stack and manipulating them, even occassionaly putting new values back on. |
== An example to illustrate: Subtraction == | == An example to illustrate: Subtraction == | ||
Line 13: | Line 13: | ||
a b '(note that if this were real code, you'd be more likely to see 5 6, or *.in1 *.in2 | a b '(note that if this were real code, you'd be more likely to see 5 6, or *.in1 *.in2 | ||
− | Next we'll want to invoke the subtraction command [[sub]] | + | Next we'll want to invoke the subtraction command [[sub]], which we simply do by having it in the DNA. |
a b sub | a b sub | ||
Line 19: | Line 19: | ||
That's it. Sub will take a and b from the stack, subtract b from a, and place the new answer on the top of the stack. In essence, we're basically just moving the sub command from the middle of the two numbers to the end. | That's it. Sub will take a and b from the stack, subtract b from a, and place the new answer on the top of the stack. In essence, we're basically just moving the sub command from the middle of the two numbers to the end. | ||
− | = Where | + | == Second Example: Conditionless Behavior == |
+ | |||
+ | The stack can be used to create conditionless behavior. | ||
+ | |||
+ | start | ||
+ | -6 *.refeye *.myeye sub sgn abs mult .shoot store | ||
+ | stop | ||
+ | |||
+ | The above gene will fire a body shot at a robot if it is identified as an enemy. Notice that it does not use any conditions, but rather uses math to determine when *.refeye and *.myeye are equal. Notice that the -6 remains on the stack until it is used by the mult. | ||
+ | |||
+ | = Where it's used = | ||
A stack is used in the following bits of the progam: | A stack is used in the following bits of the progam: | ||
− | * conditions - A | + | * conditions - A separate stack for conditions is kept for the logical operators to act on. At the end of every condition block in the DNA, all values remaining in this stack are [[and]]-ed together and the result saved in an [[Execution Flag]]. |
− | * integers - basic, advanced, and bit commands, and any others I may have forgotten, use the integer stack, which is what is commonly | + | * integers - basic, advanced, and bit commands, and any others I may have forgotten, use the integer stack, which is what is commonly meant by "the stack". |
= Technical Details = | = Technical Details = | ||
Line 43: | Line 53: | ||
As of 2.4, after a bot finishes executing its DNA for the cycle, its stack is cleared. That means that a bot ''can't'' use the stack to store values between cycles. For that, you have to use memory. | As of 2.4, after a bot finishes executing its DNA for the cycle, its stack is cleared. That means that a bot ''can't'' use the stack to store values between cycles. For that, you have to use memory. | ||
− | + | {{DNA}} | |
− | |||
− | |||
− | |||
− |
Latest revision as of 18:40, 13 February 2014
Contents
Overview
The principle is quite simple: values are placed in a temporary holding area that is last-in-first-out (LIFO): like a stack of dishes. Almost all language commands make use of some stack or other, taking values off the stack and manipulating them, even occassionaly putting new values back on.
An example to illustrate: Subtraction
a - b = c
Say you want to calculate c. In the end we'll want c to be on the top of the stack so we can use it again.
With the stack, you must first place the two operands on. Now, this is very important: Darwinbots uses reverse-Polish notation. Which means that in general the second number on the stack will be the first number used in the calculation. This may seem complicated, but it means that the DNA is easier to read in a way we're accustomed to. Anyway, here's what it'll look like:
a b '(note that if this were real code, you'd be more likely to see 5 6, or *.in1 *.in2
Next we'll want to invoke the subtraction command sub, which we simply do by having it in the DNA.
a b sub
That's it. Sub will take a and b from the stack, subtract b from a, and place the new answer on the top of the stack. In essence, we're basically just moving the sub command from the middle of the two numbers to the end.
Second Example: Conditionless Behavior
The stack can be used to create conditionless behavior.
start -6 *.refeye *.myeye sub sgn abs mult .shoot store stop
The above gene will fire a body shot at a robot if it is identified as an enemy. Notice that it does not use any conditions, but rather uses math to determine when *.refeye and *.myeye are equal. Notice that the -6 remains on the stack until it is used by the mult.
Where it's used
A stack is used in the following bits of the progam:
- conditions - A separate stack for conditions is kept for the logical operators to act on. At the end of every condition block in the DNA, all values remaining in this stack are and-ed together and the result saved in an Execution Flag.
- integers - basic, advanced, and bit commands, and any others I may have forgotten, use the integer stack, which is what is commonly meant by "the stack".
Technical Details
Empty
If a command tries to read from the stack when it's empty, it gets the appropriate default value. For the integer stack, this is 0. For the conditions stack, this is TRUE.
Full
In the same way that a pile of dishes can get too large, so can the stack. After the stack begins to hold 20 numbers, it will begin to push older numbers on the bottom off. This helps ensure that new DNA will always be operable even if it comes after sloppy DNA that doesn't clean up after itself and leaves a mess all over the kitchen floor, and won't pay their half of the rent, and stays in the bathroom too long...
Oh, right, Darwinbots.
It is easy to empty the stack by placing several add, sub, or mult commands in a row.
Number Range
As of 2.4, you are allowed to place numbers as large as +/-2000000000 (that's +/-2E9 or +/-2 billion) on the stack. Note that the bots' memory can only hold integers as large as +/- 32000. If a value greater in magnitude than this is stored in a memory location, your computer will explode. (Okay, not really. I think it's "mod"ed 32000. I'd have to check --Numsgil 11:06, 19 Sep 2005 (MST)).
At the end of the cycle
As of 2.4, after a bot finishes executing its DNA for the cycle, its stack is cleared. That means that a bot can't use the stack to store values between cycles. For that, you have to use memory.
How the DNA works
- Genes
- Execution Flag
- The Stack
- Custom labels (or variables if you prefer)
- Order of actions