remove Terminal, correct , bugs

This commit is contained in:
Mark Thom
2017-12-25 00:04:52 -07:00
parent 2dc25400ce
commit 2d4e4c59de
6 changed files with 140 additions and 179 deletions

View File

@@ -422,15 +422,10 @@ pub enum ChoiceInstruction {
TryMeElse(usize) TryMeElse(usize)
} }
#[derive(Clone, Copy)]
pub enum Terminal {
Terminal, Non
}
pub enum CutInstruction { pub enum CutInstruction {
Cut(Terminal), Cut,
GetLevel, GetLevel,
NeckCut(Terminal) NeckCut
} }
pub enum IndexedChoiceInstruction { pub enum IndexedChoiceInstruction {
@@ -722,14 +717,15 @@ pub enum BuiltInInstruction {
Fail, Fail,
GetBall, GetBall,
GetCurrentBlock, GetCurrentBlock,
GetCutPoint, GetCutPoint(RegType),
InstallNewBlock, InstallNewBlock,
InternalCallN, InternalCallN,
IsAtomic(RegType), IsAtomic(RegType),
IsVar(RegType), IsVar(RegType),
ResetBlock, ResetBlock,
SetBall, SetBall,
SetCutPoint, SetNeckCutPoint(RegType),
SetNonNeckCutPoint(RegType),
Succeed, Succeed,
Unify, Unify,
UnwindStack UnwindStack

View File

@@ -65,7 +65,7 @@ fn get_builtins() -> Code {
get_level!(), get_level!(),
fact![get_var_in_fact!(perm_v!(2), 3)], fact![get_var_in_fact!(perm_v!(2), 3)],
unify!(), unify!(),
cut!(non_terminal!()), cut!(),
erase_ball!(), erase_ball!(),
query![put_value!(perm_v!(2), 1)], query![put_value!(perm_v!(2), 1)],
deallocate!(), deallocate!(),
@@ -95,7 +95,7 @@ fn get_builtins() -> Code {
allocate!(1), allocate!(1),
get_level!(), get_level!(),
call_n!(1), call_n!(1),
cut!(non_terminal!()), cut!(),
deallocate!(), deallocate!(),
goto!(61, 0), goto!(61, 0),
trust_me!(), trust_me!(),
@@ -110,71 +110,50 @@ fn get_builtins() -> Code {
trust_me!(), trust_me!(),
query![put_value!(temp_v!(2), 1)], query![put_value!(temp_v!(2), 1)],
execute_n!(1), execute_n!(1),
allocate!(3), // ','/2, 81. get_cp!(temp_v!(3)), // ','/2, 81
fact![get_var_in_fact!(perm_v!(2), 1), goto!(83, 3),
get_var_in_fact!(perm_v!(1), 2)], try_me_else!(18), // ','/3, 83.
query![put_var!(perm_v!(3), 1)],
get_cp!(),
query![put_value!(perm_v!(2), 1),
put_value!(perm_v!(1), 2),
put_unsafe_value!(3, 3)],
deallocate!(),
goto!(88, 3),
try_me_else!(25), // ','/3, 88.
switch_on_term!(4, 1, 0, 0), switch_on_term!(4, 1, 0, 0),
indexed_try!(4), indexed_try!(4),
retry!(11), retry!(7),
trust!(14), trust!(10),
try_me_else!(8), try_me_else!(4),
allocate!(3),
fact![get_constant!(Constant::from("!"), temp_v!(1)), fact![get_constant!(Constant::from("!"), temp_v!(1)),
get_structure!(String::from(","), 2, temp_v!(2)), get_structure!(String::from(","), 2, temp_v!(2)),
unify_variable!(perm_v!(2)), unify_variable!(temp_v!(1)),
unify_variable!(perm_v!(1)), unify_variable!(temp_v!(2))],
get_var_in_fact!(perm_v!(3), 3)], set_neck_cut!(temp_v!(3)),
query![put_value!(perm_v!(3), 1)], goto!(83, 3),
set_cp!(),
query![put_unsafe_value!(2, 1),
put_unsafe_value!(1, 2),
put_value!(perm_v!(3), 3)],
deallocate!(),
goto!(88, 3),
retry_me_else!(4), retry_me_else!(4),
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)], set_neck_cut!(temp_v!(3)),
set_cp!(), proceed!(),
trust_me!(), trust_me!(),
allocate!(1), fact![get_constant!(Constant::from("!"), temp_v!(1))],
fact![get_constant!(Constant::from("!"), temp_v!(1)), set_neck_cut!(temp_v!(3)),
get_var_in_fact!(perm_v!(1), 2)], query![put_value!(temp_v!(2), 1)],
query![put_value!(temp_v!(3), 1)],
set_cp!(),
query![put_value!(perm_v!(1), 1)],
deallocate!(),
execute_n!(1), execute_n!(1),
retry_me_else!(8), retry_me_else!(8),
allocate!(1), allocate!(2),
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_cp!(),
retry_me_else!(8), // 121.
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)], neck_cut!(),
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)],
put_value!(perm_v!(3), 3)],
deallocate!(), deallocate!(),
goto!(88, 3), goto!(83, 3),
retry_me_else!(9),
allocate!(1),
get_level!(),
fact![get_constant!(Constant::from("!"), temp_v!(2))],
neck_cut!(),
call_n!(1),
set_non_neck_cut!(temp_v!(3)),
deallocate!(),
proceed!(),
trust_me!(), trust_me!(),
allocate!(1), allocate!(1),
fact![get_var_in_fact!(perm_v!(1), 2)], fact![get_var_in_fact!(perm_v!(1), 2)],
@@ -182,11 +161,11 @@ 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, 136. allocate!(2), // (->)/2, 124.
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),
cut!(non_terminal!()), cut!(),
query![put_value!(perm_v!(2), 1)], query![put_value!(perm_v!(2), 1)],
deallocate!(), deallocate!(),
execute_n!(1) execute_n!(1)
@@ -256,7 +235,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, 136)); code_dir.insert((String::from("->"), 2), (PredicateKeyType::BuiltIn, 124));
(builtin_code, code_dir, op_dir) (builtin_code, code_dir, op_dir)
} }

View File

@@ -233,11 +233,6 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
ConjunctInfo::new(vs, num_of_chunks, has_deep_cut) ConjunctInfo::new(vs, num_of_chunks, has_deep_cut)
} }
fn add_conditional_call_inlined(_: &InlinedQueryTerm, code: &mut Code)
{
code.push(proceed!());
}
fn add_conditional_call(code: &mut Code, qt: &QueryTerm, pvs: usize) fn add_conditional_call(code: &mut Code, qt: &QueryTerm, pvs: usize)
{ {
match qt { match qt {
@@ -247,8 +242,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
}, },
&QueryTerm::Catch(_) => &QueryTerm::Catch(_) =>
code.push(Line::Control(ControlInstruction::CatchCall)), code.push(Line::Control(ControlInstruction::CatchCall)),
&QueryTerm::Inlined(ref term) => &QueryTerm::Inlined(_) =>
Self::add_conditional_call_inlined(term, code), code.push(proceed!()),
&QueryTerm::Term(Term::Constant(_, Constant::Atom(ref atom))) => { &QueryTerm::Term(Term::Constant(_, Constant::Atom(ref atom))) => {
let call = ControlInstruction::Call(atom.clone(), 0, pvs); let call = ControlInstruction::Call(atom.clone(), 0, pvs);
code.push(Line::Control(call)); code.push(Line::Control(call));
@@ -267,25 +262,30 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
{ {
let mut dealloc_index = code.len() - 1; let mut dealloc_index = code.len() - 1;
if let Some(&mut Line::Control(ref mut ctrl)) = code.last_mut() { match code.last_mut() {
let mut instr = ControlInstruction::Proceed; Some(&mut Line::Control(ref mut ctrl)) => {
swap(ctrl, &mut instr); let mut instr = ControlInstruction::Proceed;
swap(ctrl, &mut instr);
match instr { match instr {
ControlInstruction::Call(name, arity, _) => ControlInstruction::Call(name, arity, _) =>
*ctrl = ControlInstruction::Execute(name, arity), *ctrl = ControlInstruction::Execute(name, arity),
ControlInstruction::CallN(arity) => ControlInstruction::CallN(arity) =>
*ctrl = ControlInstruction::ExecuteN(arity), *ctrl = ControlInstruction::ExecuteN(arity),
ControlInstruction::CatchCall => ControlInstruction::CatchCall =>
*ctrl = ControlInstruction::CatchExecute, *ctrl = ControlInstruction::CatchExecute,
ControlInstruction::ThrowCall => ControlInstruction::ThrowCall =>
*ctrl = ControlInstruction::ThrowExecute, *ctrl = ControlInstruction::ThrowExecute,
ControlInstruction::IsCall(r, at) => ControlInstruction::IsCall(r, at) =>
*ctrl = ControlInstruction::IsExecute(r, at), *ctrl = ControlInstruction::IsExecute(r, at),
ControlInstruction::Proceed => {}, ControlInstruction::Proceed => {},
_ => dealloc_index += 1 // = code.len() _ => dealloc_index += 1 // = code.len()
} }
} },
Some(&mut Line::Cut(CutInstruction::Cut)) =>
dealloc_index += 1,
_ => {}
};
dealloc_index dealloc_index
} }
@@ -361,16 +361,10 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
match *term { match *term {
&QueryTerm::Cut => { &QueryTerm::Cut => {
let is_terminal = if i + 1 < terms.len() {
Terminal::Non
} else {
Terminal::Terminal
};
code.push(if chunk_num == 0 { code.push(if chunk_num == 0 {
Line::Cut(CutInstruction::NeckCut(is_terminal)) Line::Cut(CutInstruction::NeckCut)
} else { } else {
Line::Cut(CutInstruction::Cut(is_terminal)) Line::Cut(CutInstruction::Cut)
}); });
}, },
&QueryTerm::Is(ref terms) => { &QueryTerm::Is(ref terms) => {
@@ -455,8 +449,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker>
fn compile_cleanup(code: &mut Code, conjunct_info: &ConjunctInfo, toc: &'a QueryTerm) fn compile_cleanup(code: &mut Code, conjunct_info: &ConjunctInfo, toc: &'a QueryTerm)
{ {
match toc { match toc {
&QueryTerm::Inlined(ref term) => &QueryTerm::Inlined(_) | &QueryTerm::Cut =>
Self::add_conditional_call_inlined(term, code), code.push(proceed!()),
_ => {} _ => {}
}; };

View File

@@ -141,17 +141,6 @@ impl fmt::Display for IndexedChoiceInstruction {
} }
} }
impl fmt::Display for Terminal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Terminal::Terminal =>
write!(f, "terminal"),
&Terminal::Non =>
write!(f, "non_terminal")
}
}
}
impl fmt::Display for BuiltInInstruction { impl fmt::Display for BuiltInInstruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
@@ -169,8 +158,8 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "get_ball X1"), write!(f, "get_ball X1"),
&BuiltInInstruction::GetCurrentBlock => &BuiltInInstruction::GetCurrentBlock =>
write!(f, "get_current_block X1"), write!(f, "get_current_block X1"),
&BuiltInInstruction::GetCutPoint => &BuiltInInstruction::GetCutPoint(r) =>
write!(f, "get_cp"), write!(f, "get_cp {}", r),
&BuiltInInstruction::InstallNewBlock => &BuiltInInstruction::InstallNewBlock =>
write!(f, "install_new_block"), write!(f, "install_new_block"),
&BuiltInInstruction::InternalCallN => &BuiltInInstruction::InternalCallN =>
@@ -183,8 +172,10 @@ 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::SetCutPoint => &BuiltInInstruction::SetNeckCutPoint(r) =>
write!(f, "set_cp"), write!(f, "set_neck_cp {}", r),
&BuiltInInstruction::SetNonNeckCutPoint(r) =>
write!(f, "set_non_neck_cp {}", r),
&BuiltInInstruction::Succeed => &BuiltInInstruction::Succeed =>
write!(f, "true"), write!(f, "true"),
&BuiltInInstruction::UnwindStack => &BuiltInInstruction::UnwindStack =>
@@ -272,9 +263,9 @@ impl fmt::Display for ArithmeticInstruction {
impl fmt::Display for CutInstruction { impl fmt::Display for CutInstruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
&CutInstruction::Cut(_) => &CutInstruction::Cut =>
write!(f, "cut"), write!(f, "cut"),
&CutInstruction::NeckCut(_) => &CutInstruction::NeckCut =>
write!(f, "neck_cut"), write!(f, "neck_cut"),
&CutInstruction::GetLevel => &CutInstruction::GetLevel =>
write!(f, "get_level") write!(f, "get_level")
@@ -382,6 +373,7 @@ pub fn eval<'a, 'b: 'a>(wam: &'a mut Machine, tl: &'b TopLevel) -> EvalSession<'
Err(e) => return EvalSession::ParserError(e) Err(e) => return EvalSession::ParserError(e)
}; };
print_code(&compiled_pred);
wam.add_predicate(clauses, compiled_pred) wam.add_predicate(clauses, compiled_pred)
}, },
&TopLevel::Fact(ref fact) => { &TopLevel::Fact(ref fact) => {

View File

@@ -1622,13 +1622,6 @@ impl MachineState {
self.write_constant_to_var(addr, &c); self.write_constant_to_var(addr, &c);
self.p += 1; self.p += 1;
}, },
&BuiltInInstruction::GetCutPoint => {
let c = Constant::Usize(self.b);
let addr = self[temp_v!(1)].clone();
self.write_constant_to_var(addr, &c);
self.p += 1;
},
&BuiltInInstruction::EraseBall => { &BuiltInInstruction::EraseBall => {
self.ball.0 = 0; self.ball.0 = 0;
self.ball.1.truncate(0); self.ball.1.truncate(0);
@@ -1655,6 +1648,12 @@ impl MachineState {
_ => self.fail = true _ => self.fail = true
}; };
}, },
&BuiltInInstruction::GetCutPoint(r) => {
let c = Constant::Usize(self.b);
self[r] = Addr::Con(c);
self.p += 1;
},
&BuiltInInstruction::SetBall => { &BuiltInInstruction::SetBall => {
let addr = self[temp_v!(1)].clone(); let addr = self[temp_v!(1)].clone();
self.ball.0 = self.heap.h; self.ball.0 = self.heap.h;
@@ -1666,17 +1665,24 @@ impl MachineState {
self.p += 1; self.p += 1;
}, },
&BuiltInInstruction::SetCutPoint => { &BuiltInInstruction::SetNeckCutPoint(r) => {
let nb = self.store(self.deref(self[temp_v!(1)].clone())); let addr = self.store(self.deref(self[r].clone()));
match nb { match addr {
Addr::Con(Constant::Usize(nb)) => { Addr::Con(Constant::Usize(nb)) => {
self.or_stack.truncate(nb);
self.b = nb; self.b = nb;
self.neck_cut();
},
_ => self.fail = true
};
},
&BuiltInInstruction::SetNonNeckCutPoint(r) => {
let addr = self.store(self.deref(self[r].clone()));
self.tidy_trail(); match addr {
Addr::Con(Constant::Usize(nb)) => {
self.p += 1; self.b = nb;
self.non_neck_cut();
}, },
_ => self.fail = true _ => self.fail = true
}; };
@@ -2019,7 +2025,7 @@ impl MachineState {
} }
} }
fn neck_cut(&mut self, term: Terminal) fn neck_cut(&mut self)
{ {
let b = self.b; let b = self.b;
let b0 = self.b0; let b0 = self.b0;
@@ -2029,14 +2035,10 @@ impl MachineState {
self.tidy_trail(); self.tidy_trail();
} }
if let Terminal::Terminal = term { self.p += 1;
self.p = self.cp;
} else {
self.p += 1;
}
} }
fn non_neck_cut(&mut self, term: Terminal) fn non_neck_cut(&mut self)
{ {
let b = self.b; let b = self.b;
let e = self.e; let e = self.e;
@@ -2047,17 +2049,13 @@ impl MachineState {
self.tidy_trail(); self.tidy_trail();
} }
if let Terminal::Terminal = term { self.p += 1;
self.p = self.cp;
} else {
self.p += 1;
}
} }
fn execute_cut_instr(&mut self, instr: &CutInstruction) { fn execute_cut_instr(&mut self, instr: &CutInstruction) {
match instr { match instr {
&CutInstruction::Cut(term) => &CutInstruction::Cut =>
self.non_neck_cut(term), self.non_neck_cut(),
&CutInstruction::GetLevel => { &CutInstruction::GetLevel => {
let b0 = self.b0; let b0 = self.b0;
let e = self.e; let e = self.e;
@@ -2065,8 +2063,8 @@ impl MachineState {
self.and_stack[e].b0 = b0; self.and_stack[e].b0 = b0;
self.p += 1; self.p += 1;
}, },
&CutInstruction::NeckCut(term) => &CutInstruction::NeckCut =>
self.neck_cut(term) self.neck_cut()
} }
} }

View File

@@ -165,21 +165,15 @@ macro_rules! proceed {
) )
} }
macro_rules! non_terminal {
() => (
Terminal::Non
)
}
macro_rules! cut { macro_rules! cut {
($term:expr) => ( () => (
Line::Cut(CutInstruction::Cut($term)) Line::Cut(CutInstruction::Cut)
) )
} }
macro_rules! neck_cut { macro_rules! neck_cut {
($term:expr) => ( () => (
Line::Cut(CutInstruction::NeckCut($term)) Line::Cut(CutInstruction::NeckCut)
) )
} }
@@ -297,18 +291,6 @@ macro_rules! trust {
) )
} }
macro_rules! get_cp {
() => (
Line::BuiltIn(BuiltInInstruction::GetCutPoint)
)
}
macro_rules! set_cp {
() => (
Line::BuiltIn(BuiltInInstruction::SetCutPoint)
)
}
macro_rules! get_constant { macro_rules! get_constant {
($c:expr, $r:expr) => ( ($c:expr, $r:expr) => (
FactInstruction::GetConstant(Level::Shallow, $c, $r) FactInstruction::GetConstant(Level::Shallow, $c, $r)
@@ -326,3 +308,23 @@ macro_rules! unify_variable {
FactInstruction::UnifyVariable($r) FactInstruction::UnifyVariable($r)
) )
} }
macro_rules! set_neck_cut {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::SetNeckCutPoint($r))
)
}
macro_rules! set_non_neck_cut {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::SetNonNeckCutPoint($r))
)
}
macro_rules! get_cp {
($r:expr) => (
Line::BuiltIn(BuiltInInstruction::GetCutPoint($r))
)
}