another big refactor

This commit is contained in:
Mark Thom
2018-01-11 20:56:04 -07:00
parent 59ccf5d8e8
commit c513bbeafa
8 changed files with 190 additions and 193 deletions

22
Cargo.lock generated
View File

@@ -1,14 +1,3 @@
[root]
name = "rusty-wam"
version = "0.7.4"
dependencies = [
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.6.4" version = "0.6.4"
@@ -180,6 +169,17 @@ name = "rustc-serialize"
version = "0.3.24" version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rusty-wam"
version = "0.7.4"
dependencies = [
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "termion" name = "termion"
version = "1.5.1" version = "1.5.1"

View File

@@ -5,14 +5,14 @@ use std::cmp::{min, max};
use std::vec::Vec; use std::vec::Vec;
pub struct ArithExprIterator<'a> { pub struct ArithExprIterator<'a> {
state_stack: Vec<IteratorState<'a>> state_stack: Vec<TermIterState<'a>>
} }
pub type ArithCont = (Code, Option<ArithmeticTerm>); pub type ArithCont = (Code, Option<ArithmeticTerm>);
impl<'a> ArithExprIterator<'a> { impl<'a> ArithExprIterator<'a> {
fn push_subterm(&mut self, lvl: Level, term: &'a Term) { fn push_subterm(&mut self, lvl: Level, term: &'a Term) {
self.state_stack.push(IteratorState::to_state(lvl, term)); self.state_stack.push(TermIterState::to_state(lvl, term));
} }
fn new(term: &'a Term) -> Result<Self, ArithmeticError> { fn new(term: &'a Term) -> Result<Self, ArithmeticError> {
@@ -20,13 +20,13 @@ impl<'a> ArithExprIterator<'a> {
&Term::AnonVar => &Term::AnonVar =>
return Err(ArithmeticError::InvalidTerm), return Err(ArithmeticError::InvalidTerm),
&Term::Clause(_, _, ref terms) => &Term::Clause(_, _, ref terms) =>
IteratorState::Clause(0, ClauseType::Root, terms), TermIterState::Clause(0, ClauseType::Root, terms),
&Term::Constant(ref cell, ref cons) => &Term::Constant(ref cell, ref cons) =>
IteratorState::Constant(Level::Shallow, cell, cons), TermIterState::Constant(Level::Shallow, cell, cons),
&Term::Cons(_, _, _) => &Term::Cons(_, _, _) =>
return Err(ArithmeticError::InvalidTerm), return Err(ArithmeticError::InvalidTerm),
&Term::Var(ref cell, ref var) => &Term::Var(ref cell, ref var) =>
IteratorState::Var(Level::Shallow, cell, var) TermIterState::Var(Level::Shallow, cell, var)
}; };
Ok(ArithExprIterator { state_stack: vec![state] }) Ok(ArithExprIterator { state_stack: vec![state] })
@@ -45,26 +45,26 @@ impl<'a> Iterator for ArithExprIterator<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while let Some(iter_state) = self.state_stack.pop() { while let Some(iter_state) = self.state_stack.pop() {
match iter_state { match iter_state {
IteratorState::AnonVar(lvl) => TermIterState::AnonVar(lvl) =>
return Some(TermRef::AnonVar(lvl)), return Some(TermRef::AnonVar(lvl)),
IteratorState::Clause(child_num, ct, child_terms) => { TermIterState::Clause(child_num, ct, child_terms) => {
if child_num == child_terms.len() { if child_num == child_terms.len() {
return Some(TermRef::Clause(ct, child_terms)); return Some(TermRef::Clause(ct, child_terms));
} else { } else {
self.state_stack.push(IteratorState::Clause(child_num + 1, ct, child_terms)); self.state_stack.push(TermIterState::Clause(child_num + 1, ct, child_terms));
self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref()); self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref());
} }
}, },
IteratorState::InitialCons(lvl, cell, head, tail) => { TermIterState::InitialCons(lvl, cell, head, tail) => {
self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); self.state_stack.push(TermIterState::FinalCons(lvl, cell, head, tail));
self.push_subterm(Level::Deep, tail); self.push_subterm(Level::Deep, tail);
self.push_subterm(Level::Deep, head); self.push_subterm(Level::Deep, head);
}, },
IteratorState::FinalCons(lvl, cell, head, tail) => TermIterState::FinalCons(lvl, cell, head, tail) =>
return Some(TermRef::Cons(lvl, cell, head, tail)), return Some(TermRef::Cons(lvl, cell, head, tail)),
IteratorState::Constant(lvl, cell, constant) => TermIterState::Constant(lvl, cell, constant) =>
return Some(TermRef::Constant(lvl, cell, constant)), return Some(TermRef::Constant(lvl, cell, constant)),
IteratorState::Var(lvl, cell, var) => TermIterState::Var(lvl, cell, var) =>
return Some(TermRef::Var(lvl, cell, var)) return Some(TermRef::Var(lvl, cell, var))
}; };
} }

View File

@@ -864,6 +864,16 @@ impl Addr {
} }
pub fn as_ref(&self) -> Option<Ref> { pub fn as_ref(&self) -> Option<Ref> {
match self {
&Addr::HeapCell(hc) => Some(Ref::HeapCell(hc)),
&Addr::StackCell(fr, sc) => Some(Ref::StackCell(fr, sc)),
&Addr::Lis(hc) => Some(Ref::HeapCell(hc)),
&Addr::Str(hc) => Some(Ref::HeapCell(hc)),
_ => None
}
}
pub fn as_var(&self) -> Option<Ref> {
match self { match self {
&Addr::HeapCell(hc) => Some(Ref::HeapCell(hc)), &Addr::HeapCell(hc) => Some(Ref::HeapCell(hc)),
&Addr::StackCell(fr, sc) => Some(Ref::StackCell(fr, sc)), &Addr::StackCell(fr, sc) => Some(Ref::StackCell(fr, sc)),
@@ -896,37 +906,14 @@ pub enum Ref {
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub enum HeapCellValue { pub enum HeapCellValue {
Con(Constant), Addr(Addr),
Lis(usize), NamedStr(usize, Rc<Atom>), // arity, name.
NamedStr(usize, Rc<Atom>),
Ref(Ref),
Str(usize)
}
impl From<Addr> for HeapCellValue {
fn from(addr: Addr) -> HeapCellValue {
match addr {
Addr::Con(constant) =>
HeapCellValue::Con(constant),
Addr::HeapCell(hc) =>
HeapCellValue::Ref(Ref::HeapCell(hc)),
Addr::Lis(a) =>
HeapCellValue::Lis(a),
Addr::StackCell(fr, sc) =>
HeapCellValue::Ref(Ref::StackCell(fr, sc)),
Addr::Str(hc) =>
HeapCellValue::Str(hc)
}
}
} }
impl HeapCellValue { impl HeapCellValue {
pub fn as_addr(&self, focus: usize) -> Addr { pub fn as_addr(&self, focus: usize) -> Addr {
match self { match self {
&HeapCellValue::Con(ref c) => Addr::Con(c.clone()), &HeapCellValue::Addr(ref a) => a.clone(),
&HeapCellValue::Lis(a) => Addr::Lis(a),
&HeapCellValue::Ref(r) => Addr::from(r),
&HeapCellValue::Str(s) => Addr::Str(s),
&HeapCellValue::NamedStr(_, _) => Addr::Str(focus) &HeapCellValue::NamedStr(_, _) => Addr::Str(focus)
} }
} }
@@ -1064,7 +1051,7 @@ impl Term {
} }
} }
pub enum IteratorState<'a> { pub enum TermIterState<'a> {
AnonVar(Level), AnonVar(Level),
Clause(usize, ClauseType<'a>, &'a Vec<Box<Term>>), Clause(usize, ClauseType<'a>, &'a Vec<Box<Term>>),
Constant(Level, &'a Cell<RegType>, &'a Constant), Constant(Level, &'a Cell<RegType>, &'a Constant),
@@ -1073,19 +1060,19 @@ pub enum IteratorState<'a> {
Var(Level, &'a Cell<VarReg>, &'a Var) Var(Level, &'a Cell<VarReg>, &'a Var)
} }
impl<'a> IteratorState<'a> { impl<'a> TermIterState<'a> {
pub fn to_state(lvl: Level, term: &'a Term) -> IteratorState<'a> { pub fn to_state(lvl: Level, term: &'a Term) -> TermIterState<'a> {
match term { match term {
&Term::AnonVar => &Term::AnonVar =>
IteratorState::AnonVar(lvl), TermIterState::AnonVar(lvl),
&Term::Clause(ref cell, ref atom, ref child_terms) => &Term::Clause(ref cell, ref atom, ref child_terms) =>
IteratorState::Clause(0, ClauseType::Deep(lvl, cell, atom), child_terms), TermIterState::Clause(0, ClauseType::Deep(lvl, cell, atom), child_terms),
&Term::Cons(ref cell, ref head, ref tail) => &Term::Cons(ref cell, ref head, ref tail) =>
IteratorState::InitialCons(lvl, cell, head.as_ref(), tail.as_ref()), TermIterState::InitialCons(lvl, cell, head.as_ref(), tail.as_ref()),
&Term::Constant(ref cell, ref constant) => &Term::Constant(ref cell, ref constant) =>
IteratorState::Constant(lvl, cell, constant), TermIterState::Constant(lvl, cell, constant),
&Term::Var(ref cell, ref var) => &Term::Var(ref cell, ref var) =>
IteratorState::Var(lvl, cell, var) TermIterState::Var(lvl, cell, var)
} }
} }
} }

View File

@@ -17,18 +17,20 @@ pub trait CopierTarget
// after it's been copied to L2. // after it's been copied to L2.
fn duplicate_term(&mut self, a: Addr) where Self: IndexMut<usize, Output=HeapCellValue> fn duplicate_term(&mut self, a: Addr) where Self: IndexMut<usize, Output=HeapCellValue>
{ {
let mut forward_trail: Vec<(Ref, HeapCellValue)>= Vec::new(); let mut trail: Vec<(Ref, HeapCellValue)>= Vec::new();
let mut scan = self.source(); let mut scan = self.source();
let old_h = self.threshold(); let old_h = self.threshold();
self.push(HeapCellValue::from(a)); self.push(HeapCellValue::Addr(a));
while scan < self.threshold() { while scan < self.threshold() {
match self[scan].clone() { match self[scan].clone() {
HeapCellValue::Con(_) | HeapCellValue::NamedStr(_, _) => HeapCellValue::NamedStr(_, _) =>
scan += 1, scan += 1,
HeapCellValue::Lis(a) => { HeapCellValue::Addr(a) =>
self[scan] = HeapCellValue::Lis(self.threshold()); match a.clone() {
Addr::Lis(a) => {
self[scan] = HeapCellValue::Addr(Addr::Lis(self.threshold()));
let hcv = self[a].clone(); let hcv = self[a].clone();
self.push(hcv); self.push(hcv);
@@ -38,40 +40,42 @@ pub trait CopierTarget
scan += 1; scan += 1;
}, },
HeapCellValue::Ref(r) => { Addr::HeapCell(_) | Addr::StackCell(_, _) => {
let ra = Addr::from(r); let ra = a;
let rd = self.store(self.deref(ra.clone())); let rd = self.store(self.deref(ra.clone()));
match rd { match rd.clone() {
Addr::HeapCell(hc) if hc >= old_h => { Addr::HeapCell(hc) if hc >= old_h => {
self[scan] = HeapCellValue::Ref(Ref::HeapCell(hc)); self[scan] = HeapCellValue::Addr(rd);
scan += 1; scan += 1;
}, },
_ if ra == rd => { _ if ra == rd => {
self[scan] = HeapCellValue::Ref(Ref::HeapCell(scan)); self[scan] = HeapCellValue::Addr(Addr::HeapCell(scan));
match r { if let Addr::HeapCell(hc) = ra.clone() {
Ref::HeapCell(hc) => self[hc] = HeapCellValue::Addr(Addr::HeapCell(scan));
self[hc] = HeapCellValue::Ref(Ref::HeapCell(scan)), trail.push((Ref::HeapCell(hc),
Ref::StackCell(fr, sc) => HeapCellValue::Addr(Addr::HeapCell(hc))));
self.stack()[fr][sc] = Addr::HeapCell(scan) } else if let Addr::StackCell(fr, sc) = ra {
}; self.stack()[fr][sc] = Addr::HeapCell(scan);
trail.push((Ref::StackCell(fr, sc),
HeapCellValue::Addr(Addr::StackCell(fr, sc))));
}
forward_trail.push((r, HeapCellValue::Ref(r)));
scan += 1; scan += 1;
}, },
_ => self[scan] = HeapCellValue::from(rd) _ => self[scan] = HeapCellValue::Addr(rd)
} };
}, },
HeapCellValue::Str(s) => { Addr::Str(s) => {
match self[s].clone() { match self[s].clone() {
HeapCellValue::NamedStr(arity, name) => { HeapCellValue::NamedStr(arity, name) => {
let threshold = self.threshold(); let threshold = self.threshold();
self[scan] = HeapCellValue::Str(threshold); self[scan] = HeapCellValue::Addr(Addr::Str(threshold));
self[s] = HeapCellValue::Str(threshold); self[s] = HeapCellValue::Addr(Addr::Str(threshold));
forward_trail.push((Ref::HeapCell(s), trail.push((Ref::HeapCell(s),
HeapCellValue::NamedStr(arity, name.clone()))); HeapCellValue::NamedStr(arity, name.clone())));
self.push(HeapCellValue::NamedStr(arity, name)); self.push(HeapCellValue::NamedStr(arity, name));
@@ -81,17 +85,19 @@ pub trait CopierTarget
self.push(hcv); self.push(hcv);
} }
}, },
HeapCellValue::Str(o) => HeapCellValue::Addr(Addr::Str(o)) =>
self[scan] = HeapCellValue::Str(o), self[scan] = HeapCellValue::Addr(Addr::Str(o)),
_ => {} _ => {}
}; }
scan += 1; scan += 1;
},
Addr::Con(_) => scan += 1
}
} }
};
} }
for (r, hcv) in forward_trail { for (r, hcv) in trail {
match r { match r {
Ref::HeapCell(hc) => self[hc] = hcv, Ref::HeapCell(hc) => self[hc] = hcv,
Ref::StackCell(fr, sc) => self.stack()[fr][sc] = hcv.as_addr(0) Ref::StackCell(fr, sc) => self.stack()[fr][sc] = hcv.as_addr(0)

View File

@@ -128,10 +128,6 @@ impl<'a> HeapCellViewer<'a>
fn from_heap(&mut self, mut focus: usize) -> CellView<'a> { fn from_heap(&mut self, mut focus: usize) -> CellView<'a> {
loop { loop {
match &self.heap[focus] { match &self.heap[focus] {
&HeapCellValue::Con(ref c) =>
return CellView::Con(c),
&HeapCellValue::Lis(a) =>
return self.handle_list(a),
&HeapCellValue::NamedStr(arity, ref name) => { &HeapCellValue::NamedStr(arity, ref name) => {
self.state_stack.push(CellRef::TToken(TToken::RRBracket)); self.state_stack.push(CellRef::TToken(TToken::RRBracket));
@@ -145,21 +141,29 @@ impl<'a> HeapCellViewer<'a>
return CellView::Str(arity, name); return CellView::Str(arity, name);
}, },
&HeapCellValue::Ref(Ref::HeapCell(hc)) => &HeapCellValue::Addr(ref a) =>
match a {
&Addr::Con(ref c) =>
return CellView::Con(c),
&Addr::Lis(a) =>
return self.handle_list(a),
&Addr::HeapCell(hc) =>
if focus == hc { if focus == hc {
return CellView::HeapVar(hc); return CellView::HeapVar(hc);
} else { } else {
focus = hc; focus = hc;
}, },
&HeapCellValue::Ref(Ref::StackCell(fr, sc)) => &Addr::StackCell(fr, sc) =>
match self.deref_cell(&self.and_stack[fr][sc]) { match self.deref_cell(&self.and_stack[fr][sc]) {
CellRef::Lis(hc) => return self.handle_list(hc), CellRef::Lis(hc) => return self.handle_list(hc),
CellRef::View(cell_view) => return cell_view, CellRef::View(cell_view) => return cell_view,
CellRef::Redirect(hc) => focus = hc, CellRef::Redirect(hc) => focus = hc,
CellRef::TToken(token) => return CellView::TToken(token) CellRef::TToken(token) => return CellView::TToken(token)
}, },
&HeapCellValue::Str(cell_num) => &Addr::Str(cell_num) =>
focus = cell_num, focus = cell_num,
},
} }
} }
} }

View File

@@ -5,12 +5,12 @@ use std::iter::*;
use std::vec::Vec; use std::vec::Vec;
pub struct QueryIterator<'a> { pub struct QueryIterator<'a> {
state_stack: Vec<IteratorState<'a>> state_stack: Vec<TermIterState<'a>>
} }
impl<'a> QueryIterator<'a> { impl<'a> QueryIterator<'a> {
fn push_subterm(&mut self, lvl: Level, term: &'a Term) { fn push_subterm(&mut self, lvl: Level, term: &'a Term) {
self.state_stack.push(IteratorState::to_state(lvl, term)); self.state_stack.push(TermIterState::to_state(lvl, term));
} }
fn from_term(term: &'a Term) -> Self { fn from_term(term: &'a Term) -> Self {
@@ -18,13 +18,13 @@ impl<'a> QueryIterator<'a> {
&Term::AnonVar => &Term::AnonVar =>
return QueryIterator { state_stack: vec![] }, return QueryIterator { state_stack: vec![] },
&Term::Clause(_, _, ref terms) => &Term::Clause(_, _, ref terms) =>
IteratorState::Clause(0, ClauseType::Root, terms), TermIterState::Clause(0, ClauseType::Root, terms),
&Term::Cons(_, _, _) => &Term::Cons(_, _, _) =>
return QueryIterator { state_stack: vec![] }, return QueryIterator { state_stack: vec![] },
&Term::Constant(_, _) => &Term::Constant(_, _) =>
return QueryIterator { state_stack: vec![] }, return QueryIterator { state_stack: vec![] },
&Term::Var(ref cell, ref var) => &Term::Var(ref cell, ref var) =>
IteratorState::Var(Level::Shallow, cell, var) TermIterState::Var(Level::Shallow, cell, var)
}; };
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
@@ -33,21 +33,21 @@ impl<'a> QueryIterator<'a> {
fn new(term: &'a QueryTerm) -> Self { fn new(term: &'a QueryTerm) -> Self {
match term { match term {
&QueryTerm::CallN(ref terms) => { &QueryTerm::CallN(ref terms) => {
let state = IteratorState::Clause(1, ClauseType::CallN, terms); let state = TermIterState::Clause(1, ClauseType::CallN, terms);
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
}, },
&QueryTerm::Catch(ref terms) => { &QueryTerm::Catch(ref terms) => {
let state = IteratorState::Clause(0, ClauseType::Catch, terms); let state = TermIterState::Clause(0, ClauseType::Catch, terms);
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
}, },
&QueryTerm::Arg(ref terms) &QueryTerm::Arg(ref terms)
| &QueryTerm::Functor(ref terms) => { | &QueryTerm::Functor(ref terms) => {
let state = IteratorState::Clause(0, ClauseType::Root, terms); let state = TermIterState::Clause(0, ClauseType::Root, terms);
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
}, },
&QueryTerm::Inlined(InlinedQueryTerm::CompareNumber(_, ref terms)) &QueryTerm::Inlined(InlinedQueryTerm::CompareNumber(_, ref terms))
| &QueryTerm::Is(ref terms) => { | &QueryTerm::Is(ref terms) => {
let state = IteratorState::Clause(0, ClauseType::Is, terms); let state = TermIterState::Clause(0, ClauseType::Is, terms);
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
}, },
&QueryTerm::Inlined(InlinedQueryTerm::IsAtomic(ref terms)) &QueryTerm::Inlined(InlinedQueryTerm::IsAtomic(ref terms))
@@ -57,7 +57,7 @@ impl<'a> QueryIterator<'a> {
&QueryTerm::Term(ref term) => &QueryTerm::Term(ref term) =>
Self::from_term(term), Self::from_term(term),
&QueryTerm::Throw(ref term) => { &QueryTerm::Throw(ref term) => {
let state = IteratorState::Clause(0, ClauseType::Throw, term); let state = TermIterState::Clause(0, ClauseType::Throw, term);
QueryIterator { state_stack: vec![state] } QueryIterator { state_stack: vec![state] }
}, },
&QueryTerm::Cut => QueryIterator { state_stack: vec![] } &QueryTerm::Cut => QueryIterator { state_stack: vec![] }
@@ -77,9 +77,9 @@ impl<'a> Iterator for QueryIterator<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while let Some(iter_state) = self.state_stack.pop() { while let Some(iter_state) = self.state_stack.pop() {
match iter_state { match iter_state {
IteratorState::AnonVar(lvl) => TermIterState::AnonVar(lvl) =>
return Some(TermRef::AnonVar(lvl)), return Some(TermRef::AnonVar(lvl)),
IteratorState::Clause(child_num, ct, child_terms) => { TermIterState::Clause(child_num, ct, child_terms) => {
if child_num == child_terms.len() { if child_num == child_terms.len() {
match ct { match ct {
ClauseType::CallN => ClauseType::CallN =>
@@ -90,20 +90,20 @@ impl<'a> Iterator for QueryIterator<'a> {
return None return None
}; };
} else { } else {
self.state_stack.push(IteratorState::Clause(child_num + 1, ct, child_terms)); self.state_stack.push(TermIterState::Clause(child_num + 1, ct, child_terms));
self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref()); self.push_subterm(ct.level_of_subterms(), child_terms[child_num].as_ref());
} }
}, },
IteratorState::InitialCons(lvl, cell, head, tail) => { TermIterState::InitialCons(lvl, cell, head, tail) => {
self.state_stack.push(IteratorState::FinalCons(lvl, cell, head, tail)); self.state_stack.push(TermIterState::FinalCons(lvl, cell, head, tail));
self.push_subterm(Level::Deep, tail); self.push_subterm(Level::Deep, tail);
self.push_subterm(Level::Deep, head); self.push_subterm(Level::Deep, head);
}, },
IteratorState::FinalCons(lvl, cell, head, tail) => TermIterState::FinalCons(lvl, cell, head, tail) =>
return Some(TermRef::Cons(lvl, cell, head, tail)), return Some(TermRef::Cons(lvl, cell, head, tail)),
IteratorState::Constant(lvl, cell, constant) => TermIterState::Constant(lvl, cell, constant) =>
return Some(TermRef::Constant(lvl, cell, constant)), return Some(TermRef::Constant(lvl, cell, constant)),
IteratorState::Var(lvl, cell, var) => TermIterState::Var(lvl, cell, var) =>
return Some(TermRef::Var(lvl, cell, var)) return Some(TermRef::Var(lvl, cell, var))
}; };
} }
@@ -113,29 +113,29 @@ impl<'a> Iterator for QueryIterator<'a> {
} }
pub struct FactIterator<'a> { pub struct FactIterator<'a> {
state_queue: VecDeque<IteratorState<'a>>, state_queue: VecDeque<TermIterState<'a>>,
} }
impl<'a> FactIterator<'a> { impl<'a> FactIterator<'a> {
fn push_subterm(&mut self, lvl: Level, term: &'a Term) { fn push_subterm(&mut self, lvl: Level, term: &'a Term) {
self.state_queue.push_back(IteratorState::to_state(lvl, term)); self.state_queue.push_back(TermIterState::to_state(lvl, term));
} }
fn new(term: &'a Term) -> FactIterator<'a> { fn new(term: &'a Term) -> FactIterator<'a> {
let states = match term { let states = match term {
&Term::AnonVar => &Term::AnonVar =>
vec![IteratorState::AnonVar(Level::Shallow)], vec![TermIterState::AnonVar(Level::Shallow)],
&Term::Clause(_, _, ref terms) => &Term::Clause(_, _, ref terms) =>
vec![IteratorState::Clause(0, ClauseType::Root, terms)], vec![TermIterState::Clause(0, ClauseType::Root, terms)],
&Term::Cons(ref cell, ref head, ref tail) => &Term::Cons(ref cell, ref head, ref tail) =>
vec![IteratorState::InitialCons(Level::Shallow, vec![TermIterState::InitialCons(Level::Shallow,
cell, cell,
head.as_ref(), head.as_ref(),
tail.as_ref())], tail.as_ref())],
&Term::Constant(ref cell, ref constant) => &Term::Constant(ref cell, ref constant) =>
vec![IteratorState::Constant(Level::Shallow, cell, constant)], vec![TermIterState::Constant(Level::Shallow, cell, constant)],
&Term::Var(ref cell, ref var) => &Term::Var(ref cell, ref var) =>
vec![IteratorState::Var(Level::Shallow, cell, var)] vec![TermIterState::Var(Level::Shallow, cell, var)]
}; };
FactIterator { state_queue: VecDeque::from(states) } FactIterator { state_queue: VecDeque::from(states) }
@@ -148,9 +148,9 @@ impl<'a> Iterator for FactIterator<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while let Some(state) = self.state_queue.pop_front() { while let Some(state) = self.state_queue.pop_front() {
match state { match state {
IteratorState::AnonVar(lvl) => TermIterState::AnonVar(lvl) =>
return Some(TermRef::AnonVar(lvl)), return Some(TermRef::AnonVar(lvl)),
IteratorState::Clause(_, ct, child_terms) => { TermIterState::Clause(_, ct, child_terms) => {
for child_term in child_terms { for child_term in child_terms {
self.push_subterm(ct.level_of_subterms(), child_term); self.push_subterm(ct.level_of_subterms(), child_term);
} }
@@ -162,15 +162,15 @@ impl<'a> Iterator for FactIterator<'a> {
continue continue
}; };
}, },
IteratorState::InitialCons(lvl, cell, head, tail) => { TermIterState::InitialCons(lvl, cell, head, tail) => {
self.push_subterm(Level::Deep, head); self.push_subterm(Level::Deep, head);
self.push_subterm(Level::Deep, tail); self.push_subterm(Level::Deep, tail);
return Some(TermRef::Cons(lvl, cell, head, tail)); return Some(TermRef::Cons(lvl, cell, head, tail));
}, },
IteratorState::Constant(lvl, cell, constant) => TermIterState::Constant(lvl, cell, constant) =>
return Some(TermRef::Constant(lvl, cell, constant)), return Some(TermRef::Constant(lvl, cell, constant)),
IteratorState::Var(lvl, cell, var) => TermIterState::Var(lvl, cell, var) =>
return Some(TermRef::Var(lvl, cell, var)), return Some(TermRef::Var(lvl, cell, var)),
_ => {} _ => {}
} }

View File

@@ -669,7 +669,7 @@ impl MachineState {
Ref::StackCell(fr, sc) => Ref::StackCell(fr, sc) =>
self.and_stack[fr][sc] = t2, self.and_stack[fr][sc] = t2,
Ref::HeapCell(hc) => Ref::HeapCell(hc) =>
self.heap[hc] = HeapCellValue::from(t2) self.heap[hc] = HeapCellValue::Addr(t2)
}; };
self.trail(r1); self.trail(r1);
@@ -764,7 +764,7 @@ impl MachineState {
for i in a1 .. a2 { for i in a1 .. a2 {
match self.trail[i] { match self.trail[i] {
Ref::HeapCell(r) => Ref::HeapCell(r) =>
self.heap[r] = HeapCellValue::Ref(Ref::HeapCell(r)), self.heap[r] = HeapCellValue::Addr(Addr::HeapCell(r)),
Ref::StackCell(fr, sc) => Ref::StackCell(fr, sc) =>
self.and_stack[fr][sc] = Addr::StackCell(fr, sc) self.and_stack[fr][sc] = Addr::StackCell(fr, sc)
} }
@@ -818,7 +818,7 @@ impl MachineState {
match self.store(addr) { match self.store(addr) {
Addr::HeapCell(hc) => { Addr::HeapCell(hc) => {
self.heap[hc] = HeapCellValue::Con(c.clone()); self.heap[hc] = HeapCellValue::Addr(Addr::Con(c.clone()));
self.trail(Ref::HeapCell(hc)); self.trail(Ref::HeapCell(hc));
}, },
Addr::StackCell(fr, sc) => { Addr::StackCell(fr, sc) => {
@@ -1142,7 +1142,7 @@ impl MachineState {
Addr::HeapCell(hc) => { Addr::HeapCell(hc) => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Lis(h+1)); self.heap.push(HeapCellValue::Addr(Addr::Lis(h+1)));
self.bind(Ref::HeapCell(hc), Addr::HeapCell(h)); self.bind(Ref::HeapCell(hc), Addr::HeapCell(h));
self.mode = MachineMode::Write; self.mode = MachineMode::Write;
@@ -1150,7 +1150,7 @@ impl MachineState {
Addr::StackCell(fr, sc) => { Addr::StackCell(fr, sc) => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Lis(h+1)); self.heap.push(HeapCellValue::Addr(Addr::Lis(h+1)));
self.bind(Ref::StackCell(fr, sc), Addr::HeapCell(h)); self.bind(Ref::StackCell(fr, sc), Addr::HeapCell(h));
self.mode = MachineMode::Write; self.mode = MachineMode::Write;
@@ -1181,10 +1181,10 @@ impl MachineState {
Addr::HeapCell(_) | Addr::StackCell(_, _) => { Addr::HeapCell(_) | Addr::StackCell(_, _) => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Str(h + 1)); self.heap.push(HeapCellValue::Addr(Addr::Str(h + 1)));
self.heap.push(HeapCellValue::NamedStr(arity, name.clone())); self.heap.push(HeapCellValue::NamedStr(arity, name.clone()));
self.bind(addr.as_ref().unwrap(), Addr::HeapCell(h)); self.bind(addr.as_var().unwrap(), Addr::HeapCell(h));
self.mode = MachineMode::Write; self.mode = MachineMode::Write;
}, },
@@ -1206,7 +1206,7 @@ impl MachineState {
self.write_constant_to_var(addr, c.clone()); self.write_constant_to_var(addr, c.clone());
}, },
MachineMode::Write => { MachineMode::Write => {
self.heap.push(HeapCellValue::Con(c.clone())); self.heap.push(HeapCellValue::Addr(Addr::Con(c.clone())));
} }
}; };
@@ -1219,7 +1219,7 @@ impl MachineState {
MachineMode::Write => { MachineMode::Write => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self[reg] = Addr::HeapCell(h); self[reg] = Addr::HeapCell(h);
} }
}; };
@@ -1249,7 +1249,7 @@ impl MachineState {
} }
} }
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self.bind(Ref::HeapCell(h), addr); self.bind(Ref::HeapCell(h), addr);
} }
}; };
@@ -1266,7 +1266,7 @@ impl MachineState {
}, },
MachineMode::Write => { MachineMode::Write => {
let heap_val = self.store(self[reg].clone()); let heap_val = self.store(self[reg].clone());
self.heap.push(HeapCellValue::from(heap_val)); self.heap.push(HeapCellValue::Addr(heap_val));
} }
}; };
@@ -1280,7 +1280,7 @@ impl MachineState {
let h = self.heap.h; let h = self.heap.h;
for i in h .. h + n { for i in h .. h + n {
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(i))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(i)));
} }
} }
}; };
@@ -1374,7 +1374,7 @@ impl MachineState {
} else { } else {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self.bind(Ref::HeapCell(h), addr); self.bind(Ref::HeapCell(h), addr);
self.registers[arg] = self.heap[h].as_addr(h); self.registers[arg] = self.heap[h].as_addr(h);
@@ -1392,15 +1392,15 @@ impl MachineState {
}, },
RegType::Temp(_) => { RegType::Temp(_) => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self[norm] = Addr::HeapCell(h); self[norm] = Addr::HeapCell(h);
self.registers[arg] = Addr::HeapCell(h); self.registers[arg] = Addr::HeapCell(h);
} }
}; };
}, },
&QueryInstruction::SetConstant(ref constant) => { &QueryInstruction::SetConstant(ref c) => {
self.heap.push(HeapCellValue::Con(constant.clone())); self.heap.push(HeapCellValue::Addr(Addr::Con(c.clone())));
}, },
&QueryInstruction::SetLocalValue(reg) => { &QueryInstruction::SetLocalValue(reg) => {
let addr = self.deref(self[reg].clone()); let addr = self.deref(self[reg].clone());
@@ -1408,28 +1408,28 @@ impl MachineState {
if let Addr::HeapCell(hc) = addr { if let Addr::HeapCell(hc) = addr {
if hc < h { if hc < h {
self.heap.push(HeapCellValue::from(addr)); self.heap.push(HeapCellValue::Addr(addr));
return; return;
} }
} }
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self.bind(Ref::HeapCell(h), addr); self.bind(Ref::HeapCell(h), addr);
}, },
&QueryInstruction::SetVariable(reg) => { &QueryInstruction::SetVariable(reg) => {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
self[reg] = Addr::HeapCell(h); self[reg] = Addr::HeapCell(h);
}, },
&QueryInstruction::SetValue(reg) => { &QueryInstruction::SetValue(reg) => {
let heap_val = self[reg].clone(); let heap_val = self[reg].clone();
self.heap.push(HeapCellValue::from(heap_val)); self.heap.push(HeapCellValue::Addr(heap_val));
}, },
&QueryInstruction::SetVoid(n) => { &QueryInstruction::SetVoid(n) => {
let h = self.heap.h; let h = self.heap.h;
for i in h .. h + n { for i in h .. h + n {
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(i))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(i)));
} }
} }
} }
@@ -1539,7 +1539,7 @@ impl MachineState {
self.throw_exception(functor!("type_error", self.throw_exception(functor!("type_error",
2, 2,
[atom!("callable"), [atom!("callable"),
HeapCellValue::from(addr)])); HeapCellValue::Addr(addr)]));
return None; return None;
} }
}; };
@@ -1552,11 +1552,11 @@ impl MachineState {
for heap_value in self.ball.1.iter().cloned() { for heap_value in self.ball.1.iter().cloned() {
self.heap.push(match heap_value { self.heap.push(match heap_value {
HeapCellValue::Con(c) => HeapCellValue::Con(c), HeapCellValue::Addr(Addr::Con(c)) => HeapCellValue::Addr(Addr::Con(c)),
HeapCellValue::Lis(a) => HeapCellValue::Lis(a - diff), HeapCellValue::Addr(Addr::Lis(a)) => HeapCellValue::Addr(Addr::Lis(a - diff)),
HeapCellValue::Ref(Ref::HeapCell(hc)) => HeapCellValue::Addr(Addr::HeapCell(hc)) =>
HeapCellValue::Ref(Ref::HeapCell(hc - diff)), HeapCellValue::Addr(Addr::HeapCell(hc - diff)),
HeapCellValue::Str(s) => HeapCellValue::Str(s - diff), HeapCellValue::Addr(Addr::Str(s)) => HeapCellValue::Addr(Addr::Str(s - diff)),
_ => heap_value _ => heap_value
}); });
} }
@@ -1658,7 +1658,7 @@ impl MachineState {
let ball = self.heap[h].as_addr(h); let ball = self.heap[h].as_addr(h);
match addr.as_ref() { match addr.as_var() {
Some(r) => { Some(r) => {
self.bind(r, ball); self.bind(r, ball);
self.p += 1; self.p += 1;
@@ -1822,7 +1822,7 @@ impl MachineState {
for _ in 0 .. arity { for _ in 0 .. arity {
let h = self.heap.h; let h = self.heap.h;
self.heap.push(HeapCellValue::Ref(Ref::HeapCell(h))); self.heap.push(HeapCellValue::Addr(Addr::HeapCell(h)));
} }
self.unify(a1, f_a); self.unify(a1, f_a);

View File

@@ -53,7 +53,7 @@ macro_rules! functor {
macro_rules! atom { macro_rules! atom {
($name:expr) => ( ($name:expr) => (
HeapCellValue::Con(Constant::Atom(Rc::new(String::from($name)))) HeapCellValue::Addr(Addr::Con(Constant::Atom(Rc::new(String::from($name)))))
) )
} }