From 92e0a22e24b9634d068a8e9efbe0c5f588de8411 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sat, 12 Jun 2021 10:45:25 -0500 Subject: [PATCH 1/3] Integrate `const.rs` as `consts` --- src/{const.rs => consts.rs} | 0 src/main.rs | 1 + 2 files changed, 1 insertion(+) rename src/{const.rs => consts.rs} (100%) diff --git a/src/const.rs b/src/consts.rs similarity index 100% rename from src/const.rs rename to src/consts.rs diff --git a/src/main.rs b/src/main.rs index 847fa2e..0b7f257 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod ast; mod base_55; mod brian; +mod consts; mod error; mod interpret; mod lexer; From da9ff64486826e305c7f6bd57c20d37c0389519f Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sat, 12 Jun 2021 10:48:44 -0500 Subject: [PATCH 2/3] Make arithmetic errors evaluate to 42 --- src/error.rs | 1 - src/interpret.rs | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index b1da312..8d430f5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,7 +18,6 @@ pub enum ErrorKind { MeloVariable(String), TypeError(String), TopLevelBreak, - ArithmeticError, BfInterpretError(InterpretError), MissingLhs, } diff --git a/src/interpret.rs b/src/interpret.rs index eb6bb2e..27dd2b1 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -19,7 +19,7 @@ use rand::random; use crate::{ ast::{Expr, Iden, Stmt, StmtKind}, - base_55, + base_55, consts, error::{Error, ErrorKind}, variables::{Functio, Value, Variable}, }; @@ -134,10 +134,7 @@ impl ExecEnv { Divide => lhs.checked_div(rhs), _ => unreachable!(), } - .ok_or(Error { - kind: ErrorKind::ArithmeticError, - span: expr.span.clone(), - })?; + .unwrap_or(consts::ANSWER); Int(res) } From d1064802ec82ebbafb60e240eb5245aa6f8faddb Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Tue, 15 Jun 2021 11:29:52 -0500 Subject: [PATCH 3/3] Fix unit tests --- src/interpret.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/interpret.rs b/src/interpret.rs index 27dd2b1..da0560a 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -442,7 +442,7 @@ mod tests { // Integer overflow should throw a recoverable error instead // of panicking. let env = ExecEnv::new(); - assert!(matches!( + assert_eq!( env.eval_expr(&Expr { kind: ExprKind::BinOp { lhs: Box::new(Expr { @@ -456,15 +456,13 @@ mod tests { kind: crate::ast::BinOpKind::Add, }, span: 1..1 - }), - Err(Error { - kind: ErrorKind::ArithmeticError, - span: _, }) - )); + .unwrap(), + Value::Int(42) + ); // And the same for divide by zero. - assert!(matches!( + assert_eq!( env.eval_expr(&Expr { kind: ExprKind::BinOp { lhs: Box::new(Expr { @@ -478,12 +476,10 @@ mod tests { kind: crate::ast::BinOpKind::Divide, }, span: 1..1 - }), - Err(Error { - kind: ErrorKind::ArithmeticError, - span: _, }) - )); + .unwrap(), + Value::Int(42) + ); } // From here on out, I'll use this function to parse and run