In episode 1 we built a program that asks for your name and says hello. This time we grow it into a guest list, with types, a list, a loop, a function and a switch.
Everything below follows the guided tour in the published StructOp handbook, and every terminal block is a real StructOp 0.1.0 session.
Four files, one program
The project has four files. intent.st is the same as last time and picks C as the build target:
structop { type_app { exec = { lang = "C" } } } StructOp reads every .st file in the folder together, in name order (files whose names begin with a dot are ignored), so you can split a program however you like. Here, types go in types.st, data in vars.st and behaviour in main.st.
Step 1: types
Types are abilities too. enum and struct come from the portable core provider; once declared, their names work anywhere a type is expected.
ability "enum" "Mood" { values = [Happy, Sleepy] } ability "struct" "Guest" { fields = [ { name = "name", type = String }, { name = "age", type = Int }, { name = "mood", type = Mood }, ] } Step 2: data
The guests live in a value whose type is a List of Guest. A List is growable, up to a fixed capacity (size). Its starting contents are written as struct values, and structop validate checks every field against the struct.
ability "value" "guests" { type = List of = Guest size = 8 initial = [ { name = "Ada", age = 36, mood = Mood.Happy }, { name = "Linus", age = 12, mood = Mood.Sleepy }, ] } data "value" "guests" { of = ability.value.guests } ability "value" "i" { type = Int initial = 0 } data "value" "i" { of = ability.value.i } $ structop validate Success! The configuration is valid. The rule from episode 1 still holds: data that changes lives in a value. Whatever changes it names the value (list = ability.value.guests); everything else reads it through its data block, as data.value.guests.current. Enum values are written with their type: Mood.Happy.
Step 3: behaviour
First, an add ability puts a newcomer into the list:
ability "add" "Newcomer" { list = ability.value.guests value = { name = "Grace", age = 45, mood = Mood.Happy } } Then a for loop walks the list. Its counter is the value i; it starts from 0, runs while i is less than the list's length, and adds one each time. Its body lists what runs on every pass, and depends_on makes it wait for Newcomer, so Grace is in the list before anyone is greeted.
ability "for" "Each" { counter = ability.value.i from = 0 while = data.value.i.current < length(data.value.guests.current) next = data.value.i.current + 1 body = [ability.call.Greet] depends_on = [ability.add.Newcomer] } The ordering rule: top-level abilities run in the order they are written, and depends_on makes a wait explicit. An ability listed in another's body runs there, inside its owner, and only there.
The loop's body is a call. It passes a pointer to the guest at position i to a function that takes a Pointer to Guest (structs are passed as pointers):
ability "call" "Greet" { function = ability.function.greet args = [address_of(ability.value.guests[data.value.i.current])] } ability "function" "greet" { params = [{ name = "guest", type = Pointer, to = Guest }] body = [ability.switch.Mood] } Inside a function, parameters and locals are bare names, so guest.name reads a field through the pointer. The function's body is a switch on the guest's mood:
ability "switch" "Mood" { value = guest.mood cases = [ { value = Mood.Happy, run = [ability.print.Happy] }, { value = Mood.Sleepy, run = [ability.print.Sleepy] }, ] } ability "print" "Happy" { message = "Welcome, ${guest.name}! (${guest.age})" } ability "print" "Sleepy" { message = "Shh, ${guest.name} is asleep." } Step 4: run it
$ structop plan # program.app will be created + program.app (.structop/build/app) + ability.add.Newcomer + ability.for.Each + ability.call.Greet + ability.function.greet + ability.switch.Mood + ability.print.Happy + ability.print.Sleepy + ability.enum.Mood + ability.struct.Guest + ability.value.guests + ability.value.i Plan: 1 to add, 0 to change, 0 to destroy. $ structop apply ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Welcome, Ada! (36) Shh, Linus is asleep. Welcome, Grace! (45) Three guests, in list order, each greeted by mood.
structop graph -depth=2 opens the frames: the call sits inside the loop, and the switch sits inside the function, with a frame for each case.
Step 5: read the C
$ structop apply -source -no-run No changes. Your application matches the configuration. Exported out/main.c That writes the generated C into an out folder in the project, for you to read on your own machine. Your configuration never contains C: StructOp parses and types every expression, and the provider prints it. That is why the same .st files can also be built for Rust.
Next
We declared types, a list, a loop, a function and a switch, and StructOp checked, ordered and built them as native code. Next episode: plan, state and graph, the lifecycle behind every apply.
StructOp is created and owned by Sibusiso Nxumalo.








