From f9448e894e6db7093d4c892649c3df7f4c0a3095 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Mon, 22 Apr 2019 19:48:09 -0600 Subject: [PATCH] use binary_pow for (^)/2 --- Cargo.toml | 2 +- src/prolog/machine/compile.rs | 2 ++ src/prolog/machine/machine_errors.rs | 2 -- src/prolog/machine/machine_state_impl.rs | 20 +++++++++++++++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c096128..89d699e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ cfg-if = "0.1.7" downcast = "0.10.0" num = "0.2" ordered-float = "0.5.0" -prolog_parser = "0.8.20" +prolog_parser = "0.8.21" readline_rs_compat = { version = "0.1.9", optional = true } ref_thread_local = "0.0.0" diff --git a/src/prolog/machine/compile.rs b/src/prolog/machine/compile.rs index ac60200a..919d7b82 100644 --- a/src/prolog/machine/compile.rs +++ b/src/prolog/machine/compile.rs @@ -123,6 +123,8 @@ fn compile_query(terms: Vec, queue: VecDeque, flags: Machin let mut code = try!(cg.compile_query(&terms)); compile_appendix(&mut code, &queue, false, flags)?; + + print_code(&code); Ok((code, cg.take_vars())) } diff --git a/src/prolog/machine/machine_errors.rs b/src/prolog/machine/machine_errors.rs index ddff619f..bdfdd343 100644 --- a/src/prolog/machine/machine_errors.rs +++ b/src/prolog/machine/machine_errors.rs @@ -249,7 +249,6 @@ impl RepFlag { #[derive(Clone, Copy)] pub enum EvalError { // FloatOverflow, -// IntOverflow, // Undefined, // FloatUnderflow, ZeroDivisor, @@ -260,7 +259,6 @@ impl EvalError { pub fn as_str(self) -> &'static str { match self { // EvalError::FloatOverflow => "float_overflow", -// EvalError::IntOverflow => "int_overflow", // EvalError::Undefined => "undefined", // EvalError::FloatUnderflow => "underflow", EvalError::ZeroDivisor => "zero_divisor", diff --git a/src/prolog/machine/machine_state_impl.rs b/src/prolog/machine/machine_state_impl.rs index 516dccc6..736e7440 100644 --- a/src/prolog/machine/machine_state_impl.rs +++ b/src/prolog/machine/machine_state_impl.rs @@ -721,7 +721,7 @@ impl MachineState { let caller = MachineError::functor_stub(clause_name!("(is)"), 2); let mut interms: Vec = Vec::with_capacity(64); - for heap_val in self.post_order_iter(a) { + for heap_val in self.heap.post_order_iter(a) { match heap_val { HeapCellValue::NamedStr(2, name, _) => { let a2 = interms.pop().unwrap(); @@ -733,7 +733,7 @@ impl MachineState { "*" => interms.push(a1 * a2), "/" => interms.push(self.div(a1, a2)?), "**" => interms.push(self.pow(a1, a2)?), - "^" => interms.push(self.pow(a1, a2)?), + "^" => interms.push(self.binary_pow(a1, a2)?), "max" => interms.push(self.max(a1, a2)?), "rdiv" => { let r1 = self.get_rational(&ArithmeticTerm::Number(a1), &caller)?; @@ -841,6 +841,20 @@ impl MachineState { } } + fn binary_pow(&self, n1: Number, n2: Number) -> Result + { + match (n1, n2) { + (Number::Integer(n1), Number::Integer(n2)) => + self.pow(Number::Integer(n1), Number::Integer(n2)), + (Number::Integer(_), n) | (n, _) => { + let n = Addr::Con(Constant::Number(n)); + let stub = MachineError::functor_stub(clause_name!("^"), 2); + + Err(self.error_form(MachineError::type_error(ValidType::Integer, n), stub)) + } + } + } + fn pow(&self, n1: Number, n2: Number) -> Result { match n1.pow(n2) { @@ -1033,7 +1047,7 @@ impl MachineState { let n1 = try_or_fail!(self, self.get_number(a1)); let n2 = try_or_fail!(self, self.get_number(a2)); - self.interms[t - 1] = try_or_fail!(self, self.pow(n1, n2)); + self.interms[t - 1] = try_or_fail!(self, self.binary_pow(n1, n2)); self.p += 1; }, &ArithmeticInstruction::Pow(ref a1, ref a2, t) => { -- 2.54.0