wutongshenqiu / pl0 Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
L208 这里确实没必要 Vec<Box<>>
的,直接 Vec<>
就行了,Vec<>
本身就是 : Sized
的,甚至用 Option<>
也是可以的,因为这些只是类型上循环引用了,本身的值是个树状结构,是有很明确的 ownership 的,Option<>
能节约一些内存分配。
L220 有一个办法就是让其他所有的类型直接引用 ASTNode
,然后在 ASTNode
里存在类型引用循环的地方改用 Option<>
。
L251 这里其实可以直接用 parse(self)
consume 掉整个 parser,毕竟从语义上来说,Lexer
本来也就是一次性的。
L7 Operator
和 Keyword
其实可以都用 enum 来定义,这样在后面的代码里面做 pattern matching 会方便很多。
L576 其实 Rust 的 pattern matching 可以直接 match box 的:
match *ast {
ASTNode::Program(box ASTNode::Block {
consts,
vars,
procedures,
stmt,
}) => ...
}
不过这需要 nightly rust 以及 #![feature(box_patterns)]
Line 165 in 872f77a
loop ... match ...
这里可以直接写成while matches!(self.lexer.peek_next_token()?, Token::Keyword(KeywordToken::Procedure)) {
// ...
}
或者
while let Token::Keyword(KeywordToken::Procedure) = self.lexer.peek_next_token()? {
// ...
}
我看 Token
是 derive 了 PartialEq
的,那其实也是可以直接写 ==
的:
while self.lexer.peek_next_token()? == Token::Keyword(KeywordToken::Procedure) {
// ...
}
Line 94 in 872f77a
impl From
可以合成一个:impl<T: AsRef<str>> From<T> for Token {
// ...
}
Line 128 in 872f77a
match
可以跟外层打平:match self.lexer.get_next_token()? {
Token::Operator(OperatorToken::Semi) => break,
Token::Operator(OperatorToken::Comma) => {
// ...
}
Token::Operator(x) => {
// ...
}
x => {
// ...
}
}
Line 150 in 872f77a
Line 193 in 872f77a
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.