5 个版本
0.2.2 | 2022年5月12日 |
---|---|
0.2.1 | 2022年5月11日 |
0.2.0 | 2022年5月10日 |
0.1.1 | 2022年5月4日 |
0.1.0 | 2022年5月4日 |
#311 在 编程语言 中
79KB
1.5K SLoC
Sorth
这是一个基于Rust语言的、受FORTH启发的语言解释器。其主要特点是易于扩展,并且对无std环境友好,只需全局分配器。
语言特性
将值推送到栈上
12 // int
" hello " // string
12.0 // double
12.0f // float
2l // long
0x23 // byte
" hello world " // Note. This will be interpreted as " hello world ". This is the limitation of the interpreters architecture
特殊字符
<ASCII character number as byte> emit // Emits the number as a character
// For convenience purposes
nl // Writes a newline to the output
数学函数
// Currently available math operations
+ - * /
// Exmaple
12 24 + . // 36 Ok.
逻辑运算符和比较
and // logic and
or // logic or
not // logic not
== // equal
!= // not equal
> // higher
< // Lower
-1 // true
0 // false
栈操作函数
. // Pops and prints
peek // Just prints
dup // duplicates top of the stack
2dup // duplicates two highest values on the stack
drop // Drops top of the stack
swap // Swaps two highest values on the stack
rot // Rotates 3 top values on the stack example [ 3 2 1 ] -> [ 1 3 2 ]
自定义单词
: // start defining
<name>
... // here put the contents of the word
; // end defining
if语句(仅限于自定义单词)
if ... else ... then
^ execute if true
^ execute if false
^ pop top of the stack and see if true
for语句(仅限于自定义单词)
<end> <start> for ... next // increment by 1 until >= than <end>
<end> <start> for ... <increment> bynext // increment by <increment> until >= than <end>
while语句(仅限于自定义单词)
while <condition> do ... again (you can ommit "<condition> do" if you want a never ending loop)
变量
let <variable name> // create a variable
@<variable name> // get variable address
<variable addr> <val> push // push <val> to varable
<variable addr> pop // pop value from variable and put it on the main stack
<variable addr> <addr> get // get value from variable table at position <addr>
<variable addr> <addr> <val> set // set value of variable table at position <addr> to <val> if position exists
<varible addr> len // get lenght of the variable internal stack
类型转换
// Convert top of the stack to the respectable value
to_int
to_long
to_float
to_double
to_byte
to_str
输入
" This message will be displayed as a question/request for the input to the user " input // pushes user input to the stack
静默模式
<state either -1/0 > silent // turn the "Ok." messages on or off
注释
( this is a comment )
斐波那契示例
: fib
0
for
dup
rot
+
peek
next
;
0l 1l 10 fib
可以使用以下方式执行
cargo run --example file_exec fibonacci
用法
将此添加到 Cargo.toml
[dependencies]
sorth = "0.2"
示例
终端示例
use sorth::prelude::*;
fn main() {
let mut engine = Engine::new();
let std_words = Standard::new();
engine.import_word_list(std_words);
while engine.running {
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
match engine.eval(line) {
Ok(ok) => println!("{}", ok.trim()),
Err(err) => println!("Error: {}", err.trim()),
}
}
}
运行
cargo run --examples terminal
从文件执行示例
要运行示例,请使用
cargo run --example file_exec <Sorth program example>
目前可用的Sorth程序示例
fibonacci
turing
例如
cargo run --example file_exec turing
扩展单词集
如果您想从您的代码中添加额外的功能来扩展单词集,这是完全可能的。这正好是以包括标准单词集相同的方式进行。您需要创建一个实现WordList
trait的结构。这个trait将允许您将单词集导入sorth引擎。下面是这样一个结构的示例
sturct standard {
words: Vec<Word>,
}
单词类型基本上是一个包含两个主要元素的元组。第1个元素是一个函数,它告诉引擎何时执行单词。而第2个元素是一个函数,它告诉引擎确切要做什么。以下是单词的定义和用法示例。
// Taken from word.rs
pub type WordSymbol = fn(s: &Engine) -> bool;
pub type WordDefinition = fn(s: &mut Engine) -> Result<String, String>;
pub type Word = (WordSymbol, WordDefinition);
// Taken from standard/mod.rs
(|s| s.get_curr_word() == "let" && s.mode_normal(), let_word),
// Remember to specify this ^^^^^^^^^^^^^^^ Important!!!
如果您不希望破坏标准单词集,正常模式是您想进行的唯一模式!!!
no_std环境
如果您想在no_std
环境中使用sorth,那么您需要提供的只是一个global allocator
。以下是一个示例,说明如何实现它。[此处插入链接]
变更日志
待办事项
- 泛型数学运算
- 泛型逻辑运算
- 泛型栈运算
- 运行时单词定义
- 条件逻辑
- 循环
- 字符串支持
- Int, Long 和 Byte 支持
- Float 和 Double 支持
- 变量支持
- 扩展数学运算
- 文件访问词集标准
- 排序用户输入接口
- 将排序重构成库模块
- 在 crates.io 上分享
- 创建一本书
许可证
Sorth 在 MIT 许可证下分发。详情请见 LICENSE。