Refactored again, Fixed most warnings, Removed unused code
This commit is contained in:
parent
5bd49acb67
commit
98023fe219
|
@ -1,6 +1,5 @@
|
|||
use super::ParseError;
|
||||
use crate::lexer::Token;
|
||||
use crate::parser::Parser;
|
||||
use crate::parser::{ParseError, Parser};
|
||||
use std::error::Error;
|
||||
|
||||
pub enum Node {
|
||||
|
@ -14,15 +13,6 @@ pub enum StatementType {
|
|||
Ident(Identifier),
|
||||
}
|
||||
|
||||
pub trait Statement {
|
||||
fn parse(&mut Parser) -> Result<StatementType, Box<dyn Error>>
|
||||
where
|
||||
Self: Sized;
|
||||
fn new() -> StatementType
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct LetStatement {
|
||||
name: Identifier,
|
||||
|
|
|
@ -3,7 +3,7 @@ mod program;
|
|||
|
||||
pub use self::program::Program;
|
||||
|
||||
use self::ast::{LetStatement, Statement, StatementType};
|
||||
use self::ast::{LetStatement, StatementType};
|
||||
use crate::lexer::{Lexer, Token};
|
||||
use std::iter::Peekable;
|
||||
|
||||
|
@ -27,17 +27,14 @@ impl<'a> Parser<'a> {
|
|||
Err(_) => None, //TODO: Return appropriate error
|
||||
}
|
||||
}
|
||||
n @ _ => {
|
||||
println!("{:?}", n);
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_peek(&mut self, token: Token) -> bool {
|
||||
if let Some(v) = self.lexer.peek() {
|
||||
if v == &token {
|
||||
self.lexer.next();
|
||||
self.current_token = self.lexer.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
@ -53,7 +50,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ParseError {
|
||||
pub struct ParseError {
|
||||
desc: String,
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::lexer::{Lexer, Token};
|
||||
use crate::parser::ast::{Statement, StatementType};
|
||||
use crate::parser::ast::StatementType;
|
||||
use crate::parser::Parser;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -13,7 +13,7 @@ impl Program {
|
|||
let mut parser = Parser::new(lexer);
|
||||
loop {
|
||||
if let Some(token) = parser.lexer.next() {
|
||||
if token == Token::EOF {
|
||||
if parser.current_token_is(Token::EOF) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -53,14 +53,12 @@ mod tests {
|
|||
let lexer = Lexer::new(ip);
|
||||
let ast_tree = Program::parse(lexer);
|
||||
|
||||
println!("{:?}", ast_tree);
|
||||
if ast_tree.statements.len() != 3 {
|
||||
assert_eq!(ast_tree.statements.len(), 3);
|
||||
}
|
||||
|
||||
for (out, expected_out) in ast_tree.statements.iter().zip(out.statements.iter()) {
|
||||
assert_eq!(out, expected_out);
|
||||
println!("out={:?}, expected_out={:?}", out, expected_out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,6 @@ fn start<R: BufRead, W: Write>(mut ip: R, mut out: W) {
|
|||
println!("{:?}", token);
|
||||
}
|
||||
|
||||
let parser = Program::parse(tokens);
|
||||
let _parser = Program::parse(tokens);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user