From: Mark Thom Date: Fri, 9 Feb 2018 01:29:45 +0000 (-0700) Subject: minor cleanup. X-Git-Tag: v0.8.110~576 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=df3e8b8fce193a929d972da190accd452756ec15;p=scryer-prolog.git minor cleanup. --- diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index 7fde3b6d..24223854 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -155,7 +155,7 @@ impl RegType { } } -#[derive(Clone, Copy)] +#[derive(PartialEq, Eq, Clone, Copy)] pub enum VarReg { ArgAndNorm(RegType, usize), Norm(RegType) @@ -306,7 +306,7 @@ pub enum Fixity { #[derive(Clone, Eq, Hash, PartialEq)] pub enum Constant { Atom(TabledRc), - Number(Number), + Number(Number), String(Rc), Usize(usize), EmptyList @@ -320,7 +320,7 @@ impl fmt::Display for Constant { &Constant::EmptyList => write!(f, "[]"), &Constant::Number(ref n) => - write!(f, "{}", n), + write!(f, "{}", n), &Constant::String(ref s) => write!(f, "{}", s), &Constant::Usize(integer) => @@ -329,7 +329,7 @@ impl fmt::Display for Constant { } } -#[derive(Clone)] +#[derive(PartialEq, Eq, Clone)] pub enum Term { AnonVar, Clause(Cell, TabledRc, Vec>, Option), @@ -339,7 +339,7 @@ pub enum Term { } pub enum InlinedQueryTerm { - CompareNumber(CompareNumberQT, Vec>), + CompareNumber(CompareNumberQT, Vec>), IsAtomic(Vec>), IsCompound(Vec>), IsInteger(Vec>), @@ -347,13 +347,13 @@ pub enum InlinedQueryTerm { IsString(Vec>), IsFloat(Vec>), IsNonVar(Vec>), - IsVar(Vec>), + IsVar(Vec>), } impl InlinedQueryTerm { pub fn arity(&self) -> usize { match self { - &InlinedQueryTerm::CompareNumber(_, _) => 2, + &InlinedQueryTerm::CompareNumber(_, _) => 2, &InlinedQueryTerm::IsAtomic(_) => 1, &InlinedQueryTerm::IsCompound(_) => 1, &InlinedQueryTerm::IsFloat(_) => 1, @@ -361,7 +361,7 @@ impl InlinedQueryTerm { &InlinedQueryTerm::IsString(_) => 1, &InlinedQueryTerm::IsNonVar(_) => 1, &InlinedQueryTerm::IsInteger(_) => 1, - &InlinedQueryTerm::IsVar(_) => 1, + &InlinedQueryTerm::IsVar(_) => 1, } } } @@ -376,6 +376,19 @@ pub enum CompareNumberQT { Equal } +impl CompareNumberQT { + fn name<'a>(self) -> &'a str { + match self { + CompareNumberQT::GreaterThan => ">", + CompareNumberQT::LessThan => "<", + CompareNumberQT::GreaterThanOrEqual => ">=", + CompareNumberQT::LessThanOrEqual => "=<", + CompareNumberQT::NotEqual => "=\\=", + CompareNumberQT::Equal => "=:=" + } + } +} + // vars of predicate, toplevel offset. Vec is always a vector // of vars (we get their adjoining cells this way). pub type JumpStub = Vec; @@ -383,7 +396,7 @@ pub type JumpStub = Vec; pub enum QueryTerm { Arg(Vec>), CallN(Vec>), - Catch(Vec>), + Catch(Vec>), Cut, Display(Vec>), DuplicateTerm(Vec>), @@ -402,7 +415,7 @@ impl QueryTerm { &QueryTerm::Arg(_) => 3, &QueryTerm::Catch(_) => 3, &QueryTerm::Display(_) => 1, - &QueryTerm::Throw(_) => 1, + &QueryTerm::Throw(_) => 1, &QueryTerm::DuplicateTerm(_) => 2, &QueryTerm::Functor(_) => 3, &QueryTerm::Inlined(ref term) => term.arity(), @@ -421,13 +434,12 @@ pub struct Rule { pub clauses: Vec } - - #[derive(Clone, Copy)] pub enum ClauseType<'a> { Arg, CallN, Catch, + CompareNumber(CompareNumberQT), Deep(Level, &'a Cell, &'a TabledRc, Option), Display, DuplicateTerm, @@ -444,17 +456,18 @@ impl<'a> ClauseType<'a> { &ClauseType::Arg => "arg", &ClauseType::CallN => "call", &ClauseType::Catch => "catch", + &ClauseType::CompareNumber(qt) => qt.name(), &ClauseType::Display => "display", - &ClauseType::Deep(_, _, name, _) => name.as_str(), + &ClauseType::Deep(_, _, name, _) => name.as_str(), &ClauseType::DuplicateTerm => "duplicate_term", - &ClauseType::Functor => "functor", + &ClauseType::Functor => "functor", &ClauseType::Is => "is", &ClauseType::Root(name) => name.as_str(), &ClauseType::SetupCallCleanup => "setup_call_cleanup", &ClauseType::Throw => "throw" } } - + pub fn level_of_subterms(self) -> Level { match self { ClauseType::Deep(..) => Level::Deep, @@ -531,7 +544,7 @@ impl fmt::Display for Number { match self { &Number::Float(fl) => write!(f, "{}", fl), &Number::Integer(ref bi) => write!(f, "{}", bi), - &Number::Rational(ref r) => write!(f, "{}", r) + &Number::Rational(ref r) => write!(f, "{}", r) } } } @@ -558,7 +571,7 @@ impl Number { NumberPair::Rational(n1, n2) => n1 > n2 } } - + pub fn gte(self, n2: Number) -> bool { match NumberPair::from(self, n2) { NumberPair::Integer(n1, n2) => n1 >= n2, @@ -597,7 +610,7 @@ impl Number { NumberPair::Float(n1, n2) => n1 == n2, NumberPair::Rational(n1, n2) => n1 == n2 } - } + } } pub enum NumberPair { @@ -655,7 +668,7 @@ impl NumberPair { (Number::Float(n1), Number::Float(n2)) => NumberPair::Float(n1, n2), (Number::Rational(n1), Number::Rational(n2)) => - NumberPair::Rational(n1, n2), + NumberPair::Rational(n1, n2), (Number::Integer(n1), Number::Float(n2)) => Self::integer_float_pair(n1, n2), (Number::Float(n1), Number::Integer(n2)) => @@ -713,10 +726,10 @@ impl Mul for Number { Number::Integer(Rc::new(&*n1 * &*n2)), NumberPair::Rational(r1, r2) => Number::Rational(Rc::new(&*r1 * &*r2)) - } + } } } - + impl Div for Number { type Output = Number; @@ -796,7 +809,7 @@ pub enum ArithmeticInstruction { pub enum BuiltInInstruction { CleanUpBlock, - CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), + CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm), EraseBall, Fail, GetArgCall, @@ -849,7 +862,7 @@ pub enum ControlInstruction { GotoCall(usize, usize), // p, arity. GotoExecute(usize, usize), // p, arity. JmpByCall(usize, usize), // arity, global_offset. - JmpByExecute(usize, usize), + JmpByExecute(usize, usize), IsCall(RegType, ArithmeticTerm), IsExecute(RegType, ArithmeticTerm), Proceed, @@ -877,7 +890,7 @@ impl ControlInstruction { &ControlInstruction::ThrowCall => true, &ControlInstruction::ThrowExecute => true, &ControlInstruction::GetCleanerCall => true, - &ControlInstruction::GotoCall(..) => true, + &ControlInstruction::GotoCall(..) => true, &ControlInstruction::GotoExecute(..) => true, &ControlInstruction::Proceed => true, &ControlInstruction::IsCall(..) => true, @@ -967,7 +980,7 @@ impl Addr { _ => false } } - + pub fn as_var(&self) -> Option { match self { &Addr::HeapCell(hc) => Some(Ref::HeapCell(hc)), @@ -1069,7 +1082,7 @@ impl Heap { pub fn with_capacity(cap: usize) -> Self { Heap { heap: Vec::with_capacity(cap), h: 0 } } - + pub fn push(&mut self, val: HeapCellValue) { self.heap.push(val); self.h += 1; @@ -1086,7 +1099,7 @@ impl Heap { pub fn append(&mut self, vals: Vec) { let n = vals.len(); - + self.heap.extend(vals.into_iter()); self.h += n; } diff --git a/src/prolog/codegen.rs b/src/prolog/codegen.rs index 7eda2611..a52cbe64 100644 --- a/src/prolog/codegen.rs +++ b/src/prolog/codegen.rs @@ -100,23 +100,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker> { match self.marker.bindings().get(name) { Some(&VarData::Temp(_, t, _)) if t != 0 => RegType::Temp(t), - Some(&VarData::Perm(p)) if p != 0 => - /* if let GenContext::Last(chunk_num) = term_loc { - let mut target = Vec::new(); - - self.marker.evacuate_arg(chunk_num, &mut target); - - self.marker.reset_arg(arity); - self.marker.mark_var(name, Level::Shallow, vr, term_loc, &mut target); - - if !target.is_empty() { - code.push(Line::Query(target)); - } - - vr.get().norm() - } else { */ - RegType::Perm(p), - //}, + Some(&VarData::Perm(p)) if p != 0 => RegType::Perm(p), _ => { let mut target = Vec::new(); @@ -422,7 +406,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker> match inner_term[0].as_ref() { &Term::AnonVar => { code.push(fail!()); - }, + }, &Term::Var(ref vr, ref name) => { let r = self.mark_non_callable(name, 1, term_loc, vr, code); code.push(is_nonvar!(r)); @@ -524,7 +508,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker> _ if chunk_num == 0 => { self.marker.reset_arg(term.arity()); - let iter = term.post_order_iter(); + let iter = term.post_order_iter(); let query = self.compile_target(iter, term_loc, is_exposed); if !query.is_empty() { diff --git a/src/prolog/iterators.rs b/src/prolog/iterators.rs index ab63e2dd..b309f7ef 100644 --- a/src/prolog/iterators.rs +++ b/src/prolog/iterators.rs @@ -60,10 +60,13 @@ impl<'a> QueryIterator<'a> { let state = TermIterState::Clause(0, ClauseType::Display, terms); QueryIterator { state_stack: vec![state] } }, - &QueryTerm::Inlined(InlinedQueryTerm::CompareNumber(_, ref terms)) - | &QueryTerm::Is(ref terms) => { - let state = TermIterState::Clause(0, ClauseType::Is, terms); - QueryIterator { state_stack: vec![state] } + &QueryTerm::Inlined(InlinedQueryTerm::CompareNumber(qt, ref terms)) => { + let state = TermIterState::Clause(0, ClauseType::CompareNumber(qt), terms); + QueryIterator { state_stack: vec![state] } + }, + &QueryTerm::Is(ref terms) => { + let state = TermIterState::Clause(0, ClauseType::Is, terms); + QueryIterator { state_stack: vec![state] } }, &QueryTerm::Inlined(InlinedQueryTerm::IsAtomic(ref terms)) | &QueryTerm::Inlined(InlinedQueryTerm::IsInteger(ref terms)) diff --git a/src/prolog/macros.rs b/src/prolog/macros.rs index 98220187..a0859e99 100644 --- a/src/prolog/macros.rs +++ b/src/prolog/macros.rs @@ -554,7 +554,7 @@ macro_rules! check_cp_execute { macro_rules! get_cleaner_call { () => ( Line::Control(ControlInstruction::GetCleanerCall) - ) + ) } macro_rules! restore_cut_policy {