Resuming project. Refactored project
This commit is contained in:
parent
7409606f3d
commit
f202d0dacd
|
@ -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(),
|
||||
|
|
|
@ -8,4 +8,3 @@ mod repl;
|
|||
fn main() {
|
||||
repl::init();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<dyn Statement>;
|
||||
}
|
||||
|
||||
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<dyn Statement> {
|
||||
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 {}
|
||||
|
|
|
@ -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<Box<dyn Statement>>,
|
||||
}
|
||||
|
||||
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<Self::Item> {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<R: BufRead, W: Write>(mut ip: R, mut out: W) {
|
|||
println!("{:?}", token);
|
||||
}
|
||||
|
||||
let parser = Parser::new(tokens);
|
||||
let parser = Program::parse(tokens);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user