CREATE DOES>
The tokenizer/evaluator handling of DOES> follows the ANS standard.
The builder handling of DOES> is not standard. In the case of the builder, the code after DOES> can't execute on the host PC. Defining words can only define words that execute on the target. These defining words are defined in host mode. When IDOES> is encountered, the interpreter switches to building mode and compiles the code to be executed at run time. Semicolon switches back to host mode.
IDOES> provides an elegant way to associate code and data. It patches the last ICREATE or IPCREATE code by replacing its return instruction with a jump to the IDOES> code.
Example 1. Simple data structure in ROM.
| <builder> | Place the following definition(s) in BUILDER | |
| : mydef | ( n1 n2 <name> -- ) | Compilation semantics |
| ( -- n1 n2 ) | Execution semantics | |
| ipcreate i, i, | Create a data structure in program space | |
| idoes> dup @p swap cell+ @p swap | Read double from program space | |
| ; | ||
| </builder> | Switch back to ROM building | |
| 100 200 | mydef foo1 | Switch back to ROM building |
Example 2. Voltage reading.
| <builder> | Place the following definition(s) in BUILDER | |
| : voltage | ( zero span channel <name> -- ) | Compilation semantics |
| ( -- millivolts ) | Execution semantics | |
| icreate i, swap i, i, | Create a data structure in data space | |
| idoes> a! @a+ ADC@ | Read raw ADC data | |
| @a+ - | Subtract the zero point | |
| @a+ m* nip | Scale by span | |
| ; | Conveniently ignore calibration issues. | |
| </builder> | Switch back to ROM building | |
| -2 20000 0 | voltage outside_temp | Define a voltage reading word |