Skip to main content

regenerate_zero_copy_bytecode/
regenerate_zero_copy_bytecode.rs

1//! Regenerate the binary fixture used by the zero-copy include_bytes
2//! example.
3//!
4//! Reads `examples/zero_copy_demo.kel`, compiles it through the
5//! Keleusma frontend, and writes the resulting framed bytecode to
6//! `examples/zero_copy_demo.kel.bin`. The binary is committed to the
7//! repository so the consuming example can `include_bytes!` it
8//! without a build script.
9//!
10//! Run after a wire format change. The size of the resulting binary
11//! becomes the value of the `BYTECODE_LEN` constant in
12//! `examples/zero_copy_include_bytes.rs`.
13//!
14//! Usage:
15//!
16//!     cargo run --example regenerate_zero_copy_bytecode
17
18use std::fs;
19use std::path::PathBuf;
20
21use keleusma::compiler::compile;
22use keleusma::lexer::tokenize;
23use keleusma::parser::parse;
24
25fn main() {
26    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
27    let source_path = manifest_dir.join("examples").join("zero_copy_demo.kel");
28    let binary_path = manifest_dir.join("examples").join("zero_copy_demo.kel.bin");
29
30    let source = fs::read_to_string(&source_path).expect("read source");
31    let tokens = tokenize(&source).expect("lex");
32    let program = parse(&tokens).expect("parse");
33    let module = compile(&program).expect("compile");
34    let bytes = module.to_bytes().expect("encode");
35
36    fs::write(&binary_path, &bytes).expect("write binary");
37    println!("wrote {} ({} bytes)", binary_path.display(), bytes.len());
38}