From a1fb4f529ba2b5305c8374439c9d36a99d7aa401 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Mon, 12 Feb 2018 18:30:44 -0700 Subject: [PATCH] move several builtins to control. --- src/prolog/ast.rs | 41 ++++-- src/prolog/io.rs | 48 +++---- src/prolog/machine/machine_state_impl.rs | 170 +++++++++++------------ src/prolog/macros.rs | 22 +-- 4 files changed, 146 insertions(+), 135 deletions(-) diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index 79f72642..14b035b2 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -820,28 +820,17 @@ pub enum ArithmeticInstruction { } pub enum BuiltInInstruction { - CleanUpBlock, - CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), + CleanUpBlock, EraseBall, Fail, GetArgCall, GetArgExecute, GetBall, GetCurrentBlock, - GetCutPoint(RegType), - DynamicCompareNumber(CompareNumberQT), - DynamicIs, + GetCutPoint(RegType), InstallCleaner, InstallNewBlock, - InternalCallN, - IsAtomic(RegType), - IsCompound(RegType), - IsFloat(RegType), - IsInteger(RegType), - IsNonVar(RegType), - IsRational(RegType), - IsString(RegType), - IsVar(RegType), + InternalCallN, ResetBlock, RestoreCutPolicy, SetBall, @@ -861,11 +850,14 @@ pub enum ControlInstruction { CatchCall, CatchExecute, CheckCpExecute, + CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), DisplayCall, DisplayExecute, Deallocate, DuplicateTermCall, DuplicateTermExecute, + DynamicCompareNumber(CompareNumberQT), + DynamicIs, EqCall, EqExecute, Execute(TabledRc, usize), @@ -876,10 +868,18 @@ pub enum ControlInstruction { GotoCall(usize, usize), // p, arity. GotoExecute(usize, usize), // p, arity. GroundCall, - GroundExecute, + GroundExecute, JmpByCall(usize, usize), // arity, global_offset. JmpByExecute(usize, usize), IsCall(RegType, ArithmeticTerm), + IsAtomic(RegType), + IsCompound(RegType), + IsFloat(RegType), + IsInteger(RegType), + IsNonVar(RegType), + IsRational(RegType), + IsString(RegType), + IsVar(RegType), IsExecute(RegType, ArithmeticTerm), NotEqCall, NotEqExecute, @@ -896,10 +896,13 @@ impl ControlInstruction { &ControlInstruction::Call(_, _, _) => true, &ControlInstruction::CatchCall => true, &ControlInstruction::CatchExecute => true, + &ControlInstruction::CompareNumber(..) => true, &ControlInstruction::DisplayCall => true, &ControlInstruction::DisplayExecute => true, &ControlInstruction::DuplicateTermCall => true, &ControlInstruction::DuplicateTermExecute => true, + &ControlInstruction::DynamicCompareNumber(_) => true, + &ControlInstruction::DynamicIs => true, &ControlInstruction::EqCall => true, &ControlInstruction::EqExecute => true, &ControlInstruction::Execute(_, _) => true, @@ -916,6 +919,14 @@ impl ControlInstruction { &ControlInstruction::GotoExecute(..) => true, &ControlInstruction::GroundCall => true, &ControlInstruction::GroundExecute => true, + &ControlInstruction::IsAtomic(_) => true, + &ControlInstruction::IsCompound(_) => true, + &ControlInstruction::IsFloat(_) => true, + &ControlInstruction::IsInteger(_) => true, + &ControlInstruction::IsNonVar(_) => true, + &ControlInstruction::IsRational(_) => true, + &ControlInstruction::IsString(_) => true, + &ControlInstruction::IsVar(_) => true, &ControlInstruction::IsCall(..) => true, &ControlInstruction::IsExecute(..) => true, &ControlInstruction::JmpByCall(..) => true, diff --git a/src/prolog/io.rs b/src/prolog/io.rs index 55d0d9ee..1562dd0c 100644 --- a/src/prolog/io.rs +++ b/src/prolog/io.rs @@ -151,6 +151,24 @@ impl fmt::Display for ControlInstruction { write!(f, "is_call {}, {}", r, at), &ControlInstruction::IsExecute(r, ref at) => write!(f, "is_execute {}, {}", r, at), + &ControlInstruction::IsAtomic(r) => + write!(f, "is_atomic {}", r), + &ControlInstruction::IsCompound(r) => + write!(f, "is_compound {}", r), + &ControlInstruction::IsFloat(r) => + write!(f, "is_float {}", r), + &ControlInstruction::IsRational(r) => + write!(f, "is_rational {}", r), + &ControlInstruction::IsNonVar(r) => + write!(f, "is_non_var {}", r), + &ControlInstruction::IsString(r) => + write!(f, "is_string {}", r), + &ControlInstruction::IsInteger(r) => + write!(f, "is_integer {}", r), + &ControlInstruction::DynamicIs => + write!(f, "call_is"), + &ControlInstruction::IsVar(r) => + write!(f, "is_var {}", r), &ControlInstruction::JmpByCall(arity, offset) => write!(f, "jmp_by_call {}/{}", offset, arity), &ControlInstruction::JmpByExecute(arity, offset) => @@ -165,6 +183,10 @@ impl fmt::Display for ControlInstruction { write!(f, "call_throw"), &ControlInstruction::ThrowExecute => write!(f, "execute_throw"), + &ControlInstruction::CompareNumber(cmp, ref at_1, ref at_2) => + write!(f, "number_test {}, {}, {} ", cmp, at_1, at_2), + &ControlInstruction::DynamicCompareNumber(cmp) => + write!(f, "dynamic_number_test {}", cmp), } } } @@ -186,11 +208,7 @@ impl fmt::Display for BuiltInInstruction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { &BuiltInInstruction::CleanUpBlock => - write!(f, "clean_up_block"), - &BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) => - write!(f, "number_test {}, {}, {} ", cmp, at_1, at_2), - &BuiltInInstruction::DynamicCompareNumber(cmp) => - write!(f, "dynamic_number_test {}", cmp), + write!(f, "clean_up_block"), &BuiltInInstruction::EraseBall => write!(f, "erase_ball"), &BuiltInInstruction::Fail => @@ -210,25 +228,7 @@ impl fmt::Display for BuiltInInstruction { &BuiltInInstruction::InstallNewBlock => write!(f, "install_new_block"), &BuiltInInstruction::InternalCallN => - write!(f, "internal_call_N"), - &BuiltInInstruction::IsAtomic(r) => - write!(f, "is_atomic {}", r), - &BuiltInInstruction::IsCompound(r) => - write!(f, "is_compound {}", r), - &BuiltInInstruction::IsFloat(r) => - write!(f, "is_float {}", r), - &BuiltInInstruction::IsRational(r) => - write!(f, "is_rational {}", r), - &BuiltInInstruction::IsNonVar(r) => - write!(f, "is_non_var {}", r), - &BuiltInInstruction::IsString(r) => - write!(f, "is_string {}", r), - &BuiltInInstruction::IsInteger(r) => - write!(f, "is_integer {}", r), - &BuiltInInstruction::DynamicIs => - write!(f, "call_is"), - &BuiltInInstruction::IsVar(r) => - write!(f, "is_var {}", r), + write!(f, "internal_call_N"), &BuiltInInstruction::ResetBlock => write!(f, "reset_block"), &BuiltInInstruction::RestoreCutPolicy => diff --git a/src/prolog/machine/machine_state_impl.rs b/src/prolog/machine/machine_state_impl.rs index a9c594b8..2a294451 100644 --- a/src/prolog/machine/machine_state_impl.rs +++ b/src/prolog/machine/machine_state_impl.rs @@ -1128,26 +1128,7 @@ impl MachineState { cut_policy: &mut Box, instr: &BuiltInInstruction) { - match instr { - &BuiltInInstruction::CompareNumber(cmp, ref at_1, ref at_2) => { - let n1 = try_or_fail!(self, self.get_number(at_1)); - let n2 = try_or_fail!(self, self.get_number(at_2)); - - self.compare_numbers(cmp, n1, n2); - }, - &BuiltInInstruction::DynamicCompareNumber(cmp) => { - let n1 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(1))); - let n2 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2))); - - self.compare_numbers(cmp, n1, n2); - }, - &BuiltInInstruction::DynamicIs => { - let a = self[temp_v!(1)].clone(); - let result = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2))); - - self.unify(a, Addr::Con(Constant::Number(result))); - self.p += 1; - }, + match instr { &BuiltInInstruction::GetArgCall => try_or_fail!(self, { let val = self.try_get_arg(); @@ -1292,71 +1273,7 @@ SetupCallCleanupCutPolicy.") self.or_stack.truncate(self.b); self.fail = true; - }, - &BuiltInInstruction::IsAtomic(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Con(_) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsInteger(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Con(Constant::Number(Number::Integer(_))) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsCompound(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Str(_) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsFloat(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Con(Constant::Number(Number::Float(_))) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsRational(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Con(Constant::Number(Number::Rational(_))) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsString(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::Con(Constant::String(_)) => self.p += 1, - _ => self.fail = true - }; - }, - &BuiltInInstruction::IsNonVar(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::HeapCell(_) | Addr::StackCell(..) => self.fail = true, - _ => self.p += 1 - }; - }, - &BuiltInInstruction::IsVar(r) => { - let d = self.store(self.deref(self[r].clone())); - - match d { - Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1, - _ => self.fail = true - }; - }, + }, &BuiltInInstruction::InternalCallN => self.handle_internal_call_n(code_dir), &BuiltInInstruction::Fail => { @@ -1615,6 +1532,89 @@ SetupCallCleanupCutPolicy.") self.duplicate_term(); self.p = self.cp; }, + &ControlInstruction::CompareNumber(cmp, ref at_1, ref at_2) => { + let n1 = try_or_fail!(self, self.get_number(at_1)); + let n2 = try_or_fail!(self, self.get_number(at_2)); + + self.compare_numbers(cmp, n1, n2); + }, + &ControlInstruction::DynamicCompareNumber(cmp) => { + let n1 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(1))); + let n2 = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2))); + + self.compare_numbers(cmp, n1, n2); + }, + &ControlInstruction::DynamicIs => { + let a = self[temp_v!(1)].clone(); + let result = try_or_fail!(self, self.arith_eval_by_metacall(temp_v!(2))); + + self.unify(a, Addr::Con(Constant::Number(result))); + self.p += 1; + }, + &ControlInstruction::IsAtomic(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Con(_) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsInteger(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Con(Constant::Number(Number::Integer(_))) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsCompound(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Str(_) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsFloat(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Con(Constant::Number(Number::Float(_))) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsRational(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Con(Constant::Number(Number::Rational(_))) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsString(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::Con(Constant::String(_)) => self.p += 1, + _ => self.fail = true + }; + }, + &ControlInstruction::IsNonVar(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::HeapCell(_) | Addr::StackCell(..) => self.fail = true, + _ => self.p += 1 + }; + }, + &ControlInstruction::IsVar(r) => { + let d = self.store(self.deref(self[r].clone())); + + match d { + Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1, + _ => self.fail = true + }; + }, &ControlInstruction::EqCall => { self.fail = self.eq_test(); self.p += 1; diff --git a/src/prolog/macros.rs b/src/prolog/macros.rs index 281129a8..07fc9f1d 100644 --- a/src/prolog/macros.rs +++ b/src/prolog/macros.rs @@ -30,7 +30,7 @@ macro_rules! deallocate { macro_rules! compare_number_instr { ($cmp: expr, $at_1: expr, $at_2: expr) => ( - Line::BuiltIn(BuiltInInstruction::CompareNumber($cmp, $at_1, $at_2)) + Line::Control(ControlInstruction::CompareNumber($cmp, $at_1, $at_2)) ) } @@ -158,50 +158,50 @@ macro_rules! retry_me_else { macro_rules! is_atomic { ($reg:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsAtomic($reg)) + Line::Control(ControlInstruction::IsAtomic($reg)) ) } macro_rules! is_integer { ($reg:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsInteger($reg)) + Line::Control(ControlInstruction::IsInteger($reg)) ) } macro_rules! is_compound { ($r:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsCompound($r)) + Line::Control(ControlInstruction::IsCompound($r)) ) } macro_rules! is_float { ($r:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsFloat($r)) + Line::Control(ControlInstruction::IsFloat($r)) ) } macro_rules! is_rational { ($r:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsRational($r)) + Line::Control(ControlInstruction::IsRational($r)) ) } macro_rules! is_nonvar { ($r:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsNonVar($r)) + Line::Control(ControlInstruction::IsNonVar($r)) ) } macro_rules! is_string { ($r:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsString($r)) + Line::Control(ControlInstruction::IsString($r)) ) } macro_rules! is_var { ($reg:expr) => ( - Line::BuiltIn(BuiltInInstruction::IsVar($reg)) + Line::Control(ControlInstruction::IsVar($reg)) ) } @@ -469,13 +469,13 @@ macro_rules! display { macro_rules! dynamic_is { () => ( - Line::BuiltIn(BuiltInInstruction::DynamicIs) + Line::Control(ControlInstruction::DynamicIs) ) } macro_rules! dynamic_num_test { ($cmp:expr) => ( - Line::BuiltIn(BuiltInInstruction::DynamicCompareNumber($cmp)) + Line::Control(ControlInstruction::DynamicCompareNumber($cmp)) ) } -- 2.54.0