Verified examples
Read real source and compare recorded output by engine.
These examples were run using the manifest-matched Windows preview.5 CLI. Output is recorded build evidence. The Contract Playground compiles single-file contracts in the browser.
easy
examples/easy/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode
needs console
name = "Zebvix"
task welcome(user: text):
show "Welcome " + user
welcome(name)
total = 0
repeat value in [10, 20, 30]:
total = total + value
show total
task test_total():
assert(1 + 2 * 3 == 7)
Welcome Zebvix
60advanced
examples/advanced/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode
shape Account:
name: text
balance: i64
task multiply(a: i64, b: i64) gives i64:
give a * b
task safe_divide(a: i64, b: i64) gives i64:
attempt:
give a / b
otherwise problem:
show problem.kind
give 0
fixed account = Account("Ada", 120)
items: List[i64] = []
repeat i in 1..4:
items = append(items, multiply(i, 2))
counts: Map[text, i64] = {"items": len(items)}
when account.balance > counts["items"]:
show account.name
otherwise:
show "empty"
show items[2]
show safe_divide(10, 0)
task test_collections():
assert({"a": 3}["a"] == [3][0])
Ada
6
DivisionByZero
0ownership-foundation
examples/ownership-foundation/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode, native
task consume(value: i64) gives i64:
give move_value(value) + 1
fixed original = 40
next = move_value(original)
show consume(move_value(next)) + 1
next = 7
show next
task test_owned_scalar():
value = 41
moved = move_value(value)
assert(consume(move_value(moved)) == 42)
42
7parallel-foundation
examples/parallel-foundation/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode, native
task square(value: i64) gives i64:
give value * value
inputs = [3, 1, 4, 2]
results = parallel_map("square", move_value(inputs), 2)
repeat result in results:
show result
task test_parallel_values():
values = parallel_map("square", [3, 1, 4, 2], 2)
assert(values[0] == 9)
assert(values[1] == 1)
assert(values[2] == 16)
assert(values[3] == 4)
9
1
16
4native-structured
examples/native-structured/src/main.zbl · v0.5.0-preview.5 · interpreter, bytecode, native
shape Row:
label: text
values: List[i64]
choice Answer:
Ready(row: Row)
Missing
task square(x: i64) gives i64:
give x * x
rows: Map[text, List[Row]] = {}
values = parallel_map("square", [3, 2, 4], 2)
rows = put(rows, "numbers", [Row("native", move_value(values))])
result = Answer.Ready(rows["numbers"][0])
choose result:
case Missing:
show "missing"
case Ready(row):
show row.label
repeat value in row.values:
show value
native
9
4
16