A guided tour
One small program, grown a step at a time: a guest list with types, a list, a loop, a function and a switch. Every snippet on this page is a real file; together they build and print the output shown at the end.
On this page
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 },
]
}2. Data#
A List is a growable list with a fixed capacity (size). Its default is written as struct values; 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
}Data that changes lives in a value ability. Abilities that change it name the value (list = ability.value.guests); everything else reads it through its data "value" block, as data.value.guests.current. Enum values are written Mood.Happy.
3. Behaviour#
Abilities in a list (body, run, then) run inside their owner, in dependency order: a member runs after the members it names in depends_on, and members that wait for nothing keep the order they are written in. Abilities at the top level run in the order they are written, with depends_on when one must wait for another.
ability "add" "Newcomer" {
list = ability.value.guests
value = { name = "Grace", age = 45, mood = Mood.Happy }
}
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]
}
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]
}
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."
}Inside a function, parameters and locals are bare names (guest), and guest.name reads a field through the pointer.
4. Run#
Welcome, Ada! (36) Shh, Linus is asleep. Welcome, Grace! (45)
5. Read the C#
Every ability becomes a commented block of ordinary C.
The generated C is yours to read: structop apply -source -no-run writes it out beside the project.