refactor(tazjin/rlox): Introduce declarations in parser

Change-Id: I873fdd53319ec36da18926d9477e809a69dbace7
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2288
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2020-12-21 00:13:22 +01:00 committed by tazjin
parent 75ae25daa9
commit a104afa6ea
2 changed files with 37 additions and 16 deletions

View file

@ -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<Literal, Error> {
}
}
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)
}
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 decl in program {
match decl {
Declaration::Stmt(stmt) => run_stmt(stmt)?,
}
}