6 个版本 (3 个重大更新)

0.4.2 2023 年 3 月 13 日
0.4.1 2022 年 7 月 7 日
0.4.0 2021 年 10 月 21 日
0.3.0 2021 年 6 月 15 日
0.1.0 2021 年 5 月 29 日

#291编程语言

每月 33 次下载
用于 cranefack-cli

MIT/Apache

395KB
10K SLoC

Cranefack

docs.rs badge crates.io badge Rust

一个由 cranelift 驱动的优化 brainfuck 编译器套件。

命令行界面

Cranefack 提供了一个命令行实用工具来运行、编译和基准测试程序。

cargo install cranefack-cli

运行

使用解释器或 JIT 运行程序。
传递 -v 选项将打印一些统计信息和执行时间。

USAGE:
    cranefack run [FLAGS] [OPTIONS] <FILE>

FLAGS:
        --debug-optimizations    Print statistics for optimization passes
    -j, --jit                    Use JIT compiler
    -v, --verbose                
        --wrapping-is-ub         Wrapping overflows are undefined behavior during optimization
    -h, --help                   Prints help information
    -V, --version                Prints version information

OPTIONS:
        --jit-level <level>    Optimization level for JIT [possible values: none, speed, speed_and_size]
    -O <mode>                  Optimization mode [default: 2]  [possible values: 0, 1, 2, 3, s, wtf]

ARGS:
    <FILE>    Brainfuck source file. Use - to read from stdin

编译

编译程序。
目前,这将默认创建一个类似于汇编的表示,这对于调试很有用。
如果您需要将内容编译成原生二进制文件,可以使用 rust 输出格式来获取可以与 rustc 编译的丑陋的 Rust 代码。

cranefack compile -f=rust some_app.bf > some_app.rs
rustc -O some_app.rs
./some_app
USAGE:
    cranefack compile [FLAGS] [OPTIONS] <FILE>

FLAGS:
        --debug-optimizations    Print statistics for optimization passes
    -v, --verbose                
        --wrapping-is-ub         Wrapping overflows are undefined behavior during optimization
    -h, --help                   Prints help information
    -V, --version                Prints version information

OPTIONS:
    -f, --format <format>      Format of compiled code [default: dump]  [possible values: dump, clir, rust]
        --jit-level <level>    Optimization level for JIT [possible values: none, speed, speed_and_size]
    -O <mode>                  Optimization mode [default: 2]  [possible values: 0, 1, 2, 3, s, wtf]

ARGS:
    <FILE>    Brainfuck source file. Use - to read from stdin

基准测试

以不同的优化设置运行程序,并返回一个表格,显示每次程序运行的执行时间。

USAGE:
    cranefack benchmark [FLAGS] [OPTIONS] <FILE>

FLAGS:
    -j, --jit               Only benchmark jit
    -o, --optimized-only    Don't benchmark O0
    -h, --help              Prints help information
    -V, --version           Prints version information

OPTIONS:
    -i, --iterations <ITERATIONS>    Number of benchmarking iterations [default: 2]
    -r, --runs <RUNS>                Number of runs per optimization in each round [default: 4]

ARGS:
    <FILE>    Brainfuck source file. Use - to read from stdin

将 cranefack 作为库使用

要使用 cranefack 作为库,请将以下内容添加到您的 Cargo.toml 依赖项中

cranefack = "0.4"

使用 JIT 编译运行程序

use std::error::Error;
use cranefack::{parse, optimize_with_config, OptimizeConfig, CompiledJitModule};

fn main() -> Result<(), Box<dyn Error>> {

    // Parse program
    let mut program = parse("++[<].")?;

    // Create optimization config for level 2
    let opt_level = OptimizeConfig::o2();

    // Optimize with optimization level 2
    optimize_with_config(&mut program, &opt_level);

    // Compile program into module
    let module = CompiledJitModule::new(&program, &opt_level)?;

    // Execute compiled module reading from stdin and writing to stdout
    module.execute(std::io::stdin(), std::io::stdout());

    Ok(())
}

许可证

此项目根据以下任一项获得许可

贡献

除非您明确声明,否则您提交给 cranefack 的任何有意贡献,根据 Apache-2.0 许可证定义,将根据上述条款双许可,不附加任何额外条款或条件。

依赖项

~7–15MB
~197K SLoC