Bytes order
Console program134 lines of .st324 lines of generated Cbuilds as C and Rust
8081 37151 -2 -257 16909060 67305985 33685502 -16842494 -3 -144115188075855873 72623859790382856 578437695752307201 24 31 145 8081
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 "value" "buf" {
type = Bytes
size = 24
}
data "value" "buf" {
of = ability.value.buf
}
ability "put_number" "Port" {
target = ability.value.buf
at = 0
value = uint16(0x1F90)
order = "big"
}
ability "put_number" "Count" {
target = ability.value.buf
at = 2
value = uint32(0x0102_0304)
order = "little"
depends_on = [ability.put_number.Port]
}
ability "put_number" "Offset" {
target = ability.value.buf
at = uint8(6)
value = int16(-2)
order = "big"
depends_on = [ability.put_number.Count]
}
ability "put_number" "Stamp" {
target = ability.value.buf
at = 8
value = int64(-3)
order = "little"
depends_on = [ability.put_number.Offset]
}
ability "put_number" "Tail" {
target = ability.value.buf
at = var.tail
value = uint64(0x0102_0304_0506_0708)
order = "big"
depends_on = [ability.put_number.Stamp]
}
ability "put_number" "Overwrite" {
target = ability.value.buf
at = var.over
value = uint16(0x1F91)
order = "big"
depends_on = [ability.put_number.Tail]
}
ability "print" "Small" {
message = "${uint16_big(data.value.buf.current, 0)} ${uint16_little(data.value.buf.current, 0)} ${int16_big(data.value.buf.current, 6)} ${int16_little(data.value.buf.current, 6)}"
depends_on = [ability.put_number.Overwrite]
}
ability "print" "Middle" {
message = "${uint32_little(data.value.buf.current, 2)} ${uint32_big(data.value.buf.current, 2)} ${int32_big(data.value.buf.current, 4)} ${int32_little(data.value.buf.current, 4)}"
depends_on = [ability.print.Small]
}
ability "print" "Wide" {
message = "${int64_little(data.value.buf.current, 8)} ${int64_big(data.value.buf.current, 8)} ${uint64_big(data.value.buf.current, size(16))} ${uint64_little(data.value.buf.current, 16)}"
depends_on = [ability.print.Middle]
}
ability "print" "Length" {
message = "${length(data.value.buf.current)} ${data.value.buf.current[0]} ${data.value.buf.current[1]} ${uint16_big(data.value.buf.current, var.probe)}"
depends_on = [ability.print.Wide]
}vars.st
variable "tail" {
type = Int
default = 16
}
variable "probe" {
type = Int
default = 0
}
variable "over" {
type = Int
default = 0
}tests/order.sttest
run "reads past the length" {
variables = { probe = 23 }
expect {
exit_code = 1
output_contains = "uint16_big at 23 needs 2 bytes, ability.value.buf has length 24"
}
}
run "reads before the start" {
variables = { probe = -1 }
expect {
exit_code = 1
output_contains = "uint16_big at -1 needs 2 bytes, ability.value.buf has length 24"
}
}
run "leaves a gap" {
variables = { tail = 17 }
expect {
exit_code = 1
output_contains = "index 17 is outside ability.value.buf (length 16)"
}
}
run "fills the buffer" {
variables = { over = 23 }
expect {
exit_code = 1
output_contains = "bytes ability.value.buf is full (24)"
}
}Input, plan and generated C#
structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.buf
+ ability.put_number.Port
+ ability.put_number.Count
+ ability.put_number.Offset
+ ability.put_number.Stamp
+ ability.put_number.Tail
+ ability.put_number.Overwrite
+ ability.print.Small
+ ability.print.Middle
+ ability.print.Wide
+ ability.print.Length
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 bytes-order # a folder holding the .st files above
structop init
structop plan
structop apply