Guestlist struct
Console program122 lines of .st142 lines of generated Rust
Hello, what's your name? Hello Bert, enjoy your treehouse.
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
}main.st
# visitor_list.iter().find(...) becomes a loop that keeps the index; -1 is None.
ability "struct" "Visitor" {
fields = [{ name = "name", type = String }, { name = "greeting", type = String }]
}
ability "function" "greet_visitor" {
params = [{ name = "visitor", type = Pointer, to = Visitor }]
body = [ability.print.Greeting]
}
ability "print" "Greeting" {
message = "${visitor.greeting}"
}
ability "function" "what_is_your_name" {
returns = String
locals = [{ name = "your_name", type = String, size = 64 }]
body = [ability.read_line.Line, ability.trim.Ends, ability.lower.Case]
return = your_name
}
ability "read_line" "Line" {
target = your_name
}
ability "trim" "Ends" {
target = your_name
}
ability "lower" "Case" {
target = your_name
}
ability "print" "Question" {
message = "Hello, what's your name?"
}
ability "call" "GetName" {
function = ability.function.what_is_your_name
result = ability.value.name
depends_on = [ability.print.Question]
}
ability "for" "Find" {
counter = ability.value.i
from = 0
while = data.value.i.current < length(data.value.visitor_list.current)
next = data.value.i.current + 1
body = [ability.if.Same]
depends_on = [ability.call.GetName]
}
ability "if" "Same" {
condition = data.value.visitor_list.current[data.value.i.current].name == data.value.name.current
then = [ability.set.Found]
}
ability "set" "Found" {
target = ability.value.known_visitor
value = data.value.i.current
}
ability "if" "Match" {
condition = data.value.known_visitor.current >= 0
then = [ability.call.Greet]
else = [ability.print.Leave]
depends_on = [ability.for.Find]
}
ability "call" "Greet" {
function = ability.function.greet_visitor
args = [address_of(ability.value.visitor_list[data.value.known_visitor.current])]
}
ability "print" "Leave" {
message = "You are not on the visitor list. Please leave."
}vars.st
ability "value" "visitor_list" {
type = Array
of = Visitor
size = 3
initial = [
{ name = "bert", greeting = "Hello Bert, enjoy your treehouse." },
{ name = "steve", greeting = "Hi Steve. Your milk is in the fridge." },
{ name = "fred", greeting = "Wow, who invited Fred?" },
]
}
data "value" "visitor_list" {
of = ability.value.visitor_list
}
ability "value" "name" {
type = String
size = 64
}
data "value" "name" {
of = ability.value.name
}
ability "value" "known_visitor" {
type = Int
initial = -1
}
data "value" "known_visitor" {
of = ability.value.known_visitor
}
ability "value" "i" {
type = Int
initial = 0
}
data "value" "i" {
of = ability.value.i
}Input, plan and generated Rust#
input.txt · typed input (1 lines)
input.txt · typed input (1 lines)
Bertstructop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.struct.Visitor
+ ability.function.greet_visitor
+ ability.print.Greeting
+ ability.function.what_is_your_name
+ ability.read_line.Line
+ ability.trim.Ends
+ ability.lower.Case
+ ability.print.Question
+ ability.call.GetName
+ ability.for.Find
+ ability.if.Same
+ ability.set.Found
+ ability.if.Match
+ ability.call.Greet
+ ability.print.Leave
+ ability.value.visitor_list
+ ability.value.name
+ ability.value.known_visitor
+ ability.value.i
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
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 rust-guestlist-struct # a folder holding the .st files above
structop init
structop plan
structop apply < input.txt