move atom_tbl up to Machine
This commit is contained in:
@@ -99,7 +99,8 @@ fn compile_query(terms: Vec<QueryTerm>, queue: Vec<TopLevel>, flags: MachineFlag
|
|||||||
|
|
||||||
fn package_term(wam: &mut Machine, term: Term) -> Result<TopLevelPacket, ParserError> {
|
fn package_term(wam: &mut Machine, term: Term) -> Result<TopLevelPacket, ParserError> {
|
||||||
let code_dir = wam.code_dir.clone();
|
let code_dir = wam.code_dir.clone();
|
||||||
let indices = machine_code_indices!(&mut CodeDir::new(),
|
let indices = machine_code_indices!(wam.atom_tbl.clone(),
|
||||||
|
&mut CodeDir::new(),
|
||||||
&mut wam.op_dir,
|
&mut wam.op_dir,
|
||||||
&mut wam.modules);
|
&mut wam.modules);
|
||||||
|
|
||||||
@@ -118,7 +119,7 @@ pub fn compile_term(wam: &mut Machine, term: Term) -> EvalSession
|
|||||||
},
|
},
|
||||||
TopLevelPacket::Decl(TopLevel::Declaration(decl), _) => {
|
TopLevelPacket::Decl(TopLevel::Declaration(decl), _) => {
|
||||||
let mut compiler = ListingCompiler::new();
|
let mut compiler = ListingCompiler::new();
|
||||||
let mut indices = default_machine_code_indices!();
|
let mut indices = default_machine_code_indices!(wam.atom_tbl.clone());
|
||||||
|
|
||||||
try_eval_session!(compiler.process_decl(decl, wam, &mut indices));
|
try_eval_session!(compiler.process_decl(decl, wam, &mut indices));
|
||||||
try_eval_session!(compiler.add_code(wam, vec![], indices));
|
try_eval_session!(compiler.add_code(wam, vec![], indices));
|
||||||
@@ -275,7 +276,8 @@ fn compile_listing<'a, R: Read>(wam: &mut Machine, src: R, mut indices: MachineC
|
|||||||
mut toplevel_indices: MachineCodeIndices<'a>)
|
mut toplevel_indices: MachineCodeIndices<'a>)
|
||||||
-> EvalSession
|
-> EvalSession
|
||||||
{
|
{
|
||||||
let mut worker = TopLevelBatchWorker::new(src, wam.atom_tbl(), wam.machine_flags(),
|
let mut worker = TopLevelBatchWorker::new(src, wam.atom_tbl.clone(),
|
||||||
|
wam.machine_flags(),
|
||||||
wam.code_dir.clone());
|
wam.code_dir.clone());
|
||||||
let mut compiler = ListingCompiler::new();
|
let mut compiler = ListingCompiler::new();
|
||||||
let mut toplevel_results = vec![];
|
let mut toplevel_results = vec![];
|
||||||
@@ -316,7 +318,10 @@ fn setup_indices(wam: &Machine, indices: &mut MachineCodeIndices) -> Result<(),
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_user_module<R: Read>(wam: &mut Machine, src: R) -> EvalSession {
|
pub fn compile_user_module<R: Read>(wam: &mut Machine, src: R) -> EvalSession {
|
||||||
let mut indices = default_machine_code_indices!();
|
let atom_tbl = wam.atom_tbl.clone();
|
||||||
|
let mut indices = default_machine_code_indices!(atom_tbl.clone());
|
||||||
|
|
||||||
try_eval_session!(setup_indices(&wam, &mut indices));
|
try_eval_session!(setup_indices(&wam, &mut indices));
|
||||||
compile_listing(wam, src, indices, default_machine_code_indices!())
|
|
||||||
|
compile_listing(wam, src, indices, default_machine_code_indices!(atom_tbl))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1057,6 +1057,7 @@ pub fn as_module_code_dir(code_dir: CodeDir) -> ModuleCodeDir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait SubModuleUser {
|
pub trait SubModuleUser {
|
||||||
|
fn atom_tbl(&self) -> TabledData<Atom>;
|
||||||
fn op_dir(&mut self) -> &mut OpDir;
|
fn op_dir(&mut self) -> &mut OpDir;
|
||||||
fn remove_code_index(&mut self, PredicateKey);
|
fn remove_code_index(&mut self, PredicateKey);
|
||||||
fn get_code_index(&self, PredicateKey, ClauseName) -> Option<CodeIndex>;
|
fn get_code_index(&self, PredicateKey, ClauseName) -> Option<CodeIndex>;
|
||||||
@@ -1127,6 +1128,10 @@ pub trait SubModuleUser {
|
|||||||
|
|
||||||
if let Some(code_data) = submodule.code_dir.get(&(name.clone(), arity)) {
|
if let Some(code_data) = submodule.code_dir.get(&(name.clone(), arity)) {
|
||||||
let name = name.with_table(submodule.atom_tbl.clone());
|
let name = name.with_table(submodule.atom_tbl.clone());
|
||||||
|
|
||||||
|
let mut atom_tbl = self.atom_tbl();
|
||||||
|
atom_tbl.borrow_mut().insert(name.to_rc());
|
||||||
|
|
||||||
self.insert_dir_entry(name, arity, code_data.clone());
|
self.insert_dir_entry(name, arity, code_data.clone());
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@@ -1162,6 +1167,10 @@ pub trait SubModuleUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SubModuleUser for Module {
|
impl SubModuleUser for Module {
|
||||||
|
fn atom_tbl(&self) -> TabledData<Atom> {
|
||||||
|
self.atom_tbl.clone()
|
||||||
|
}
|
||||||
|
|
||||||
fn op_dir(&mut self) -> &mut OpDir {
|
fn op_dir(&mut self) -> &mut OpDir {
|
||||||
&mut self.op_dir
|
&mut self.op_dir
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use prolog_parser::ast::*;
|
use prolog_parser::ast::*;
|
||||||
use prolog_parser::string_list::*;
|
use prolog_parser::string_list::*;
|
||||||
use prolog_parser::tabled_rc::*;
|
|
||||||
|
|
||||||
use prolog::instructions::*;
|
use prolog::instructions::*;
|
||||||
use prolog::and_stack::*;
|
use prolog::and_stack::*;
|
||||||
@@ -234,7 +233,6 @@ pub(super) enum MachineMode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct MachineState {
|
pub struct MachineState {
|
||||||
pub(crate) atom_tbl: TabledData<Atom>,
|
|
||||||
pub(super) s: usize,
|
pub(super) s: usize,
|
||||||
pub(super) p: CodePtr,
|
pub(super) p: CodePtr,
|
||||||
pub(super) b: usize,
|
pub(super) b: usize,
|
||||||
@@ -559,7 +557,7 @@ pub(crate) trait CallPolicy: Any {
|
|||||||
return_from_clause!(machine_st.last_call, machine_st)
|
return_from_clause!(machine_st.last_call, machine_st)
|
||||||
},
|
},
|
||||||
&BuiltInClauseType::Read => {
|
&BuiltInClauseType::Read => {
|
||||||
match machine_st.read(stdin(), &indices.op_dir) {
|
match machine_st.read(stdin(), indices.atom_tbl.clone(), &indices.op_dir) {
|
||||||
Ok(offset) => {
|
Ok(offset) => {
|
||||||
let addr = machine_st[temp_v!(1)].clone();
|
let addr = machine_st[temp_v!(1)].clone();
|
||||||
machine_st.unify(addr, Addr::HeapCell(offset));
|
machine_st.unify(addr, Addr::HeapCell(offset));
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use prolog_parser::ast::*;
|
use prolog_parser::ast::*;
|
||||||
use prolog_parser::string_list::StringList;
|
use prolog_parser::string_list::StringList;
|
||||||
use prolog_parser::tabled_rc::TabledData;
|
|
||||||
|
|
||||||
use prolog::instructions::*;
|
use prolog::instructions::*;
|
||||||
use prolog::and_stack::*;
|
use prolog::and_stack::*;
|
||||||
@@ -33,8 +32,7 @@ macro_rules! try_or_fail {
|
|||||||
|
|
||||||
impl MachineState {
|
impl MachineState {
|
||||||
pub(super) fn new() -> Self {
|
pub(super) fn new() -> Self {
|
||||||
MachineState {
|
MachineState {
|
||||||
atom_tbl: TabledData::new(Rc::new("user".to_string())),
|
|
||||||
s: 0,
|
s: 0,
|
||||||
p: CodePtr::default(),
|
p: CodePtr::default(),
|
||||||
b: 0,
|
b: 0,
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use std::rc::Rc;
|
|||||||
static BUILTINS: &str = include_str!("../lib/builtins.pl");
|
static BUILTINS: &str = include_str!("../lib/builtins.pl");
|
||||||
|
|
||||||
pub struct MachineCodeIndices<'a> {
|
pub struct MachineCodeIndices<'a> {
|
||||||
|
pub(super) atom_tbl: TabledData<Atom>,
|
||||||
pub(super) code_dir: &'a mut CodeDir,
|
pub(super) code_dir: &'a mut CodeDir,
|
||||||
pub(super) op_dir: &'a mut OpDir,
|
pub(super) op_dir: &'a mut OpDir,
|
||||||
pub(super) modules: &'a mut ModuleDir
|
pub(super) modules: &'a mut ModuleDir
|
||||||
@@ -54,6 +55,7 @@ pub struct Machine {
|
|||||||
call_policy: Box<CallPolicy>,
|
call_policy: Box<CallPolicy>,
|
||||||
cut_policy: Box<CutPolicy>,
|
cut_policy: Box<CutPolicy>,
|
||||||
code: Code,
|
code: Code,
|
||||||
|
pub(super) atom_tbl: TabledData<Atom>,
|
||||||
pub(super) code_dir: Rc<RefCell<CodeDir>>,
|
pub(super) code_dir: Rc<RefCell<CodeDir>>,
|
||||||
pub(super) op_dir: OpDir,
|
pub(super) op_dir: OpDir,
|
||||||
term_dir: TermDir,
|
term_dir: TermDir,
|
||||||
@@ -80,6 +82,10 @@ impl Index<LocalCodePtr> for Machine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SubModuleUser for MachineCodeIndices<'a> {
|
impl<'a> SubModuleUser for MachineCodeIndices<'a> {
|
||||||
|
fn atom_tbl(&self) -> TabledData<Atom> {
|
||||||
|
self.atom_tbl.clone()
|
||||||
|
}
|
||||||
|
|
||||||
fn op_dir(&mut self) -> &mut OpDir {
|
fn op_dir(&mut self) -> &mut OpDir {
|
||||||
self.op_dir
|
self.op_dir
|
||||||
}
|
}
|
||||||
@@ -126,6 +132,7 @@ impl Machine {
|
|||||||
call_policy: Box::new(DefaultCallPolicy {}),
|
call_policy: Box::new(DefaultCallPolicy {}),
|
||||||
cut_policy: Box::new(DefaultCutPolicy {}),
|
cut_policy: Box::new(DefaultCutPolicy {}),
|
||||||
code: Code::new(),
|
code: Code::new(),
|
||||||
|
atom_tbl: TabledData::new(Rc::new("user".to_string())),
|
||||||
code_dir: Rc::new(RefCell::new(CodeDir::new())),
|
code_dir: Rc::new(RefCell::new(CodeDir::new())),
|
||||||
op_dir: default_op_dir(),
|
op_dir: default_op_dir(),
|
||||||
term_dir: TermDir::new(),
|
term_dir: TermDir::new(),
|
||||||
@@ -134,9 +141,11 @@ impl Machine {
|
|||||||
cached_query: None
|
cached_query: None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let atom_tbl = wam.atom_tbl.clone();
|
||||||
|
|
||||||
compile_listing(&mut wam, BUILTINS.as_bytes(),
|
compile_listing(&mut wam, BUILTINS.as_bytes(),
|
||||||
default_machine_code_indices!(),
|
default_machine_code_indices!(atom_tbl.clone()),
|
||||||
default_machine_code_indices!());
|
default_machine_code_indices!(atom_tbl));
|
||||||
|
|
||||||
compile_user_module(&mut wam, LISTS.as_bytes());
|
compile_user_module(&mut wam, LISTS.as_bytes());
|
||||||
compile_user_module(&mut wam, CONTROL.as_bytes());
|
compile_user_module(&mut wam, CONTROL.as_bytes());
|
||||||
@@ -157,11 +166,6 @@ impl Machine {
|
|||||||
self.ms.fail
|
self.ms.fail
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn atom_tbl(&self) -> TabledData<Atom> {
|
|
||||||
self.ms.atom_tbl.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_batched_code(&mut self, code: Code, code_dir: CodeDir) -> Result<(), SessionError>
|
pub fn add_batched_code(&mut self, code: Code, code_dir: CodeDir) -> Result<(), SessionError>
|
||||||
{
|
{
|
||||||
for (ref key, ref idx) in code_dir.iter() {
|
for (ref key, ref idx) in code_dir.iter() {
|
||||||
@@ -217,7 +221,8 @@ impl Machine {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove_module(&mut self, module: &Module) {
|
pub fn remove_module(&mut self, module: &Module) {
|
||||||
let mut indices = machine_code_indices!(&mut self.code_dir.borrow_mut(),
|
let mut indices = machine_code_indices!(self.atom_tbl.clone(),
|
||||||
|
&mut self.code_dir.borrow_mut(),
|
||||||
&mut self.op_dir,
|
&mut self.op_dir,
|
||||||
&mut self.modules);
|
&mut self.modules);
|
||||||
indices.remove_module(clause_name!("user"), module);
|
indices.remove_module(clause_name!("user"), module);
|
||||||
@@ -295,6 +300,8 @@ impl Machine {
|
|||||||
None => return
|
None => return
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let atom_tbl = self.atom_tbl.clone();
|
||||||
|
|
||||||
match instr {
|
match instr {
|
||||||
Line::Arithmetic(ref arith_instr) =>
|
Line::Arithmetic(ref arith_instr) =>
|
||||||
self.ms.execute_arith_instr(arith_instr),
|
self.ms.execute_arith_instr(arith_instr),
|
||||||
@@ -303,7 +310,8 @@ impl Machine {
|
|||||||
Line::Cut(ref cut_instr) =>
|
Line::Cut(ref cut_instr) =>
|
||||||
self.ms.execute_cut_instr(cut_instr, &mut self.cut_policy),
|
self.ms.execute_cut_instr(cut_instr, &mut self.cut_policy),
|
||||||
Line::Control(ref control_instr) => {
|
Line::Control(ref control_instr) => {
|
||||||
let indices = machine_code_indices!(&mut self.code_dir.borrow_mut(),
|
let indices = machine_code_indices!(atom_tbl,
|
||||||
|
&mut self.code_dir.borrow_mut(),
|
||||||
&mut self.op_dir,
|
&mut self.op_dir,
|
||||||
&mut self.modules);
|
&mut self.modules);
|
||||||
|
|
||||||
|
|||||||
@@ -210,16 +210,18 @@ macro_rules! set_code_index {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! machine_code_indices {
|
macro_rules! machine_code_indices {
|
||||||
($code_dir:expr, $op_dir:expr, $modules:expr) => (
|
($atom_tbl:expr, $code_dir:expr, $op_dir:expr, $modules:expr) => (
|
||||||
MachineCodeIndices { code_dir: $code_dir,
|
MachineCodeIndices { atom_tbl: $atom_tbl,
|
||||||
|
code_dir: $code_dir,
|
||||||
op_dir: $op_dir,
|
op_dir: $op_dir,
|
||||||
modules: $modules }
|
modules: $modules }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! default_machine_code_indices {
|
macro_rules! default_machine_code_indices {
|
||||||
() => (
|
($atom_tbl:expr) => (
|
||||||
machine_code_indices!(&mut CodeDir::new(),
|
machine_code_indices!($atom_tbl,
|
||||||
|
&mut CodeDir::new(),
|
||||||
&mut default_op_dir(),
|
&mut default_op_dir(),
|
||||||
&mut HashMap::new())
|
&mut HashMap::new())
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use prolog_parser::ast::*;
|
use prolog_parser::ast::*;
|
||||||
use prolog_parser::parser::*;
|
use prolog_parser::parser::*;
|
||||||
|
use prolog_parser::tabled_rc::TabledData;
|
||||||
|
|
||||||
use prolog::instructions::*;
|
use prolog::instructions::*;
|
||||||
use prolog::iterators::*;
|
use prolog::iterators::*;
|
||||||
@@ -44,7 +45,8 @@ pub fn read_toplevel(wam: &mut Machine) -> Result<Input, ParserError> {
|
|||||||
Ok(Input::Batch)
|
Ok(Input::Batch)
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
let mut term_stream = TermStream::new(stdin.lock(), wam.atom_tbl(), wam.machine_flags());
|
let mut term_stream = TermStream::new(stdin.lock(), wam.atom_tbl.clone(),
|
||||||
|
wam.machine_flags());
|
||||||
term_stream.add_to_top(buffer.as_str());
|
term_stream.add_to_top(buffer.as_str());
|
||||||
|
|
||||||
Ok(Input::Term(term_stream.read_term(wam, &OpDir::new())?))
|
Ok(Input::Term(term_stream.read_term(wam, &OpDir::new())?))
|
||||||
@@ -53,9 +55,10 @@ pub fn read_toplevel(wam: &mut Machine) -> Result<Input, ParserError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MachineState {
|
impl MachineState {
|
||||||
pub fn read<R: Read>(&mut self, inner: R, op_dir: &OpDir) -> Result<usize, ParserError>
|
pub fn read<R: Read>(&mut self, inner: R, atom_tbl: TabledData<Atom>, op_dir: &OpDir)
|
||||||
|
-> Result<usize, ParserError>
|
||||||
{
|
{
|
||||||
let mut parser = Parser::new(inner, self.atom_tbl.clone(), self.flags);
|
let mut parser = Parser::new(inner, atom_tbl, self.flags);
|
||||||
let term = parser.read_term(composite_op!(op_dir))?;
|
let term = parser.read_term(composite_op!(op_dir))?;
|
||||||
|
|
||||||
Ok(write_term_to_heap(&term, self))
|
Ok(write_term_to_heap(&term, self))
|
||||||
|
|||||||
@@ -714,7 +714,7 @@ pub fn parse_term<R: Read>(wam: &Machine, buf: R) -> Result<Term, ParserError>
|
|||||||
{
|
{
|
||||||
use prolog_parser::parser::*;
|
use prolog_parser::parser::*;
|
||||||
|
|
||||||
let mut parser = Parser::new(buf, wam.atom_tbl(), wam.machine_flags());
|
let mut parser = Parser::new(buf, wam.atom_tbl.clone(), wam.machine_flags());
|
||||||
parser.read_term(composite_op!(&wam.op_dir))
|
parser.read_term(composite_op!(&wam.op_dir))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user