cowc.rs: COW compiler in Rust
cowc compiles COW (.cow) programs into native executables using only Rust tooling (rustc).
It is based on the classic COW implementation by BigZaphod and matches the semantics of the C++ reference sources:
cow.cpp- interpretercowcomp.cpp- compiler variant (emits a native program via C++)
This repository provides a Rust compiler (cowc.rs) that emits a dedicated
Rust program for your .cow file and then uses rustc to build a binary.
Read more about COW on the classic COW webpage
You can read more about this project on my blog, including the performance comparisons here.
Features
- Semantic parity with the C++ compiler variant (
cowcomp.cpp)- same 3-character tokenization (“sliding window” parsing)
- same loop matching behavior (including the original quirks)
- same
mOO(eval) behavior, including exit on value3 - same
Moochar I/O behavior (input flushes to newline) oominteger input usesatoi-style parsing (and preserves the reference behavior on long lines)
- Fast output binaries
- PC-dispatch loop (
match pc) that the optimizer turns into a tight jump table - wrapping arithmetic (
wrapping_add/sub) for predictable, fast cell math rustctuned flags:-C opt-level=3,-C codegen-units=1,-C panic=abort- optional fat LTO (
--lto) - optional chunked stdin buffering (on-demand, interactive-safe) + buffered stdout writes (minimal syscall overhead)
- optional
-C target-cpu=native(default for host builds)
- PC-dispatch loop (
- Cross-target builds via
rustc --target- requires installing the target with
rustup target add …
- requires installing the target with
Repository layout
cowc.rs- the compiler (single-file Rust program)README.md- this fileDETAILS.md- deep dive: semantics, code structure, performance choices, and comparison to the C++ files
Build cowc
rustc -O cowc.rs -o cowc
Compile a .cow program (native)
./cowc hello.cow -o hello
Keep the generated Rust (helpful for inspection/debugging):
./cowc hello.cow --keep-rs -o hello
Only emit Rust and stop:
./cowc hello.cow --emit-rs -o hello
Cross-compiling
Install the target:
rustup target add x86_64-pc-windows-gnu
Compile:
./cowc --target x86_64-pc-windows-gnu hello.cow -o hello.exe
Notes:
- Cross targets require the Rust std components for that target.
- Some targets also require a system linker/toolchain;
rustcwill tell you if something is missing.
Max performance
After running the benchmarks, it turns out that fat LTO doesn’t provide any real benefit for most COW applications.
You can enable it with the LTO flag
./cowc hello.cow --lto -o hello
In the previous version, output was automatically buffered and only written when the program finished for performance. This is now an optional flag, since the behavior made some programs unusable. See the issue here
./cowc hello.cow --vec-stdout -o hello
For host builds, keep the default native CPU tuning (-C target-cpu=native). Disable it with:
./cowc hello.cow --no-native-cpu -o hello