Resuming project. Refactored project
This commit is contained in:
parent
7409606f3d
commit
f202d0dacd
|
@ -89,6 +89,7 @@ impl<'a> Lexer<'a> {
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn peek_is_ascii_digit(&mut self) -> bool {
|
fn peek_is_ascii_digit(&mut self) -> bool {
|
||||||
match self.input.peek() {
|
match self.input.peek() {
|
||||||
Some(v) => v.is_ascii_digit(),
|
Some(v) => v.is_ascii_digit(),
|
||||||
|
|
|
@ -8,4 +8,3 @@ mod repl;
|
||||||
fn main() {
|
fn main() {
|
||||||
repl::init();
|
repl::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,41 @@
|
||||||
use crate::lexer::Lexer;
|
use crate::lexer::Lexer;
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub trait Node {
|
||||||
pub struct LetStatement {
|
fn token_literal(&self) -> String;
|
||||||
name: String,
|
}
|
||||||
|
pub trait Statement: Node {
|
||||||
|
fn parse(&self, &mut Lexer) -> Box<dyn Statement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LetStatement {
|
pub trait Expression: Node {}
|
||||||
pub fn parse(lexer: &mut Lexer) -> Self {}
|
|
||||||
|
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 ast;
|
||||||
mod statement;
|
|
||||||
|
|
||||||
use self::statement::Statement;
|
use self::ast::{Let, Statement};
|
||||||
use crate::lexer::{Lexer, Token};
|
use super::lexer::Lexer;
|
||||||
|
|
||||||
pub struct Parser<'a> {
|
pub struct Program {
|
||||||
lexer: Lexer<'a>,
|
statements: Vec<Box<dyn Statement>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl Program {
|
||||||
pub fn new(lexer: Lexer<'a>) -> Parser<'a> {
|
pub fn token_literal(&self) -> String {
|
||||||
return Parser { lexer };
|
if self.statements.len() > 0 {
|
||||||
}
|
self.statements[0].token_literal()
|
||||||
}
|
} else {
|
||||||
|
"".to_owned()
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse(lexer: Lexer) -> Program {
|
||||||
|
let mut statements = vec![];
|
||||||
|
|
||||||
|
Program { statements }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::lexer::Lexer;
|
use crate::lexer::Lexer;
|
||||||
use crate::parser::Parser;
|
use crate::parser::Program;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn let_statements() {
|
fn let_statements() {
|
||||||
|
@ -40,11 +37,13 @@ mod tests {
|
||||||
";
|
";
|
||||||
|
|
||||||
let lexer = Lexer::new(ip);
|
let lexer = Lexer::new(ip);
|
||||||
|
let ast_tree = Program::parse(lexer);
|
||||||
|
|
||||||
let stmts = Parser::new(lexer);
|
if ast_tree.statements.len() != 3 {
|
||||||
|
eprintln!(
|
||||||
for stmt in stmts {
|
"statements length not equal to 3. got {}",
|
||||||
println!("{:?}", stmt);
|
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};
|
use std::io::{self, BufRead, Write};
|
||||||
|
|
||||||
const PROMPT: &'static str = ">> ";
|
const PROMPT: &'static str = ">> ";
|
||||||
|
@ -24,6 +24,6 @@ fn start<R: BufRead, W: Write>(mut ip: R, mut out: W) {
|
||||||
println!("{:?}", token);
|
println!("{:?}", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
let parser = Parser::new(tokens);
|
let parser = Program::parse(tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user