indexing bug fixes, corrections to ,

This commit is contained in:
Mark Thom
2017-12-24 15:44:40 -07:00
parent eab95a360a
commit 2dc25400ce
7 changed files with 75 additions and 81 deletions

2
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
[root] [root]
name = "rusty-wam" name = "rusty-wam"
version = "0.7.3" version = "0.7.4"
dependencies = [ dependencies = [
"lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -729,8 +729,7 @@ pub enum BuiltInInstruction {
IsVar(RegType), IsVar(RegType),
ResetBlock, ResetBlock,
SetBall, SetBall,
SetNeckCutPoint(Terminal), SetCutPoint,
SetNonNeckCutPoint(Terminal),
Succeed, Succeed,
Unify, Unify,
UnwindStack UnwindStack

View File

@@ -121,7 +121,7 @@ fn get_builtins() -> Code {
deallocate!(), deallocate!(),
goto!(88, 3), goto!(88, 3),
try_me_else!(25), // ','/3, 88. try_me_else!(25), // ','/3, 88.
switch_on_term!(4, 0, 0, 0), switch_on_term!(4, 1, 0, 0),
indexed_try!(4), indexed_try!(4),
retry!(11), retry!(11),
trust!(14), trust!(14),
@@ -133,7 +133,7 @@ fn get_builtins() -> Code {
unify_variable!(perm_v!(1)), unify_variable!(perm_v!(1)),
get_var_in_fact!(perm_v!(3), 3)], get_var_in_fact!(perm_v!(3), 3)],
query![put_value!(perm_v!(3), 1)], query![put_value!(perm_v!(3), 1)],
set_non_neck_cp!(non_terminal!()), set_cp!(),
query![put_unsafe_value!(2, 1), query![put_unsafe_value!(2, 1),
put_unsafe_value!(1, 2), put_unsafe_value!(1, 2),
put_value!(perm_v!(3), 3)], put_value!(perm_v!(3), 3)],
@@ -143,30 +143,32 @@ fn get_builtins() -> Code {
fact![get_constant!(Constant::from("!"), temp_v!(1)), fact![get_constant!(Constant::from("!"), temp_v!(1)),
get_constant!(Constant::from("!"), temp_v!(2))], get_constant!(Constant::from("!"), temp_v!(2))],
query![put_value!(temp_v!(3), 1)], query![put_value!(temp_v!(3), 1)],
set_neck_cp!(terminal!()), set_cp!(),
trust_me!(), trust_me!(),
allocate!(1), allocate!(1),
fact![get_constant!(Constant::from("!"), temp_v!(1)), fact![get_constant!(Constant::from("!"), temp_v!(1)),
get_var_in_fact!(perm_v!(1), 2)], get_var_in_fact!(perm_v!(1), 2)],
query![put_value!(temp_v!(3), 1)], query![put_value!(temp_v!(3), 1)],
set_non_neck_cp!(non_terminal!()), set_cp!(),
query![put_value!(perm_v!(1), 1)], query![put_value!(perm_v!(1), 1)],
deallocate!(), deallocate!(),
execute_n!(1), execute_n!(1),
retry_me_else!(7), retry_me_else!(8),
allocate!(1), allocate!(1),
fact![get_constant!(Constant::from("!"), temp_v!(2)), fact![get_constant!(Constant::from("!"), temp_v!(2)),
get_var_in_fact!(perm_v!(1), 3)], get_var_in_fact!(perm_v!(1), 3)],
neck_cut!(non_terminal!()),
call_n!(1), call_n!(1),
query![put_value!(perm_v!(1), 1)], query![put_value!(perm_v!(1), 1)],
deallocate!(), deallocate!(),
set_non_neck_cp!(terminal!()), set_cp!(),
retry_me_else!(7), retry_me_else!(8), // 121.
allocate!(3), allocate!(3),
fact![get_structure!(String::from(","), 2, temp_v!(2)), fact![get_structure!(String::from(","), 2, temp_v!(2)),
unify_variable!(perm_v!(2)), unify_variable!(perm_v!(2)),
unify_variable!(perm_v!(1)), 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), call_n!(1),
query![put_unsafe_value!(2, 1), query![put_unsafe_value!(2, 1),
put_unsafe_value!(1, 2), put_unsafe_value!(1, 2),
@@ -180,7 +182,7 @@ fn get_builtins() -> Code {
query![put_value!(perm_v!(1), 1)], query![put_value!(perm_v!(1), 1)],
deallocate!(), deallocate!(),
execute_n!(1), execute_n!(1),
allocate!(2), // (->)/2, 134. allocate!(2), // (->)/2, 136.
get_level!(), get_level!(),
fact![get_var_in_fact!(perm_v!(2), 2)], fact![get_var_in_fact!(perm_v!(2), 2)],
call_n!(1), 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, 76));
code_dir.insert((String::from(","), 2), (PredicateKeyType::BuiltIn, 81)); 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) (builtin_code, code_dir, op_dir)
} }

View File

@@ -116,6 +116,15 @@ impl CodeOffsets {
flattened_index 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<Constant, ThirdLevelIndex>, prelude: &mut CodeDeque) fn switch_on_constant(con_ind: HashMap<Constant, ThirdLevelIndex>, prelude: &mut CodeDeque)
-> IntIndex -> IntIndex
{ {
@@ -130,24 +139,11 @@ impl CodeOffsets {
IntIndex::Internal(1) IntIndex::Internal(1)
} else { } else {
con_ind.values().next() con_ind.values().next()
.map(|i| *i) .map(|index| Self::adjust_internal_index(*index))
.unwrap_or(IntIndex::Fail) .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) fn switch_on_structure(str_ind: HashMap<(Atom, usize), ThirdLevelIndex>, prelude: &mut CodeDeque)
-> IntIndex -> IntIndex
{ {
@@ -162,11 +158,23 @@ impl CodeOffsets {
IntIndex::Internal(1) IntIndex::Internal(1)
} else { } else {
str_ind.values().next() str_ind.values().next()
.map(|i| *i) .map(|index| Self::adjust_internal_index(*index))
.unwrap_or(IntIndex::Fail) .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) fn switch_on_str_offset_from(str_loc: IntIndex, prelude_len: usize, con_loc: IntIndex)
-> usize -> usize

View File

@@ -183,10 +183,8 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "reset_block"), write!(f, "reset_block"),
&BuiltInInstruction::SetBall => &BuiltInInstruction::SetBall =>
write!(f, "set_ball"), write!(f, "set_ball"),
&BuiltInInstruction::SetNeckCutPoint(terminal) => &BuiltInInstruction::SetCutPoint =>
write!(f, "set_neck_cp {}", terminal), write!(f, "set_cp"),
&BuiltInInstruction::SetNonNeckCutPoint(terminal) =>
write!(f, "set_non_neck_cp {}", terminal),
&BuiltInInstruction::Succeed => &BuiltInInstruction::Succeed =>
write!(f, "true"), write!(f, "true"),
&BuiltInInstruction::UnwindStack => &BuiltInInstruction::UnwindStack =>

View File

@@ -1666,24 +1666,17 @@ impl MachineState {
self.p += 1; self.p += 1;
}, },
&BuiltInInstruction::SetNeckCutPoint(terminal) => { &BuiltInInstruction::SetCutPoint => {
let nb = self.store(self.deref(self[temp_v!(1)].clone())); let nb = self.store(self.deref(self[temp_v!(1)].clone()));
match nb { match nb {
Addr::Con(Constant::Usize(nb)) => { Addr::Con(Constant::Usize(nb)) => {
self.or_stack.truncate(nb);
self.b = 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()));
match nb { self.tidy_trail();
Addr::Con(Constant::Usize(nb)) => {
self.b = nb; self.p += 1;
self.non_neck_cut(terminal);
}, },
_ => self.fail = true _ => self.fail = true
}; };
@@ -1731,7 +1724,7 @@ impl MachineState {
self.fail = true; self.fail = true;
}, },
&BuiltInInstruction::IsAtomic(r) => { &BuiltInInstruction::IsAtomic(r) => {
let d = self.deref(self[r].clone()); let d = self.store(self.deref(self[r].clone()));
match d { match d {
Addr::Con(_) => self.p += 1, Addr::Con(_) => self.p += 1,
@@ -1739,7 +1732,7 @@ impl MachineState {
}; };
}, },
&BuiltInInstruction::IsVar(r) => { &BuiltInInstruction::IsVar(r) => {
let d = self.deref(self[r].clone()); let d = self.store(self.deref(self[r].clone()));
match d { match d {
Addr::HeapCell(_) | Addr::StackCell(_,_) => self.p += 1, 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 b = self.b;
let e = self.e; let b0 = self.b0;
let b0 = self.and_stack[e].b0; // STACK[E+2+1]
if b > b0 { if b > b0 {
self.b = b0; self.b = b0;
@@ -2044,10 +2036,11 @@ impl MachineState {
} }
} }
fn neck_cut(&mut self, term: Terminal) fn non_neck_cut(&mut self, term: Terminal)
{ {
let b = self.b; 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 { if b > b0 {
self.b = b0; self.b = b0;

View File

@@ -165,12 +165,6 @@ macro_rules! proceed {
) )
} }
macro_rules! terminal {
() => (
Terminal::Terminal
)
}
macro_rules! non_terminal { macro_rules! non_terminal {
() => ( () => (
Terminal::Non 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 { macro_rules! get_current_block {
() => ( () => (
Line::BuiltIn(BuiltInInstruction::GetCurrentBlock) Line::BuiltIn(BuiltInInstruction::GetCurrentBlock)
@@ -303,15 +303,9 @@ macro_rules! get_cp {
) )
} }
macro_rules! set_neck_cp { macro_rules! set_cp {
($term:expr) => ( () => (
Line::BuiltIn(BuiltInInstruction::SetNeckCutPoint($term)) Line::BuiltIn(BuiltInInstruction::SetCutPoint)
)
}
macro_rules! set_non_neck_cp {
($term:expr) => (
Line::BuiltIn(BuiltInInstruction::SetNonNeckCutPoint($term))
) )
} }