HIL LISP Language Reference
HIL LISP is a small, embedded LISP dialect designed for resource-constrained environments. It borrows its S-expression syntax from McCarthy's (1960) original LISP but operates primarily on integers, making it suitable for microcontrollers where floating-point support is limited (Stallings, 2020).
Syntax Overview
HIL LISP programs are sequences of S-expressions (symbolic expressions). Each expression is either an atom (a number, symbol, or string) or a list (a parenthesized sequence of expressions).
; This is a comment (semicolon to end of line)
; Atoms
42 ; integer literal
0xFF ; hex literal (0x prefix)
"hello" ; string literal (only in LOG and SET)
myVar ; symbol (variable name or operator)
; Lists
(+ 1 2 3) ; function call: adds 1, 2, and 3
(SET x 42) ; assignment: x = 42
(IF (> x 10) (LOG "big") (LOG "small"))
Data Types
HIL LISP is dynamically typed. All values have one of these types:
| Type | Description | Example |
|---|---|---|
| Integer | 32-bit signed integer | 42, -7, 0xFF |
| String | Quoted character sequence | "hello" |
| Boolean | Integer: 0 = false, nonzero = true | 0 (false), 1 (true) |
| Function | User-defined via DEFUN | (DEFUN f (x) (+ x 1)) |
| Nil | Represented as integer 0 | 0 |
As Sussman and Steele (1975) note, LISP's simple type system enables powerful metaprogramming. HIL LISP leverages this by allowing functions to be stored as values and passed to other functions.
Variables
Variables are created using SET and persist in the global environment until the session is reset.
; Create variable x with value 42
(SET x 42)
; Reference x in expressions
(+ x 8) ; evaluates to 50
; Update x
(SET x (* x 2)) ; x is now 100
; Display all variables
; (use VARS command in REPL)
Variables are global by default. When a function is called, its parameters are temporarily bound in the function's scope and restored after the call returns (Abelson et al., 1996).
Strings
Strings are only meaningful in LOG (output) and SET (storage). They cannot be manipulated with string operations.
; Log a string
(LOG "Temperature: " temp "C")
; Store a string (stored as 0 internally)
(SET greeting "hello")
; String in LOG with computed values
(LOG "x=" x " y=" y " sum=" (+ x y))
Comments
; This is a comment
# This is also a comment
Comments are stripped during tokenization and have no effect on execution.
Arithmetic Operators
Addition: +
(+ 10 20) ; 30
(+ 1 2 3) ; 6 (multiple operands)
(+ x 1) ; x + 1
Subtraction: -
(- 10 3) ; 7
(- 10) ; -10 (unary negation)
(- x 1) ; x - 1
Multiplication: *
(* 6 7) ; 42
(* 2 x) ; 2 * x
Division: /
Division uses integer division (truncates toward zero):
(/ 10 3) ; 3 (not 3.333)
(/ -10 3) ; -3 (truncates toward zero)
(/ 10 0) ; 0 (division by zero returns 0, no error)
Integer division is standard in embedded systems where floating-point hardware is unavailable (Hennessy & Patterson, 2017).
Modulo: %
(% 10 3) ; 1
(% 15 5) ; 0
Comparison Operators
All comparison operators return 1 for true and 0 for false:
| Operator | Meaning | Example |
|---|---|---|
== | Equal | (== 5 5) → 1 |
!= | Not equal | (!= 5 3) → 1 |
> | Greater than | (> 10 5) → 1 |
< | Less than | (< 5 10) → 1 |
>= | Greater or equal | (>= 5 5) → 1 |
<= | Less or equal | (<= 5 5) → 1 |
; Use comparisons in IF statements
(SET temp 35)
(IF (>= temp 30) (LOG "Hot!") (LOG "OK"))
; Chain comparisons
(AND (>= temp 20) (<= temp 28)) ; temperature in comfortable range
Boolean Operators
Boolean operators treat 0 as false and any nonzero value as true. They return 1 (true) or 0 (false):
(AND 1 1) ; 1 (both true)
(AND 1 0) ; 0 (one false)
(OR 0 1) ; 1 (at least one true)
(OR 0 0) ; 0 (both false)
(NOT 0) ; 1 (not false = true)
(NOT 1) ; 0 (not true = false)
Short-circuit evaluation is not implemented. Both operands are always evaluated. This is a deliberate simplification for embedded systems (Kernighan & Ritchie, 1988).
Utility Operators
MIN and MAX
(MIN 3 7) ; 3
(MAX 3 7) ; 7
(MIN x y) ; smaller of x and y
ABS (Absolute Value)
(ABS -42) ; 42
(ABS 42) ; 42
MAP (Linear Interpolation)
Maps a value from one range to another. Widely used in sensor calibration:
; Map analog reading (0-1023) to PWM output (0-255)
(MAP (READ A 0) 0 1023 0 255)
; Map temperature (20-40°C) to fan speed (0-255)
(MAP temp 20 40 0 255)
The formula is: outMin + (value - inMin) * (outMax - outMin) / (inMax - inMin). This is equivalent to Arduino's map() function (Arduino Foundation, 2023).
CONSTRAIN
Clamps a value to a range:
; Ensure fan speed is between 0 and 255
(CONSTRAIN (MAP temp 20 40 0 255) 0 255)
RANDOM
Returns a random integer in the range [min, max]:
(RANDOM 1 6) ; random number 1-6 (simulates a die)
(RANDOM 0 255) ; random PWM value
Variable Operations
SET
Creates or updates a variable:
(SET x 42) ; x = 42
(SET y (+ x 8)) ; y = 50
(SET counter (+ counter 1)) ; increment counter
SET returns the assigned value, so it can be used inline:
(LOG "x is now " (SET x (+ x 1))) ; logs "x is now 43"
VARS (REPL Command)
Displays all defined variables in the REPL:
> (SET x 42)
42
> (SET y 100)
100
> VARS
x = 42
y = 100
Output: LOG
LOG prints values to the console. It accepts any number of arguments, which are printed space-separated:
(LOG "Hello, world!") ; Hello, world!
(LOG x) ; 42
(LOG "x=" x " y=" y) ; x=42 y=100
(LOG (+ 10 20)) ; 30
(LOG "Sum: " (+ x y)) ; Sum: 142
LOG internally converts all values to strings and concatenates them. In the headless IPC mode, LOG messages are transmitted as JSON objects over stdout (Rashwan, 2026).
Hardware I/O Operators
These operators control physical hardware pins. They are no-ops in headless mode:
WRITE
Sets a digital or analog output:
; Digital write: set pin 13 HIGH
(WRITE D 13 1)
; Digital write: set pin 13 LOW
(WRITE D 13 0)
; Analog write (PWM): set pin 9 to 50% duty cycle
(WRITE A 9 128)
; PWM alias
(WRITE P 9 128)
READ
Reads a digital or analog input:
; Read analog pin A0 (returns 0-1023)
(READ A 0)
; Read digital pin 2 (returns 0 or 1)
(READ D 2)
PIN-MODE
Configures a pin as input or output:
; Set pin 13 as output (1)
(PIN-MODE D 13 1)
; Set pin 2 as input (0)
(PIN-MODE D 2 0)
TONE
Plays a tone on a pin:
; Play 1000 Hz tone on pin 8 for 200ms
(TONE D 8 1000 200)
DELAY
Pauses execution:
; Wait 500 milliseconds
(DELAY 500)
Control Flow
IF
Conditional execution:
; Simple IF
(IF condition then-expr)
; IF with else
(IF condition then-expr else-expr)
(SET temp 35)
(IF (>= temp 30) (LOG "Hot!") (LOG "OK"))
COND (Multi-way branch)
(COND
((== state 0) (LOG "IDLE"))
((== state 1) (LOG "RUNNING"))
((== state 2) (LOG "STOPPED")))
The first matching condition is executed. If no condition matches, COND returns 0 (Steele & Sussman, 1978).
WHILE
Loop with a condition:
(SET x 0)
(WHILE (< x 10)
(DO
(LOG x)
(SET x (+ x 1))))
FOR
Counted loop; the stop value is exclusive (works upward or downward):
; Log numbers 0 through 5
(FOR i 0 5 (LOG i)) ; 0, 1, 2, 3, 4
; Nested loops
(FOR i 1 3
(FOR j 1 3
(LOG i " x " j " = " (* i j))))
DO
Execute multiple expressions in sequence:
(DO
(SET x 1)
(SET y 2)
(LOG (+ x y)))
BREAK and CONTINUE
Control loop iteration:
(FOR i 0 10
(DO
(IF (== (% i 2) 0) (CONTINUE)) ; skip even numbers
(LOG i)))
Functions: DEFUN
Define reusable functions:
; Simple function
(DEFUN DOUBLE (x) (* x 2))
(DOUBLE 5) ; 10
; Function with multiple parameters
(DEFUN ADD (a b) (+ a b))
(ADD 3 4) ; 7
; Recursive function
(DEFUN FIB (n)
(IF (<= n 2) n
(+ (FIB (- n 1)) (FIB (- n 2)))))
(FIB 10) ; 55
Functions support lexical scoping: parameters are saved before the call and restored after (Abelson et al., 1996). This enables recursion without corrupting the global environment:
Hardware Control Operators
The following operators are no-ops in headless mode but control physical hardware when running on real devices:
| Operator | Description | Example |
|---|---|---|
WRITE | Digital/Analog write | (WRITE D 13 1) |
READ | Digital/Analog read | (READ A 0) |
PIN-MODE | Configure pin direction | (PIN-MODE D 13 1) |
TONE | Play a tone | (TONE D 8 1000 200) |
DELAY | Pause execution (ms) | (DELAY 500) |
MILLIS | Current time in ms | (MILLIS) |
LM35 | Read LM35 temperature sensor | (LM35) |
ULTRASONIC | Read ultrasonic distance | (ULTRASONIC) |
I2C and Peripheral Operators
| Operator | Description | Example |
|---|---|---|
I2C-WRITE | Write to I2C device | (I2C-WRITE 0x68 data) |
I2C-READ | Read from I2C device | (I2C-READ 0x68) |
LCD-INIT | Initialize LCD | (LCD-INIT) |
LCD-CLEAR | Clear LCD | (LCD-CLEAR) |
LCD-PRINT | Print to LCD | (LCD-PRINT "Hello") |
SERVO | Set servo angle | (SERVO 9 90) |
NEO-SET | Set NeoPixel color | (NEO-SET 0 255 0 0) |
NEO-SHOW | Update NeoPixels | (NEO-SHOW) |
SD-WRITE | Write to SD card | (SD-WRITE "data.csv" line) |
DLY-US | Microsecond delay | (DLY-US 100) |
Event Operators
| Operator | Description | Example |
|---|---|---|
ON-EVENT | Register digital event handler | (ON-EVENT D 2 3 handler) |
ON-CHANGE | Register analog change handler | (ON-CHANGE A 0 512 handler) |
DHT-READ | Read DHT sensor | (DHT-READ 4) |
RTC-READ | Read RTC | (RTC-READ) |
References
Abelson, H., Sussman, G. J., & Sussman, J. (1996). Structure and interpretation of computer programs (2nd ed.). MIT Press.
Arduino Foundation. (2023). Arduino language reference: map(). https://www.arduino.cc/reference/en/language/functions/math/map/
Hennessy, J. L., & Patterson, D. A. (2017). Computer architecture: A quantitative approach (6th ed.). Morgan Kaufmann.
Kernighan, B. W., & Ritchie, D. M. (1988). The C programming language (2nd ed.). Prentice Hall.
McCarthy, J. (1960). Recursive functions of symbolic expressions and their computation by machine, Part I. Communications of the ACM, 3(4), 184–195.
Rashwan, S. (2026). HIL: A LISP-based hardware-in-the-loop operating system. GitHub Repository. https://github.com/SRashwan/hilp
Steele, G. L., & Sussman, G. J. (1978). The revised report on Scheme: A dialect of LISP. AI Memo 452, MIT Artificial Intelligence Laboratory.
Stallings, W. (2020). Computer organization and architecture (11th ed.). Pearson.
Sussman, G. J., & Steele, G. L. (1975). Scheme: An interpreter for extended lambda calculus. AI Memo 349, MIT Artificial Intelligence Laboratory.