chore: import upstream snapshot with attribution
CI / Deep Native Runtime Cases (1/6) (push) Has been skipped
CI / Native Preflight (push) Failing after 1s
CI / Native Runtime Cases (1/2) (push) Failing after 0s
CI / Native Runtime Cases (2/2) (push) Failing after 1s
CI / Native Metadata Reports (push) Failing after 0s
CI / Native Direct Backend Artifacts (push) Failing after 0s
CI / Native Sanitizer Smoke (push) Failing after 1s
CI / Command Contract Snapshots (push) Failing after 1s
CI / Deep Conformance Suite (push) Has been skipped
CI / Graph Build Perf (push) Failing after 1s
CI / Deep Native Preflight (push) Has been skipped
CI / Deep Native Runtime Cases (2/6) (push) Has been skipped
CI / Deep Native Runtime Cases (3/6) (push) Has been skipped
CI / Conformance Suite (push) Failing after 1s
CI / Workspace Checks (push) Failing after 0s
CI / Deep Native Runtime Cases (5/6) (push) Has been skipped
CI / Deep Native Runtime Cases (6/6) (push) Has been skipped
CI / Deep Native Runtime Cases (4/6) (push) Has been skipped
CI / Deep Graph Build Perf (push) Has been skipped

This commit is contained in:
wehub-resource-sync
2026-07-13 12:29:30 +08:00
commit e7738de6d2
1723 changed files with 216139 additions and 0 deletions
+162
View File
@@ -0,0 +1,162 @@
# Zero Examples
Use these examples as the hands-on path through Zero. Start at the top, run
`bin/zero check` on graph inputs, and open the matching docs article when you
want an explanation. `.0` files beside these examples are human-readable
projections for review; the commands below use `.graph` stores.
## Profile And Size Checks
Use `hello.graph` and `fixed-vec.graph` to compare profile contracts and size metadata:
```sh
bin/zero build --json --profile debug --target linux-musl-x64 examples/hello.graph --out .zero/out/hello-debug
bin/zero build --json --profile fast --target linux-musl-x64 examples/hello.graph --out .zero/out/hello-fast
bin/zero build --json --profile small --target linux-musl-x64 examples/hello.graph --out .zero/out/hello-small
bin/zero size --json --profile tiny --target linux-musl-x64 examples/fixed-vec.graph
```
Build JSON reports `profileSemantics` and `profileBudget`. Size JSON adds `sizeBreakdown`, `retentionReasons`, and `optimizationHints`.
## First Programs
| Example | What it teaches | Try it |
| --- | --- | --- |
| `hello.graph` | `pub fn main`, `World`, `check`, stdout | `bin/zero check examples/hello.graph` |
| `hello-let.graph` | immutable `let` bindings | `bin/zero check examples/hello-let.graph` |
| `add.graph` | helper functions, `return`, `if` / `else` | `bin/zero build --emit exe --target linux-musl-x64 examples/add.graph --out .zero/out/add` |
| `direct-u8-helper-call.graph` | direct backend signed LEB literals, byte arrays, and helper calls | `bin/zero check examples/direct-u8-helper-call.graph` |
| `direct-array-bounds-trap.graph` | direct backend stack-memory bounds traps | `bin/zero check examples/direct-array-bounds-trap.graph` |
| `direct-string-len.graph` | direct backend string literal length for compiler token scans | `bin/zero check examples/direct-string-len.graph` |
| `direct-string-literal.graph` | direct backend readonly string data segments and byte loads | `bin/zero check examples/direct-string-literal.graph` |
| `direct-span-read.graph` | direct backend readonly string slices as byte-span views | `bin/zero check examples/direct-span-read.graph` |
| `direct-string-eql.graph` | direct backend byte-span equality over readonly string views | `bin/zero check examples/direct-string-eql.graph` |
| `direct-byte-view-locals.graph` | direct backend local `String`/`Span<u8>` pointer-length byte views | `bin/zero check examples/direct-byte-view-locals.graph` |
| `direct-mutspan-len.graph` | direct backend local `MutSpan<u8>` pointer-length byte views | `bin/zero check examples/direct-mutspan-len.graph` |
| `direct-byte-copy-fill.graph` | direct backend mutable byte copy/fill over fixed buffers | `bin/zero check examples/direct-byte-copy-fill.graph` |
| `direct-alloc-bump.graph` | direct backend explicit `FixedBufAlloc` bump allocation over caller storage | `bin/zero check examples/direct-alloc-bump.graph` |
| `direct-alloc-overflow.graph` | direct backend fixed-buffer allocation overflow returns `Maybe.none` without hidden heap growth | `bin/zero check examples/direct-alloc-overflow.graph` |
| `direct-token-shape.graph` | direct backend stack layout for type literals, field loads, defaults, and field stores | `bin/zero check examples/direct-token-shape.graph` |
| `direct-enum-match.graph` | direct backend compact enum cases and exhaustive match branches without matcher tables | `bin/zero check examples/direct-enum-match.graph` |
| `direct-raises-basic.graph` | direct backend packed error-result propagation for `raise` and `check` | `bin/zero check examples/direct-raises-basic.graph` |
| `direct-rescue-basic.graph` | direct backend local `rescue` fallback over a raised error | `bin/zero check examples/direct-rescue-basic.graph` |
| `direct-byte-buf.graph` | direct backend monomorphic byte buffer push, length, capacity, and overflow checks | `bin/zero check examples/direct-byte-buf.graph` |
| `direct-generic-identity.graph` | direct backend explicit generic function specialization without runtime metadata | `bin/zero check examples/direct-generic-identity.graph` |
| `direct-generic-fixedbuf.graph` | direct backend generic storage type with concrete type and static value arguments | `bin/zero check examples/direct-generic-fixedbuf.graph` |
| `direct-generic-vec.graph` | direct backend generic fixed-capacity vector layout for byte, token, and AST-node element kinds | `bin/zero check examples/direct-generic-vec.graph` |
| `direct-i64-return.graph` | direct ELF64 object backend support for i64/u64 values | `bin/zero build --emit obj --target linux-musl-x64 examples/direct-i64-return.graph --out .zero/out/direct-i64-return.o` |
| `direct-byte-view-reloc.graph` | direct ELF64 readonly byte-view relocations for string-backed span locals | `bin/zero build --emit obj --target linux-musl-x64 examples/direct-byte-view-reloc.graph --out .zero/out/direct-byte-view-reloc.o` |
| `functions.graph` | calling functions and ignoring return values | `bin/zero check examples/functions.graph` |
| `branch.graph` | booleans and branches | `bin/zero check examples/branch.graph` |
| `countdown.graph` | `while` loop syntax | `bin/zero check examples/countdown.graph` |
## Data And Types
| Example | What it teaches | Try it |
| --- | --- | --- |
| `point.graph` | `type`, type literals, field access | `bin/zero check examples/point.graph` |
| `result-choice.graph` | `enum`, payload `choice`, exhaustive `match`, payload binding | `bin/zero check examples/result-choice.graph` |
| `primitive-language-gaps.graph` | fixed arrays, `var`, assignment | `bin/zero check examples/primitive-language-gaps.graph` |
| `memory-primitives.graph` | `Span`, `Maybe`, references, allocator vocabulary, `std.mem` spans | `bin/zero check examples/memory-primitives.graph` |
| `allocator-collections.graph` | fixed-buffer allocation, `Vec` capacity helpers, and fixed-capacity set operations without a global heap | `bin/zero check examples/allocator-collections.graph && bin/zero mem --json examples/allocator-collections.graph` |
| `const-arithmetic.graph` | top-level deterministic `const` values and arithmetic | `bin/zero check examples/const-arithmetic.graph` |
| `compile-time-v1.graph` | bounded `meta`, target/type reflection facts, Bool and enum static values, and compile-time JSON metadata | `bin/zero check --json examples/compile-time-v1.graph` |
| `generic-pair.graph` | multi-parameter generic type storage and field access | `bin/zero run examples/generic-pair.graph` |
| `static-value-params.graph` | integer static value parameters and fixed-capacity generic storage | `bin/zero check examples/static-value-params.graph` |
| `fixed-vec.graph` | field defaults, constructor-style type methods, receiver calls, `Self`, and static capacity | `bin/zero check examples/fixed-vec.graph` |
| `type-alias.graph` | `alias Name ExistingType` as compile-time spelling | `bin/zero check examples/type-alias.graph` |
| `static-method.graph` | static type method namespace calls without dispatch | `bin/zero check examples/static-method.graph` |
| `static-interface.graph` | static interface constraints over generic functions with direct calls | `bin/zero check examples/static-interface.graph` |
| `fallibility.graph` | `raise`, `check`, and explicit `raises [...]` error sets | `bin/zero build --emit exe --target linux-musl-x64 examples/fallibility.graph --out .zero/out/fallibility && ./.zero/out/fallibility` |
| `ownership-cleanup.graph` | `owned<T>` cleanup, canonical `drop`, and `defer` at lexical scope exit | `bin/zero build --emit exe --target linux-musl-x64 examples/ownership-cleanup.graph --out .zero/out/ownership-cleanup && ./.zero/out/ownership-cleanup` |
## Standard Library
| Example | What it teaches | Try it |
| --- | --- | --- |
| `std-math.graph` | pure fixed-width integer helpers and number-theory routines | `bin/zero check examples/std-math.graph` |
| `codec-varint.graph` | `use std.codec`, varint length, CRC-32 | `bin/zero check examples/codec-varint.graph` |
| `parse-cursor.graph` | `use std.parse`, scanner predicates | `bin/zero check examples/parse-cursor.graph` |
| `std-path-io.graph` | `std.path` fixed-buffer path helpers and `std.io` caller-owned buffers | `bin/zero check examples/std-path-io.graph` |
| `grep-scan.graph` | Line-oriented scanning with `std.io` and `std.str` | `bin/zero check examples/grep-scan.graph` |
| `std-str.graph` | allocation-free byte-string helpers over spans and caller-owned storage | `bin/zero check examples/std-str.graph` |
| `std-testing-log.graph` | `std.testing` expectations and explicit-buffer `std.log` JSON Lines output | `bin/zero test examples/std-testing-log.graph && bin/zero check examples/std-testing-log.graph` |
| `std-text-format-parse.graph` | ASCII helpers, runtime parsing, caller-buffer formatting, and UTF-8 validation | `bin/zero check examples/std-text-format-parse.graph` |
| `std-data-formats.graph` | codec encode/decode, JSON lookup/writing, and URL query helpers over caller-owned buffers | `bin/zero check examples/std-data-formats.graph` |
| `std-json-bytes.graph` | byte-span JSON validation, parsing, and token streaming | `bin/zero run --out /tmp/zero-json-bytes examples/std-json-bytes.graph` |
| `std-http-json.graph` | hosted HTTP request envelope into caller storage, then byte-span JSON parsing | `bin/zero check examples/std-http-json.graph` |
| `std-http-request.graph` | hosted HTTP request envelope with custom method, headers, and body | `bin/zero check examples/std-http-request.graph` |
| `std-http-headers.graph` | hosted HTTP request envelope, response buffer, and header-value lookup | `bin/zero check examples/std-http-headers.graph` |
| `json-api-client.graph` | hosted JSON API client with request-envelope writing and response-body parsing | `bin/zero check examples/json-api-client.graph` |
| `json-api-router.graph` | dependency-free JSON API request parsing and response-envelope writing | `bin/zero check examples/json-api-router.graph` |
| `crm-api/` | binary graph-first CRM API router with account/contact/deal CRUD, activity, health, and search routes | `bin/zero check examples/crm-api` |
| `binary-graph-store/` | graph-first package with binary `zero.graph` storage and a synced `.0` projection | `bin/zero status examples/binary-graph-store && bin/zero run examples/binary-graph-store` |
| `std-platform.graph` | `std.time`, `std.rand`, `std.proc`, and `std.crypto` capability-shaped helpers | `bin/zero check examples/std-platform.graph` |
| `cli-file.graph` | `std.args`, `std.env`, byte-span file writes, stderr/stdout | `bin/zero check examples/cli-file.graph` |
| `cli-config.graph` | `std.cli`, `std.env`, and JSON output checks | `bin/zero check examples/cli-config.graph` |
| `file-copy.graph` | `Fs`, explicit scratch storage, and hosted file copy | `bin/zero check examples/file-copy.graph` |
| `zero-hash/` | File checksum CLI with args, fixed buffers, `readAll`, and CRC-32 bytes | `bin/zero check examples/zero-hash` |
## Native Workflow Coverage
These examples are the small native workflow set used by docs and tests:
| Surface | Example | Try it |
| --- | --- | --- |
| arguments and environment | `cli-file.graph` | `ZERO_CLI_FILE_MODE=verbose bin/zero check examples/cli-file.graph` |
| filesystem resources | `zero-hash/` | `bin/zero check examples/zero-hash` |
| deterministic exit status | `direct-exe-return.graph` | `bin/zero build --emit exe --target linux-musl-x64 examples/direct-exe-return.graph --out .zero/out/direct-exe-return` |
| unhandled error exit path | `direct-unhandled-error-exit.graph` | `bin/zero check examples/direct-unhandled-error-exit.graph` |
## Interop And Packages
| Example | What it teaches | Try it |
| --- | --- | --- |
| `config-shape.graph` | `extern c`, `extern type`, C-shaped data | `bin/zero check examples/config-shape.graph` |
| `systems-package/` | `zero.toml`, multiple source files, `std.codec`/`std.parse`/`std.time` helpers | `bin/zero check examples/systems-package` |
| `readall-cli/` | package-local imports, named errors, `std.fs.readAll`, explicit fixed-buffer allocation | `bin/zero check examples/readall-cli` |
| `batch3-cli/` | module graph metadata, args fallbacks, path helpers, named fs errors, explicit allocation | `bin/zero check examples/batch3-cli` |
| `resource-cli/` | args/env fallback, path joins, `std.mem.copy`/`fill`, named-error owned-file resources | `bin/zero check examples/resource-cli` |
| `memory-package/` | target-neutral package imports and byte-span helper checks without hosted file I/O | `bin/zero build --target linux-musl-x64 examples/memory-package --out .zero/out/memory-package` |
| `crm-api/` | binary repository graph multi-module HTTP API package with 10+ CRM routes over request envelopes | `bin/zero build --emit exe --profile debug --out /tmp/zero-crm-api examples/crm-api` |
| `binary-graph-store/` | binary repository graph store loaded through normal package commands; `.0` is the human-readable projection | `bin/zero check examples/binary-graph-store && bin/zero test examples/binary-graph-store` |
| `direct-package-call-order/` | direct backend package merge order and cross-module helper calls | `bin/zero check examples/direct-package-call-order` |
| `error-tour/` | copyable failing commands and repaired fixtures for common diagnostics | `bin/zero explain TAR002` |
## Build A Runnable Program
Most examples are designed for `check`. To build and run an executable, use a CLI entry point:
```sh
bin/zero dev --json --target linux-musl-x64 examples/add.graph
bin/zero build --emit exe --target linux-musl-x64 examples/add.graph --out .zero/out/add
./.zero/out/add
```
Expected output:
```text
math works
```
The larger CLI path is `examples/zero-hash/`. It seeds a small file, reads it through a fixed-buffer allocator, computes CRC-32 over the read bytes without heap allocation, and prints:
```text
zero-hash ok
```
For target-neutral direct cross builds, use `examples/memory-package/`. It exercises a multi-file package and byte-span helper checks without `std.fs`, so it can be checked and built against non-host targets:
```sh
bin/zero build --target linux-musl-x64 examples/memory-package --out .zero/out/memory-package
```
## More Guidance
Run the docs site with:
```sh
pnpm run docs:dev
```
Start with Getting Started, then Learn Zero.
+12
View File
@@ -0,0 +1,12 @@
fn answer() -> i32 {
return 40 + 2
}
pub fn main(world: World) -> Void raises {
let value: i32 = answer()
if value == 42 {
check world.out.write("math works\n")
} else {
check world.out.write("math broke\n")
}
}
Binary file not shown.
+30
View File
@@ -0,0 +1,30 @@
# Agent Repair Demo
This demo shows the intended agent loop on a real diagnostic. The checked-in
example stays graph-backed; the script writes its intentionally broken input
under `.zero/agent-repair-demo/` at runtime.
Explain the diagnostic:
```sh
bin/zero explain --json TYP009
```
Inspect the repair plan:
```sh
pnpm run agent:demo
```
Apply the suggested edit:
```diff
- let dst: [4]u8 = [0, 0, 0, 0]
+ var dst: [4]u8 = [0, 0, 0, 0]
```
Review the fixed projection:
```sh
bin/zero check examples/agent-repair-demo/fixed.graph
```
+7
View File
@@ -0,0 +1,7 @@
use std.mem
pub fn main() -> Void {
var dst: [4]u8 = [0, 0, 0, 0]
let src: [4]u8 = [122, 101, 114, 111]
let _copied: usize = std.mem.copy(dst, src)
}
Binary file not shown.
+92
View File
@@ -0,0 +1,92 @@
pub fn main(world: World) -> Void raises {
var storage: [32]u8 = [0_u8; 32]
var alloc: FixedBufAlloc = std.mem.fixedBufAlloc(storage)
let bytes: Maybe<MutSpan<u8>> = std.mem.allocBytes(alloc, 4)
var vecStorage: [4]u8 = [0_u8; 4]
var vec: Vec = std.mem.vec(vecStorage)
let vecEmpty: Bool = std.mem.vecIsEmpty(&vec)
let pushed: Bool = std.mem.vecPush(&mut vec, 42_u8)
let setExisting: Bool = std.mem.vecSet(&mut vec, 0_usize, 43_u8)
let setMissing: Bool = std.mem.vecSet(&mut vec, 9_usize, 44_u8)
let pushedAgain: Bool = std.mem.vecPush(&mut vec, 45_u8)
let vecHasFortyThree: Bool = std.mem.vecContains(&vec, 43_u8)
let vecMissingFortySix: Bool = std.mem.vecContains(&vec, 46_u8)
let vecFortyFiveIndex: usize = std.mem.vecIndex(&vec, 45_u8)
let vecMissingIndex: usize = std.mem.vecIndex(&vec, 46_u8)
let vecDuplicate: Bool = std.mem.vecInsertUnique(&mut vec, 45_u8)
let vecUnique: Bool = std.mem.vecInsertUnique(&mut vec, 46_u8)
let vecRemovedValue: Bool = std.mem.vecRemoveValue(&mut vec, 43_u8)
let vecRemoveValueMissing: Bool = std.mem.vecRemoveValue(&mut vec, 43_u8)
let removedFirst: Bool = std.mem.vecRemoveSwap(&mut vec, 0_usize)
let removeMissing: Bool = std.mem.vecRemoveSwap(&mut vec, 9_usize)
let firstAfterRemove: Maybe<u8> = std.mem.vecGet(&vec, 0_usize)
let pushedThird: Bool = std.mem.vecPush(&mut vec, 46_u8)
let vecBytesBeforeTruncate: Span<u8> = std.mem.vecBytes(&vec)
let truncatedHigh: usize = std.mem.vecTruncate(&mut vec, 4_usize)
let truncated: usize = std.mem.vecTruncate(&mut vec, 1_usize)
let vecBytesBeforePop: Span<u8> = std.mem.vecBytes(&vec)
let vecRemaining: usize = std.mem.vecRemaining(&vec)
let vecFull: Bool = std.mem.vecIsFull(&vec)
let vecItem: Maybe<u8> = std.mem.vecGet(&vec, 0_usize)
let vecMissing: Maybe<u8> = std.mem.vecGet(&vec, 9_usize)
let popped: Bool = std.mem.vecPop(&mut vec)
let vecBytesAfterPop: Span<u8> = std.mem.vecBytes(&vec)
let cleared: usize = std.mem.vecClear(&mut vec)
let vecEmptyAfterClear: Bool = std.mem.vecIsEmpty(&vec)
var values: [4]u8 = [0_u8; 4]
var len: usize = 0
len = std.collections.insertUnique(values, len, 7_u8)
len = std.collections.insertUnique(values, len, 7_u8)
len = std.collections.insertUnique(values, len, 9_u8)
len = std.collections.removeValue(values, len, 7_u8)
let live: Span<u8> = std.collections.view(values, len)
var setStorage: [4]u8 = [1_u8, 2_u8, 0_u8, 0_u8]
var fixedSet: FixedSet<u8> = std.collections.fixedSet(setStorage, 2_usize)
let setInserted: Bool = std.collections.fixedSetInsert(&mut fixedSet, 3_u8)
let setDuplicate: Bool = std.collections.fixedSetInsert(&mut fixedSet, 3_u8)
let setRemoved: Bool = std.collections.fixedSetRemove(&mut fixedSet, 1_u8)
let setHasTwo: Bool = std.collections.fixedSetContains(&fixedSet, 2_u8)
let setLive: Span<u8> = std.collections.fixedSetView(&fixedSet)
let setRemaining: usize = std.collections.fixedSetRemaining(&fixedSet)
var allocatorSetBacking: [4]u8 = [0_u8; 4]
var allocatorDequeBacking: [4]u8 = [0_u8; 4]
var allocatorMapKeyBacking: [3]u8 = [0_u8; 3]
var allocatorMapValueBacking: [3]u8 = [0_u8; 3]
var allocatorSetAlloc: FixedBufAlloc = std.mem.fixedBufAlloc(allocatorSetBacking)
var allocatorDequeAlloc: FixedBufAlloc = std.mem.fixedBufAlloc(allocatorDequeBacking)
var allocatorMapKeyAlloc: FixedBufAlloc = std.mem.fixedBufAlloc(allocatorMapKeyBacking)
var allocatorMapValueAlloc: FixedBufAlloc = std.mem.fixedBufAlloc(allocatorMapValueBacking)
var allocatorContainersOk: Bool = false
let allocatorSetStorageMaybe: Maybe<MutSpan<u8>> = std.mem.allocBytes(allocatorSetAlloc, 4_usize)
let allocatorDequeStorageMaybe: Maybe<MutSpan<u8>> = std.mem.allocBytes(allocatorDequeAlloc, 4_usize)
let allocatorMapKeysMaybe: Maybe<MutSpan<u8>> = std.mem.allocBytes(allocatorMapKeyAlloc, 3_usize)
let allocatorMapValuesMaybe: Maybe<MutSpan<u8>> = std.mem.allocBytes(allocatorMapValueAlloc, 3_usize)
if allocatorSetStorageMaybe.has && allocatorDequeStorageMaybe.has && allocatorMapKeysMaybe.has && allocatorMapValuesMaybe.has {
var allocatorSet: FixedSet<u8> = std.collections.fixedSet(allocatorSetStorageMaybe.value, 0_usize)
let allocatorSetInsertedA: Bool = std.collections.fixedSetInsert(&mut allocatorSet, 11_u8)
let allocatorSetInsertedB: Bool = std.collections.fixedSetInsert(&mut allocatorSet, 12_u8)
let allocatorSetDuplicate: Bool = std.collections.fixedSetInsert(&mut allocatorSet, 12_u8)
let allocatorSetRemoved: Bool = std.collections.fixedSetRemove(&mut allocatorSet, 11_u8)
let allocatorSetLive: Span<u8> = std.collections.fixedSetView(&allocatorSet)
var allocatorDeque: FixedDeque<u8> = std.collections.fixedDeque(allocatorDequeStorageMaybe.value, 0_usize)
let allocatorDequeBackPushed: Bool = std.collections.fixedDequePushBack(&mut allocatorDeque, 21_u8)
let allocatorDequeFrontPushed: Bool = std.collections.fixedDequePushFront(&mut allocatorDeque, 20_u8)
let allocatorDequeBack: Maybe<u8> = std.collections.fixedDequeBack(&allocatorDeque)
let allocatorDequeFront: Maybe<u8> = std.collections.fixedDequeFront(&allocatorDeque)
let allocatorDequePopped: Maybe<u8> = std.collections.fixedDequePopFront(&mut allocatorDeque)
let allocatorDequeLive: Span<u8> = std.collections.fixedDequeView(&allocatorDeque)
var allocatorMap: FixedMap<u8, u8> = std.collections.fixedMap(allocatorMapKeysMaybe.value, allocatorMapValuesMaybe.value, 0_usize)
let allocatorMapPutA: Bool = std.collections.fixedMapPut(&mut allocatorMap, 1_u8, 31_u8)
let allocatorMapPutB: Bool = std.collections.fixedMapPut(&mut allocatorMap, 2_u8, 32_u8)
let allocatorMapUpdated: Bool = std.collections.fixedMapPut(&mut allocatorMap, 2_u8, 33_u8)
let allocatorMapValue: Maybe<u8> = std.collections.fixedMapGet(&allocatorMap, 2_u8)
let allocatorMapKeys: Span<u8> = std.collections.fixedMapKeys(&allocatorMap)
let allocatorMapValues: Span<u8> = std.collections.fixedMapValues(&allocatorMap)
let allocatorMapBeforeRemoveOk: Bool = allocatorMapValue.has && allocatorMapValue.value == 33_u8 && std.mem.len(allocatorMapKeys) == 2 && std.mem.len(allocatorMapValues) == 2 && allocatorMapKeys[0] == 1_u8 && allocatorMapKeys[1] == 2_u8 && allocatorMapValues[0] == 31_u8 && allocatorMapValues[1] == 33_u8
let allocatorMapRemoved: Bool = std.collections.fixedMapRemove(&mut allocatorMap, 1_u8)
allocatorContainersOk = allocatorSetInsertedA && allocatorSetInsertedB && !allocatorSetDuplicate && allocatorSetRemoved && std.collections.fixedSetContains(&allocatorSet, 12_u8) && std.mem.len(allocatorSetLive) == 1 && allocatorSetLive[0] == 12_u8 && std.collections.fixedSetRemaining(&allocatorSet) == 3 && allocatorDequeBackPushed && allocatorDequeFrontPushed && allocatorDequeBack.has && allocatorDequeBack.value == 21_u8 && allocatorDequeFront.has && allocatorDequeFront.value == 20_u8 && allocatorDequePopped.has && allocatorDequePopped.value == 20_u8 && std.mem.len(allocatorDequeLive) == 1 && allocatorDequeLive[0] == 21_u8 && std.collections.fixedDequeRemaining(&allocatorDeque) == 3 && allocatorMapPutA && allocatorMapPutB && allocatorMapUpdated && allocatorMapBeforeRemoveOk && allocatorMapRemoved && std.collections.fixedMapContains(&allocatorMap, 2_u8) && std.collections.fixedMapLen(&allocatorMap) == 1 && std.collections.fixedMapRemaining(&allocatorMap) == 2
}
if bytes.has && allocatorContainersOk && vecEmpty && pushed && setExisting && !setMissing && pushedAgain && vecHasFortyThree && !vecMissingFortySix && vecFortyFiveIndex == 1_usize && vecMissingIndex == 2_usize && !vecDuplicate && vecUnique && vecRemovedValue && !vecRemoveValueMissing && removedFirst && !removeMissing && firstAfterRemove.has && firstAfterRemove.value == 45_u8 && pushedThird && std.mem.len(vecBytesBeforeTruncate) == 2 && vecBytesBeforeTruncate[0] == 45_u8 && vecBytesBeforeTruncate[1] == 46_u8 && truncatedHigh == 2 && truncated == 1 && std.mem.len(vecBytesBeforePop) == 1 && vecBytesBeforePop[0] == 45_u8 && vecItem.has && vecItem.value == 45_u8 && !vecMissing.has && popped && std.mem.len(vecBytesAfterPop) == 0 && cleared == 0 && vecEmptyAfterClear && std.mem.vecLen(&vec) == 0 && std.mem.vecCapacity(&vec) == 4 && vecRemaining == 3 && !vecFull && std.mem.len(live) == 1 && values[0] == 9_u8 && std.collections.remaining(values, len) == 3 && !std.collections.isFull(values, len) && std.collections.contains(values, len, 9_u8) && setInserted && !setDuplicate && setRemoved && setHasTwo && std.mem.len(setLive) == 2 && setLive[0] == 3_u8 && setLive[1] == 2_u8 && setRemaining == 2 {
check world.out.write("allocator collections ok\n")
}
}
Binary file not shown.
+26
View File
@@ -0,0 +1,26 @@
use names
use validate
pub fn main(world: World) -> Void raises [NotFound, TooLarge, Io] {
let fs: Fs = std.fs.host()
var path_storage: [64]u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let joined: Maybe<String> = std.path.join(path_storage, ".zero", inputName())
if !joined.has {
raise TooLarge
}
let path: String = joined.value
let created: Maybe<owned<File>> = std.fs.create(fs, path)
if created.has {
var file: owned<File> = created.value
if !std.fs.writeAll(&mut file, std.mem.span("batch3 cli ok\n")) {
return
}
}
var read_storage: [64]u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var alloc: FixedBufAlloc = std.mem.fixedBufAlloc(read_storage)
var body: owned<ByteBuf> = check std.fs.readAllOrRaise(alloc, fs, path, 64)
if isExpected(std.mem.bufBytes(&body)) {
check world.out.write("batch3 cli ok\n")
}
}
+3
View File
@@ -0,0 +1,3 @@
pub fn inputName() -> String {
return std.args.getOr(1, "batch3.txt")
}
+3
View File
@@ -0,0 +1,3 @@
pub fn isExpected(bytes: MutSpan<u8>) -> Bool {
return std.mem.eqlBytes(bytes, std.mem.span("batch3 cli ok\n"))
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "batch3-cli"
version = "0.1.0"
[targets.cli]
kind = "exe"
main = "src/main.0"
+11
View File
@@ -0,0 +1,11 @@
pub fn main(world: World) -> Void raises {
check world.out.write("binary graph store example\n")
}
fn add(x: i32, y: i32) -> i32 {
return x + y
}
test "add works" {
expect (add(40, 2) == 42)
}
Binary file not shown.
+19
View File
@@ -0,0 +1,19 @@
[package]
name = "binary-graph-store"
version = "0.1.0"
license = "MIT"
[targets.cli]
kind = "exe"
main = "src/main.0"
defaultTarget = "linux-musl-x64"
devTarget = "host"
releaseProfile = "release-small"
[deps]
[profiles.dev]
inherits = "dev"
[profiles.release-small]
inherits = "release-small"
+8
View File
@@ -0,0 +1,8 @@
pub fn main(world: World) -> Void raises {
let ok: Bool = true
if ok {
check world.out.write("branch yes\n")
} else {
check world.out.write("branch no\n")
}
}
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
extern c "examples/c-interop/vendor/include/mathshim.h" as math
pub fn main(world: World) -> Void raises {
check world.out.write("c interop metadata ok\n")
}
+3
View File
@@ -0,0 +1,3 @@
#define MATHSHIM_VERSION 1
int mathshim_add(int a, int b);
+1
View File
@@ -0,0 +1 @@
placeholder archive for manifest validation
Binary file not shown.
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "c-interop"
version = "0.1.0"
[targets.cli]
kind = "exe"
main = "src/main.0"
[c.libs.math]
headers = ["vendor/include/mathshim.h"]
include = ["vendor/include"]
lib = ["vendor/lib/libmathshim.a"]
link = ["mathshim"]
mode = "static"
pkg_config = "mathshim"
+10
View File
@@ -0,0 +1,10 @@
pub fn main(world: World) -> Void raises {
let name: String = std.cli.optionValueOr("--name", "zero")
let count: Maybe<u32> = std.cli.optionU32("--count")
let mode: String = std.env.getOr("ZERO_CLI_MODE", "dev")
var json_buf: [64]u8 = [0_u8; 64]
let output: Maybe<Span<u8>> = std.json.writeObject1String(json_buf, "mode", mode)
if std.cli.hasFlag("--json") && count.has && count.value == 2_u32 && std.mem.eql(name, "agent") && output.has && std.mem.eql(output.value, "{\"mode\":\"test\"}") {
check world.out.write("cli config ok\n")
}
}
Binary file not shown.
+17
View File
@@ -0,0 +1,17 @@
pub fn main(world: World) -> Void raises {
let first: Maybe<String> = std.args.get(1)
let mode: Maybe<String> = std.env.get("ZERO_CLI_FILE_MODE")
if first.has && std.mem.eql(first.value, "write") {
let bytes: Span<u8> = std.mem.span(first.value)
let written: Maybe<usize> = std.fs.writeBytes(".zero/out/cli-file.txt", bytes)
if written.has && written.value > 0 && std.fs.exists(".zero/out/cli-file.txt") {
if mode.has && std.mem.eql(mode.value, "verbose") {
check world.err.write(mode.value)
check world.err.write("\n")
}
check world.out.write("cli file ok\n")
}
} else {
check world.out.write("pass an argument\n")
}
}
Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
use std.codec
pub fn main(world: World) -> Void raises {
let len: usize = std.codec.encodedVarintLen(300)
let checksum: u32 = std.codec.crc32("zero")
if len == 2 && checksum > 0 {
check world.out.write("codec primitives ok\n")
}
}
Binary file not shown.
+35
View File
@@ -0,0 +1,35 @@
const enabled: Bool = meta(target.pointerWidth >= 32 && hasField(Point, "y"))
const selected: Mode = Mode.tiny
const fields: usize = meta fieldCount(Point)
const field_type: String = meta fieldType(Point, "x")
type Point {
x: i32,
y: i32,
}
type Gate<static enabledFlag: Bool, static selectedMode: Mode> {
value: i32,
}
enum Mode: u8 {
fast,
tiny,
}
fn readGate<static enabledFlag: Bool, static selectedMode: Mode>(gate: ref<Gate<enabledFlag, selectedMode>>) -> i32 {
if enabledFlag {
return gate.value
}
return 0
}
pub fn main(world: World) -> Void raises {
let gate: Gate<enabled, selected> = Gate { value: 29 }
if readGate<enabled, selected>(&gate) == 29 && fields == 2 && std.mem.eql(field_type, "i32") {
check world.out.write("compile time v1 ok\n")
}
}
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
extern c "examples/vendor/include/config.h" as cconfig
extern type CConfig {
port: i32,
workers: i32,
}
type Config {
port: i32,
workers: i32,
}
pub fn main(world: World) -> Void raises {
let config: Config = Config { port: 3000, workers: 4 }
if config.workers > 0 {
check world.out.write("config shape parsed\n")
}
}
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
const base: i32 = 40
const answer: i32 = base + 2
pub fn main(world: World) -> Void raises {
if answer == 42 {
check world.out.write("const arithmetic ok\n")
} else {
check world.out.write("const arithmetic broke\n")
}
}
Binary file not shown.
+6
View File
@@ -0,0 +1,6 @@
pub fn main(world: World) -> Void raises {
while false {
check world.out.write("unreachable\n")
}
check world.out.write("countdown done\n")
}
Binary file not shown.
+30
View File
@@ -0,0 +1,30 @@
use http_util
pub fn listAccounts(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if tenantOk(request) {
return jsonResponse(response, 200_u16, "{\"accounts\":[{\"id\":1,\"name\":\"Acme\",\"owner\":\"Ada\"},{\"id\":2,\"name\":\"Globex\",\"owner\":\"Lin\"}],\"next\":null}")
}
return badRequest(response, "tenant")
}
pub fn createAccount(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) && bodyHasU32(request, "owner_id", 256) {
return jsonResponse(response, 201_u16, "{\"account\":{\"id\":3,\"name\":\"New account\",\"owner_id\":9},\"created\":true}")
}
return badRequest(response, "json")
}
pub fn readAccount(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"account\":{\"id\":1,\"name\":\"Acme\",\"stage\":\"customer\",\"owner\":\"Ada\"}}")
}
pub fn updateAccount(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) {
return jsonResponse(response, 200_u16, "{\"account\":{\"id\":1,\"name\":\"Acme Updated\",\"stage\":\"customer\"},\"updated\":true}")
}
return badRequest(response, "json")
}
pub fn deleteAccount(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"account\":{\"id\":1},\"deleted\":true}")
}
+12
View File
@@ -0,0 +1,12 @@
use http_util
pub fn listActivities(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"activities\":[{\"id\":77,\"type\":\"call\",\"account_id\":1},{\"id\":78,\"type\":\"email\",\"account_id\":1}]}")
}
pub fn createActivity(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) && bodyHasU32(request, "account_id", 256) {
return jsonResponse(response, 201_u16, "{\"activity\":{\"id\":102,\"kind\":\"note\",\"account_id\":1},\"created\":true}")
}
return badRequest(response, "json")
}
+30
View File
@@ -0,0 +1,30 @@
use http_util
pub fn listContacts(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if tenantOk(request) {
return jsonResponse(response, 200_u16, "{\"contacts\":[{\"id\":7,\"name\":\"Grace Hopper\",\"account_id\":1},{\"id\":8,\"name\":\"Katherine Johnson\",\"account_id\":2}]}")
}
return badRequest(response, "tenant")
}
pub fn createContact(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) && bodyHasU32(request, "account_id", 256) {
return jsonResponse(response, 201_u16, "{\"contact\":{\"id\":9,\"name\":\"New contact\",\"account_id\":1},\"created\":true}")
}
return badRequest(response, "json")
}
pub fn readContact(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"contact\":{\"id\":7,\"name\":\"Grace Hopper\",\"email\":\"grace@example.com\",\"account_id\":1}}")
}
pub fn updateContact(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) {
return jsonResponse(response, 200_u16, "{\"contact\":{\"id\":7,\"name\":\"Grace Hopper\",\"email\":\"updated@example.com\"},\"updated\":true}")
}
return badRequest(response, "json")
}
pub fn deleteContact(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"contact\":{\"id\":7},\"deleted\":true}")
}
+30
View File
@@ -0,0 +1,30 @@
use http_util
pub fn listDeals(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if tenantOk(request) {
return jsonResponse(response, 200_u16, "{\"deals\":[{\"id\":42,\"name\":\"Expansion\",\"amount\":120000},{\"id\":43,\"name\":\"Renewal\",\"amount\":45000}]}")
}
return badRequest(response, "tenant")
}
pub fn createDeal(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) && bodyHasU32(request, "account_id", 256) {
return jsonResponse(response, 201_u16, "{\"deal\":{\"id\":44,\"name\":\"New deal\",\"account_id\":1,\"amount\":10000},\"created\":true}")
}
return badRequest(response, "json")
}
pub fn readDeal(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"deal\":{\"id\":42,\"name\":\"Expansion\",\"stage\":\"proposal\",\"amount\":120000}}")
}
pub fn updateDeal(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
if bodyIsJson(request, 256) {
return jsonResponse(response, 200_u16, "{\"deal\":{\"id\":42,\"stage\":\"won\",\"amount\":120000},\"updated\":true}")
}
return badRequest(response, "json")
}
pub fn deleteDeal(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"deal\":{\"id\":42},\"deleted\":true}")
}
+60
View File
@@ -0,0 +1,60 @@
pub fn jsonResponse(response: MutSpan<u8>, status: u16, body: Span<u8>) -> Maybe<Span<u8>> {
return std.http.writeJsonResponse(response, status, body)
}
pub fn notFound(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return std.http.writeJsonNotFound(response, "{\"error\":\"not_found\",\"resource\":\"crm\"}")
}
pub fn methodNotAllowed(response: MutSpan<u8>) -> Maybe<Span<u8>> {
return std.http.writeJsonMethodNotAllowed(response, "{\"error\":\"method_not_allowed\"}")
}
pub fn badRequest(response: MutSpan<u8>, field: Span<u8>) -> Maybe<Span<u8>> {
if std.mem.eql(field, "tenant") {
return std.http.writeJsonBadRequest(response, "{\"error\":\"bad_request\",\"field\":\"tenant\"}")
}
if std.mem.eql(field, "json") {
return std.http.writeJsonBadRequest(response, "{\"error\":\"bad_request\",\"field\":\"body\"}")
}
if std.mem.eql(field, "id") {
return std.http.writeJsonBadRequest(response, "{\"error\":\"bad_request\",\"field\":\"id\"}")
}
return std.http.writeJsonBadRequest(response, "{\"error\":\"bad_request\"}")
}
pub fn tenantOk(request: Span<u8>) -> Bool {
let tenant: Maybe<Span<u8>> = std.http.requestQueryValue(request, "tenant")
return tenant.has
}
pub fn bodyIsJson(request: Span<u8>, max_bytes: usize) -> Bool {
let body: Maybe<Span<u8>> = std.http.requestJsonBodyWithin(request, max_bytes)
return body.has
}
pub fn bodyHasU32(request: Span<u8>, field: Span<u8>, max_bytes: usize) -> Bool {
let body: Maybe<Span<u8>> = std.http.requestJsonBodyWithin(request, max_bytes)
if !body.has {
return false
}
let value: Maybe<u32> = std.json.u32(body.value, field)
return value.has
}
pub fn responseBody(output: Span<u8>) -> Span<u8> {
let body: Maybe<Span<u8>> = std.http.responseBodyBytes(output)
if body.has {
return body.value
}
let end: usize = std.mem.len(output)
return output[end..]
}
pub fn responseHasStatus(output: Span<u8>, status: Span<u8>) -> Bool {
return std.str.contains(output, status)
}
pub fn responseBodyEquals(output: Span<u8>, expected: Span<u8>) -> Bool {
return std.mem.eql(responseBody(output), expected)
}
+16
View File
@@ -0,0 +1,16 @@
use router
pub fn main(world: World) -> Void raises {
let maybe_request: Maybe<String> = std.args.get(1)
if !maybe_request.has {
check world.err.write("usage: pass one HTTP request envelope\n")
return
}
var response: [1024]u8 = [0_u8; 1024]
let output: Maybe<Span<u8>> = handleCrm(std.mem.span(maybe_request.value), response)
if output.has {
check world.out.write(output.value)
return
}
check world.err.write("crm api router failed\n")
}
+92
View File
@@ -0,0 +1,92 @@
use accounts
use activities
use contacts
use deals
use http_util
use search
pub fn handleCrm(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
let method: Maybe<Span<u8>> = std.http.requestMethodName(request)
let path: Maybe<Span<u8>> = std.http.requestPath(request)
if !method.has {
return badRequest(response, "json")
}
if !path.has {
return badRequest(response, "json")
}
if std.http.requestIsGet(request, "/health") {
return jsonResponse(response, 200_u16, "{\"ok\":true,\"service\":\"crm\"}")
}
if std.http.requestIsGet(request, "/crm/accounts") {
return listAccounts(request, response)
}
if std.http.requestIsPost(request, "/crm/accounts") {
return createAccount(request, response)
}
if std.http.requestIsGet(request, "/crm/accounts/1") {
return readAccount(response)
}
if std.http.requestIsPost(request, "/crm/accounts/1/update") {
return updateAccount(request, response)
}
if std.http.requestIsPost(request, "/crm/accounts/1/delete") {
return deleteAccount(response)
}
if std.http.requestIsGet(request, "/crm/contacts") {
return listContacts(request, response)
}
if std.http.requestIsPost(request, "/crm/contacts") {
return createContact(request, response)
}
if std.http.requestIsGet(request, "/crm/contacts/7") {
return readContact(response)
}
if std.http.requestIsPost(request, "/crm/contacts/7/update") {
return updateContact(request, response)
}
if std.http.requestIsPost(request, "/crm/contacts/7/delete") {
return deleteContact(response)
}
if std.http.requestIsGet(request, "/crm/deals") {
return listDeals(request, response)
}
if std.http.requestIsPost(request, "/crm/deals") {
return createDeal(request, response)
}
if std.http.requestIsGet(request, "/crm/deals/42") {
return readDeal(response)
}
if std.http.requestIsPost(request, "/crm/deals/42/update") {
return updateDeal(request, response)
}
if std.http.requestIsPost(request, "/crm/deals/42/delete") {
return deleteDeal(response)
}
if std.http.requestIsGet(request, "/crm/activities") {
return listActivities(request, response)
}
if std.http.requestIsPost(request, "/crm/activities") {
return createActivity(request, response)
}
if std.http.requestIsGet(request, "/crm/search") {
return searchCrm(request, response)
}
if std.http.requestMethodIs(request, "PUT") {
return methodNotAllowed(response)
}
if std.http.requestMethodIs(request, "PATCH") {
return methodNotAllowed(response)
}
if std.http.requestMethodIs(request, "DELETE") {
return methodNotAllowed(response)
}
if std.http.requestMethodIs(request, "POST") {
return methodNotAllowed(response)
}
return notFound(response)
}
+5
View File
@@ -0,0 +1,5 @@
use http_util
pub fn searchCrm(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
return jsonResponse(response, 200_u16, "{\"results\":[{\"type\":\"account\",\"id\":1,\"label\":\"Acme\"},{\"type\":\"deal\",\"id\":42,\"label\":\"Expansion\"}]}")
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "crm-api"
version = "0.1.0"
[targets.cli]
kind = "exe"
main = "src/main.0"
+12
View File
@@ -0,0 +1,12 @@
export c fn main() -> u8 {
var storage: [8]u8 = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8]
var alloc: FixedBufAlloc = std.mem.fixedBufAlloc(storage)
let first: Maybe<MutSpan<u8>> = std.mem.allocBytes(alloc, 4)
if first.has {
let copied: usize = std.mem.copy(first.value, "zero"[0..4])
if copied == 4 {
return first.value[2]
}
}
return 0_u8
}
Binary file not shown.
+14
View File
@@ -0,0 +1,14 @@
export c fn main() -> u8 {
var storage: [8]u8 = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8]
var alloc: FixedBufAlloc = std.mem.fixedBufAlloc(storage)
let first: Maybe<MutSpan<u8>> = std.mem.allocBytes(alloc, 5)
let second: Maybe<MutSpan<u8>> = std.mem.allocBytes(alloc, 4)
if first.has {
if second.has {
return 0_u8
} else {
return 1_u8
}
}
return 0_u8
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
export c fn main() -> i32 {
let bytes: [2]u8 = [1_u8, 2_u8]
if bytes[2] == 1_u8 {
return 1
}
return 0
}
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
export c fn main() -> i32 {
var values: [4]i32 = [0, 0, 0, 0]
var i: usize = 0
var value: i32 = 2
while i < 4 {
values[i] = value
value = value + 2
i = i + 1
}
return values[0] + values[1] + values[2] + values[3]
}
Binary file not shown.
+10
View File
@@ -0,0 +1,10 @@
export c fn main() -> i32 {
let values: [4]i32 = [1, 2, 3, 4]
var i: usize = 0
var sum: i32 = 0
while i < 4 {
sum = sum + values[i]
i = i + 1
}
return sum
}
Binary file not shown.
+16
View File
@@ -0,0 +1,16 @@
export c fn main() -> u8 {
var storage: [4]u8 = [0, 0, 0, 0]
var buf: Vec = std.mem.vec(storage)
let first: Bool = std.mem.vecPush(&mut buf, 65_u8)
let second: Bool = std.mem.vecPush(&mut buf, 66_u8)
if first {
if second {
if std.mem.vecLen(&buf) == 2 {
if std.mem.vecCapacity(&buf) == 4 {
return storage[1]
}
}
}
}
return 0_u8
}
Binary file not shown.
+12
View File
@@ -0,0 +1,12 @@
export c fn main() -> u8 {
var dst: [5]u8 = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8]
let span: MutSpan<u8> = dst
let filled: usize = std.mem.fill(span, 33_u8)
let copied: usize = std.mem.copy(span, "token"[0..5])
if filled == 5 {
if copied == 5 {
return dst[1]
}
}
return 0_u8
}
Binary file not shown.
+10
View File
@@ -0,0 +1,10 @@
export c fn main() -> u8 {
let text: String = "token"
let part: Span<u8> = text[1..4]
if std.mem.len(text) == 5 {
if std.mem.eqlBytes(part, "oke"[0..3]) {
return part[1]
}
}
return 0_u8
}
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
export c fn main() -> u8 {
let text: String = "token"
let part: Span<u8> = text[1..4]
if std.mem.len(part) == 3 {
return part[1]
}
return 0_u8
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
fn add(a: i32, b: i32) -> i32 {
return a + b
}
export c fn main(a: i32, b: i32) -> i32 {
return add(a, b)
}
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
fn choose(flag: Bool, a: i32, b: i32) -> i32 {
if flag {
return a
} else {
return b
}
}
export c fn main(a: i32, b: i32) -> i32 {
return choose(a > b, a, b)
}
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
fn sum_to(n: i32) -> i32 {
var i: i32 = 0
var sum: i32 = 0
while i <= n {
sum = sum + i
i = i + 1
}
return sum
}
export c fn main() -> i32 {
return sum_to(4)
}
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
export c fn main() -> u32 {
return std.codec.crc32Bytes(std.mem.span("zero hash payload\n"))
}
Binary file not shown.
+21
View File
@@ -0,0 +1,21 @@
enum Mode: u8 {
lex,
parse,
done,
}
export c fn main() -> u8 {
let mode: Mode = Mode.parse
match mode {
lex {
return 1_u8
}
parse {
return 2_u8
}
done {
return 3_u8
}
}
return 0_u8
}
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
export c fn main() -> i32 {
return 42
}
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
type FixedBuf<T: Type, static N: usize> {
len: usize = 0,
first: T,
items: [N]T,
}
export c fn main() -> u8 {
let buf: FixedBuf<u8, 4> = FixedBuf { first: 55_u8, items: [1_u8, 2_u8, 3_u8, 4_u8] }
if buf.len == 0 {
return buf.first
}
return 0_u8
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
fn identity<T: Type>(value: T) -> T {
return value
}
export c fn main() -> u8 {
return identity<u8>(42_u8)
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
type Vec<T: Type, static N: usize> {
len: usize = 0,
first: T,
items: [N]T,
}
enum TokenKind: u8 {
identifier,
eof,
}
enum NodeKind: u8 {
root,
literal,
}
export c fn main() -> u8 {
let bytes: Vec<u8, 2> = Vec { first: 3_u8, items: [1_u8, 2_u8] }
let tokens: Vec<TokenKind, 2> = Vec { first: TokenKind.identifier, items: [TokenKind.identifier, TokenKind.eof] }
let nodes: Vec<NodeKind, 2> = Vec { first: NodeKind.root, items: [NodeKind.root, NodeKind.literal] }
if bytes.first == 3_u8 {
if tokens.first == TokenKind.identifier {
if nodes.first == NodeKind.root {
return 61_u8
}
}
}
return 0_u8
}
Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
export c fn main() -> i64 {
return 9223372036854775807
}
export c fn wide_sum() -> u64 {
let left: u64 = 4000000000
let right: u64 = 42
return left + right
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
export c fn main(a: i32) -> i32 {
if a > 10 {
return 7
} else {
return 3
}
}
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
export c fn main() -> usize {
var dst: [5]u8 = [0_u8, 0_u8, 0_u8, 0_u8, 0_u8]
let span: MutSpan<u8> = dst
return std.mem.len(span)
}
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
export c fn main(a: i32, b: i32) -> i32 {
let sum: i32 = a + b
return sum
}
Binary file not shown.
@@ -0,0 +1,9 @@
fn record(value: i32) -> Void {
return
}
fn package_sum() -> i32 {
var values: [3]i32 = [2, 4, 6]
values[1] = values[1] + 1
return values[0] + values[1] + values[2]
}
@@ -0,0 +1,6 @@
use arrays
export c fn main() -> i32 {
record(3)
return package_sum()
}
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "direct-package-arrays"
version = "0.1.0"
[targets.cli]
kind = "exe"
main = "src/main.0"
@@ -0,0 +1,19 @@
fn is_upper_a(code: u8) -> Bool {
if code == 65_u8 {
return true
}
return false
}
fn left_score() -> i32 {
let bytes: [3]u8 = [65_u8, 90_u8, 65_u8]
var i: usize = 0
var score: i32 = 0
while i < 3 {
if is_upper_a(bytes[i]) {
score = score + 10
}
i = i + 1
}
return score
}
@@ -0,0 +1,7 @@
use right
use left
export c fn main() -> i32 {
return left_score() + right_score()
}
@@ -0,0 +1,3 @@
fn right_score() -> i32 {
return 7
}
Binary file not shown.
@@ -0,0 +1,7 @@
[package]
name = "direct-package-call-order"
version = "0.1.0"
[targets.cli]
kind = "exe"
main = "src/main.0"

Some files were not shown because too many files have changed in this diff Show More