From 3c979acdf367ea18ec2248bbe8e0befa15966bb4 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 31 Dec 2020 17:59:19 +0300 Subject: refactor(tazjin/rlox): Unify parser::Statement & parser::Declaration Change-Id: I6f21b246eb9d3bf760edb3220ce6be5de5b05b08 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2302 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter.rs | 22 ++++++++++------------ users/tazjin/rlox/src/parser.rs | 25 ++++++++++--------------- 2 files changed, 20 insertions(+), 27 deletions(-) (limited to 'users/tazjin') diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index c0af7d047652..edaa5e28f234 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -1,5 +1,5 @@ use crate::errors::{Error, ErrorKind}; -use crate::parser::{self, Declaration, Expr, Literal, Program, Statement}; +use crate::parser::{self, Expr, Literal, Program, Statement}; use crate::scanner::{self, TokenKind}; use std::collections::HashMap; use std::rc::Rc; @@ -78,6 +78,14 @@ pub struct Interpreter { } impl Interpreter { + pub fn interpret<'a>(&mut self, program: &Program<'a>) -> Result<(), Error> { + for stmt in program { + self.interpret_stmt(stmt)?; + } + + Ok(()) + } + fn interpret_stmt<'a>(&mut self, stmt: &Statement<'a>) -> Result<(), Error> { match stmt { Statement::Expr(expr) => { @@ -87,6 +95,7 @@ impl Interpreter { let result = self.eval(expr)?; println!("{:?}", result) } + Statement::Var(var) => return self.interpret_var(var), } Ok(()) @@ -102,17 +111,6 @@ impl Interpreter { return Ok(()); } - pub fn interpret<'a>(&mut self, program: &Program<'a>) -> Result<(), Error> { - for decl in program { - match decl { - Declaration::Stmt(stmt) => self.interpret_stmt(stmt)?, - Declaration::Var(var) => self.interpret_var(var)?, - } - } - - Ok(()) - } - fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result { match expr { Expr::Assign(assign) => self.eval_assign(assign), diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index b46e570994c4..1a271fd0ae2a 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -54,13 +54,8 @@ pub enum Expr<'a> { Variable(Variable<'a>), } -#[derive(Debug)] -pub enum Statement<'a> { - Expr(Expr<'a>), - Print(Expr<'a>), -} - -// Not to be confused with `Variable`, which is for access. +// Variable assignment. Not to be confused with `Variable`, which is +// for access. #[derive(Debug)] pub struct Var<'a> { pub name: Token<'a>, @@ -68,12 +63,13 @@ pub struct Var<'a> { } #[derive(Debug)] -pub enum Declaration<'a> { - Stmt(Statement<'a>), +pub enum Statement<'a> { + Expr(Expr<'a>), + Print(Expr<'a>), Var(Var<'a>), } -pub type Program<'a> = Vec>; +pub type Program<'a> = Vec>; // Parser @@ -109,20 +105,19 @@ 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> { + fn declaration(&mut self) -> StmtResult<'a> { if self.match_token(&[TokenKind::Var]) { return self.var_declaration(); } - Ok(Declaration::Stmt(self.statement()?)) + self.statement() } - fn var_declaration(&mut self) -> DeclResult<'a> { + fn var_declaration(&mut self) -> StmtResult<'a> { // Since `TokenKind::Identifier` carries data, we can't use // `consume`. if let TokenKind::Identifier(_) = self.peek().kind { @@ -136,7 +131,7 @@ impl<'a> Parser<'a> { } self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?; - return Ok(Declaration::Var(var)); + return Ok(Statement::Var(var)); } return Err(Error { -- cgit 1.4.1