From e0378acfc2deb61f98e6fd0938997ed907a1d39a Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sat, 23 May 2020 12:39:29 -0600 Subject: [PATCH] throw type_error(number, E) in arith_eval_by_metacall (#392) --- src/prolog/machine/arithmetic_ops.rs | 14 +++++++------- src/prolog/machine/machine_errors.rs | 19 +++++++++++++++++-- src/prolog/macros.rs | 3 +++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/prolog/machine/arithmetic_ops.rs b/src/prolog/machine/arithmetic_ops.rs index d5f1c694..4039bcf4 100644 --- a/src/prolog/machine/arithmetic_ops.rs +++ b/src/prolog/machine/arithmetic_ops.rs @@ -143,12 +143,10 @@ impl MachineState { pub(crate) fn arith_eval_by_metacall(&self, r: RegType) -> Result { - let a = self[r].clone(); - let caller = MachineError::functor_stub(clause_name!("(is)"), 2); let mut interms: Vec = Vec::with_capacity(64); - for addr in self.post_order_iter(a) { + for addr in self.post_order_iter(self[r]) { match self.heap.index_addr(&addr).as_ref() { &HeapCellValue::NamedStr(2, ref name, _) => { let a2 = interms.pop().unwrap(); @@ -241,10 +239,12 @@ impl MachineState { &HeapCellValue::Atom(ref name, _) if name.as_str() == "pi" => { interms.push(Number::Float(OrderedFloat(f64::consts::PI))) } - _ => { - return Err(self.error_form( - MachineError::instantiation_error(), - caller, + val => { + return Err(self.type_error( + ValidType::Number, + val.context_free_clone(), + clause_name!("(is)"), + 2, )); } } diff --git a/src/prolog/machine/machine_errors.rs b/src/prolog/machine/machine_errors.rs index 74374e33..ec8bf80f 100644 --- a/src/prolog/machine/machine_errors.rs +++ b/src/prolog/machine/machine_errors.rs @@ -43,6 +43,21 @@ impl TypeError for Addr { } } +impl TypeError for HeapCellValue { + fn type_error(self, _: usize, valid_type: ValidType) -> MachineError { + let stub = functor!( + "type_error", + [atom(valid_type.as_str()), value(self)] + ); + + MachineError { + stub, + location: None, + from: ErrorProvenance::Received + } + } +} + impl TypeError for MachineStub { fn type_error(self, h: usize, valid_type: ValidType) -> MachineError { let stub = functor!( @@ -502,7 +517,7 @@ pub enum ValidType { InCharacter, Integer, List, - // Number, + Number, Pair, // PredicateIndicator, // Variable @@ -525,7 +540,7 @@ impl ValidType { ValidType::InCharacter => "in_character", ValidType::Integer => "integer", ValidType::List => "list", - // ValidType::Number => "number", + ValidType::Number => "number", ValidType::Pair => "pair", // ValidType::PredicateIndicator => "predicate_indicator", // ValidType::Variable => "variable" diff --git a/src/prolog/macros.rs b/src/prolog/macros.rs index 0398d4c8..dabdb3de 100644 --- a/src/prolog/macros.rs +++ b/src/prolog/macros.rs @@ -121,6 +121,9 @@ macro_rules! functor_term { (atom($e:expr), $arity:expr, $aux_lens:expr, $addendum: ident) => ( HeapCellValue::Atom(clause_name!($e), None) ); + (value($e:expr), $arity:expr, $aux_lens:expr, $addendum: ident) => ( + $e + ); (string($h:expr, $e:expr), $arity:expr, $aux_lens:expr, $addendum: ident) => ({ let len: usize = $aux_lens.iter().sum(); let h = len + $arity + 1 + $addendum.h() + $h; -- 2.54.0