Transpilation to C++
The HIL transpiler converts HIL LISP scripts into Arduino-compatible C++ code for the ATmega328P. This enables standalone execution without the host monitor and supports the CRUMB circuit simulator (Aho et al., 2006).
Transpilation Pipeline
Two-Pass Architecture
Pass 1: Form Scanning
The first pass recursively scans the AST to:
- Register
DEFUNdefinitions for forward declaration - Collect
SETtargets as global variables - Resolve
INCLUDEdirectives with cycle detection
Pass 2: Code Emission
The second pass emits C++ code for each form, producing a complete Arduino sketch:
// Generated C++ output
#include <Arduino.h>
// Global variables
static int x = 0;
static int y = 0;
// Forward declarations
int add(int a, int b);
// Helper functions
// ... (hardware abstraction)
// User functions
int add(int a, int b) {
return a + b;
}
void setup() {
Serial.begin(9600);
}
void loop() {
static bool _hil_ran = false;
if (_hil_ran) return;
_hil_ran = true;
x = 10;
y = 20;
int result = add(x, y);
Serial.println(result);
}
Supported Constructs
| HIL LISP | C++ Output |
|---|---|
(SET x 10) | static int x = 10; |
(IF ...) | if (...) { ... } else { ... } |
(WHILE ...) | while (...) { ... } |
(FOR i 0 5 ...) | for (int i = 0; i <= 5; i++) { ... } |
(DO ...) | { ... } |
(DEFUN F (x) ...) | int F(int x) { ... } |
(WRITE D 13 1) | digitalWrite(13, HIGH); |
(READ A 0) | analogRead(0) |
(LOG x) | Serial.println(x); |
(DELAY 1000) | delay(1000); |
(SERVO 9 90) | myservo.write(90); |
Building
# Transpile only
hil-cli firmware build script.hil --output script.cpp
# Full build (transpile + compile)
hil-cli firmware build script.hil --output script.hex
# Build for CRUMB simulator
hil-cli firmware build script.hil --output script.cru --target crumb
INCLUDE Directive
The transpiler resolves INCLUDE directives by inlining the referenced files:
; Include another HIL file
(INCLUDE "lib/sensors.hil")
(INCLUDE "lib/motors.hil")
; Use included functions
(SET temp (LM35-READ 0))
The transpiler detects circular includes and reports an error.
CRUMB Integration
For the CRUMB circuit simulator, the transpiler wraps the output in a .cru XML file:
<crumb>
<component type="arduino_nano">
<sketch language="cpp">
// Transpiled C++ code here
</sketch>
</component>
</crumb>
Memory Considerations
The ATmega328P has limited resources. The transpiler optimizes for size:
| Resource | Available | Typical Usage |
|---|---|---|
| Flash | 32KB | ~16KB (TinyGo runtime) + user code |
| SRAM | 2KB | Global variables + stack |
| EEPROM | 1KB | Persistent storage |
Keep these limits in mind when transpiling large scripts.
References
Aho, A. V., Lam, M. S., Sethi, R., & Ullman, J. D. (2006). Compilers: Principles, techniques, and tools (2nd ed.). Pearson.