Guestlist vector
Console program175 lines of .st180 lines of generated Rust
Hello, what's your name? (Leave empty and press ENTER to quit)
Hello Bert. Enjoy your treehouse.
Hello, what's your name? (Leave empty and press ENTER to quit)
Hi Steve. Your milk is in the fridge.
Hello, what's your name? (Leave empty and press ENTER to quit)
Wow, who invited Fred?
Hello, what's your name? (Leave empty and press ENTER to quit)
bob is not on the visitor list.
Hello, what's your name? (Leave empty and press ENTER to quit)
The final list of visitors:
[
Visitor {
name: "bert",
greeting: "Hello Bert. Enjoy your treehouse.",
},
Visitor {
name: "steve",
greeting: "Hi Steve. Your milk is in the fridge.",
},
Visitor {
name: "fred",
greeting: "Wow, who invited Fred?",
},
Visitor {
name: "bob",
greeting: "New friend",
},
]On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
}main.st
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 "loop" "Bouncer" {
condition = true
body = [
ability.print.Question,
ability.call.GetName,
ability.set.Forget,
ability.for.Find,
ability.if.Match,
]
}
ability "print" "Question" {
message = "Hello, what's your name? (Leave empty and press ENTER to quit)"
}
ability "call" "GetName" {
function = ability.function.what_is_your_name
result = ability.value.name
}
ability "set" "Forget" {
target = ability.value.known_visitor
value = -1
}
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]
}
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.if.Empty]
}
ability "call" "Greet" {
function = ability.function.greet_visitor
args = [address_of(ability.value.visitor_list[data.value.known_visitor.current])]
}
ability "if" "Empty" {
condition = data.value.name.current == ""
then = [ability.break.Quit]
else = [ability.print.NotOnList, ability.add.NewFriend]
}
ability "break" "Quit" {
}
ability "print" "NotOnList" {
message = "${data.value.name.current} is not on the visitor list."
}
ability "add" "NewFriend" {
list = ability.value.visitor_list
value = {
name = data.value.name.current
greeting = "New friend"
}
}
# println!("{:#?}", visitor_list), written out field by field
ability "print" "Final" {
message = "The final list of visitors:\n["
depends_on = [ability.loop.Bouncer]
}
ability "for" "Show" {
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.print.Entry]
depends_on = [ability.print.Final]
}
ability "print" "Entry" {
message = " Visitor {\n name: \"${data.value.visitor_list.current[data.value.i.current].name}\",\n greeting: \"${data.value.visitor_list.current[data.value.i.current].greeting}\",\n },"
}
ability "print" "Close" {
message = "]"
depends_on = [ability.for.Show]
}vars.st
ability "value" "visitor_list" {
type = List
of = Visitor
size = 32
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 (5 lines)
input.txt · typed input (5 lines)
bert
steve
fred
bobstructop 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.loop.Bouncer
+ ability.print.Question
+ ability.call.GetName
+ ability.set.Forget
+ ability.for.Find
+ ability.if.Same
+ ability.set.Found
+ ability.if.Match
+ ability.call.Greet
+ ability.if.Empty
+ ability.break.Quit
+ ability.print.NotOnList
+ ability.add.NewFriend
+ ability.print.Final
+ ability.for.Show
+ ability.print.Entry
+ ability.print.Close
+ 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-vector # a folder holding the .st files above
structop init
structop plan
structop apply < input.txt