01 / Records and procedures

Model a score

Define a record, pass it to a procedure, and return the answer.

Score :: struct {
    value: s32;
    bonus: s32;
}

Total :: (score: Score) -> s32 {
    return score.value + score.bonus;
}

Answer :: () -> s32 {
    score: Score = Score.{value = 40, bonus = 2};
    return Total(score);
}
build/bin/ziran check --root . site/examples/hello.zi
build/bin/ziran bundle --root . --entry hello:Answer -o hello.zib site/examples/hello.zi
build/bin/ziran run hello.zib
# 42

Download hello.zi ↓

02 / Standard module

Process text

Import the standard text module with --module-path std. This program returns 1 when a string starts with a prefix after ASCII case folding.

#import "text"

Answer :: () -> s32 {
    if StartsWithFoldASCII("Ziran", "zi") {
        return 1;
    }
    return 0;
}
build/bin/ziran check --root . --module-path std site/examples/text_demo.zi
build/bin/ziran bundle --root . --module-path std --entry text_demo:Answer -o text_demo.zib site/examples/text_demo.zi
build/bin/ziran run text_demo.zib
# 1

Download text_demo.zi ↓

03 / Native output

Generate native code

The same checked source can generate C, C++, or Go. These commands write source into separate output directories. A C compiler and make build the Ziran tools; Go is needed only if you compile the generated Go output.

build/bin/ziran build --target=c --root . -o out/c site/examples/hello.zi
build/bin/ziran build --target=cpp --root . -o out/cpp site/examples/hello.zi
build/bin/ziran build --target=go --root . -o out/go site/examples/hello.zi

Native and portable support can differ by feature. See the current status before choosing a target.