By convention, LF programs are put in a src directory. This src directory
initially contains one program, HelloWorld.lf, which demonstrates:
-
Reactors - Modular components that encapsulate behavior
Greeter: Generates periodic greetingsCounter: Receives and counts messagesHelloWorld: Main reactor that composes the system
-
Timers - Periodic and one-shot events
- Startup reaction (executes once at program start)
- Periodic timer (
500 msecintervals) - Shutdown reaction (executes once at program end)
-
Ports - Communication between reactors
output greeting: char*- Output port in Greeterinput message: char*- Input port in Counter- Connection:
greeter.greeting -> counter.message
-
State Variables - Persistent data across reactions
state count: int = 0- Tracks greeting countstate total: int = 0- Tracks received messages
-
Reactions - Event-driven code blocks
- Triggered by:
startup,timer,input,shutdown - C code embedded between
{= =}delimiters
- Triggered by:
-
Timeout - Program execution duration
timeout: 2 sec- Program runs for 2 seconds
You need the Lingua Franca compiler (lfc) for command-line compilation
and/or the Visual Studio Code extension for compiling and running within
VSCode or Cursor.
See https://www.lf-lang.org/ for installation instructions.
lfc src/HelloWorld.lfThis will:
- Generate C code in
src-gen/HelloWorld/ - Compile the generated code
- Create an executable in
bin/HelloWorld
./bin/HelloWorldIn the icon bar, click on the "run" triangle. Or from the command palette (Command-Shift P), select "Lingua Franca: Build and Run".
% bin/HelloWorld
---- System clock resolution: 1000 nsec
---- Start execution on Tue Jan 13 10:09:58 2026 ---- plus 977859000 nanoseconds
=== Greeter started ===
╔════════════════════════════════════════════╗
║ Lingua Franca Hello World Demo ║
║ Demonstrating reactive programming ║
╚════════════════════════════════════════════╝
[Message #1 at 0 ms] Hello, World! Welcome to Lingua Franca!
[Message #2 at 500 ms] First periodic greeting!
[Message #3 at 1000 ms] Second periodic greeting!
[Message #4 at 1500 ms] Greetings from Lingua Franca!
[Message #5 at 2000 ms] Greetings from Lingua Franca!
=== Greeter shutting down after 4 greetings ===
=== Counter received 5 messages total ===
---- Elapsed logical time (in nsec): 2,000,000,000
---- Elapsed physical time (in nsec): 2,003,074,000
Lingua Franca uses logical time - a deterministic notion of time that ensures reproducible execution.
Unless the target property fast is set to true, events are handled only when physical time advances to their logical time, giving real-time behavior.
The output is data deterministic. Running the program multiple times will produce identical results with the same logical timestamps.
Connections between reactors (like greeter.greeting -> counter.message) have zero logical delay by default, meaning the downstream reaction executes at the same logical time.
- Try modifying the timer period
- Add more reactors to the composition
- Experiment with logical actions and delays using
after - Explore physical actions for non-deterministic inputs
- Try changing the program to a federated one
For many more examples, see the src directories within the ../context directory.