From f202d0dacd8719c79dc508484455f724bc90baaa Mon Sep 17 00:00:00 2001 From: ishanjain28 Date: Sat, 31 Aug 2019 01:28:20 +0530 Subject: [PATCH] Resuming project. Refactored project --- src/lexer/mod.rs | 1 + src/main.rs | 3 +-- src/parser/ast/mod.rs | 41 +++++++++++++++++++++++++++---- src/parser/mod.rs | 47 ++++++++++++++++++------------------ src/parser/statement/mod.rs | 21 ---------------- src/{repl/mod.rs => repl.rs} | 4 +-- 6 files changed, 63 insertions(+), 54 deletions(-) delete mode 100644 src/parser/statement/mod.rs rename src/{repl/mod.rs => repl.rs} (87%) diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 2d83b7c..a4d3423 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -89,6 +89,7 @@ impl<'a> Lexer<'a> { None => false, } } + fn peek_is_ascii_digit(&mut self) -> bool { match self.input.peek() { Some(v) => v.is_ascii_digit(), diff --git a/src/main.rs b/src/main.rs index 33a6520..a14f690 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,5 +7,4 @@ mod repl; fn main() { repl::init(); -} - +} \ No newline at end of file diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index 7eef0fe..a3b1edf 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -1,10 +1,41 @@ use crate::lexer::Lexer; -#[derive(Debug)] -pub struct LetStatement { - name: String, +pub trait Node { + fn token_literal(&self) -> String; +} +pub trait Statement: Node { + fn parse(&self, &mut Lexer) -> Box; } -impl LetStatement { - pub fn parse(lexer: &mut Lexer) -> Self {} +pub trait Expression: Node {} + +pub struct Let { + name: Identifier, + // value: dyn Expression, } + +impl Node for Let { + fn token_literal(&self) -> String { + "let".to_owned() + } +} + +impl Statement for Let { + fn parse(&self, lexer: &mut Lexer) -> Vec { + vec![Let { + name: Identifier { name: "potato" }, + // value: "", + }] + } +} + +pub struct Identifier { + name: &'static str, +} + +impl Node for Identifier { + fn token_literal(&self) -> String { + "IDENT".to_owned() + } +} +impl Expression for Identifier {} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 5223ed5..1e997e9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,35 +1,32 @@ mod ast; -mod statement; -use self::statement::Statement; -use crate::lexer::{Lexer, Token}; +use self::ast::{Let, Statement}; +use super::lexer::Lexer; -pub struct Parser<'a> { - lexer: Lexer<'a>, +pub struct Program { + statements: Vec>, } -impl<'a> Parser<'a> { - pub fn new(lexer: Lexer<'a>) -> Parser<'a> { - return Parser { lexer }; - } -} - -impl<'a> Iterator for Parser<'a> { - type Item = Statement; - - fn next(&mut self) -> Option { - match self.lexer.next() { - Some(Token::Let) => Statement::Let.parse(&mut self.lexer), - _ => None, - None => None, +impl Program { + pub fn token_literal(&self) -> String { + if self.statements.len() > 0 { + self.statements[0].token_literal() + } else { + "".to_owned() } } + + pub fn parse(lexer: Lexer) -> Program { + let mut statements = vec![]; + + Program { statements } + } } #[cfg(test)] mod tests { use crate::lexer::Lexer; - use crate::parser::Parser; + use crate::parser::Program; #[test] fn let_statements() { @@ -40,11 +37,13 @@ mod tests { "; let lexer = Lexer::new(ip); + let ast_tree = Program::parse(lexer); - let stmts = Parser::new(lexer); - - for stmt in stmts { - println!("{:?}", stmt); + if ast_tree.statements.len() != 3 { + eprintln!( + "statements length not equal to 3. got {}", + ast_tree.statements + ); } } } diff --git a/src/parser/statement/mod.rs b/src/parser/statement/mod.rs deleted file mode 100644 index 8e0adfc..0000000 --- a/src/parser/statement/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -use super::ast; -use crate::lexer::Lexer; - -#[derive(Debug)] -pub enum Statement { - Let, -} - -impl Statement { - pub fn token_literal(&self) -> String { - match self { - Let => "let".to_owned(), - } - } - - pub fn parse(&self, lexer: &mut Lexer) -> ast::Statement { - match self { - Let => Statement::Let(ast::LetStatement::parse(lexer)), - } - } -} diff --git a/src/repl/mod.rs b/src/repl.rs similarity index 87% rename from src/repl/mod.rs rename to src/repl.rs index 0e75a14..ba4306a 100644 --- a/src/repl/mod.rs +++ b/src/repl.rs @@ -1,4 +1,4 @@ -use crate::{lexer::Lexer, parser::Parser}; +use crate::{lexer::Lexer, parser::Program}; use std::io::{self, BufRead, Write}; const PROMPT: &'static str = ">> "; @@ -24,6 +24,6 @@ fn start(mut ip: R, mut out: W) { println!("{:?}", token); } - let parser = Parser::new(tokens); + let parser = Program::parse(tokens); } }