Rena是我在Rust中实现的Lox语言的树遍历解释器。我用rust重写它,以熟悉rust的borrow checker和提供的各种工具链。
在这篇文章中,我将解释我如何为解释器实现一个简单的环境。
Gitlab 链接,https://github.com/veera-sivarajan/rena
文章链接,https://veera.app/rena's_memory_model.html
Yap是一个小型的、零依赖的解释器库,灵感来自于parser-combinator。我试图以简洁性换取简单性,并以迭代器接口的灵活性为基础。它的目标是使解析字符串和切片变得容易,并且易于使用。
在过去的几个星期里,我一直在构建和使用它,我认为它已经准备好向其他可能有兴趣使用它的人发布了!
下面是它的用法:
use yap::{
// This trait has all of the parsing methods on it:
Tokens,
// Allows you to use `.into_tokens()` on strings and slices,
// to get an instance of the above:
IntoTokens
};
// Step 1: convert our input into something implementing `Tokens`
// ================================================================
let mut tokens = "10 + 2 x 12-4,foobar".into_tokens();
// Step 2: Parse some things from our tokens
// =========================================
#[derive(PartialEq,Debug)]
enum Op { Plus, Minus, Multiply }
#[derive(PartialEq,Debug)]
enum OpOrDigit { Op(Op), Digit(u32) }
// The `Tokens` trait builds on `Iterator`, so we get a `next` method.
fn parse_op(t: &mut impl Tokens<Item=char>) -> Option<Op> {
match t.next()? {
'-' => Some(Op::Minus),
'+' => Some(Op::Plus),
'x' => Some(Op::Multiply),
_ => None
}
}
// We also get other useful functions..
fn parse_digits(t: &mut impl Tokens<Item=char>) -> Option<u32> {
let s: String = t
.tokens_while(|c| c.is_digit(10))
.collect();
s.parse().ok()
}
// As well as combinator functions like `sep_by_all` and `surrounded_by`..
let op_or_digit = tokens.sep_by_all(
|t| t.surrounded_by(
|t| parse_digits(t).map(OpOrDigit::Digit),
|t| { t.skip_tokens_while(|c| c.is_ascii_whitespace()); }
),
|t| parse_op(t).map(OpOrDigit::Op)
);
// Now we've parsed our input into OpOrDigits, let's calculate the result..
let mut current_op = Op::Plus;
let mut current_digit = 0;
for d in op_or_digit {
match d {
OpOrDigit::Op(op) => {
current_op = op
},
OpOrDigit::Digit(n) => {
match current_op {
Op::Plus => { current_digit += n },
Op::Minus => { current_digit -= n },
Op::Multiply => { current_digit *= n },
}
},
}
}
assert_eq!(current_digit, 140);
// Step 3: do whatever you like with the rest of the input!
// ========================================================
// This is available on the concrete type that strings
// are converted into (rather than on the `Tokens` trait):
let remaining = tokens.remaining();
assert_eq!(remaining, ",foobar");
Gitlab 链接,https://github.com/jsdw/yap
文章链接,https://www.reddit.com/r/rust/comments/r3blx1/announcing_yap_a_small_iterator_based_zero/
今天我发布了新版本的AMD显卡冷却和电压守护程序。
当前版本包括:
非常简单的电压管理
有关如何启用电压管理的信息 ...
Gitlab 链接,https://github.com/Eraden/amdgpud
文章链接,https://www.reddit.com/r/rust/comments/r1wyu7/release_amdfand_v106/
From 日报小组 TOM
社区学习交流平台订阅:
Rustcc论坛: 支持rss
微信公众号:Rust语言中文社区