Skip to content

Commit

Permalink
Boxed NumbatError
Browse files Browse the repository at this point in the history
  • Loading branch information
rben01 committed Sep 17, 2024
1 parent 6e3043b commit fec3f4f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ impl Cli {
PrettyPrintMode::Auto => interactive,
};

match result {
match result.map_err(|b| *b) {
Ok((statements, interpreter_result)) => {
if interactive || pretty_print {
println!();
Expand Down Expand Up @@ -518,7 +518,7 @@ impl Cli {
ControlFlow::Continue(())
}
Err(NumbatError::ResolverError(e)) => {
self.print_diagnostic(e.clone());
self.print_diagnostic(e);
execution_mode.exit_status_in_case_of_error()
}
Err(NumbatError::NameResolutionError(
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl InterpreterResult {
}
}

pub type Result<T> = std::result::Result<T, RuntimeError>;
pub type Result<T> = std::result::Result<T, Box<RuntimeError>>;

pub type PrintFunction = dyn FnMut(&Markup) + Send;

Expand Down Expand Up @@ -234,7 +234,7 @@ mod tests {
#[track_caller]
fn assert_runtime_error(input: &str, err_expected: RuntimeError) {
if let Err(err_actual) = get_interpreter_result(input) {
assert_eq!(err_actual, err_expected);
assert_eq!(*err_actual, err_expected);
} else {
panic!();
}
Expand Down
8 changes: 4 additions & 4 deletions numbat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub enum NumbatError {
RuntimeError(RuntimeError),
}

type Result<T> = std::result::Result<T, NumbatError>;
type Result<T> = std::result::Result<T, Box<NumbatError>>;

#[derive(Clone)]
pub struct Context {
Expand Down Expand Up @@ -734,9 +734,9 @@ impl Context {
let erc = ExchangeRatesCache::fetch();

if erc.is_none() {
return Err(NumbatError::RuntimeError(
return Err(Box::new(NumbatError::RuntimeError(
RuntimeError::CouldNotLoadExchangeRates,
));
)));
}
}

Expand Down Expand Up @@ -785,7 +785,7 @@ impl Context {
self.interpreter = interpreter_old;
}

let result = result.map_err(NumbatError::RuntimeError)?;
let result = result.map_err(|err| NumbatError::RuntimeError(*err))?;

Ok((typed_statements, result))
}
Expand Down
6 changes: 3 additions & 3 deletions numbat/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,9 @@ impl Vm {
.to_f64();

if lhs < 0. {
return Err(RuntimeError::FactorialOfNegativeNumber);
return Err(Box::new(RuntimeError::FactorialOfNegativeNumber));
} else if lhs.fract() != 0. {
return Err(RuntimeError::FactorialOfNonInteger);
return Err(Box::new(RuntimeError::FactorialOfNonInteger));
}

self.push_quantity(Quantity::from_scalar(math::factorial(lhs)));
Expand Down Expand Up @@ -862,7 +862,7 @@ impl Vm {
match result {
std::ops::ControlFlow::Continue(()) => {}
std::ops::ControlFlow::Break(runtime_error) => {
return Err(runtime_error);
return Err(Box::new(runtime_error));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion numbat/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn get_test_context_without_prelude() -> Context {
}

pub fn get_test_context() -> Context {
static CONTEXT: Lazy<Result<Context, NumbatError>> = Lazy::new(|| {
static CONTEXT: Lazy<Result<Context, Box<NumbatError>>> = Lazy::new(|| {
let mut context = get_test_context_without_prelude();

let _ = context.interpret("use prelude", CodeSource::Internal)?;
Expand Down
2 changes: 1 addition & 1 deletion numbat/tests/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn fail(code: &str) -> NumbatError {
let mut ctx = get_test_context();
let ret = ctx.interpret(code, CodeSource::Internal);
match ret {
Err(e) => e,
Err(e) => *e,
Ok((_stmts, ret)) => {
if let InterpreterResult::Value(val) = ret {
let fmt = PlainTextFormatter {};
Expand Down
16 changes: 12 additions & 4 deletions numbat/tests/prelude_and_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ fn assert_runs_without_prelude(code: &str) {

fn assert_parse_error(code: &str) {
assert!(matches!(
get_test_context().interpret(code, CodeSource::Internal),
get_test_context()
.interpret(code, CodeSource::Internal)
.map_err(|b| *b),
Err(NumbatError::ResolverError(
ResolverError::ParseErrors { .. }
))
Expand All @@ -39,21 +41,27 @@ fn assert_parse_error(code: &str) {

fn assert_name_resolution_error(code: &str) {
assert!(matches!(
get_test_context().interpret(code, CodeSource::Internal),
get_test_context()
.interpret(code, CodeSource::Internal)
.map_err(|b| *b),
Err(NumbatError::NameResolutionError(_))
));
}

fn assert_typecheck_error(code: &str) {
assert!(matches!(
get_test_context().interpret(code, CodeSource::Internal),
get_test_context()
.interpret(code, CodeSource::Internal)
.map_err(|b| *b),
Err(NumbatError::TypeCheckError(_))
));
}

fn assert_runtime_error(code: &str) {
assert!(matches!(
get_test_context().interpret(code, CodeSource::Internal),
get_test_context()
.interpret(code, CodeSource::Internal)
.map_err(|b| *b),
Err(NumbatError::RuntimeError(_))
));
}
Expand Down

0 comments on commit fec3f4f

Please sign in to comment.