Control Flow
HIL LISP provides several control flow constructs for conditional execution, looping, and branching. Every control structure evaluates to a value—the result of the last executed branch (Abelson et al., 1996).
Conditional Execution: IF
The IF operator evaluates a condition and executes one of two branches:
; Simple IF (no else branch)
(IF (> temp 30)
(LOG "Too hot!"))
; IF with else branch
(IF (>= temp 30)
(LOG "Hot!")
(LOG "OK"))
Behavior:
- If the condition is nonzero (truthy), the then-branch executes
- If the condition is zero (falsy), the else-branch executes (if present)
- If no else-branch,
IFreturns0 - The condition is always evaluated; both branches are never evaluated
Multi-way Branching: COND
The COND operator evaluates multiple conditions in sequence and executes the first matching branch:
(SET state 0)
(COND
((== state 0) (LOG "IDLE"))
((== state 1) (LOG "RUNNING"))
((== state 2) (LOG "STOPPED")))
Behavior:
- Each clause is a list
(condition body) - Conditions can be integers (nonzero = true), symbols (checked in environment), or expressions
- The first matching clause executes; subsequent clauses are skipped
- If no clause matches,
CONDreturns0
Example: Temperature Control System:
(SET temp (LM35))
(COND
((< temp 18) (DO (LOG "Heating ON") (WRITE D 8 1)))
((AND (>= temp 18) (<= temp 26)) (LOG "Comfortable"))
((> temp 26) (DO (LOG "Cooling ON") (WRITE D 9 1))))
While Loop: WHILE
The WHILE operator repeatedly executes a body expression as long as the condition is true:
(SET x 0)
(WHILE (< x 5)
(DO
(LOG "x = " x)
(SET x (+ x 1))))
Output:
x = 0
x = 1
x = 2
x = 3
x = 4
Behavior:
- The condition is evaluated before each iteration
- The body is a single expression (use
DOfor multiple statements) WHILEreturns the value of the last body evaluation- An iteration limit (10,000) prevents infinite loops in headless mode
For Loop: FOR
The FOR operator iterates a variable from from toward to in steps of one. The stop value is exclusive, and the loop can count upward or downward:
; Log numbers 0 through 4
(FOR i 0 5 (LOG i))
; Output: 0, 1, 2, 3, 4
Syntax: (FOR var from to body)
Examples:
; Loop with DO for multiple statements
(FOR i 1 4
(DO
(LOG "Square of " i " = " (* i i))))
; Nested FOR loops
(FOR i 1 3
(FOR j 1 3
(LOG i " x " j " = " (* i j))))
; FOR with computed bounds
(SET n 10)
(FOR i 0 n (LOG i))
Note: The stop value is exclusive. (FOR i 0 5 ...) runs for i = 0, 1, 2, 3, 4. A descending loop uses the same rule: (FOR i 5 0 ...) runs 5, 4, 3, 2, 1.
Sequence: DO
The DO operator executes multiple expressions in order and returns the last value:
(DO
(SET x 1)
(SET y 2)
(SET z (+ x y))
(LOG "z = " z))
; Output: z = 3
; Returns: 3
DO is essential for combining multiple operations inside WHILE, FOR, and IF branches.
Loop Control: BREAK and CONTINUE
; BREAK exits the innermost loop
(FOR i 0 10
(DO
(IF (== i 5) (BREAK))
(LOG i)))
; Output: 0, 1, 2, 3, 4
; CONTINUE skips to the next iteration
(FOR i 0 5
(DO
(IF (== (% i 2) 0) (CONTINUE))
(LOG i)))
; Output: 1, 3, 5 (skips even numbers)
Important: BREAK and CONTINUE only work inside WHILE or FOR loops. Outside a loop, they are treated as undefined variables (evaluating to 0).
State Machine Pattern
A common pattern in embedded systems is the state machine. HIL LISP's COND makes this natural:
(SET state 0)
(SET tick 0)
(WHILE (< tick 20)
(DO
(SET tick (+ tick 1))
(COND
((== state 0)
(DO
(LOG "IDLE -> RUNNING t=" tick)
(SET state 1)))
((== state 1)
(DO
(LOG "RUNNING t=" tick)
(IF (>= tick 8) (SET state 2))))
((== state 2)
(DO
(LOG "STOPPED -> IDLE")
(SET state 0))))))
This pattern is used extensively in robotics, IoT devices, and control systems (Gamma et al., 1994).
References
Abelson, H., Sussman, G. J., & Sussman, J. (1996). Structure and interpretation of computer programs (2nd ed.). MIT Press.
Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design patterns: Elements of reusable object-oriented software. Addison-Wesley.