Refactored again, Fixed most warnings, Removed unused code

This commit is contained in:
Ishan Jain 2019-09-08 00:17:08 +05:30
parent 5bd49acb67
commit 98023fe219
4 changed files with 8 additions and 23 deletions

View File

@ -1,6 +1,5 @@
use super::ParseError;
use crate::lexer::Token; use crate::lexer::Token;
use crate::parser::Parser; use crate::parser::{ParseError, Parser};
use std::error::Error; use std::error::Error;
pub enum Node { pub enum Node {
@ -14,15 +13,6 @@ pub enum StatementType {
Ident(Identifier), 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)] #[derive(Debug, PartialEq)]
pub struct LetStatement { pub struct LetStatement {
name: Identifier, name: Identifier,

View File

@ -3,7 +3,7 @@ mod program;
pub use self::program::Program; pub use self::program::Program;
use self::ast::{LetStatement, Statement, StatementType}; use self::ast::{LetStatement, StatementType};
use crate::lexer::{Lexer, Token}; use crate::lexer::{Lexer, Token};
use std::iter::Peekable; use std::iter::Peekable;
@ -27,17 +27,14 @@ impl<'a> Parser<'a> {
Err(_) => None, //TODO: Return appropriate error Err(_) => None, //TODO: Return appropriate error
} }
} }
n @ _ => { _ => None,
println!("{:?}", n);
None
}
} }
} }
fn expect_peek(&mut self, token: Token) -> bool { fn expect_peek(&mut self, token: Token) -> bool {
if let Some(v) = self.lexer.peek() { if let Some(v) = self.lexer.peek() {
if v == &token { if v == &token {
self.lexer.next(); self.current_token = self.lexer.next();
true true
} else { } else {
false false
@ -53,7 +50,7 @@ impl<'a> Parser<'a> {
} }
#[derive(Debug)] #[derive(Debug)]
struct ParseError { pub struct ParseError {
desc: String, desc: String,
} }

View File

@ -1,5 +1,5 @@
use crate::lexer::{Lexer, Token}; use crate::lexer::{Lexer, Token};
use crate::parser::ast::{Statement, StatementType}; use crate::parser::ast::StatementType;
use crate::parser::Parser; use crate::parser::Parser;
#[derive(Debug)] #[derive(Debug)]
@ -13,7 +13,7 @@ impl Program {
let mut parser = Parser::new(lexer); let mut parser = Parser::new(lexer);
loop { loop {
if let Some(token) = parser.lexer.next() { if let Some(token) = parser.lexer.next() {
if token == Token::EOF { if parser.current_token_is(Token::EOF) {
break; break;
} }
@ -53,14 +53,12 @@ mod tests {
let lexer = Lexer::new(ip); let lexer = Lexer::new(ip);
let ast_tree = Program::parse(lexer); let ast_tree = Program::parse(lexer);
println!("{:?}", ast_tree);
if ast_tree.statements.len() != 3 { if ast_tree.statements.len() != 3 {
assert_eq!(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()) { for (out, expected_out) in ast_tree.statements.iter().zip(out.statements.iter()) {
assert_eq!(out, expected_out); assert_eq!(out, expected_out);
println!("out={:?}, expected_out={:?}", out, expected_out);
} }
} }
} }

View File

@ -24,6 +24,6 @@ fn start<R: BufRead, W: Write>(mut ip: R, mut out: W) {
println!("{:?}", token); println!("{:?}", token);
} }
let parser = Program::parse(tokens); let _parser = Program::parse(tokens);
} }
} }