Refactored parser back to using traits rather than enum, Added gitignore, Added return statement
This commit is contained in:
parent
98023fe219
commit
68330a8156
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
target/
|
||||
.vscode/settings.json
|
||||
.vscode/launch.json
|
||||
|
|
|
@ -1,52 +1,68 @@
|
|||
use crate::lexer::Token;
|
||||
use crate::parser::{ParseError, Parser};
|
||||
use crate::{
|
||||
lexer::Token,
|
||||
parser::{ParseError, Parser},
|
||||
};
|
||||
use std::any::Any;
|
||||
use std::error::Error;
|
||||
|
||||
pub enum Node {
|
||||
Statement(StatementType),
|
||||
Expression,
|
||||
pub trait Node {
|
||||
fn string(&self) -> String
|
||||
where
|
||||
Self: Sized + Any;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum StatementType {
|
||||
Let(LetStatement),
|
||||
Ident(Identifier),
|
||||
pub trait Statement: Node {
|
||||
fn parse(&mut Parser) -> Result<Self, ParseError>
|
||||
where
|
||||
Self: Sized + Any;
|
||||
}
|
||||
|
||||
pub trait Expression: Node {
|
||||
fn parse(&mut Parser) -> Result<Self, Box<dyn Error>>
|
||||
where
|
||||
Self: Sized + Any;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct LetStatement {
|
||||
pub struct Let {
|
||||
name: Identifier,
|
||||
// value: dyn Expression,
|
||||
}
|
||||
|
||||
impl LetStatement {
|
||||
impl Let {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(identifier: Identifier) -> StatementType {
|
||||
StatementType::Let(LetStatement { name: identifier })
|
||||
pub fn new(identifier: Identifier) -> Let {
|
||||
Let { name: identifier }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(parser: &mut Parser) -> Result<LetStatement, Box<dyn Error>> {
|
||||
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<Let, ParseError> {
|
||||
let name;
|
||||
|
||||
//TODO: Add expression parser
|
||||
match parser.lexer.next() {
|
||||
Some(v) => match v {
|
||||
Token::Ident(q) => name = Identifier { name: q },
|
||||
n @ _ => {
|
||||
return Err(Box::new(ParseError::new(&format!(
|
||||
"expected IDENT, Found {:?}",
|
||||
n
|
||||
))))
|
||||
return Err(ParseError::new(&format!("expected IDENT, Found {:?}", n)));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(Box::new(ParseError::new(
|
||||
return Err(ParseError::new(
|
||||
"expected IDENT after let, Could not find it",
|
||||
)))
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if !parser.expect_peek(Token::Assign) {
|
||||
return Err(Box::new(ParseError::new("expected =, Could not find it")));
|
||||
return Err(ParseError::new("expected =, Could not find it"));
|
||||
}
|
||||
|
||||
// TODO: Replace this with code to parse expressions correctly
|
||||
|
@ -54,7 +70,37 @@ impl LetStatement {
|
|||
parser.current_token = parser.lexer.next();
|
||||
}
|
||||
|
||||
Ok(LetStatement { name })
|
||||
Ok(Let { name })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Return {
|
||||
return_value: Expr,
|
||||
}
|
||||
|
||||
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<Return, ParseError> {
|
||||
while !parser.current_token_is(Token::Semicolon) {
|
||||
parser.current_token = parser.lexer.next();
|
||||
}
|
||||
|
||||
Ok(Return { return_value: Expr })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ mod program;
|
|||
|
||||
pub use self::program::Program;
|
||||
|
||||
use self::ast::{LetStatement, StatementType};
|
||||
use self::ast::{Let, Return, Statement};
|
||||
use crate::lexer::{Lexer, Token};
|
||||
use std::iter::Peekable;
|
||||
|
||||
|
@ -19,28 +19,31 @@ impl<'a> Parser<'a> {
|
|||
current_token: None,
|
||||
}
|
||||
}
|
||||
fn parse_statement(&mut self, token: Token) -> Option<ast::StatementType> {
|
||||
fn parse_statement(&mut self, token: Token) -> Result<Box<dyn ast::Statement>, ParseError> {
|
||||
match token {
|
||||
Token::Let => {
|
||||
match LetStatement::parse(self) {
|
||||
Ok(v) => Some(StatementType::Let(v)),
|
||||
Err(_) => None, //TODO: Return appropriate error
|
||||
Token::Let => match Let::parse(self) {
|
||||
Ok(v) => Ok(Box::new(v)),
|
||||
Err(e) => Err(e), //TODO: Return appropriate error
|
||||
},
|
||||
Token::Return => match Return::parse(self) {
|
||||
Ok(v) => Ok(Box::new(v)),
|
||||
Err(e) => Err(e),
|
||||
},
|
||||
n @ _ => {
|
||||
println!("{:?}", n);
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_peek(&mut self, token: Token) -> bool {
|
||||
if let Some(v) = self.lexer.peek() {
|
||||
if v == &token {
|
||||
match self.lexer.peek() {
|
||||
Some(v) if v == &token => {
|
||||
self.current_token = self.lexer.next();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
Some(v) => false,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use crate::lexer::{Lexer, Token};
|
||||
use crate::parser::ast::StatementType;
|
||||
use crate::parser::Parser;
|
||||
use crate::parser::{ast::Statement, Parser};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Program {
|
||||
statements: Vec<StatementType>,
|
||||
statements: Vec<Box<dyn Statement>>,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
|
@ -13,20 +11,22 @@ impl Program {
|
|||
let mut parser = Parser::new(lexer);
|
||||
loop {
|
||||
if let Some(token) = parser.lexer.next() {
|
||||
parser.current_token = Some(token.clone());
|
||||
if parser.current_token_is(Token::EOF) {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(stmt) = parser.parse_statement(token) {
|
||||
statements.push(stmt);
|
||||
} else {
|
||||
match parser.parse_statement(token) {
|
||||
Ok(v) => statements.push(v),
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("statements={:?}", statements);
|
||||
Program { statements }
|
||||
}
|
||||
}
|
||||
|
@ -34,8 +34,9 @@ impl Program {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::lexer::Lexer;
|
||||
use crate::parser::ast::{Identifier, LetStatement};
|
||||
use crate::parser::ast::{Identifier, Let, Statement};
|
||||
use crate::parser::Program;
|
||||
use std::any::Any;
|
||||
#[test]
|
||||
fn let_statements() {
|
||||
let ip = "
|
||||
|
@ -43,22 +44,39 @@ mod tests {
|
|||
let qq = 10;
|
||||
let foobar = 8388383;
|
||||
";
|
||||
let out = Program {
|
||||
statements: vec![
|
||||
LetStatement::new(Identifier::new("yr")),
|
||||
LetStatement::new(Identifier::new("qq")),
|
||||
LetStatement::new(Identifier::new("foobar")),
|
||||
],
|
||||
};
|
||||
|
||||
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"))),
|
||||
];
|
||||
let lexer = Lexer::new(ip);
|
||||
let ast_tree = Program::parse(lexer);
|
||||
let as_tree = Program::parse(lexer);
|
||||
|
||||
if ast_tree.statements.len() != 3 {
|
||||
assert_eq!(ast_tree.statements.len(), 3);
|
||||
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<Let> =
|
||||
if std::any::TypeId::of::<Let>() == std::any::TypeId::of::<dyn Statement>() {
|
||||
Some(unsafe { std::mem::transmute::<Box<dyn Statement>, Let>(out) })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if Box::new(out.unwrap()) == expected_out {}
|
||||
}
|
||||
}
|
||||
|
||||
for (out, expected_out) in ast_tree.statements.iter().zip(out.statements.iter()) {
|
||||
assert_eq!(out, expected_out);
|
||||
}
|
||||
#[test]
|
||||
fn return_statements() {
|
||||
let ip = "
|
||||
return 5;
|
||||
return 10;
|
||||
return 80932;
|
||||
";
|
||||
|
||||
let lexer = Lexer::new(ip);
|
||||
let as_tree = Program::parse(lexer);
|
||||
assert_eq!(as_tree.statements.len(), 3);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user