Lib libc
Console program90 lines of .st191 lines of generated C
sorted: 1 3 5 7 9 abs: 42 atoi: 123 STRUCTOP_SEED: 1
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
standard = "c11"
}
}
lib_provider {
C_lang = {
source = "StructOp/c"
version = "@latest"
}
Libc = {
source = "C/libc"
version = "@latest"
}
}
}main.st
# The C standard library through the provider structop bind writes: qsort calls back
# without user data (the state goes through a static pointer), abs, atoi and getenv.
ability "value" "nums" {
type = List
of = Int32
size = 8
initial = [5, 3, 9, 1, 7]
}
data "value" "nums" {
of = ability.value.nums
}
ability "value" "n" {
type = Int32
}
data "value" "n" {
of = ability.value.n
}
ability "value" "seed" {
type = String
size = 16
}
data "value" "seed" {
of = ability.value.seed
}
ability "qsort" "Sort" {
base = ability.value.nums
compar = []
compar_result = data.qsort_compar.Cmp.arg1 - data.qsort_compar.Cmp.arg2
}
data "qsort_compar" "Cmp" {
of = ability.qsort.Sort
}
ability "print" "Sorted" {
message = "sorted: ${data.value.nums.current[0]} ${data.value.nums.current[1]} ${data.value.nums.current[2]} ${data.value.nums.current[3]} ${data.value.nums.current[4]}"
}
ability "abs" "Abs" {
value = -42
result = ability.value.n
}
ability "print" "ShowAbs" {
message = "abs: ${data.value.n.current}"
}
ability "atoi" "Atoi" {
text = "123"
result = ability.value.n
}
ability "print" "ShowAtoi" {
message = "atoi: ${data.value.n.current}"
}
ability "getenv" "Seed" {
name = "STRUCTOP_SEED"
result = ability.value.seed
}
ability "print" "ShowSeed" {
message = "STRUCTOP_SEED: ${data.value.seed.current}"
}Input, plan and generated C#
bind/libc.bind.st · hints for structop bind
bind/libc.bind.st · hints for structop bind
# The C standard library: macOS and Linux spell these parameters differently (or not at all),
# so names gives every kept function the same attribute names on both.
bind "libc" {
header = "stdlib.h"
version = "1.0.0"
prefix = ["qsort", "abs", "atoi", "getenv"]
skip = ["qsort_r", "qsort_b", "getenv_r"]
}
function "qsort" {
names = ["base", "count", "width", "compar"]
array = [{ data = "base", count = "count", size = "width", of = Int32 }]
points_to = Int32
}
function "abs" {
names = ["value"]
}
function "atoi" {
names = ["text"]
}
function "getenv" {
names = ["name"]
returns_text = "library"
}structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.nums
+ ability.value.n
+ ability.value.seed
+ ability.qsort.Sort
+ ability.print.Sorted
+ ability.abs.Abs
+ ability.print.ShowAbs
+ ability.atoi.Atoi
+ ability.print.ShowAtoi
+ ability.getenv.Seed
+ ability.print.ShowSeed
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
structop bind bind/libc.bind.st # from the repository root
cd lib-libc # a folder holding the .st files above
structop init
structop plan
structop apply