Value method
Console program102 lines of .st154 lines of generated Cbuilds as C and Rust
Score -7, best 13
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "2.2.0"
}
}
}main.st
ability "value" "Best" {
type = Int
initial = 0
}
data "value" "Best" {
of = ability.value.Best
}
ability "interface" "Adder" {
methods = [{ name = "add", params = [{ name = "by", type = Int }] }]
}
ability "class" "Score" {
fields = [{ name = "points", type = Int }]
methods = [ability.function.score_add]
implements = [ability.interface.Adder]
}
ability "function" "score_add" {
params = [{ name = "by", type = Int }]
body = [ability.set.AddPoints]
}
ability "set" "AddPoints" {
target = self.points
value = self.points + by
}
ability "function" "add_ten" {
params = [{ name = "a", type = Adder }]
body = [ability.call.AddTen]
}
ability "call" "AddTen" {
function = a.add
args = [10]
}
ability "value" "s" {
type = Score
initial = {
points = 0
}
}
data "value" "s" {
of = ability.value.s
}
ability "call" "AddThree" {
function = ability.value.s.add
args = [3]
}
ability "set" "BestAfterThree" {
target = ability.value.Best
value = max(data.value.Best.current, data.value.s.current.points)
depends_on = [ability.call.AddThree]
}
ability "call" "AddViaInterface" {
function = ability.function.add_ten
args = [ability.value.s]
depends_on = [ability.set.BestAfterThree]
}
ability "set" "BestAfterTen" {
target = ability.value.Best
value = max(data.value.Best.current, data.value.s.current.points)
depends_on = [ability.call.AddViaInterface]
}
ability "call" "Lose" {
function = ability.value.s.add
args = [-20]
depends_on = [ability.set.BestAfterTen]
}
ability "set" "BestAfterLoss" {
target = ability.value.Best
value = max(data.value.Best.current, data.value.s.current.points)
depends_on = [ability.call.Lose]
}
ability "print" "Show" {
message = "Score ${data.value.s.current.points}, best ${data.value.Best.current}"
depends_on = [ability.set.BestAfterLoss]
}Input, plan and generated C#
structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.Best
+ ability.interface.Adder
+ ability.class.Score
+ ability.function.score_add
+ ability.set.AddPoints
+ ability.function.add_ten
+ ability.call.AddTen
+ ability.value.s
+ ability.call.AddThree
+ ability.set.BestAfterThree
+ ability.call.AddViaInterface
+ ability.set.BestAfterTen
+ ability.call.Lose
+ ability.set.BestAfterLoss
+ ability.print.Show
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 value-method # a folder holding the .st files above
structop init
structop plan
structop apply