#esolang #interpreter #poetry #esopo

bin+lib ashpaper-plus

William Hicks构想的Esopo语言AshPaper的Rust解释器

7个版本

0.5.1 2021年3月6日
0.5.0 2021年3月6日
0.4.4 2021年2月28日

#89 in 模拟器

每月25次下载

MIT许可

1MB
1K SLoC

.github/workflows/ci.yml Crates.io Version Crates.io Crates.io Crates.io

AshPaper Plus

AshPaper Plus是完全符合规范的Esopo语言解释器,由William Hicks构想。您可以在William Hicks的亲自讲述中了解更多关于它和Esopo项目的信息这里。Daniel Temkin也在esoteric.codes上写了关于它的文章,您可以在这里阅读。当然,还有规范!您可以在这里查看它这里

安装

命令行界面

cargo install --features="cli" ashpaper-plus

带有即时编译

cargo install --features="cli jit" ashpaper-plus

将此添加到您的cargo.toml

ashpaper-plus = "0.5"

带有即时编译

ashpaper-plus = { version = "0.5", features = ["jit"] }

使用方法

从命令行界面

# execute a program
ashpaper-plus poems/lovely-poem.eso # prints 24
# jit execute a program
ashpaper-plus --jit poems/lovely-poem.eso # prints 24
# count syllables
ashpaper-plus -s "hello world, born to think and not to feel" # prints 10

作为库

use std::fs;

use ashpaper_plus as ashpaper;

pub fn main() {
    let fname = "lovely-poem.eso";
    let contents = fs::read_to_string(fname).expect("Something went wrong reading input file!");
    print!("{}", ashpaper::program::execute(&contents));
    // or for jit compilation:
    print!("{}", ashpaper::program::jit_execute(&contents).unwrap())
}

将生成以下字符串

24

工作原理

诗歌是你的程序。

你有两个寄存器可供使用,r0和r1,它们存储有符号整数(i64)。你还有一个栈,可以存储有符号整数(边界仅为Vec<i64>isize::MAX = 9_223_372_036_854_775_807),或128个用于即时编译)。

寄存器的选择基于是否缩进一行——如果是,则选择r1,否则选择r0。

以下是您可用的指令(按优先级排序)

  • 与前一行押韵:如果寄存器0 < register 1,将前一行中出现的音节数推送到栈中。否则,将当前行中的音节数推送到栈中。
  • 行包含/:如果活动寄存器中的值大于行中的音节数,转到与活动寄存器中的值对应的行号。如果abs(n) <= lines,则n,否则n % lines。
  • 单词内部出现大写字母:取消活动寄存器的激活。
  • 单词开头出现大写字母:将寄存器相乘并将结果存储在活动寄存器中。
  • 行包含“like”或“as”:将寄存器相加并存储在活动寄存器中。
  • 行包含 ?:打印与活动寄存器值相关的ASCII字符。如果abs(n) <= u8::MAX n,否则n % u8::MAX。
  • 行包含 .:打印活动寄存器的整数值。
  • 行包含 ,:从堆栈中弹出并存储在活动寄存器中。
  • 行包含 -:将活动寄存器的值推送到堆栈。
  • 连续单词的变化:转到由活动寄存器指示的行
  • 空白行:无操作。
  • 其他所有内容:将行中音节数存储到活动寄存器中。

让我们以一个名为 lovely-poem.eso 的文件中的这首诗为例。这个诗程序(poegram‽)计算阶乘并输入标题中的音节数。(我从阅读威廉·希克斯的《其他木工》这首诗中学到了很多东西)

lovely poem

  it is a calculator, like a
      poem, is a poem, and finds
        factori-
          als
  The input is the syllAbles
in the title, count them, as one counts
  (q) what other poem, programs can be writ
  (a) anything a Turing
    machine-machine-machine
    would do
re/cur
    sion works too, in poems, programs, and this
       a lovely.
poem or a calculator or nothing
how lovely can it be?

当在命令行界面的环境变量中设置 RUST_LOG=info 时,你可以获取程序评估信息。下面是 lovely-poem.eso 的样子。

instruction                                         |  r0  |  r1  |  stack
--------------------------------------------------- | ---- | ---- | -------
lovely poem                                         |  4   |  0   | []
                                                    |  4   |  0   | []
  it is a calculator, like a                        |  4   |  4   | []
      poem, is a poem, and finds                    |  4   |  4   | []
        factori-                                    |  4   |  4   | [4]
          als                                       |  4   |  1   | [4]
  The input is the syllAbles                        |  4   |  -1  | [4]
in the title, count them, as one counts             |  3   |  -1  | [4]
  (q) what other poem, programs can be writ         |  3   |  4   | []
  (a) anything a Turing                             |  3   |  12  | []
    machine-machine-machine                         |  3   |  12  | [12]
    would do                                        |  3   |  2   | [12]
  it is a calculator, like a                        |  3   |  5   | [12]
      poem, is a poem, and finds                    |  3   |  12  | []
        factori-                                    |  3   |  12  | [12]
          als                                       |  3   |  1   | [12]
  The input is the syllAbles                        |  3   |  -1  | [12]
in the title, count them, as one counts             |  2   |  -1  | [12]
  (q) what other poem, programs can be writ         |  2   |  12  | []
  (a) anything a Turing                             |  2   |  24  | []
    machine-machine-machine                         |  2   |  24  | [24]
    would do                                        |  2   |  2   | [24]
re/cur                                              |  2   |  2   | [24]
    sion works too, in poems, programs, and this    |  2   |  24  | []
       a lovely.                                    |  2   |  24  | []
poem or calculator or nothing                     |  10  |  24  | []
how lovely can it be?                               |  10  |  24  | []

关于符合非正式规范的警告

  • 此时,我的实现可能与规范以意想不到的方式不符。如果您发现任何此类问题,请提出问题 ❤️ ❤️

依赖关系

~3–14MB
~139K SLoC