parser
This commit is contained in:
+46
-1
@@ -1,3 +1,5 @@
|
||||
use crate::token;
|
||||
|
||||
trait NodeTrait {
|
||||
fn token_literal(&self) -> String;
|
||||
}
|
||||
@@ -9,6 +11,14 @@ trait StatementTrait: NodeTrait {
|
||||
trait ExpressionTrait: NodeTrait {
|
||||
fn expression_node(&self);
|
||||
}
|
||||
struct Expression {
|
||||
//
|
||||
}
|
||||
impl NodeTrait for Expression {
|
||||
fn token_literal(&self) -> String {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
struct Statement {
|
||||
//
|
||||
@@ -23,7 +33,7 @@ impl StatementTrait for Statement {
|
||||
fn statement_node(&self) {}
|
||||
}
|
||||
|
||||
struct Program {
|
||||
pub struct Program {
|
||||
statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
@@ -36,3 +46,38 @@ impl NodeTrait for Program {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LetStatement {
|
||||
token: token::Token,
|
||||
name: Identifier,
|
||||
value: Expression,
|
||||
}
|
||||
|
||||
impl NodeTrait for LetStatement {
|
||||
fn token_literal(&self) -> String {
|
||||
String::from(&self.token.literal)
|
||||
}
|
||||
}
|
||||
|
||||
impl StatementTrait for LetStatement {
|
||||
fn statement_node(&self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
struct Identifier {
|
||||
token: token::Token,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl NodeTrait for Identifier {
|
||||
fn token_literal(&self) -> String {
|
||||
String::from(&self.token.literal)
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionTrait for Identifier {
|
||||
fn expression_node(&self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
mod ast;
|
||||
mod lexer;
|
||||
mod parser;
|
||||
mod repl;
|
||||
mod token;
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
use crate::{
|
||||
ast,
|
||||
lexer::{self, LexerTraits},
|
||||
token,
|
||||
};
|
||||
|
||||
pub struct Parser {
|
||||
lexer: lexer::Lexer,
|
||||
cur_token: token::Token,
|
||||
peek_token: token::Token,
|
||||
}
|
||||
|
||||
trait PaserTrait {
|
||||
fn next_token(&mut self);
|
||||
fn parse_program(&self) -> ast::Program;
|
||||
}
|
||||
|
||||
pub fn new(l: lexer::Lexer) -> Parser {
|
||||
let mut p: Parser = Parser {
|
||||
lexer: l,
|
||||
cur_token: token::Token {
|
||||
literal: String::new(),
|
||||
type_: String::new(),
|
||||
},
|
||||
peek_token: token::Token {
|
||||
literal: String::new(),
|
||||
type_: String::new(),
|
||||
},
|
||||
};
|
||||
p.next_token();
|
||||
p.next_token();
|
||||
p
|
||||
}
|
||||
|
||||
impl PaserTrait for Parser {
|
||||
fn next_token(&mut self) {
|
||||
self.cur_token = token::Token {
|
||||
literal: String::from(&self.peek_token.literal),
|
||||
type_: String::from(&self.peek_token.type_),
|
||||
};
|
||||
self.peek_token = self.lexer.next_token();
|
||||
}
|
||||
|
||||
fn parse_program(&self) -> ast::Program {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user