From: Mark Thom Date: Sun, 24 Dec 2017 22:44:40 +0000 (-0700) Subject: indexing bug fixes, corrections to , X-Git-Tag: v0.8.110~647 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=2dc25400cea7921162dcf7874f8846566c65c963;p=scryer-prolog.git indexing bug fixes, corrections to , --- diff --git a/Cargo.lock b/Cargo.lock index bcb30e0c..56e97a19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "rusty-wam" -version = "0.7.3" +version = "0.7.4" dependencies = [ "lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index ee86c1b3..77f06586 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -729,8 +729,7 @@ pub enum BuiltInInstruction { IsVar(RegType), ResetBlock, SetBall, - SetNeckCutPoint(Terminal), - SetNonNeckCutPoint(Terminal), + SetCutPoint, Succeed, Unify, UnwindStack diff --git a/src/prolog/builtins.rs b/src/prolog/builtins.rs index 3dce6392..cdb7f53e 100644 --- a/src/prolog/builtins.rs +++ b/src/prolog/builtins.rs @@ -121,7 +121,7 @@ fn get_builtins() -> Code { deallocate!(), goto!(88, 3), try_me_else!(25), // ','/3, 88. - switch_on_term!(4, 0, 0, 0), + switch_on_term!(4, 1, 0, 0), indexed_try!(4), retry!(11), trust!(14), @@ -133,7 +133,7 @@ fn get_builtins() -> Code { unify_variable!(perm_v!(1)), get_var_in_fact!(perm_v!(3), 3)], query![put_value!(perm_v!(3), 1)], - set_non_neck_cp!(non_terminal!()), + set_cp!(), query![put_unsafe_value!(2, 1), put_unsafe_value!(1, 2), put_value!(perm_v!(3), 3)], @@ -143,30 +143,32 @@ fn get_builtins() -> Code { fact![get_constant!(Constant::from("!"), temp_v!(1)), get_constant!(Constant::from("!"), temp_v!(2))], query![put_value!(temp_v!(3), 1)], - set_neck_cp!(terminal!()), + set_cp!(), trust_me!(), allocate!(1), fact![get_constant!(Constant::from("!"), temp_v!(1)), get_var_in_fact!(perm_v!(1), 2)], query![put_value!(temp_v!(3), 1)], - set_non_neck_cp!(non_terminal!()), + set_cp!(), query![put_value!(perm_v!(1), 1)], deallocate!(), execute_n!(1), - retry_me_else!(7), + retry_me_else!(8), allocate!(1), fact![get_constant!(Constant::from("!"), temp_v!(2)), get_var_in_fact!(perm_v!(1), 3)], + neck_cut!(non_terminal!()), call_n!(1), query![put_value!(perm_v!(1), 1)], deallocate!(), - set_non_neck_cp!(terminal!()), - retry_me_else!(7), + set_cp!(), + retry_me_else!(8), // 121. allocate!(3), fact![get_structure!(String::from(","), 2, temp_v!(2)), unify_variable!(perm_v!(2)), unify_variable!(perm_v!(1)), - get_var_in_fact!(perm_v!(3), 3)], + get_var_in_fact!(perm_v!(3), 3)], + neck_cut!(non_terminal!()), call_n!(1), query![put_unsafe_value!(2, 1), put_unsafe_value!(1, 2), @@ -175,12 +177,12 @@ fn get_builtins() -> Code { goto!(88, 3), trust_me!(), allocate!(1), - fact![get_var_in_fact!(perm_v!(1), 2)], + fact![get_var_in_fact!(perm_v!(1), 2)], call_n!(1), query![put_value!(perm_v!(1), 1)], deallocate!(), execute_n!(1), - allocate!(2), // (->)/2, 134. + allocate!(2), // (->)/2, 136. get_level!(), fact![get_var_in_fact!(perm_v!(2), 2)], call_n!(1), @@ -254,7 +256,7 @@ pub fn build_code_dir() -> (Code, CodeDir, OpDir) code_dir.insert((String::from(";"), 2), (PredicateKeyType::BuiltIn, 76)); code_dir.insert((String::from(","), 2), (PredicateKeyType::BuiltIn, 81)); - code_dir.insert((String::from("->"), 2), (PredicateKeyType::BuiltIn, 134)); + code_dir.insert((String::from("->"), 2), (PredicateKeyType::BuiltIn, 136)); (builtin_code, code_dir, op_dir) } diff --git a/src/prolog/indexing.rs b/src/prolog/indexing.rs index f087c831..3d94ec02 100644 --- a/src/prolog/indexing.rs +++ b/src/prolog/indexing.rs @@ -116,6 +116,15 @@ impl CodeOffsets { flattened_index } + fn adjust_internal_index(index: IntIndex) -> IntIndex + { + match index { + IntIndex::Internal(o) => IntIndex::Internal(o + 1), + IntIndex::External(o) => IntIndex::External(o), + _ => IntIndex::Fail + } + } + fn switch_on_constant(con_ind: HashMap, prelude: &mut CodeDeque) -> IntIndex { @@ -130,24 +139,11 @@ impl CodeOffsets { IntIndex::Internal(1) } else { con_ind.values().next() - .map(|i| *i) + .map(|index| Self::adjust_internal_index(*index)) .unwrap_or(IntIndex::Fail) } } - fn switch_on_list(mut lists: ThirdLevelIndex, prelude: &mut CodeDeque) -> IntIndex - { - if lists.len() > 1 { - Self::cap_choice_seq_with_trust(&mut lists); - prelude.extend(lists.into_iter().map(|i| Line::from(i))); - IntIndex::Internal(0) - } else { - lists.first() - .map(|i| IntIndex::External(i.offset())) - .unwrap_or(IntIndex::Fail) - } - } - fn switch_on_structure(str_ind: HashMap<(Atom, usize), ThirdLevelIndex>, prelude: &mut CodeDeque) -> IntIndex { @@ -162,11 +158,23 @@ impl CodeOffsets { IntIndex::Internal(1) } else { str_ind.values().next() - .map(|i| *i) + .map(|index| Self::adjust_internal_index(*index)) .unwrap_or(IntIndex::Fail) } } + fn switch_on_list(mut lists: ThirdLevelIndex, prelude: &mut CodeDeque) -> IntIndex + { + if lists.len() > 1 { + Self::cap_choice_seq_with_trust(&mut lists); + prelude.extend(lists.into_iter().map(|i| Line::from(i))); + IntIndex::Internal(0) + } else { + lists.first() + .map(|i| IntIndex::External(i.offset())) + .unwrap_or(IntIndex::Fail) + } + } fn switch_on_str_offset_from(str_loc: IntIndex, prelude_len: usize, con_loc: IntIndex) -> usize @@ -186,7 +194,7 @@ impl CodeOffsets { match con_loc { IntIndex::External(offset) => offset + prelude_len + 1, IntIndex::Fail => 0, - IntIndex::Internal(offset) => offset, + IntIndex::Internal(offset) => offset, } } diff --git a/src/prolog/io.rs b/src/prolog/io.rs index d63b700d..5780dec3 100644 --- a/src/prolog/io.rs +++ b/src/prolog/io.rs @@ -183,10 +183,8 @@ impl fmt::Display for BuiltInInstruction { write!(f, "reset_block"), &BuiltInInstruction::SetBall => write!(f, "set_ball"), - &BuiltInInstruction::SetNeckCutPoint(terminal) => - write!(f, "set_neck_cp {}", terminal), - &BuiltInInstruction::SetNonNeckCutPoint(terminal) => - write!(f, "set_non_neck_cp {}", terminal), + &BuiltInInstruction::SetCutPoint => + write!(f, "set_cp"), &BuiltInInstruction::Succeed => write!(f, "true"), &BuiltInInstruction::UnwindStack => @@ -399,7 +397,7 @@ pub fn eval<'a, 'b: 'a>(wam: &'a mut Machine, tl: &'b TopLevel) -> EvalSession<' Ok(rule) => rule, Err(e) => return EvalSession::ParserError(e) }; - + wam.add_rule(rule, compiled_rule) }, &TopLevel::Query(ref query) => { diff --git a/src/prolog/machine.rs b/src/prolog/machine.rs index 047a05f1..4ea6dc2c 100644 --- a/src/prolog/machine.rs +++ b/src/prolog/machine.rs @@ -1666,24 +1666,17 @@ impl MachineState { self.p += 1; }, - &BuiltInInstruction::SetNeckCutPoint(terminal) => { + &BuiltInInstruction::SetCutPoint => { let nb = self.store(self.deref(self[temp_v!(1)].clone())); match nb { Addr::Con(Constant::Usize(nb)) => { + self.or_stack.truncate(nb); self.b = nb; - self.neck_cut(terminal); - }, - _ => self.fail = true - }; - }, - &BuiltInInstruction::SetNonNeckCutPoint(terminal) => { - let nb = self.store(self.deref(self[temp_v!(1)].clone())); + + self.tidy_trail(); - match nb { - Addr::Con(Constant::Usize(nb)) => { - self.b = nb; - self.non_neck_cut(terminal); + self.p += 1; }, _ => self.fail = true }; @@ -1731,7 +1724,7 @@ impl MachineState { self.fail = true; }, &BuiltInInstruction::IsAtomic(r) => { - let d = self.deref(self[r].clone()); + let d = self.store(self.deref(self[r].clone())); match d { Addr::Con(_) => self.p += 1, @@ -1739,7 +1732,7 @@ impl MachineState { }; }, &BuiltInInstruction::IsVar(r) => { - let d = self.deref(self[r].clone()); + let d = self.store(self.deref(self[r].clone())); match d { Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1, @@ -2026,11 +2019,10 @@ impl MachineState { } } - fn non_neck_cut(&mut self, term: Terminal) + fn neck_cut(&mut self, term: Terminal) { let b = self.b; - let e = self.e; - let b0 = self.and_stack[e].b0; // STACK[E+2+1] + let b0 = self.b0; if b > b0 { self.b = b0; @@ -2041,13 +2033,14 @@ impl MachineState { self.p = self.cp; } else { self.p += 1; - } + } } - fn neck_cut(&mut self, term: Terminal) + fn non_neck_cut(&mut self, term: Terminal) { let b = self.b; - let b0 = self.b0; + let e = self.e; + let b0 = self.and_stack[e].b0; // STACK[E+2+1] if b > b0 { self.b = b0; @@ -2058,7 +2051,7 @@ impl MachineState { self.p = self.cp; } else { self.p += 1; - } + } } fn execute_cut_instr(&mut self, instr: &CutInstruction) { diff --git a/src/prolog/macros.rs b/src/prolog/macros.rs index c31e5f59..bd7e72cc 100644 --- a/src/prolog/macros.rs +++ b/src/prolog/macros.rs @@ -165,12 +165,6 @@ macro_rules! proceed { ) } -macro_rules! terminal { - () => ( - Terminal::Terminal - ) -} - macro_rules! non_terminal { () => ( Terminal::Non @@ -183,6 +177,12 @@ macro_rules! cut { ) } +macro_rules! neck_cut { + ($term:expr) => ( + Line::Cut(CutInstruction::NeckCut($term)) + ) +} + macro_rules! get_current_block { () => ( Line::BuiltIn(BuiltInInstruction::GetCurrentBlock) @@ -303,15 +303,9 @@ macro_rules! get_cp { ) } -macro_rules! set_neck_cp { - ($term:expr) => ( - Line::BuiltIn(BuiltInInstruction::SetNeckCutPoint($term)) - ) -} - -macro_rules! set_non_neck_cp { - ($term:expr) => ( - Line::BuiltIn(BuiltInInstruction::SetNonNeckCutPoint($term)) +macro_rules! set_cp { + () => ( + Line::BuiltIn(BuiltInInstruction::SetCutPoint) ) }