< 返回版块

ctj12461 发表于 2023-02-11 23:11

刚学 Rust 的练手项目,欢迎大家提出修改意见。支持配置各种运行的参数,比如内存长度、EOF 处理模式等。

这个解释器没有使用 JIT ,但是有针对性的优化措施,不敢说是无 JIT 解释器中最快,但是足够超过绝大多数实现。(我在网上能够找到的无 JIT 的都没有这个快。)

brainfuck 程序 平均时间
hanoi 757.72 ms
mandelbrot 13.220 s
squares 5.2570 ms

测试环境:

  • CPU:AMD Ryzen 5 4500U
  • RAM:8 GB

使用方法:

$ git clone https://github.com/ctj12461/brainfuck-interpreter.git
$ cd brainfuck-interpreter
$ cargo install --path ./crates/bf-exec # The program will be installed to ~/.cargo/bin
$ bf-exec ./examples/helloworld.bf
Hello World!
Usage: bf-exec [OPTIONS] <SOURCE>

Arguments:
  <SOURCE>
          the path of the brainfuck program source code file.

Options:
      --len <LEN>
          the memory (tape) length the program will use.

          [default: 32768]

      --addr <ADDR>
          the address range of the memory (tape).

           - unsigned: [0, len - 1]
           - signed: [-ceil(len / 2), ceil(len / 2) - 1]

          [default: unsigned]
          [possible values: unsigned, signed]

      --cell <CELL>
          the data type of one cell in the memory (tape).

          [default: int8]
          [possible values: int8, int32]

      --overflow <OVERFLOW>
          the operation the interpreter should do when an overflow error occurs.

           - wrap: automatically wrap the value in cell (e.g.: `127 + 1` => `-127`)
           - error: throw an error and abort

          [default: wrap]
          [possible values: wrap, error]

      --eof <EOF>
          the operation the interpreter should do when an `EOF` is read.

           - zero: turn `EOF` to `0`
           - keep: keep what the `EOF` is and return it (`EOF == -1`)
           - ignore: ignore this input and leave the cell unchanged

          [default: ignore]
          [possible values: zero, keep, ignore]

      --input <INPUT>
          the input stream type.

          [default: std]
          [possible values: null, std]

      --output <OUTPUT>
          the output stream type.

          [default: char-std]
          [possible values: char-std, int-std]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Ext Link: https://github.com/ctj12461/brainfuck-interpreter

评论区

写评论
Snowmanzzz 2023-02-13 15:33

hackerrank很多好玩题目

1 共 1 条评论, 1 页