refactor(tazjin/rlox): Unify parser::Statement & parser::Declaration
Change-Id: I6f21b246eb9d3bf760edb3220ce6be5de5b05b08 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2302 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
parent
26ed836e1d
commit
3c979acdf3
2 changed files with 20 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
use crate::errors::{Error, ErrorKind};
|
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 crate::scanner::{self, TokenKind};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -78,6 +78,14 @@ pub struct Interpreter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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> {
|
fn interpret_stmt<'a>(&mut self, stmt: &Statement<'a>) -> Result<(), Error> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Statement::Expr(expr) => {
|
Statement::Expr(expr) => {
|
||||||
|
@ -87,6 +95,7 @@ impl Interpreter {
|
||||||
let result = self.eval(expr)?;
|
let result = self.eval(expr)?;
|
||||||
println!("{:?}", result)
|
println!("{:?}", result)
|
||||||
}
|
}
|
||||||
|
Statement::Var(var) => return self.interpret_var(var),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -102,17 +111,6 @@ impl Interpreter {
|
||||||
return Ok(());
|
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<Literal, Error> {
|
fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result<Literal, Error> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Assign(assign) => self.eval_assign(assign),
|
Expr::Assign(assign) => self.eval_assign(assign),
|
||||||
|
|
|
@ -54,13 +54,8 @@ pub enum Expr<'a> {
|
||||||
Variable(Variable<'a>),
|
Variable(Variable<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
// Variable assignment. Not to be confused with `Variable`, which is
|
||||||
pub enum Statement<'a> {
|
// for access.
|
||||||
Expr(Expr<'a>),
|
|
||||||
Print(Expr<'a>),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not to be confused with `Variable`, which is for access.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Var<'a> {
|
pub struct Var<'a> {
|
||||||
pub name: Token<'a>,
|
pub name: Token<'a>,
|
||||||
|
@ -68,12 +63,13 @@ pub struct Var<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Declaration<'a> {
|
pub enum Statement<'a> {
|
||||||
Stmt(Statement<'a>),
|
Expr(Expr<'a>),
|
||||||
|
Print(Expr<'a>),
|
||||||
Var(Var<'a>),
|
Var(Var<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Program<'a> = Vec<Declaration<'a>>;
|
pub type Program<'a> = Vec<Statement<'a>>;
|
||||||
|
|
||||||
// Parser
|
// Parser
|
||||||
|
|
||||||
|
@ -109,20 +105,19 @@ struct Parser<'a> {
|
||||||
|
|
||||||
type ExprResult<'a> = Result<Expr<'a>, Error>;
|
type ExprResult<'a> = Result<Expr<'a>, Error>;
|
||||||
type StmtResult<'a> = Result<Statement<'a>, Error>;
|
type StmtResult<'a> = Result<Statement<'a>, Error>;
|
||||||
type DeclResult<'a> = Result<Declaration<'a>, Error>;
|
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl<'a> Parser<'a> {
|
||||||
// recursive-descent parser functions
|
// recursive-descent parser functions
|
||||||
|
|
||||||
fn declaration(&mut self) -> DeclResult<'a> {
|
fn declaration(&mut self) -> StmtResult<'a> {
|
||||||
if self.match_token(&[TokenKind::Var]) {
|
if self.match_token(&[TokenKind::Var]) {
|
||||||
return self.var_declaration();
|
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
|
// Since `TokenKind::Identifier` carries data, we can't use
|
||||||
// `consume`.
|
// `consume`.
|
||||||
if let TokenKind::Identifier(_) = self.peek().kind {
|
if let TokenKind::Identifier(_) = self.peek().kind {
|
||||||
|
@ -136,7 +131,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;
|
self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;
|
||||||
return Ok(Declaration::Var(var));
|
return Ok(Statement::Var(var));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err(Error {
|
return Err(Error {
|
||||||
|
|
Loading…
Reference in a new issue