Struct array
Console program125 lines of .st202 lines of generated Cbuilds as C and Rust
Goblin on level 0: true; Ogre false Goblin on level 1: false; Ogre true Goblin on level 2: true; Ogre true Ogre glyphs oO radius 2, seen true false true true of 8
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "@latest"
}
}
}main.st
# Arrays inside structs and components
ability "struct" "Template" {
fields = [
{ name = "name", type = String },
{ name = "levels", type = Array, of = Bool, size = 3 },
{ name = "glyphs", type = Array, of = Char, size = 2 },
]
}
ability "component" "View" {
fields = [
{ name = "radius", type = Int },
{ name = "seen", type = Array, of = Bool, size = 8 },
]
}
ability "function" "mark" {
params = [{ name = "t", type = Pointer, to = Template }, { name = "level", type = Int }]
body = [ability.set.Mark]
}
ability "set" "Mark" {
target = t.levels[level]
value = true
}
ability "call" "MarkGoblin" {
function = ability.function.mark
args = [address_of(ability.value.templates[0]), 2]
}
ability "for" "Levels" {
counter = ability.value.i
from = 0
while = data.value.i.current < length(data.value.templates.current[0].levels)
next = data.value.i.current + 1
body = [ability.print.Level]
depends_on = [ability.call.MarkGoblin]
}
ability "print" "Level" {
message = "${data.value.templates.current[0].name} on level ${data.value.i.current}: ${data.value.templates.current[0].levels[data.value.i.current]}; ${data.value.templates.current[1].name} ${data.value.templates.current[1].levels[data.value.i.current]}"
}
ability "set" "Glyph" {
target = ability.value.templates[1].glyphs[1]
value = "O"
depends_on = [ability.for.Levels]
}
ability "print" "Glyphs" {
message = "${data.value.templates.current[1].name} glyphs ${data.value.templates.current[1].glyphs[0]}${data.value.templates.current[1].glyphs[1]}"
depends_on = [ability.set.Glyph]
}
ability "function" "look" {
body = [ability.spawn.Viewer, ability.query.Views]
}
ability "spawn" "Viewer" {
components = {
View = {
radius = 2
seen = [true, false, true]
}
}
}
ability "query" "Views" {
with = [View]
bind = {
view = View
}
body = [ability.set.Seen, ability.print.Seen]
}
ability "set" "Seen" {
target = view.seen[7]
value = true
}
ability "print" "Seen" {
message = "radius ${view.radius}, seen ${view.seen[0]} ${view.seen[1]} ${view.seen[2]} ${view.seen[7]} of ${length(view.seen)}"
}
ability "call" "Look" {
function = ability.function.look
depends_on = [ability.print.Glyphs]
}vars.st
ability "value" "templates" {
type = Array
of = Template
size = 2
initial = [
{ name = "Goblin", levels = [true] },
{ name = "Ogre", levels = [false, true, true], glyphs = ["o", "o"] },
]
}
data "value" "templates" {
of = ability.value.templates
}
ability "value" "i" {
type = Int
initial = 0
}
data "value" "i" {
of = ability.value.i
}Input, plan and generated C#
structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.struct.Template
+ ability.component.View
+ ability.function.mark
+ ability.set.Mark
+ ability.call.MarkGoblin
+ ability.for.Levels
+ ability.print.Level
+ ability.set.Glyph
+ ability.print.Glyphs
+ ability.function.look
+ ability.spawn.Viewer
+ ability.query.Views
+ ability.set.Seen
+ ability.print.Seen
+ ability.call.Look
+ ability.value.templates
+ ability.value.i
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.c
To inspect this generated file, select C in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runGenerated main.rs (the same .st built as Rust)
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
cd struct-array # a folder holding the .st files above
structop init
structop plan
structop apply