2.2 KiB
2.2 KiB
Getting Started
This guide covers the basics of writing your first Slang program.
Program Structure
A Slang program consists of top-level declarations and a main loop:
// Device declarations
device self = "db";
device sensor = "d0";
// Constants
const THRESHOLD = 100;
// Variables
let counter = 0;
// Main program loop
loop {
yield();
// Your logic here
}
The yield() Function
IC10 programs run continuously. The yield() function pauses execution for one
game tick, preventing the script from consuming excessive resources.
Important: You should always include yield() in your main loop unless you
know what you're doing.
loop {
yield(); // Recommended!
// ...
}
Your First Program
Here's a simple program that turns on a light when a gas sensor detects low pressure:
device gasSensor = "d0";
device light = "d1";
const LOW_PRESSURE = 50;
loop {
yield();
light.On = gasSensor.Pressure < LOW_PRESSURE;
}
Explanation
device gasSensor = "d0"— Binds the device at portd0to the namegasSensordevice light = "d1"— Binds the device at portd1to the namelightconst LOW_PRESSURE = 50— Defines a compile-time constantloop { ... }— Creates an infinite loopyield()— Pauses for one ticklight.On = gasSensor.Pressure < LOW_PRESSURE— Reads the pressure and sets the light state
Comments
Slang supports single-line comments and documentation comments:
// This is a regular comment
/// This is a documentation comment
/// It can span multiple lines
fn myFunction() {
// ...
}
See Also
- Language Reference — Complete syntax guide
- Built-in Functions — Available system calls
- Examples — Real-world programs and patterns