flatten CompiledQuery and CompiledFact in Code, remove CompiledQuery

This commit is contained in:
Mark Thom
2019-02-06 22:25:41 -07:00
parent 9f8dd56c3c
commit 046569b6a9
11 changed files with 179 additions and 126 deletions

View File

@@ -206,6 +206,7 @@ pub struct MachineState {
pub(super) e: usize,
pub(super) num_of_args: usize,
pub(super) cp: LocalCodePtr,
pub(super) special_form_cp: CodePtr,
pub(super) fail: bool,
pub(crate) heap: Heap,
pub(super) mode: MachineMode,

View File

@@ -40,6 +40,7 @@ impl MachineState {
e: 0,
num_of_args: 0,
cp: LocalCodePtr::default(),
special_form_cp: CodePtr::default(),
fail: false,
heap: Heap::with_capacity(256),
mode: MachineMode::Write,
@@ -2363,6 +2364,7 @@ impl MachineState {
self.pstr_tr = 0;
self.p = CodePtr::default();
self.cp = LocalCodePtr::default();
self.special_form_cp = CodePtr::default();
self.num_of_args = 0;
self.fail = false;

View File

@@ -7,6 +7,7 @@ use prolog::debray_allocator::*;
use prolog::heap_print::*;
use prolog::instructions::*;
mod attributed_variables;
mod machine_errors;
pub(super) mod machine_state;
pub(super) mod term_expansion;
@@ -14,6 +15,7 @@ pub(super) mod term_expansion;
#[macro_use] mod machine_state_impl;
mod system_calls;
use prolog::machine::attributed_variables::*;
use prolog::machine::machine_state::*;
use std::collections::{HashMap, VecDeque};
@@ -21,8 +23,6 @@ use std::mem;
use std::ops::Index;
use std::rc::Rc;
static BUILTINS: &str = include_str!("../lib/builtins.pl");
pub type InSituCodeDir = HashMap<PredicateKey, usize>;
pub struct IndexStore {
@@ -118,6 +118,7 @@ impl CodeRepo {
term_expanders: Code::new(),
code: Code::new(),
in_situ_code: Code::new(),
verify_attrs_code: Code::new(),
term_dir: TermDir::new()
}
}
@@ -308,15 +309,23 @@ impl SubModuleUser for IndexStore {
}
}
static LISTS: &str = include_str!("../lib/lists.pl");
static CONTROL: &str = include_str!("../lib/control.pl");
static QUEUES: &str = include_str!("../lib/queues.pl");
static ERROR: &str = include_str!("../lib/error.pl");
static TERMS: &str = include_str!("../lib/terms.pl");
static DCGS: &str = include_str!("../lib/dcgs.pl");
static ATTS: &str = include_str!("../lib/atts.pl");
static BUILTINS: &str = include_str!("../lib/builtins.pl");
static LISTS: &str = include_str!("../lib/lists.pl");
static CONTROL: &str = include_str!("../lib/control.pl");
static QUEUES: &str = include_str!("../lib/queues.pl");
static ERROR: &str = include_str!("../lib/error.pl");
static TERMS: &str = include_str!("../lib/terms.pl");
static DCGS: &str = include_str!("../lib/dcgs.pl");
static ATTS: &str = include_str!("../lib/atts.pl");
impl Machine {
fn compile_special_forms(&mut self) {
match compile_special_form(self, VERIFY_ATTRS.as_bytes()) {
Ok(code) => self.code_repo.verify_attrs_code = code,
Err(_e) => panic!("Machine::compile_special_forms() failed")
}
}
pub fn new() -> Self {
let mut wam = Machine {
machine_st: MachineState::new(),
@@ -338,6 +347,7 @@ impl Machine {
compile_user_module(&mut wam, DCGS.as_bytes());
compile_user_module(&mut wam, ATTS.as_bytes());
wam.compile_special_forms();
wam
}
@@ -512,30 +522,16 @@ impl MachineState {
&Line::Control(ref control_instr) =>
self.execute_ctrl_instr(indices, &mut policies.call_policy,
&mut policies.cut_policy, control_instr),
&Line::Fact(ref fact) => {
for fact_instr in fact {
if self.fail {
break;
}
self.execute_fact_instr(&fact_instr);
}
&Line::Fact(ref fact_instr) => {
self.execute_fact_instr(&fact_instr);
self.p += 1;
},
&Line::Indexing(ref indexing_instr) =>
self.execute_indexing_instr(&indexing_instr),
&Line::IndexedChoice(ref choice_instr) =>
self.execute_indexed_choice_instr(choice_instr, &mut policies.call_policy),
&Line::Query(ref query) => {
for query_instr in query {
if self.fail {
break;
}
self.execute_query_instr(&query_instr);
}
&Line::Query(ref query_instr) => {
self.execute_query_instr(&query_instr);
self.p += 1;
}
}

View File

@@ -375,6 +375,29 @@ impl MachineState {
}
};
},
&SystemClauseType::ModuleOf => {
let module = self.store(self.deref(self[temp_v!(2)].clone()));
match module {
Addr::Con(Constant::Atom(name, _)) => {
let module = Addr::Con(Constant::Atom(name.owning_module(), None));
let target = self[temp_v!(1)].clone();
self.unify(target, module);
},
Addr::Str(s) =>
match self.heap[s].clone() {
HeapCellValue::NamedStr(_, name, ..) => {
let module = Addr::Con(Constant::Atom(name.owning_module(), None));
let target = self[temp_v!(1)].clone();
self.unify(target, module);
},
_ => self.fail = true
},
_ => self.fail = true
};
},
&SystemClauseType::RemoveCallPolicyCheck => {
let restore_default =
match call_policy.downcast_mut::<CWILCallPolicy>().ok() {
@@ -418,6 +441,10 @@ impl MachineState {
None => panic!("remove_inference_counter: requires \\
CWILCallPolicy.")
},
&SystemClauseType::RestoreCodePtrFromSpecialFormCP => {
self.p = self.special_form_cp.clone();
return Ok(());
},
&SystemClauseType::RestoreCutPolicy => {
let restore_default =
if let Ok(cut_policy) = cut_policy.downcast_ref::<SCCCutPolicy>() {