Code Monkey home page Code Monkey logo

pl0's Introduction

qiufeng's github stats

Top Langs

pl0's People

Contributors

wutongshenqiu avatar

Watchers

 avatar

pl0's Issues

main.rs 改进建议

  1. L208 这里确实没必要 Vec<Box<>> 的,直接 Vec<> 就行了,Vec<> 本身就是 : Sized 的,甚至用 Option<> 也是可以的,因为这些只是类型上循环引用了,本身的值是个树状结构,是有很明确的 ownership 的,Option<> 能节约一些内存分配。
    L220 有一个办法就是让其他所有的类型直接引用 ASTNode,然后在 ASTNode 里存在类型引用循环的地方改用 Option<>

  2. L251 这里其实可以直接用 parse(self) consume 掉整个 parser,毕竟从语义上来说,Lexer 本来也就是一次性的。

  3. L7 OperatorKeyword 其实可以都用 enum 来定义,这样在后面的代码里面做 pattern matching 会方便很多。

  4. L576 其实 Rust 的 pattern matching 可以直接 match box 的:

match *ast {
    ASTNode::Program(box ASTNode::Block {
        consts,
        vars,
        procedures,
        stmt,
    }) => ...
}

不过这需要 nightly rust 以及 #![feature(box_patterns)]

修改建议 2.0

  1. Token::Keyword(kw) if matches!(kw, KeywordToken::Procedure) => {
    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) {
    // ...
}
  1. impl From<String> for Token {
    两个 impl From 可以合成一个:
impl<T: AsRef<str>> From<T> for Token {
    // ...
}
  1. Token::Operator(op) => match op {
    这个嵌套的 match 可以跟外层打平:
match self.lexer.get_next_token()? {
    Token::Operator(OperatorToken::Semi) => break,
    Token::Operator(OperatorToken::Comma) => {
        // ...
    }
    Token::Operator(x) => {
        // ...
    }
    x => {
        // ...
    }
}

Token::Operator(op) => match op {
这儿也是。

Token::Keyword(kw) => match kw {
还有这儿,以及其他类似的地方。

  1. if let 1 = cond.eval(context)?.ok_or(Pl0Error::InvalidIf)? {
    这儿直接 == 1 就可以了。
    while let 1 = cond.eval(context)?.ok_or(Pl0Error::InvalidWhile)? {
    这儿也是。

  2. pub struct Ir {
    其实 Ir 可以整个定义为一个 enum 的。

  3. pub struct Intepreter {}
    没有字段的 struct 可以直接写成 pub struct Interpreter;,这样甚至可以当做是一个全局常量来用。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.