incorporate term_expansion phase in compilation

This commit is contained in:
Mark Thom
2018-09-18 22:44:03 -06:00
parent 78ef18ad75
commit 908972eff1
10 changed files with 211 additions and 89 deletions

View File

@@ -7,6 +7,7 @@ use prolog::heap_print::*;
mod machine_errors;
pub(super) mod machine_state;
pub(super) mod term_expansion;
#[macro_use] mod machine_state_impl;
mod system_calls;
@@ -53,7 +54,8 @@ pub struct Machine {
code: Code,
pub(super) code_dir: Rc<RefCell<CodeDir>>,
pub(super) op_dir: OpDir,
// term_dir: TermDir,
term_dir: TermDir,
term_expanders: Code,
pub(super) modules: ModuleDir,
cached_query: Option<Code>
}
@@ -80,7 +82,8 @@ impl Index<LocalCodePtr> for Machine {
&None => panic!("Out-of-bounds top level index.")
}
},
LocalCodePtr::DirEntry(p, _) => &self.code[p]
LocalCodePtr::DirEntry(p, _) => &self.code[p],
LocalCodePtr::UserTermExpansion(p) => &self.term_expanders[p]
}
}
}
@@ -115,6 +118,7 @@ impl<'a> SubModuleUser for MachineCodeIndices<'a> {
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 NUMVARS: &str = include_str!("../lib/numbervars.pl");
impl Machine {
pub fn new() -> Self {
@@ -125,7 +129,8 @@ impl Machine {
code: Code::new(),
code_dir: Rc::new(RefCell::new(CodeDir::new())),
op_dir: default_op_dir(),
// term_dir: TermDir::new(),
term_dir: TermDir::new(),
term_expanders: Code::new(),
modules: HashMap::new(),
cached_query: None
};
@@ -137,6 +142,7 @@ impl Machine {
compile_user_module(&mut wam, LISTS.as_bytes());
compile_user_module(&mut wam, CONTROL.as_bytes());
compile_user_module(&mut wam, QUEUES.as_bytes());
compile_user_module(&mut wam, NUMVARS.as_bytes());
wam
}
@@ -245,6 +251,12 @@ impl Machine {
fn lookup_instr(&self, p: CodePtr) -> Option<Line> {
match p {
CodePtr::Local(LocalCodePtr::UserTermExpansion(p)) =>
if p < self.term_expanders.len() {
Some(self.term_expanders[p].clone())
} else {
None
},
CodePtr::Local(LocalCodePtr::TopLevel(_, p)) =>
match &self.cached_query {
&Some(ref cq) => Some(cq[p].clone()),
@@ -340,6 +352,8 @@ impl Machine {
match self.ms.p {
CodePtr::Local(LocalCodePtr::DirEntry(p, _)) if p < self.code.len() => {},
CodePtr::Local(LocalCodePtr::UserTermExpansion(p)) if p < self.term_expanders.len() => {},
CodePtr::Local(LocalCodePtr::UserTermExpansion(_)) => self.ms.fail = true,
CodePtr::Local(_) => break,
_ => {}
};