make use of read_term in the repl

This commit is contained in:
Mark Thom
2018-09-10 00:18:17 -06:00
parent 664f0f83dc
commit 3f06ff874b
15 changed files with 224 additions and 295 deletions

View File

@@ -61,48 +61,7 @@ impl MachineError {
}
pub(super) fn syntax_error(h: usize, err: ParserError) -> Self {
let err = match err {
ParserError::Arithmetic(_) =>
vec![heap_atom!("arithmetic_error")],
ParserError::BackQuotedString =>
vec![heap_atom!("back_quoted_string")],
ParserError::UnexpectedChar(c) =>
functor!("unexpected_char", 1, [heap_char!(c)]),
ParserError::UnexpectedEOF =>
vec![heap_atom!("unexpected_end_of_file")],
ParserError::ExpectedRel =>
vec![heap_atom!("expected_relation")],
ParserError::InadmissibleFact =>
vec![heap_atom!("inadmissible_fact")],
ParserError::InadmissibleQueryTerm =>
vec![heap_atom!("inadmissible_query_term")],
ParserError::IncompleteReduction =>
vec![heap_atom!("incomplete_reduction")],
ParserError::InconsistentEntry =>
vec![heap_atom!("inconsistent_entry")],
ParserError::InvalidModuleDecl =>
vec![heap_atom!("invalid_module_declaration")],
ParserError::InvalidModuleExport =>
vec![heap_atom!("invalid_module_export")],
ParserError::InvalidModuleResolution =>
vec![heap_atom!("invalid_module_resolution")],
ParserError::InvalidRuleHead =>
vec![heap_atom!("invalid_head_of_rule")],
ParserError::InvalidUseModuleDecl =>
vec![heap_atom!("invalid_use_module_declaration")],
ParserError::IO(_) =>
vec![heap_atom!("input_output_error")],
ParserError::MissingQuote =>
vec![heap_atom!("missing_quote")],
ParserError::NonPrologChar =>
vec![heap_atom!("non_prolog_character")],
ParserError::ParseBigInt =>
vec![heap_atom!("cannot_parse_big_int")],
ParserError::ParseFloat =>
vec![heap_atom!("cannot_parse_float")],
ParserError::Utf8Conversion(_) =>
vec![heap_atom!("utf8_conversion_error")]
};
let err = vec![heap_atom!(err.as_str())];
let mut stub = if err.len() == 1 {
functor!("syntax_error", 1)

View File

@@ -9,12 +9,12 @@ use prolog::machine::MachineCodeIndices;
use prolog::machine::machine_errors::*;
use prolog::num::{BigInt, BigUint, Zero, One};
use prolog::or_stack::*;
use prolog::read::*;
use downcast::Any;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::io::stdin;
use std::mem::swap;
use std::ops::{Index, IndexMut};
use std::rc::Rc;
@@ -297,7 +297,7 @@ pub struct MachineState {
pub(super) ball: Ball,
pub(super) interms: Vec<Number>, // intermediate numbers.
pub(super) last_call: bool,
pub(super) flags: MachineFlags
pub(crate) flags: MachineFlags
}
fn call_at_index(machine_st: &mut MachineState, module_name: ClauseName, arity: usize, idx: usize)
@@ -570,25 +570,23 @@ pub(crate) trait CallPolicy: Any {
machine_st.fail = !machine_st.is_cyclic_term(addr);
return_from_clause!(machine_st.last_call, machine_st)
},
&BuiltInClauseType::Read => {
let mut reader = Reader::new(machine_st);
match reader.read_stdin(&indices.op_dir) {
&BuiltInClauseType::Read => {
match machine_st.read(stdin(), &indices.op_dir) {
Ok(offset) => {
let addr = reader.machine_st[temp_v!(1)].clone();
reader.machine_st.unify(addr, Addr::HeapCell(offset));
let addr = machine_st[temp_v!(1)].clone();
machine_st.unify(addr, Addr::HeapCell(offset));
},
Err(e) => {
let h = reader.machine_st.heap.h;
let h = machine_st.heap.h;
let stub = MachineError::functor_stub(clause_name!("read"), 1);
let err = MachineError::syntax_error(h, e);
let err = reader.machine_st.error_form(err, stub);
let err = machine_st.error_form(err, stub);
return Err(err);
}
};
return_from_clause!(reader.machine_st.last_call, reader.machine_st)
return_from_clause!(machine_st.last_call, machine_st)
},
&BuiltInClauseType::Writeq => {
let output = machine_st.print_term(machine_st[temp_v!(1)].clone(),

View File

@@ -62,7 +62,7 @@ pub struct Machine {
code: Code,
pub(super) code_dir: CodeDir,
pub(super) op_dir: OpDir,
term_dir: TermDir,
// term_dir: TermDir,
pub(super) modules: ModuleDir,
cached_query: Option<Code>
}
@@ -116,7 +116,7 @@ impl Machine {
code: Code::new(),
code_dir: CodeDir::new(),
op_dir: default_op_dir(),
term_dir: TermDir::new(),
// term_dir: TermDir::new(),
modules: HashMap::new(),
cached_query: None
};
@@ -124,11 +124,11 @@ impl Machine {
let indices = machine_code_indices!(&mut CodeDir::new(), &mut default_op_dir(),
&mut HashMap::new());
compile_listing(&mut wam, BUILTINS, indices);
compile_listing(&mut wam, BUILTINS.as_bytes(), indices);
compile_user_module(&mut wam, LISTS);
compile_user_module(&mut wam, CONTROL);
compile_user_module(&mut wam, QUEUES);
compile_user_module(&mut wam, LISTS.as_bytes());
compile_user_module(&mut wam, CONTROL.as_bytes());
compile_user_module(&mut wam, QUEUES.as_bytes());
wam.use_module_in_toplevel(clause_name!("builtins"));
@@ -252,8 +252,7 @@ impl Machine {
self.code.extend(code.into_iter());
}
pub fn add_user_code(&mut self, name: ClauseName, arity: usize, code: Code, pred: Predicate)
-> EvalSession
pub fn add_user_code(&mut self, name: ClauseName, arity: usize, code: Code) -> EvalSession
{
match self.code_dir.get(&(name.clone(), arity)) {
Some(&CodeIndex (ref idx)) if idx.borrow().1 != clause_name!("user") =>
@@ -268,7 +267,7 @@ impl Machine {
let offset = self.code.len();
self.code.extend(code.into_iter());
self.term_dir.insert((name.clone(), arity), pred);
//self.term_dir.insert((name.clone(), arity), pred);
let idx = self.code_dir.entry((name, arity))
.or_insert(CodeIndex::from((offset, clause_name!("user"))));