4个版本

0.1.3 2022年4月10日
0.1.2 2022年4月10日
0.1.1 2022年4月10日
0.1.0 2022年4月10日

#444编程语言

每月22次下载

MIT 许可证

61KB
1.5K SLoC

libbf

类似Brainfuck的语言库。

此库可以定义类似Brainfuck的语言解析器,并可以运行解析后的程序。

示例

使用预定义的Brainfuck解释器

`bf' 特性标志需要编译此示例。

use libbf::{predefined::bf, runtime};
use std::io;

fn main() {
    let source = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
    let program = bf::parser()
        .parse_str(source)
        .expect("Failed to parse");
    runtime::run(&program, io::stdin(), io::stdout()).expect("Failed to run");
}

定义Brainfuck解释器

use libbf::{parser::Parser, runtime, token::simple::SimpleTokenSpec};
use std::io;

fn main() {
    // Create parser with token specification.
    let parser = Parser::new(
        SimpleTokenSpec {
            // You can specify tokens with `ToString` (`char`, `&str`, `String`, etc.)
            ptr_inc: '>',              // char
            ptr_dec: "<",              // &str
            data_inc: "+".to_string(), // String
            data_dec: '-',
            output: '.',
            input: ',',
            loop_head: '[',
            loop_tail: ']',
        }
        .to_tokenizer(),
    );

    let source = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
    let program = parser.parse_str(source).expect("Failed to parse");
    runtime::run(&program, io::stdin(), io::stdout()).expect("Failed to run");
}

特性标志

  • all - 所有特性
  • predefined - 以下预定义解析器
  • bf - 预定义Brainfuck解析器
  • ook - 预定义Ook!解析器

依赖项

~275–730KB
~17K SLoC