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 - interpreter
  • cowcomp.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 value 3
    • same Moo char I/O behavior (input flushes to newline)
    • oom integer input uses atoi-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
    • rustc tuned 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)
  • Cross-target builds via rustc --target
    • requires installing the target with rustup target add …

Repository layout

  • cowc.rs - the compiler (single-file Rust program)
  • README.md - this file
  • DETAILS.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; rustc will tell you if something is missing.

Max performance

After running the benchmarks, it turns out that fat LTO doesnt 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
Description
COW Programming Language "Compiler", in rust :0
Readme GPL-3.0 83 KiB
2026-02-21 16:46:13 +00:00
Languages
Rust 100%