Operators
HIL LISP provides a comprehensive set of operators organized into categories. All operators follow prefix notation—operators precede their operands—which is the standard S-expression convention established by McCarthy (1960).
Operator Reference Table
| Category | Operator | Syntax | Description | Example |
|---|---|---|---|---|
| Arithmetic | + | (+ a b) | Addition | (+ 10 20) → 30 |
- | (- a b) | Subtraction | (- 10 3) → 7 | |
- | (- a) | Unary negate | (- 5) → -5 | |
* | (* a b) | Multiplication | (* 6 7) → 42 | |
/ | (/ a b) | Integer division | (/ 10 3) → 3 | |
% | (% a b) | Modulo | (% 10 3) → 1 | |
| Comparison | == | (== a b) | Equal | (== 5 5) → 1 |
!= | (!= a b) | Not equal | (!= 5 3) → 1 | |
> | (> a b) | Greater than | (> 10 5) → 1 | |
< | (< a b) | Less than | (< 5 10) → 1 | |
>= | (>= a b) | Greater or equal | (>= 5 5) → 1 | |
<= | (<= a b) | Less or equal | (<= 5 5) → 1 | |
| Boolean | AND | (AND a b) | Logical AND | (AND 1 1) → 1 |
OR | (OR a b) | Logical OR | (OR 0 1) → 1 | |
NOT | (NOT a) | Logical NOT | (NOT 0) → 1 | |
| Math | MIN | (MIN a b) | Minimum | (MIN 3 7) → 3 |
MAX | (MAX a b) | Maximum | (MAX 3 7) → 7 | |
ABS | (ABS a) | Absolute value | (ABS -42) → 42 | |
| Mapping | MAP | (MAP v inMin inMax outMin outMax) | Linear interpolation | (MAP 512 0 1023 0 255) → 128 |
CONSTRAIN | (CONSTRAIN v min max) | Clamp to range | (CONSTRAIN 300 0 255) → 255 | |
RANDOM | (RANDOM min max) | Random integer | (RANDOM 1 6) → 1–6 | |
| I/O | LOG | (LOG val...) | Print to console | (LOG "x=" x) |
WRITE | (WRITE TYPE pin val) | Digital/Analog write | (WRITE D 13 1) | |
READ | (READ TYPE pin) | Digital/Analog read | (READ A 0) | |
| Hardware | PIN-MODE | (PIN-MODE TYPE pin mode) | Configure pin | (PIN-MODE D 13 1) |
TONE | (TONE pin freq duration) | Play tone | (TONE D 8 1000 200) | |
DELAY | (DELAY ms) | Pause execution | (DELAY 500) | |
MILLIS | (MILLIS) | Elapsed ms | (MILLIS) |
Arithmetic in Detail
Integer Division
All division in HIL LISP is integer division. The result is truncated toward zero:
(/ 7 2) ; 3 (not 3.5)
(/ -7 2) ; -3 (truncates toward zero)
(/ 10 0) ; 0 (safe: no crash on division by zero)
This behavior mirrors C's integer division and is standard in embedded systems where floating-point units are often unavailable (Hennessy & Patterson, 2017).
Operator Precedence
HIL LISP does not have operator precedence in the traditional sense. Evaluation follows left-to-right order within each list:
; In standard math: 2 + 3 * 4 = 14
; In HIL LISP: (+ 2 (* 3 4)) = 14 (explicit nesting)
; In HIL LISP: (* (+ 2 3) 4) = 20 (different grouping)
The parenthesized S-expression format makes precedence explicit—there is no ambiguity (McCarthy, 1960).
MAP: Linear Interpolation
The MAP operator is one of the most useful functions for sensor calibration and PWM control. It linearly maps a value from one range to another:
Formula:
output = outMin + (value - inMin) × (outMax - outMin) / (inMax - inMin)
Examples:
; Map analog sensor (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)
; Map distance (0-200cm) to servo angle (0-180)
(MAP distance 0 200 0 180)
CONSTRAIN: Value Clamping
The CONSTRAIN operator ensures a value stays within a valid range. It is commonly combined with MAP to prevent out-of-range outputs:
; Map temperature to fan speed, clamped to 0-255
(CONSTRAIN (MAP temp 20 40 0 255) 0 255)
RANDOM: Random Number Generation
The RANDOM operator returns a uniformly distributed random integer in the closed interval [min, max]:
(RANDOM 1 6) ; Simulates a die roll (1 to 6)
(RANDOM 0 255) ; Random PWM value
(RANDOM 1000 2000) ; Random delay between 1-2 seconds
References
Hennessy, J. L., & Patterson, D. A. (2017). Computer architecture: A quantitative approach (6th ed.). Morgan Kaufmann.
McCarthy, J. (1960). Recursive functions of symbolic expressions and their computation by machine, Part I. Communications of the ACM, 3(4), 184–195.