Sum and diff
Console program52 lines of .st131 lines of generated C
The sum of 5 and 2 is 7 The difference of 5 and 2 is 3
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "@latest"
}
}
}main.st
ability "function" "sum_and_diff" {
returns = Int
params = [{ name = "a", type = Int }, { name = "b", type = Int }, { name = "res", type = Pointer, to = Int }]
locals = [{ name = "sum", type = Int }]
body = [ability.set.Sum, ability.store.Diff]
return = sum
}
ability "set" "Sum" {
target = sum
value = a + b
}
ability "store" "Diff" {
pointer = res
value = a - b
}
ability "print" "Sum" {
message = "The sum of 5 and ${var.b} is ${sum_and_diff(5, var.b, address_of(ability.value.diff))}"
}
ability "print" "Diff" {
message = "The difference of 5 and ${var.b} is ${data.value.diff.current}"
depends_on = [ability.print.Sum]
}vars.st
variable "b" {
type = Int
default = 2
}
ability "value" "diff" {
type = Int
}
data "value" "diff" {
of = ability.value.diff
}Input, plan and generated C#
structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.function.sum_and_diff
+ ability.set.Sum
+ ability.store.Diff
+ ability.print.Sum
+ ability.print.Diff
+ ability.value.diff
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-runTry it
cd c-sum-and-diff # a folder holding the .st files above
structop init
structop plan
structop apply