add string table, StringList representation

This commit is contained in:
Mark Thom
2018-08-21 00:22:16 -06:00
parent 01d6ef099f
commit 013eb29e2b
11 changed files with 36 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ use prolog::num::bigint::BigInt;
use prolog::num::{Float, ToPrimitive, Zero};
use prolog::num::rational::Ratio;
use prolog::ordered_float::*;
use prolog::string_list::*;
use prolog::tabled_rc::*;
use std::cell::{Cell, RefCell};
@@ -531,7 +532,7 @@ pub enum Constant {
Atom(ClauseName),
Char(char),
Number(Number),
String(TabledRc<String>),
String(StringList),
Usize(usize),
EmptyList
}

View File

@@ -38,12 +38,14 @@ fn print_code(code: &Code) {
pub fn parse_code(wam: &mut Machine, buffer: &str) -> Result<TopLevelPacket, ParserError>
{
let atom_tbl = wam.atom_tbl();
let string_tbl = wam.string_tbl();
let index = MachineCodeIndices {
code_dir: &mut wam.code_dir,
op_dir: &mut wam.op_dir,
};
let mut worker = TopLevelWorker::new(buffer.as_bytes(), atom_tbl, index);
let mut worker = TopLevelWorker::new(buffer.as_bytes(), atom_tbl, string_tbl, index);
worker.parse_code()
}
@@ -239,7 +241,7 @@ fn use_qualified_module(module: &mut Option<Module>, submodule: &Module, exports
pub
fn compile_listing(wam: &mut Machine, src_str: &str, mut indices: MachineCodeIndices) -> EvalSession
{
let mut worker = TopLevelBatchWorker::new(src_str.as_bytes(), wam.atom_tbl());
let mut worker = TopLevelBatchWorker::new(src_str.as_bytes(), wam.atom_tbl(), wam.string_tbl());
let mut compiler = ListingCompiler::new(wam);
while let Some(decl) = try_eval_session!(worker.consume(&mut indices)) {

View File

@@ -324,7 +324,7 @@ impl<'a, Formatter: HCValueFormatter, Outputter: HCValueOutputter>
self.outputter.append(&format!("{}", n)),
Constant::String(s) => {
self.outputter.append("\"");
self.outputter.append(s.as_str());
self.outputter.append(s.borrow().as_str());
self.outputter.append("\"");
},
Constant::Usize(i) =>

View File

@@ -44,7 +44,7 @@ impl fmt::Display for Constant {
&Constant::Number(ref n) =>
write!(f, "{}", n),
&Constant::String(ref s) =>
write!(f, "\"{}\"", s),
write!(f, "\"{}\"", s.borrow()),
&Constant::Usize(integer) =>
write!(f, "u{}", integer)
}

View File

@@ -6,6 +6,7 @@ use prolog::machine::machine_errors::*;
use prolog::num::{BigInt, BigUint, Zero, One};
use prolog::or_stack::*;
use prolog::read::*;
use prolog::string_list::*;
use prolog::tabled_rc::*;
use downcast::Any;
@@ -288,6 +289,7 @@ impl Default for MachineFlags {
pub struct MachineState {
pub(crate) atom_tbl: TabledData<Atom>,
pub(crate) string_tbl: TabledData<StringListWrapper>,
pub(super) s: usize,
pub(super) p: CodePtr,
pub(super) b: usize,

View File

@@ -9,8 +9,8 @@ use prolog::num::{Integer, Signed, ToPrimitive, Zero};
use prolog::num::bigint::{BigInt, BigUint};
use prolog::num::rational::Ratio;
use prolog::or_stack::*;
use prolog::tabled_rc::*;
use std::cell::RefCell;
use std::cmp::{max, Ordering};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
@@ -28,9 +28,10 @@ macro_rules! try_or_fail {
}
impl MachineState {
pub(super) fn new(atom_tbl: TabledData<Atom>) -> MachineState {
pub(super) fn new() -> Self {
MachineState {
atom_tbl,
atom_tbl: Rc::new(RefCell::new(HashSet::new())),
string_tbl: Rc::new(RefCell::new(HashSet::new())),
s: 0,
p: CodePtr::default(),
b: 0,

View File

@@ -1,6 +1,7 @@
use prolog::ast::*;
use prolog::compile::*;
use prolog::heap_print::*;
use prolog::string_list::StringListWrapper;
use prolog::tabled_rc::*;
mod machine_errors;
@@ -11,8 +12,7 @@ mod system_calls;
use prolog::machine::machine_state::*;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::mem::swap;
use std::ops::Index;
use std::rc::Rc;
@@ -99,7 +99,7 @@ static QUEUES: &str = include_str!("../lib/queues.pl");
impl Machine {
pub fn new() -> Self {
let mut wam = Machine {
ms: MachineState::new(Rc::new(RefCell::new(HashSet::new()))),
ms: MachineState::new(),
call_policy: Box::new(DefaultCallPolicy {}),
cut_policy: Box::new(DefaultCutPolicy {}),
code: Code::new(),
@@ -177,6 +177,10 @@ impl Machine {
self.ms.atom_tbl.clone()
}
pub fn string_tbl(&self) -> TabledData<StringListWrapper> {
self.ms.string_tbl.clone()
}
pub fn use_qualified_module_in_toplevel(&mut self, name: ClauseName, exports: Vec<PredicateKey>)
-> EvalSession
{

View File

@@ -33,7 +33,10 @@ impl<'a> Reader<'a> {
let stdin = stdin();
stdin.read_line(&mut buffer).unwrap();
let mut parser = Parser::new(buffer.as_bytes(), self.machine_st.atom_tbl.clone());
let atom_tbl = self.machine_st.atom_tbl.clone();
let string_tbl = self.machine_st.string_tbl.clone();
let mut parser = Parser::new(buffer.as_bytes(), atom_tbl, string_tbl);
Ok(self.write_term_to_heap(parser.read_term(op_dir)?))
}

View File

@@ -3,7 +3,6 @@ use prolog::tabled_rc::*;
use std::cell::{Ref, RefCell};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::ops::{Index, RangeTo};
#[derive(PartialOrd, PartialEq, Ord, Eq)]
pub struct StringListWrapper(RefCell<String>);

View File

@@ -2,6 +2,7 @@ use prolog::ast::*;
use prolog::machine::*;
use prolog::num::*;
use prolog::parser::parser::*;
use prolog::string_list::*;
use prolog::tabled_rc::*;
use std::collections::{HashSet, VecDeque};
@@ -606,8 +607,11 @@ pub struct TopLevelWorker<'a, R: Read> {
}
impl<'a, R: Read> TopLevelWorker<'a, R> {
pub fn new(inner: R, atom_tbl: TabledData<Atom>, indices: MachineCodeIndices<'a>) -> Self {
TopLevelWorker { parser: Parser::new(inner, atom_tbl), indices }
pub fn new(inner: R, atom_tbl: TabledData<Atom>, string_tbl: TabledData<StringListWrapper>,
indices: MachineCodeIndices<'a>)
-> Self
{
TopLevelWorker { parser: Parser::new(inner, atom_tbl, string_tbl), indices }
}
pub fn parse_code(&mut self) -> Result<TopLevelPacket, ParserError>
@@ -636,8 +640,10 @@ pub struct TopLevelBatchWorker<R: Read> {
}
impl<R: Read> TopLevelBatchWorker<R> {
pub fn new(inner: R, atom_tbl: TabledData<Atom>) -> Self {
TopLevelBatchWorker { parser: Parser::new(inner, atom_tbl),
pub fn new(inner: R, atom_tbl: TabledData<Atom>, string_tbl: TabledData<StringListWrapper>)
-> Self
{
TopLevelBatchWorker { parser: Parser::new(inner, atom_tbl, string_tbl),
rel_worker: RelationWorker::new(),
source_mod: clause_name!("user"),
results: vec![] }