2 个不稳定版本
0.2.0 | 2023年10月28日 |
---|---|
0.1.0 | 2023年10月28日 |
#2125 in 解析器实现
43KB
796 行
Letterbox
由 Chris Natcharian 开发的实验性 esoteric 语言。
本软件包的目的是定义一个简单、前端无关的 API,以便更复杂的应用程序可以在隔离环境中运行 Letterbox 程序。程序从字符串进行解释并创建字符串作为输出;数据独立存储,可以在程序之间重复使用。
本软件包的组成部分
LbStorage
代表一个包含 26 个变量的存储库,每个变量都存储在字母表中的小写字母('a' 到 'z')下。每个变量可以存储一个 String 或一个 float。LbToken
是一个由 Logos 派生出来的枚举,它定义了 Letterbox 语言的合法标记。一个LbToken::lexer
实例可以将文本 Letterbox 程序转换为单个标记,并解析出它们的参数。LbProgram
消耗一个包含零个或多个标记的词法分析器,并在一些LbStorage
上执行它们。它还可以接受程序参数并公开程序输出。
有关更多详细信息,请参阅 软件包文档。
使用此软件包
letterbox-lang 可在 crates.io 上获得。将其添加到您的 Cargo.toml 文件中的依赖项。
[dependencies]
...
letterbox-lang = "0.2.0"
如果您想为此软件包做出贡献并对语言进行自己的修改,请克隆或分叉仓库并将其添加到您的项目中作为本地依赖项。例如
[dependencies]
...
letterbox-lang = { path = "../letterbox-lang" }
如何编写 Letterbox 程序
请参阅语言文档在 其 Esolang 维基页面。
如何运行 Letterbox 程序
这是一个执行 Letterbox 程序的最小 Rust 程序示例。有关更完整的示例,请参阅 Letterbox 命令行工具。
// import all required types
use letterbox_lang::prelude::*;
// get string representation of program
let program_string = "P'Hello world'".to_string();
// Create a lexer to consume the string
let lex: Lexer<LbToken> = LbToken::lexer(program_string);
// Create a new data storage struct on which the program will operate
let mut data: LbStorage = LbStorage::new();
// Get a string of whitespace-separated program arguments
let input_vec = "".to_string();
// Get an empty string with which to collect program output
let mut output_buffer = String::new();
// Define how many loop iterations are allowed before the program halts to prevent infinite loops
let loop_limit: usize = 100;
// Create a program struct which consumes the previous components
let mut program = LbProgram::new(
lex,
&mut data,
&input_vec,
&mut output_buffer,
loop_limit
).expect("Error initializing program");
// Run the program. This can be done only once.
let program_result: Result<(), String> = program.run();
// If the program results in a string, an error has occurred.
// Otherwise, the program succeeded.
if let Err(msg) = program_result {
println!("Error occurred: {}", msg);
}
else {
println!("{}", output_buffer); // prints Hello world!
}
依赖关系
~4.5MB
~54K SLoC