12 个不稳定版本 (5 个破坏性更新)
0.6.0 | 2023 年 7 月 31 日 |
---|---|
0.5.0 | 2023 年 6 月 8 日 |
0.4.1 | 2021 年 2 月 12 日 |
0.4.0 | 2021 年 1 月 30 日 |
0.1.4 | 2020 年 5 月 14 日 |
#694 在 解析器实现
每月 36 次下载
44KB
566 代码行
Pancake Stack
这是 Pancake Stack 奇特编程语言的 Rust 实现。此包包括解析器和解释器。
Pancake Stack 是由 User JWinslow23 在 2013 年创建的基于栈的奇特编程语言,其中程序需要你操作一个 薄饼 栈。
使用方法
要使用 pancakestack,首先在您的 Cargo.toml 中包含此内容
[dependencies]
"pancakestack" = "0.5"
包示例
基本使用
程序可以使用 pancakestack::parse_program_str
进行解析,并使用 pancakestack::run_program
运行。
// load program from file
let mut file = File::open("example.pancake").unwrap();
let mut program_str = String::new();
file.read_to_string(&mut program_str).unwrap();
// parse the program
let program = pancakestack::parse_program_str(&program_str);
// run the program
pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
或者您可以使用 str
或 Read
运行程序,分别使用 pancakestack::run_program_str
和 pancakestack::run_program_from_read
。
// load script file
let mut file = File::open("example.pancake").unwrap();
// write program into string
let mut program = String::new();
file.read_to_string(&mut program).unwrap();
pancakestack::run_program_str(&program, std::io::stdin(), std::io::stdout()).unwrap();
// open script file
let mut file = File::open("example.pancake").unwrap();
// run the script directly from the file
pancakestack::run_program_from_read(file, std::io::stdin(), std::io::stdout()).unwrap();
所有 pancakestack::run_*
方法接受一个 Read
作为脚本的输入和一个 Write
作为输出。
到目前为止的示例使用了 stdin()
和 stdout()
,但可以使用任何实现了 Read
和 Write
的类型。以下示例展示了如何使用字符串作为输入和输出。
let file = File::open("example.pancake").unwrap();
let input = b"some input";
let mut output_buf = Vec::new();
pancakestack::run_program_from_read(file, &input[..], &mut output_buf).unwrap();
let output = std::str::from_utf8(&output_buf).unwrap();
构建程序
程序可以使用 pancakestack::parse_program_str
从 str
解析出来。一行(即命令)可以用 BorrowedCommand::from_line
解析。
解析后的程序是 BorrowedCommand
的切片,可以用 pancakestack::run_program
运行。
use pancakestack::BorrowedCommand;
let program = [
BorrowedCommand::PutThisPancakeOnTop("test"),
BorrowedCommand::ShowMeAPancake,
BorrowedCommand::EatAllOfThePancakes
];
pancakestack::run_program(&program, std::io::stdin(), std::io::stdout()).unwrap();
语言语法
pancake stack 初始为空。
代码 | 意义 |
---|---|
将这个 X 松饼放在顶部! | 将 X 的词长度推入栈顶,即 "wonderful" 会推入 9。 |
吃掉顶部的松饼! | 从栈顶弹出值并丢弃。 |
把顶部的松饼放在一起! | 弹出顶部两个值,将它们相加,然后推入结果。 |
给我一个松饼! | 输入一个数值并将其推入栈中。 |
热饼怎么样? | 输入一个 ASCII 值并将其推入栈中。 |
给我看看一个松饼! | 将栈顶值输出为 ASCII 字符,但不弹出。 |
从顶部取下松饼! | 弹出顶部两个值,从第二个值中减去第一个值,然后推入结果。 |
翻转顶部的松饼! | 弹出顶部两个值,交换它们,然后将它们推回。 |
再放一个松饼在顶部! | 弹出顶部值并将其推入两次。 |
[标签] | 定义一个可以返回的标签(如果需要,也可以定义注释)。当你返回标签时,它会跳转到标签定义时栈顶值的行号(从 1 开始计数)。 |
如果松饼不好吃,就转到 "标签"。 | 如果顶部的值是 0,则转到标签 [标签]。 |
如果松饼好吃,就转到 "标签"。 | 与上面相同,但如果顶部的值不是 0。 |
给松饼加糖浆! | 增加所有栈值。 |
给松饼加黄油! | 仅增加顶部栈值。 |
去掉糖浆! | 减少所有栈值。 |
去掉黄油! | 仅减少顶部栈值。 |
吃掉所有的松饼! | 终止程序。 |
实现说明
热饼怎么样??
当没有输入时推入0
[label]
会覆盖同名存在的标签。- 超出和小于
u32
的范围会导致错误(不是panic
)。
语言示例
Hello World!
Put this heavenly pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put syrup on the pancakes!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Show me a pancake!
Put this appetizing pancake on top!
Put this delectable pancake on top!
Put this delicious pancake on top!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Show me a pancake!
Put this wonderful pancake on top!
Take off the syrup!
Put the top pancakes together!
Show me a pancake!
Show me a pancake!
Put this rich pancake on top!
Take off the butter!
Put the top pancakes together!
Show me a pancake!
Put this delightful pancake on top!
Put this dainty pancake on top!
Put the top pancakes together!
Put another pancake on top!
Put the top pancakes together!
Show me a pancake!
Put this tasty pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put another pancake on top!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Put the top pancakes together!
Show me a pancake!
Eat the pancake on top!
Show me a pancake!
Put this good pancake on top!
Take off the butter!
Put the top pancakes together!
Show me a pancake!
Put this divine pancake on top!
Flip the pancakes on top!
Take from the top pancakes!
Show me a pancake!
Put this pleasant pancake on top!
Flip the pancakes on top!
Take from the top pancakes!
Show me a pancake!
Put this mouthwatering pancake on top!
Put this scrumptious pancake on top!
Put this enjoyable pancake on top!
Put the top pancakes together!
Put the top pancakes together!
Show me a pancake!
Eat all of the pancakes!
Cat
Put this old pancake on top!
[CAT]
Eat the pancake on top!
How about a hotcake?
Show me a pancake!
If the pancake is tasty, go over to "CAT".
Eat all of the pancakes!
其他示例可以在 examples 目录中找到。
许可
根据MIT许可证授权(《LICENSE》或http://opensource.org/licenses/MIT)
依赖项
~3–4.5MB
~78K SLoC