From a104afa6ea72dbafdc687156aac7e392eb7786a9 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 21 Dec 2020 00:13:22 +0100 Subject: refactor(tazjin/rlox): Introduce declarations in parser Change-Id: I873fdd53319ec36da18926d9477e809a69dbace7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2288 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter.rs | 28 ++++++++++++++++++---------- users/tazjin/rlox/src/parser.rs | 23 ++++++++++++++++++----- 2 files changed, 36 insertions(+), 15 deletions(-) (limited to 'users/tazjin/rlox/src') diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index c68101cf68..5e43e075b0 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -1,5 +1,5 @@ use crate::errors::{report, Error, ErrorKind}; -use crate::parser::{self, Expr, Literal, Program, Statement}; +use crate::parser::{self, Declaration, Expr, Literal, Program, Statement}; use crate::scanner::{self, TokenKind}; // Run some Lox code and print it to stdout @@ -104,16 +104,24 @@ fn eval<'a>(expr: &Expr<'a>) -> Result { } } +fn run_stmt<'a>(stmt: &Statement<'a>) -> Result<(), Error> { + match stmt { + Statement::Expr(expr) => { + eval(expr)?; + } + Statement::Print(expr) => { + let result = eval(expr)?; + println!("{:?}", result) + } + } + + Ok(()) +} + fn run_program<'a>(program: &Program<'a>) -> Result<(), Error> { - for stmt in program { - match stmt { - Statement::Expr(expr) => { - eval(expr)?; - } - Statement::Print(expr) => { - let result = eval(expr)?; - println!("{:?}", result) - } + for decl in program { + match decl { + Declaration::Stmt(stmt) => run_stmt(stmt)?, } } diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index d7155fff10..91000db9dd 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -48,12 +48,20 @@ pub enum Statement<'a> { Print(Expr<'a>), } -pub type Program<'a> = Vec>; +#[derive(Debug)] +pub enum Declaration<'a> { + Stmt(Statement<'a>), +} + +pub type Program<'a> = Vec>; // Parser /* -program → statement* EOF ; +program → declaration* EOF ; + +declaration → varDecl + | statement ; statement → exprStmt | printStmt ; @@ -79,10 +87,15 @@ struct Parser<'a> { type ExprResult<'a> = Result, Error>; type StmtResult<'a> = Result, Error>; +type DeclResult<'a> = Result, Error>; impl<'a> Parser<'a> { // recursive-descent parser functions + fn declaration(&mut self) -> DeclResult<'a> { + Ok(Declaration::Stmt(self.statement()?)) + } + fn statement(&mut self) -> StmtResult<'a> { if self.match_token(&[TokenKind::Print]) { self.print_statement() @@ -274,13 +287,13 @@ pub fn parse<'a>(tokens: Vec>) -> Result, Vec> { let mut errors: Vec = vec![]; while !parser.is_at_end() { - match parser.statement() { + match parser.declaration() { Err(err) => { errors.push(err); parser.synchronise(); } - Ok(stmt) => { - program.push(stmt); + Ok(decl) => { + program.push(decl); } } } -- cgit 1.4.1