For my thesis, I decided to complete the task of the ICFP 2004 contest .
The task - as I translated it for myself - is to write a compiler that translates a high-level ant language into a low-level ant assembly. In my case, this means using the DSL recorded in the Clojure (a Lisp) dialog box as a high-level ant language to create an ant association.
UPDATE:
The ant assembler has several limitations: there are no build commands for function calls (that is, I cannot write CALL function1, param1
), and also do not return from functions and do not push return addresses onto the stack. In addition, there is no stack at all (for passing parameters), no heap, no memory. The only thing I have is the GOTO / JUMP instruction.
Actually, the ant assembly is intended to describe the transitions of a finite state machine (= brain of ants). For "function calls" (= state transitions) I have JUMP / GOTO.
Having nothing like a stack, heap, or the correct CALL instruction, I still want to be able to call functions in ant -assembly (using JUMPing for specific labels). In several places, I read that converting Clojure DSL function function calls to Continued Transfer (CPS) style, I can avoid using the stack [1], and I can translate my ant function calls to plain JUMP (or GOTO). This is exactly what I need, because in the ant assembler I donβt have a stack at all, only a GOTO instruction.
My problem is that after completing the ant build function, I cannot tell the interpreter (which interprets the instructions of the ant assembler) where to continue. Maybe an example helps:
High Level Clojure DSL:
(defn search-for-food [cont] (sense-food-here? ; a conditional w/ 2 branches (pickup-food ; true branch, food was found (go-home ; *** (drop-food (search-for-food cont)))) (move ; false branch, continue searching (search-for-food cont)))) (defn run-away-from-enemy [cont] (sense-enemy-here? ; a conditional w/ 2 branches (go-home ; *** (call-help-from-others cont)) (search-for-food cont))) (defn go-home [cont] (turn-backwards ; don't bother that this "while" is not in CPS now (while (not (sense-home-here?)) (move))) (cont))
The ant element that I would like to create from the go-home
function:
FUNCTION-GO-HOME: turn left nextline turn left nextline turn left nextline ; now we turned backwards SENSE-HOME: sense here home WE-ARE-AT-HOME CONTINUE-MOVING CONTINUE-MOVING: move SENSE-HOME WE-ARE-AT-HOME: JUMP ??? FUNCTION-DROP-FOOD: ... FUNCTION-CALL-HELP-FROM-OTHERS: ...
The syntax for the ant -asm statements above is:
turn direction which-line-to-jump sense direction what jump-if-true jump-if-false move which-line-to-jump
My problem is that I cannot find out what to write to the last line of the assembly ( JUMP ???
). Because - as you can see in the example - go-home
can be called with two different extensions:
(go-home (drop-food))
and
(go-home (call-help-from-others))
After go-home
I would like to call either drop-food
or call-help-from-others
. In the assembly: after I got home (= label WE-ARE-AT-HOME
), I would like to go either to the label FUNCTION-DROP-FOOD
, or to FUNCTION-CALL-HELP-FROM-OTHERS
.
How could I do this without a stack without pushing the address of the next command (= FUNCTION-DROP-FOOD
/ FUNCTION-CALL-HELP-FROM-OTHERS
) FUNCTION-CALL-HELP-FROM-OTHERS
stack? My problem is that I do not understand how the continuation-transfer style (= no stack, only GOTO / JUMP) can help me solve this problem.
(I can try to explain this again if the above things are not clear.)
And many thanks for your help!
- Page [1] "To interpret it, no management stack or other unlimited temporary storage is required." Steele: Rabbit: compiler for the circuit.