Skip to content

Commit

Permalink
Rewrite parser to include statements
Browse files Browse the repository at this point in the history
  • Loading branch information
coletonodonnell committed Feb 9, 2022
1 parent d753016 commit 1ba0e61
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::token::{Token, TokenType, Literal};
use crate::expression::{Expr};
use crate::stmt::Stmt;

pub struct Parser {
pub tokens: Vec<Token>,
Expand Down Expand Up @@ -177,6 +178,26 @@ impl Parser {
return Self::equality(self);
}

fn expression_statement(&mut self) -> Stmt {
let value: Expr = self.expression();
let _a: Result<Token, String> = self.consume(TokenType::Semicolon, "Expect ';' after value.".to_string());
return Stmt::Expression{ expression: value };
}

fn print_statement(&mut self) -> Stmt {
let value: Expr = self.expression();
let _a: Result<Token, String> = self.consume(TokenType::Semicolon, "Expect ';' after value.".to_string());
return Stmt::Print{ expression: value };
}

fn statement(&mut self) -> Stmt {
if self.match_type(vec![TokenType::Print]) {
return self.print_statement();
}

return self.expression_statement();
}

pub fn parser_builder(tokens: Vec<Token>, instance: crate::Lox) -> Parser {
return Parser {
tokens: tokens,
Expand All @@ -185,12 +206,17 @@ impl Parser {
}
}

pub fn parse(&mut self) -> Option<Expr>{
let a: Expr = Self::expression(self);
pub fn parse(&mut self) -> Option<Vec<Stmt>>{
let mut statements: Vec<Stmt> = Vec::new();

while !self.is_end() {
statements.push(self.statement());
}

if !self.instance.had_error {
return Some(a);
return Some(statements)
} else {
return None;
return None
}
}
}

0 comments on commit 1ba0e61

Please sign in to comment.