Syntax
HIL uses S-expression syntax. Every expression is wrapped in parentheses:
Comments start with ;. Everything after ; on a line is ignored.
; This is a comment
(LOG "Hello") ; Inline comment
Strings are wrapped in double quotes. Numbers are bare. Hex literals use 0x prefix. Pin names use Arduino notation: A0-A7, D0-D13.
Conditionals
IF
| Syntax | Description |
(IF cond then) | Execute then if cond is truthy |
(IF cond then else) | Execute then if truthy, else if falsy |
IF can also be used as an expression that returns a value:
(IF (> temp 30) (LOG "Hot!") (LOG "Cool"))
; As expression
(SET status (IF (> temp 25) 1 0))
COND
Multi-branch conditional. First matching clause wins. Use 1 as a catch-all default.
(COND
((== command 1) (LOG "START"))
((== command 2) (LOG "STOP"))
(1 (LOG "UNKNOWN"))
)
Loops
WHILE
FOR
Counter-based loop. The variable runs from from toward to, exclusive, and the loop can count up or down.
(FOR i 0 10 ; i goes 0, 1, 2, ... 9
body
)
(FOR angle 180 -1 ; descending: 180, 179, ... 0
(SERVO 9 angle)
)
DO
Group multiple expressions in a block:
(DO
(expr1)
(expr2)
(expr3)
)
DEFUN – Define Functions
Create reusable functions with parameters:
(DEFUN function-name (param1 param2 ...)
body
)
(DEFUN BLINK (pin count delay_ms)
(WHILE (> count 0)
(DO
(WRITE D pin 1) (DELAY delay_ms)
(WRITE D pin 0) (DELAY delay_ms)
(SET count (- count 1))
)
)
)
(BLINK 13 5 200) ; Blink pin 13, 5 times, 200ms each
(DEFUN CELSIUS_TO_FAH (c)
(+ (/ (* c 9) 5) 32)
)
(SET f (CELSIUS_TO_FAH 25)) ; => 77
Note: Function names are automatically uppercased. my-func becomes MY_FUNC.
LAMBDA – Anonymous Functions & Closures
LAMBDA creates a function without a name that you can store in a variable, pass to other functions, or return from them. Lambdas are true first-class values with lexical closures.
In Plain Terms
A DEFUN is like an appliance bolted to the wall — useful but fixed. A lambda is a small gadget you build and hand over: it fits in your pocket (store it), you can mail it (pass it), and it remembers where it was built. If you build it next to a thermostat reading 25°, it remembers 25° even after you carry it away. That memory is a closure.
Think of two prepaid transit cards made on different days: each remembers its own loaded balance. Tap one and it spends its credit — never the other card's.
Syntax
(LAMBDA (param1 param2 ...)
body...
)
Named Lambda
(SET square (LAMBDA (x) (* x x)))
(square 5) ; => 25
Immediately Invoked (IIFE)
((LAMBDA (a b) (+ (* a a) (* b b))) 3 4) ; => 25
Closure Factory
The returned function keeps n alive after its factory has finished:
(SET make-adder (LAMBDA (n)
(LAMBDA (x) (+ x n))))
(SET add5 (make-adder 5))
(SET add10 (make-adder 10))
(add5 10) ; => 15
(add10 10) ; => 20
Stateful Closure
Variables captured by a closure survive between calls — private persistent state with no globals:
(DEFUN MAKE-COUNTER ()
(DO
(SET n 0)
(LAMBDA () (DO (SET n (+ n 1)) n))))
(SET tick (MAKE-COUNTER))
(tick) ; 1
(tick) ; 2
(tick) ; 3 ← n lives inside tick's captured environment
Higher-Order Use
;; Lambda passed into a DEFUN
(DEFUN APPLY-TWICE (f v) (DO (f v) (f v)))
(APPLY-TWICE (LAMBDA (x) (* x 2)) 7)
;; Lambda passed into another lambda
(SET call2 (LAMBDA (g x) (g x)))
(call2 (LAMBDA (x) (+ x 100)) 5) ; => 105
;; Callback pattern for hardware events
(ON-EVENT 2 FALLING
(LAMBDA () (WRITE D 13 1)))
Where Lambdas Work
| Target | Support | Notes |
| Interpreter / simulate | ✅ Full | Closures, state, higher-order |
| Node.js / Python / Go ports | ✅ Full | Same semantics |
| Transpiled C (.hex / CRUMB) | ⚠️ Partial | Top-level named lambdas become static C functions; runtime closures are not possible in C (warned) |