Class interface
Console program127 lines of .st185 lines of generated Cbuilds as C and Rust
Area: 9.000000 Area: 12.000000 Area: 16.000000
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 "interface" "Shape" {
methods = [
{ name = "area", returns = Float },
{ name = "grow", params = [{ name = "by", type = Float }] },
]
}
ability "class" "Square" {
fields = [{ name = "side", type = Float }]
methods = [ability.function.square_area, ability.function.square_grow, ability.function.square_report]
implements = [ability.interface.Shape]
}
ability "function" "square_area" {
returns = Float
return = self.side * self.side
}
ability "function" "square_grow" {
params = [{ name = "by", type = Float }]
body = [ability.set.GrowSide]
}
ability "set" "GrowSide" {
target = self.side
value = self.side + by
}
ability "function" "square_report" {
body = [ability.call.ReportSelf]
}
ability "call" "ReportSelf" {
function = ability.function.report
args = [self]
}
ability "class" "Rect" {
fields = [{ name = "w", type = Float }, { name = "h", type = Float }]
methods = [ability.function.rect_area, ability.function.rect_grow]
implements = [ability.interface.Shape]
}
ability "function" "rect_area" {
returns = Float
return = self.w * self.h
}
ability "function" "rect_grow" {
params = [{ name = "by", type = Float }]
body = [ability.set.GrowW, ability.set.GrowH]
}
ability "set" "GrowW" {
target = self.w
value = self.w + by
}
ability "set" "GrowH" {
target = self.h
value = self.h + by
}
ability "function" "report" {
params = [{ name = "s", type = Shape }]
locals = [{ name = "a", type = Float }]
body = [ability.call.Grow, ability.call.Area, ability.print.Show]
}
ability "call" "Grow" {
function = s.grow
args = [1.0]
}
ability "call" "Area" {
function = s.area
result = a
}
ability "print" "Show" {
message = "Area: ${a}"
}
ability "value" "sq" {
type = Square
initial = {
side = 2.0
}
}
ability "value" "r" {
type = Rect
initial = {
w = 2.0
h = 3.0
}
}
ability "call" "ReportSquare" {
function = ability.function.report
args = [ability.value.sq]
}
ability "call" "ReportRect" {
function = ability.function.report
args = [ability.value.r]
depends_on = [ability.call.ReportSquare]
}
ability "call" "SquareAgain" {
function = ability.value.sq.report
depends_on = [ability.call.ReportRect]
}Input, plan and generated C#
structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.interface.Shape
+ ability.class.Square
+ ability.function.square_area
+ ability.function.square_grow
+ ability.set.GrowSide
+ ability.function.square_report
+ ability.call.ReportSelf
+ ability.class.Rect
+ ability.function.rect_area
+ ability.function.rect_grow
+ ability.set.GrowW
+ ability.set.GrowH
+ ability.function.report
+ ability.call.Grow
+ ability.call.Area
+ ability.print.Show
+ ability.value.sq
+ ability.value.r
+ ability.call.ReportSquare
+ ability.call.ReportRect
+ ability.call.SquareAgain
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 class-interface # a folder holding the .st files above
structop init
structop plan
structop apply