From 2a7db71ded2dfccfae8473db18ba44005cbe803b Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 1 Jun 2022 16:30:29 -0500 Subject: [PATCH] Run clippy --- rust/src/evaluator.rs | 30 ++++++++++++++++++++++++------ rust/src/main.rs | 6 +----- rust/src/parser.rs | 27 ++++++++++++++++----------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/rust/src/evaluator.rs b/rust/src/evaluator.rs index 0fff525..9dcfe3c 100644 --- a/rust/src/evaluator.rs +++ b/rust/src/evaluator.rs @@ -22,16 +22,34 @@ impl<'a> Env<'a> { values: HashMap::new(), } } + pub fn lookup(&self, id: &str) -> Atom { + if let Some(a) = self.values.get(id) { + a.clone() + } else if let Some(parent) = self.parent { + parent.lookup(id) + } else { + panic!("Variable {} not in scope.", id) + } + } } -pub fn eval(ast: &parser::Expr, env: &mut Env) -> parser::Atom { +pub fn eval(ast: &parser::Stmt, env: &mut Env) { + match &ast { + parser::Stmt::ReplPrint(expr) => + println!("{}", eval_expr(expr, env)), + _ => todo!(), + } +} + +pub fn eval_expr(ast: &parser::Expr, env: &Env) -> Atom { match ast { + parser::Expr::Id(a) => env.lookup(a), parser::Expr::Atom(a) => a.clone(), - parser::Expr::UnaryMinus(a) => -eval(&a, env), - parser::Expr::Plus(a, b) => eval(&a, env) + eval(&b, env), - parser::Expr::Minus(a, b) => eval(&a, env) - eval(&b, env), - parser::Expr::Mult(a, b) => eval(&a, env) * eval(&b, env), - parser::Expr::Div(a, b) => eval(&a, env) / eval(&b, env), + parser::Expr::UnaryMinus(a) => -eval_expr(a, env), + parser::Expr::Plus(a, b) => eval_expr(a, env) + eval_expr(b, env), + parser::Expr::Minus(a, b) => eval_expr(a, env) - eval_expr(b, env), + parser::Expr::Mult(a, b) => eval_expr(a, env) * eval_expr(b, env), + parser::Expr::Div(a, b) => eval_expr(a, env) / eval_expr(b, env), _ => panic!("Couldn't evalute expression {{ {:?} }}", ast), } } diff --git a/rust/src/main.rs b/rust/src/main.rs index c3035f2..ee34e7b 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -26,11 +26,7 @@ fn repl(cli: &Cli) { if cli.parse_only { println!("{:#?}", tree); } else { - match &tree { - parser::Stmt::ReplPrint(expr) => - println!("{}", evaluator::eval(expr, &mut global)), - _ => todo!(), - } + evaluator::eval(&tree, &mut global); } } } diff --git a/rust/src/parser.rs b/rust/src/parser.rs index 9651146..2a40e90 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -33,7 +33,7 @@ pub enum Atom { impl fmt::Display for Atom { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Atom::String(a) => write!(f, "{}", a), + Atom::String(a) => write!(f, "\"{}\"", a), Atom::Num(a) => write!(f, "{}", a), } } @@ -143,16 +143,21 @@ fn preprocess(input: &str) -> String { } else if c == '#' { break; } else { + use std::cmp::Ordering::*; let curr = stack.last().unwrap(); - if curr < &count { - stack.push(count); - output.push_str(">>>"); - } else if curr > &count { - while stack.last().unwrap() > &count { - output.push_str("<<<"); - output.push('\n'); - stack.pop(); + match curr.cmp(&count) { + Less => { + stack.push(count); + output.push_str(">>>"); + }, + Greater => { + while stack.last().unwrap() > &count { + output.push_str("<<<"); + output.push('\n'); + stack.pop(); + } } + Equal => (), } break; } @@ -164,12 +169,12 @@ fn preprocess(input: &str) -> String { } pub fn parse(prgm: &str) -> Vec { - let prgm = preprocess(&prgm); + let prgm = preprocess(prgm); deelang_parser::program(&prgm).unwrap() } pub fn parse_stmt(stmt: &str) -> Stmt { - let stmt = preprocess(&stmt); + let stmt = preprocess(stmt); deelang_parser::stmt(&stmt).unwrap() }