diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index 8d6e0fa..74369db 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -2,27 +2,19 @@ use crate::{ lexer::Token, parser::{ParseError, Parser}, }; -use std::any::Any; -use std::error::Error; -pub trait Node { - fn string(&self) -> String - where - Self: Sized + Any; +#[derive(Debug, PartialEq)] +pub enum Statement { + Let(Let), + + Return(Return), + //ExpressionStatement(ExpressionStatement), } -pub trait Statement: Node { - fn parse(&mut Parser) -> Result - where - Self: Sized + Any; +#[derive(Debug, PartialEq)] +pub enum Expression { + Ident(Identifier), } - -pub trait Expression: Node { - fn parse(&mut Parser) -> Result> - where - Self: Sized + Any; -} - #[derive(Debug, PartialEq)] pub struct Let { name: Identifier, @@ -34,16 +26,8 @@ impl Let { pub fn new(identifier: Identifier) -> Let { Let { name: identifier } } -} -impl Node for Let { - fn string(&self) -> String { - String::new() // TODO: Complete how I want to format strings - } -} - -impl Statement for Let { - fn parse(parser: &mut Parser) -> Result { + pub fn parse(parser: &mut Parser) -> Result { let name; //TODO: Add expression parser @@ -74,6 +58,9 @@ impl Statement for Let { } } +#[derive(Debug, PartialEq)] +pub struct Expr; + #[derive(Debug, PartialEq)] pub struct Return { return_value: Expr, @@ -83,19 +70,8 @@ impl Return { pub fn new() -> Return { Return { return_value: Expr } } -} -#[derive(Debug, PartialEq)] -pub struct Expr; // TODO: Replace with actual expressions parser - -impl Node for Return { - fn string(&self) -> String { - String::new() // TODO: Complete how I want to format return statements - } -} - -impl Statement for Return { - fn parse(parser: &mut Parser) -> Result { + pub fn parse(parser: &mut Parser) -> Result { while !parser.current_token_is(Token::Semicolon) { parser.current_token = parser.lexer.next(); } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1c164bd..b888115 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -19,14 +19,14 @@ impl<'a> Parser<'a> { current_token: None, } } - fn parse_statement(&mut self, token: Token) -> Result, ParseError> { + fn parse_statement(&mut self, token: Token) -> Result { match token { Token::Let => match Let::parse(self) { - Ok(v) => Ok(Box::new(v)), + Ok(v) => Ok(Statement::Let(v)), Err(e) => Err(e), //TODO: Return appropriate error }, Token::Return => match Return::parse(self) { - Ok(v) => Ok(Box::new(v)), + Ok(v) => Ok(Statement::Return(v)), Err(e) => Err(e), }, n @ _ => { @@ -42,8 +42,7 @@ impl<'a> Parser<'a> { self.current_token = self.lexer.next(); true } - Some(v) => false, - None => false, + Some(_) | None => false, } } diff --git a/src/parser/program.rs b/src/parser/program.rs index fefc695..db56c77 100644 --- a/src/parser/program.rs +++ b/src/parser/program.rs @@ -2,7 +2,7 @@ use crate::lexer::{Lexer, Token}; use crate::parser::{ast::Statement, Parser}; pub struct Program { - statements: Vec>, + statements: Vec, } impl Program { @@ -36,7 +36,7 @@ mod tests { use crate::lexer::Lexer; use crate::parser::ast::{Identifier, Let, Statement}; use crate::parser::Program; - use std::any::Any; + #[test] fn let_statements() { let ip = " @@ -46,9 +46,9 @@ mod tests { "; let expected_out = vec![ - Box::new(Let::new(Identifier::new("yr"))), - Box::new(Let::new(Identifier::new("qq"))), - Box::new(Let::new(Identifier::new("foobar"))), + Statement::Let(Let::new(Identifier::new("yr"))), + Statement::Let(Let::new(Identifier::new("qq"))), + Statement::Let(Let::new(Identifier::new("foobar"))), ]; let lexer = Lexer::new(ip); let as_tree = Program::parse(lexer); @@ -56,14 +56,7 @@ mod tests { assert_eq!(as_tree.statements.len(), 3); for (out, expected_out) in as_tree.statements.into_iter().zip(expected_out.into_iter()) { - let out: Option = - if std::any::TypeId::of::() == std::any::TypeId::of::() { - Some(unsafe { std::mem::transmute::, Let>(out) }) - } else { - None - }; - - if Box::new(out.unwrap()) == expected_out {} + assert_eq!(out, expected_out); } }