Guestlist enum
Console program267 lines of .st219 lines of generated Rust
Hello, what's your name? (Leave empty and press ENTER to quit)
Welcome to the treehouse, bert
Hello, what's your name? (Leave empty and press ENTER to quit)
Welcome to the treehouse, steve
Lactose-free milk is in the fridge
Do not serve alcohol to steve
Hello, what's your name? (Leave empty and press ENTER to quit)
Do not allow fred in!
Hello, what's your name? (Leave empty and press ENTER to quit)
joebob 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",
action: Accept,
age: 45,
},
Visitor {
name: "steve",
action: AcceptWithNote {
note: "Lactose-free milk is in the fridge",
},
age: 15,
},
Visitor {
name: "fred",
action: Refuse,
age: 30,
},
Visitor {
name: "joebob",
action: Probation,
age: 0,
},
]On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
}main.st
# Rust's AcceptWithNote { note } carries data; here the note is a Visitor field
# that only AcceptWithNote uses.
ability "enum" "VisitorAction" {
values = [Accept, AcceptWithNote, Refuse, Probation]
}
ability "struct" "Visitor" {
fields = [
{ name = "name", type = String },
{ name = "action", type = VisitorAction },
{ name = "note", type = String },
{ name = "age", type = Int },
]
}
ability "function" "greet_visitor" {
params = [{ name = "visitor", type = Pointer, to = Visitor }]
body = [ability.switch.Action]
}
ability "switch" "Action" {
value = visitor.action
cases = [
{ value = VisitorAction.Accept, run = [ability.print.Welcome] },
{ value = VisitorAction.AcceptWithNote, run = [ability.print.WelcomeNote, ability.print.Note, ability.if.Young] },
{ value = VisitorAction.Probation, run = [ability.print.Probation] },
{ value = VisitorAction.Refuse, run = [ability.print.Refuse] },
]
}
ability "print" "Welcome" {
message = "Welcome to the treehouse, ${visitor.name}"
}
ability "print" "WelcomeNote" {
message = "Welcome to the treehouse, ${visitor.name}"
}
ability "print" "Note" {
message = "${visitor.note}"
}
ability "if" "Young" {
condition = visitor.age < 21
then = [ability.print.NoAlcohol]
}
ability "print" "NoAlcohol" {
message = "Do not serve alcohol to ${visitor.name}"
}
ability "print" "Probation" {
message = "${visitor.name} is now a probationary member"
}
ability "print" "Refuse" {
message = "Do not allow ${visitor.name} in!"
}
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
action = VisitorAction.Probation
age = 0
}
}
# 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.Head, ability.if.Noted, ability.print.Tail]
depends_on = [ability.print.Final]
}
ability "print" "Head" {
message = " Visitor {\n name: \"${data.value.visitor_list.current[data.value.i.current].name}\","
}
ability "if" "Noted" {
condition = data.value.visitor_list.current[data.value.i.current].action == VisitorAction.AcceptWithNote
then = [ability.print.WithNote]
else = [ability.print.Plain]
}
ability "print" "WithNote" {
message = " action: AcceptWithNote {\n note: \"${data.value.visitor_list.current[data.value.i.current].note}\",\n },"
}
ability "print" "Plain" {
message = " action: ${data.value.visitor_list.current[data.value.i.current].action},"
}
ability "print" "Tail" {
message = " age: ${data.value.visitor_list.current[data.value.i.current].age},\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", action = VisitorAction.Accept, age = 45 },
{ name = "steve", action = VisitorAction.AcceptWithNote, note = "Lactose-free milk is in the fridge", age = 15 },
{ name = "fred", action = VisitorAction.Refuse, age = 30 },
]
}
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
}tests/bouncer.sttest
run "known_visitor_is_welcomed" {
input = "bert\n\n"
expect {
output_contains = "Welcome to the treehouse, bert\n"
}
}
run "young_visitor_gets_the_note_and_no_alcohol" {
input = "Steve\n\n"
expect {
output_contains = "Lactose-free milk is in the fridge\nDo not serve alcohol to steve\n"
}
}
run "new_visitor_joins_on_probation" {
input = "joebob\n\n"
expect {
output_contains = "joebob is not on the visitor list.\n"
}
}
run "new_visitor_is_listed_at_the_end" {
input = "joebob\n\n"
expect {
output_contains = " name: \"joebob\",\n action: Probation,\n age: 0,\n },\n]\n"
}
}Input, plan and generated Rust#
input.txt · typed input (5 lines)
input.txt · typed input (5 lines)
bert
steve
fred
joebobstructop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.enum.VisitorAction
+ ability.struct.Visitor
+ ability.function.greet_visitor
+ ability.switch.Action
+ ability.print.Welcome
+ ability.print.WelcomeNote
+ ability.print.Note
+ ability.if.Young
+ ability.print.NoAlcohol
+ ability.print.Probation
+ ability.print.Refuse
+ 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.Head
+ ability.if.Noted
+ ability.print.WithNote
+ ability.print.Plain
+ ability.print.Tail
+ 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-enum # a folder holding the .st files above
structop init
structop plan
structop apply < input.txt