Revert "remove Term"
This reverts commit 3b5879841aedecba5057c70c71da0ba23e5cd84a.
This commit is contained in:
@@ -2,18 +2,22 @@
|
||||
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::forms::PredicateKey;
|
||||
use crate::machine::heap::*;
|
||||
use crate::machine::machine_indices::*;
|
||||
use crate::types::*;
|
||||
use crate::machine::machine_indices::CodeIndex;
|
||||
use crate::parser::char_reader::*;
|
||||
use crate::types::HeapCellValueTag;
|
||||
|
||||
use std::cell::{Cell, Ref, RefCell, RefMut};
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::hash::Hasher;
|
||||
use std::io::{Error as IOError, ErrorKind};
|
||||
use std::ops::Neg;
|
||||
use std::ops::{Deref, Neg};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::vec::Vec;
|
||||
|
||||
use dashu::Integer;
|
||||
use dashu::Rational;
|
||||
use fxhash::FxBuildHasher;
|
||||
use indexmap::IndexMap;
|
||||
use scryer_modular_bitfield::error::OutOfBounds;
|
||||
@@ -138,16 +142,7 @@ pub const BTERM: u32 = 0x11000;
|
||||
pub const NEGATIVE_SIGN: u32 = 0x0200;
|
||||
|
||||
macro_rules! fixnum {
|
||||
($n:expr, $arena:expr) => {
|
||||
Fixnum::build_with_checked($n)
|
||||
.map(|n| fixnum_as_cell!(n))
|
||||
.unwrap_or_else(|_| {
|
||||
typed_arena_ptr_as_cell!(
|
||||
arena_alloc!(Integer::from($n), $arena) as TypedArenaPtr<Integer>
|
||||
)
|
||||
})
|
||||
};
|
||||
($wrapper:ty, $n:expr, $arena:expr) => {
|
||||
($wrapper:tt, $n:expr, $arena:expr) => {
|
||||
Fixnum::build_with_checked($n)
|
||||
.map(<$wrapper>::Fixnum)
|
||||
.unwrap_or_else(|_| <$wrapper>::Integer(arena_alloc!(Integer::from($n), $arena)))
|
||||
@@ -276,6 +271,37 @@ impl fmt::Display for RegType {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum VarReg {
|
||||
ArgAndNorm(RegType, usize),
|
||||
Norm(RegType),
|
||||
}
|
||||
|
||||
impl VarReg {
|
||||
pub fn norm(self) -> RegType {
|
||||
match self {
|
||||
VarReg::ArgAndNorm(reg, _) | VarReg::Norm(reg) => reg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VarReg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
VarReg::Norm(RegType::Perm(reg)) => write!(f, "Y{}", reg),
|
||||
VarReg::Norm(RegType::Temp(reg)) => write!(f, "X{}", reg),
|
||||
VarReg::ArgAndNorm(RegType::Perm(reg), arg) => write!(f, "Y{} A{}", reg, arg),
|
||||
VarReg::ArgAndNorm(RegType::Temp(reg), arg) => write!(f, "X{} A{}", reg, arg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VarReg {
|
||||
fn default() -> Self {
|
||||
VarReg::Norm(RegType::default())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! temp_v {
|
||||
($x:expr) => {
|
||||
$crate::parser::ast::RegType::Temp($x)
|
||||
@@ -373,49 +399,41 @@ pub fn default_op_dir() -> OpDir {
|
||||
op_dir
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum ArithmeticError {
|
||||
NonEvaluableFunctor(HeapCellValue, usize),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct ParserErrorSrc {
|
||||
pub col_num: usize,
|
||||
pub line_num: usize,
|
||||
NonEvaluableFunctor(Literal, usize),
|
||||
UninstantiatedVar,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum ParserError {
|
||||
BackQuotedString(ParserErrorSrc),
|
||||
IO(IOError, ParserErrorSrc),
|
||||
IncompleteReduction(ParserErrorSrc),
|
||||
InfiniteFloat(ParserErrorSrc),
|
||||
InvalidSingleQuotedCharacter(char, ParserErrorSrc),
|
||||
LexicalError(lexical::Error, ParserErrorSrc),
|
||||
MissingQuote(ParserErrorSrc),
|
||||
NonPrologChar(ParserErrorSrc),
|
||||
ParseBigInt(ParserErrorSrc),
|
||||
ResourceError(ParserErrorSrc),
|
||||
UnexpectedChar(char, ParserErrorSrc),
|
||||
BackQuotedString(usize, usize),
|
||||
IO(IOError),
|
||||
IncompleteReduction(usize, usize),
|
||||
InfiniteFloat(usize, usize),
|
||||
InvalidSingleQuotedCharacter(char),
|
||||
LexicalError(lexical::Error),
|
||||
MissingQuote(usize, usize),
|
||||
NonPrologChar(usize, usize),
|
||||
ParseBigInt(usize, usize),
|
||||
UnexpectedChar(char, usize, usize),
|
||||
// UnexpectedEOF,
|
||||
Utf8Error(ParserErrorSrc),
|
||||
Utf8Error(usize, usize),
|
||||
}
|
||||
|
||||
impl ParserError {
|
||||
pub fn err_src(&self) -> ParserErrorSrc {
|
||||
pub fn line_and_col_num(&self) -> Option<(usize, usize)> {
|
||||
match self {
|
||||
&ParserError::BackQuotedString(err_src)
|
||||
| &ParserError::IO(_, err_src)
|
||||
| &ParserError::IncompleteReduction(err_src)
|
||||
| &ParserError::InfiniteFloat(err_src)
|
||||
| &ParserError::InvalidSingleQuotedCharacter(_, err_src)
|
||||
| &ParserError::LexicalError(_, err_src)
|
||||
| &ParserError::MissingQuote(err_src)
|
||||
| &ParserError::NonPrologChar(err_src)
|
||||
| &ParserError::ParseBigInt(err_src)
|
||||
| &ParserError::ResourceError(err_src)
|
||||
| &ParserError::UnexpectedChar(_, err_src)
|
||||
| &ParserError::Utf8Error(err_src) => err_src,
|
||||
&ParserError::BackQuotedString(line_num, col_num)
|
||||
| &ParserError::IncompleteReduction(line_num, col_num)
|
||||
| &ParserError::InfiniteFloat(line_num, col_num)
|
||||
| &ParserError::MissingQuote(line_num, col_num)
|
||||
| &ParserError::NonPrologChar(line_num, col_num)
|
||||
| &ParserError::ParseBigInt(line_num, col_num)
|
||||
| &ParserError::UnexpectedChar(_, line_num, col_num)
|
||||
| &ParserError::Utf8Error(line_num, col_num) => Some((line_num, col_num)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,31 +447,30 @@ impl ParserError {
|
||||
ParserError::InfiniteFloat(..) => {
|
||||
atom!("infinite_float")
|
||||
}
|
||||
ParserError::IO(e, _) if e.kind() == ErrorKind::UnexpectedEof => {
|
||||
ParserError::IO(e) if e.kind() == ErrorKind::UnexpectedEof => {
|
||||
atom!("unexpected_end_of_file")
|
||||
}
|
||||
ParserError::IO(e, _) if e.kind() == ErrorKind::InvalidData => {
|
||||
ParserError::IO(e) if e.kind() == ErrorKind::InvalidData => {
|
||||
atom!("invalid_data")
|
||||
}
|
||||
ParserError::IO(..) => atom!("input_output_error"),
|
||||
ParserError::LexicalError(..) => atom!("lexical_error"),
|
||||
ParserError::IO(_) => atom!("input_output_error"),
|
||||
ParserError::LexicalError(_) => atom!("lexical_error"),
|
||||
ParserError::MissingQuote(..) => atom!("missing_quote"),
|
||||
ParserError::NonPrologChar(..) => atom!("non_prolog_character"),
|
||||
ParserError::ParseBigInt(..) => atom!("cannot_parse_big_int"),
|
||||
ParserError::UnexpectedChar(..) => atom!("unexpected_char"),
|
||||
ParserError::Utf8Error(..) => atom!("utf8_conversion_error"),
|
||||
ParserError::ResourceError(..) => atom!("resource_error"),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn unexpected_eof(err_src: ParserErrorSrc) -> Self {
|
||||
ParserError::IO(std::io::Error::from(ErrorKind::UnexpectedEof), err_src)
|
||||
pub fn unexpected_eof() -> Self {
|
||||
ParserError::IO(std::io::Error::from(ErrorKind::UnexpectedEof))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_unexpected_eof(&self) -> bool {
|
||||
if let ParserError::IO(e, _) = self {
|
||||
if let ParserError::IO(e) = self {
|
||||
e.kind() == ErrorKind::UnexpectedEof
|
||||
} else {
|
||||
false
|
||||
@@ -461,9 +478,25 @@ impl ParserError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParserErrorSrc> for ParserError {
|
||||
fn from(err_src: ParserErrorSrc) -> ParserError {
|
||||
ParserError::LexicalError(err_src)
|
||||
impl From<lexical::Error> for ParserError {
|
||||
fn from(e: lexical::Error) -> ParserError {
|
||||
ParserError::LexicalError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IOError> for ParserError {
|
||||
fn from(e: IOError) -> ParserError {
|
||||
ParserError::IO(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&IOError> for ParserError {
|
||||
fn from(error: &IOError) -> ParserError {
|
||||
if error.get_ref().filter(|e| e.is::<BadUtf8Error>()).is_some() {
|
||||
ParserError::Utf8Error(0, 0)
|
||||
} else {
|
||||
ParserError::IO(error.kind().into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -575,8 +608,7 @@ impl Neg for Fixnum {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Literal {
|
||||
Atom(Atom),
|
||||
CodeIndex(CodeIndex),
|
||||
@@ -584,7 +616,6 @@ pub enum Literal {
|
||||
Integer(TypedArenaPtr<Integer>),
|
||||
Rational(TypedArenaPtr<Rational>),
|
||||
Float(F64Offset),
|
||||
String(Rc<String>),
|
||||
}
|
||||
|
||||
impl From<F64Ptr> for Literal {
|
||||
@@ -606,7 +637,6 @@ impl fmt::Display for Literal {
|
||||
Literal::Integer(ref n) => write!(f, "{}", n),
|
||||
Literal::Rational(ref n) => write!(f, "{}", n),
|
||||
Literal::Float(ref n) => write!(f, "{}", *n),
|
||||
Literal::String(ref s) => write!(f, "\"{}\"", s.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -619,38 +649,109 @@ impl Literal {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub type Var = Rc<String>;
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct VarPtr(Rc<RefCell<Var>>);
|
||||
|
||||
pub(crate) fn subterm_index(heap: &impl SizedHeap, subterm_loc: usize) -> (usize, HeapCellValue) {
|
||||
let subterm = heap[subterm_loc];
|
||||
|
||||
if subterm.is_ref() {
|
||||
let subterm = heap_bound_deref(heap, subterm);
|
||||
let subterm_loc = subterm.get_value() as usize;
|
||||
let subterm = heap_bound_store(heap, subterm);
|
||||
|
||||
let subterm_loc = if subterm.is_ref() {
|
||||
subterm.get_value() as usize
|
||||
} else {
|
||||
subterm_loc
|
||||
};
|
||||
|
||||
(subterm_loc, subterm)
|
||||
} else {
|
||||
(subterm_loc, subterm)
|
||||
impl Hash for VarPtr {
|
||||
#[inline(always)]
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.borrow().hash(hasher)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for VarPtr {
|
||||
type Target = RefCell<Var>;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl VarPtr {
|
||||
#[inline(always)]
|
||||
pub(crate) fn borrow(&self) -> Ref<'_, Var> {
|
||||
self.0.borrow()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn borrow_mut(&self) -> RefMut<'_, Var> {
|
||||
self.0.borrow_mut()
|
||||
}
|
||||
|
||||
pub(crate) fn to_var_num(&self) -> Option<usize> {
|
||||
match *self.borrow() {
|
||||
Var::Generated(var_num) => Some(var_num),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set(&self, var: Var) {
|
||||
let mut var_ref = self.borrow_mut();
|
||||
*var_ref = var;
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Var> for VarPtr {
|
||||
#[inline(always)]
|
||||
fn from(value: Var) -> VarPtr {
|
||||
VarPtr(Rc::new(RefCell::new(value)))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for VarPtr {
|
||||
#[inline(always)]
|
||||
fn from(value: String) -> VarPtr {
|
||||
VarPtr::from(Var::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for VarPtr {
|
||||
#[inline(always)]
|
||||
fn from(value: &str) -> VarPtr {
|
||||
VarPtr::from(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Var {
|
||||
Generated(usize),
|
||||
InSitu(usize),
|
||||
Named(Rc<String>),
|
||||
}
|
||||
|
||||
impl From<String> for Var {
|
||||
#[inline(always)]
|
||||
fn from(value: String) -> Var {
|
||||
Var::Named(Rc::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Var {
|
||||
#[inline(always)]
|
||||
fn from(value: &str) -> Var {
|
||||
Var::Named(Rc::new(value.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Var {
|
||||
#[allow(clippy::inherent_to_string)]
|
||||
#[inline(always)]
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Var::InSitu(n) | Var::Generated(n) => format!("_{}", n),
|
||||
Var::Named(value) => value.as_ref().clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Term {
|
||||
AnonVar,
|
||||
Clause(Cell<RegType>, Atom, Vec<Term>),
|
||||
Cons(Cell<RegType>, Box<Term>, Box<Term>),
|
||||
Literal(Cell<RegType>, HeapCellValue),
|
||||
// Literal(Cell<RegType>, Literal),
|
||||
Literal(Cell<RegType>, Literal),
|
||||
// PartialString wraps a String in anticipation of it absorbing
|
||||
// other PartialString variants in as_partial_string.
|
||||
PartialString(Cell<RegType>, Rc<String>, Box<Term>),
|
||||
@@ -668,12 +769,8 @@ impl Term {
|
||||
|
||||
pub fn name(&self) -> Option<Atom> {
|
||||
match self {
|
||||
Term::Literal(_, cell) => {
|
||||
cell.to_atom()
|
||||
}
|
||||
&Term::Clause(_, atom, ..) => {
|
||||
Some(atom)
|
||||
}
|
||||
&Term::Literal(_, Literal::Atom(atom)) => Some(atom),
|
||||
&Term::Clause(_, atom, ..) => Some(atom),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -714,281 +811,3 @@ pub fn unfold_by_str(mut term: Term, s: Atom) -> Vec<Term> {
|
||||
terms.push(term);
|
||||
terms
|
||||
}
|
||||
*/
|
||||
|
||||
pub(crate) fn fetch_index_ptr(heap: &impl SizedHeap, term_loc: usize) -> Option<CodeIndex> {
|
||||
let index_cell_loc = term_loc.saturating_sub(1);
|
||||
|
||||
read_heap_cell!(heap[index_cell_loc],
|
||||
(HeapCellValueTag::Cons, c) => {
|
||||
match_untyped_arena_ptr!(c,
|
||||
(ArenaHeaderTag::IndexPtr, ptr) => {
|
||||
return Some(CodeIndex::from(ptr));
|
||||
}
|
||||
_ => {}
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn blunt_index_ptr(
|
||||
heap: &mut impl SizedHeapMut,
|
||||
key: PredicateKey,
|
||||
term_loc: usize,
|
||||
) -> bool {
|
||||
if fetch_index_ptr(heap, term_loc).is_some() {
|
||||
heap[term_loc] = atom_as_cell!(key.0, key.1);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unfold_by_str_once(
|
||||
heap: &mut impl SizedHeapMut,
|
||||
start_term: HeapCellValue,
|
||||
atom: Atom,
|
||||
) -> Option<usize> {
|
||||
let start_term = heap_bound_store(heap, heap_bound_deref(heap, start_term));
|
||||
|
||||
if let HeapCellValueTag::Str = start_term.get_tag() {
|
||||
let s = start_term.get_value() as usize;
|
||||
|
||||
let (s_atom, s_arity) = cell_as_atom_cell!(heap[s]).get_name_and_arity();
|
||||
blunt_index_ptr(heap, (s_atom, s_arity), s);
|
||||
|
||||
if (s_atom, s_arity) == (atom, 2) {
|
||||
return Some(s + 1);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn unfold_by_str(
|
||||
heap: &mut impl SizedHeapMut,
|
||||
mut start_term: HeapCellValue,
|
||||
atom: Atom,
|
||||
) -> Vec<HeapCellValue> {
|
||||
let mut terms = vec![];
|
||||
start_term = heap_bound_store(heap, heap_bound_deref(heap, start_term));
|
||||
|
||||
while let Some(fst_loc) = unfold_by_str_once(heap, start_term, atom) {
|
||||
let (_, snd) = subterm_index(heap, fst_loc + 1);
|
||||
let (_, fst) = subterm_index(heap, fst_loc);
|
||||
terms.push(fst);
|
||||
start_term = snd;
|
||||
}
|
||||
|
||||
terms
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn unfold_by_str_locs(
|
||||
heap: &mut [HeapCellValue],
|
||||
mut term_loc: usize,
|
||||
atom: Atom,
|
||||
) -> Vec<(HeapCellValue, usize)> {
|
||||
let mut terms = vec![];
|
||||
let mut current_term = heap_bound_store(
|
||||
heap,
|
||||
heap_bound_deref(heap, heap[term_loc]),
|
||||
);
|
||||
|
||||
while let Some(fst_loc) = unfold_by_str_once(heap, current_term, atom) {
|
||||
(term_loc, current_term) = subterm_index(heap, fst_loc + 1);
|
||||
let (fst_loc, fst) = subterm_index(heap, fst_loc);
|
||||
terms.push((fst, fst_loc));
|
||||
}
|
||||
|
||||
terms.push((current_term, term_loc));
|
||||
terms
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn unfold_by_str_locs(
|
||||
heap: &mut impl SizedHeapMut,
|
||||
mut term_loc: usize,
|
||||
atom: Atom,
|
||||
) -> Vec<(HeapCellValue, usize)> {
|
||||
let mut terms = vec![];
|
||||
let mut current_term = heap[term_loc];
|
||||
|
||||
while let Some(fst_loc) = unfold_by_str_once(heap, current_term, atom) {
|
||||
term_loc = fst_loc + 1;
|
||||
current_term = heap[term_loc];
|
||||
let fst = heap[fst_loc];
|
||||
terms.push((fst, fst_loc));
|
||||
}
|
||||
|
||||
terms.push((current_term, term_loc));
|
||||
terms
|
||||
}
|
||||
|
||||
pub fn term_predicate_key(heap: &impl SizedHeap, mut term_loc: usize) -> Option<PredicateKey> {
|
||||
loop {
|
||||
read_heap_cell!(heap[term_loc],
|
||||
(HeapCellValueTag::Atom, (name, arity)) => {
|
||||
return Some((name, arity));
|
||||
}
|
||||
(HeapCellValueTag::Str, s) => {
|
||||
term_loc = s;
|
||||
}
|
||||
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => {
|
||||
if h != term_loc {
|
||||
term_loc = h;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inverse_var_locs_from_iter<I: Iterator<Item = HeapCellValue>>(iter: I) -> InverseVarLocs {
|
||||
let mut occurrence_set: IndexMap<HeapCellValue, usize, FxBuildHasher> =
|
||||
IndexMap::with_hasher(FxBuildHasher::default());
|
||||
|
||||
for term in iter {
|
||||
if term.is_var() {
|
||||
let var_count = occurrence_set.entry(term).or_insert(0);
|
||||
*var_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut inverse_var_locs = InverseVarLocs::default();
|
||||
|
||||
for (var, count) in occurrence_set {
|
||||
let var_loc = var.get_value() as usize;
|
||||
|
||||
if count > 1 {
|
||||
inverse_var_locs.insert(var_loc, Rc::new(format!("_{}", var_loc)));
|
||||
}
|
||||
}
|
||||
|
||||
inverse_var_locs
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn term_deref(heap: &[HeapCellValue], mut term_loc: usize) -> HeapCellValue {
|
||||
loop {
|
||||
read_heap_cell!(heap[term_loc],
|
||||
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => {
|
||||
if h != term_loc {
|
||||
term_loc = h;
|
||||
} else {
|
||||
return heap[h];
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return heap[term_loc];
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn term_nth_arg(heap: &impl SizedHeap, mut term_loc: usize, n: usize) -> Option<usize> {
|
||||
loop {
|
||||
read_heap_cell!(heap[term_loc],
|
||||
(HeapCellValueTag::Str, s) => {
|
||||
return if cell_as_atom_cell!(heap[s]).get_arity() >= n {
|
||||
Some(s+n)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
(HeapCellValueTag::Atom, (_name, arity)) => {
|
||||
return if arity >= n {
|
||||
Some(term_loc + n)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
(HeapCellValueTag::Lis, l) => {
|
||||
return if 1 <= n && n <= 2 {
|
||||
Some(l+n-1)
|
||||
} else if n == 0 {
|
||||
Some(term_loc)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => {
|
||||
if h != term_loc {
|
||||
term_loc = h;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TermWriteResult {
|
||||
pub focus: usize,
|
||||
pub inverse_var_locs: InverseVarLocs,
|
||||
}
|
||||
|
||||
pub type VarLocs = IndexMap<Var, HeapCellValue, FxBuildHasher>;
|
||||
pub type InverseVarLocs = IndexMap<usize, Var, FxBuildHasher>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FocusedHeapRefMut<'a> {
|
||||
pub heap: &'a mut Heap,
|
||||
pub focus: usize,
|
||||
}
|
||||
|
||||
impl<'a> FocusedHeapRefMut<'a> {
|
||||
#[inline]
|
||||
pub fn from(heap: &'a mut Heap, focus: usize) -> Self {
|
||||
Self { heap, focus }
|
||||
}
|
||||
|
||||
pub fn predicate_key(&self, term_loc: usize) -> Option<PredicateKey> {
|
||||
term_predicate_key(self.heap, term_loc)
|
||||
}
|
||||
|
||||
pub fn arity(&self, term_loc: usize) -> usize {
|
||||
self.predicate_key(term_loc)
|
||||
.map(|(_, arity)| arity)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn deref_loc(&self, term_loc: usize) -> HeapCellValue {
|
||||
let cell = self.heap[term_loc];
|
||||
heap_bound_store(self.heap, heap_bound_deref(self.heap, cell))
|
||||
}
|
||||
|
||||
pub fn nth_arg(&self, term_loc: usize, n: usize) -> Option<usize> {
|
||||
term_nth_arg(self.heap, term_loc, n)
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn from_cell(heap: &'a mut Heap, cell: HeapCellValue) -> Self {
|
||||
let focus = read_heap_cell!(cell,
|
||||
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => {
|
||||
h
|
||||
}
|
||||
_ => {
|
||||
let h = heap.len();
|
||||
heap.push_cell(cell).unwrap();
|
||||
|
||||
h
|
||||
}
|
||||
);
|
||||
|
||||
Self { heap, focus }
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
use crate::arena::F64Ptr;
|
||||
use crate::arena::TypedArenaPtr;
|
||||
use lexical::{FromLexical, parse};
|
||||
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::machine::heap::*;
|
||||
pub use crate::machine::machine_state::*;
|
||||
use crate::parser::ast::*;
|
||||
use crate::parser::char_reader::*;
|
||||
use crate::parser::dashu::Integer;
|
||||
use crate::types::*;
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
@@ -35,7 +32,7 @@ struct LayoutInfo {
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Token {
|
||||
Literal(HeapCellValue),
|
||||
Literal(Literal),
|
||||
Var(String),
|
||||
String(String),
|
||||
Open, // '('
|
||||
@@ -51,26 +48,6 @@ pub enum Token {
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub(super) fn byte_size(&self, flags: MachineFlags) -> usize {
|
||||
match self {
|
||||
Token::String(string) if flags.double_quotes.is_codes() => {
|
||||
2 * string.chars().count() + 1
|
||||
}
|
||||
Token::String(string) => Heap::compute_pstr_size(&string),
|
||||
Token::Literal(_)
|
||||
| Token::Comma
|
||||
| Token::HeadTailSeparator
|
||||
| Token::Open
|
||||
| Token::OpenCT
|
||||
| Token::OpenCurly
|
||||
| Token::OpenList
|
||||
| Token::Var(_) => {
|
||||
heap_index!(1)
|
||||
}
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn is_end(&self) -> bool {
|
||||
matches!(self, Token::End)
|
||||
@@ -126,14 +103,14 @@ macro_rules! try_nt {
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) struct LexerParser<'a, R> {
|
||||
pub(crate) struct Lexer<'a, R> {
|
||||
pub(crate) reader: R,
|
||||
pub(crate) machine_st: &'a mut MachineState,
|
||||
pub(crate) line_num: usize,
|
||||
pub(crate) col_num: usize,
|
||||
}
|
||||
|
||||
impl<'a, R: fmt::Debug> fmt::Debug for LexerParser<'a, R> {
|
||||
impl<'a, R: fmt::Debug> fmt::Debug for Lexer<'a, R> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("LexerParser")
|
||||
.field("reader", &"&'a mut R") // Hacky solution.
|
||||
@@ -143,9 +120,9 @@ impl<'a, R: fmt::Debug> fmt::Debug for LexerParser<'a, R> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
impl<'a, R: CharRead> Lexer<'a, R> {
|
||||
pub fn new(src: R, machine_st: &'a mut MachineState) -> Self {
|
||||
LexerParser {
|
||||
Self {
|
||||
reader: src,
|
||||
machine_st,
|
||||
line_num: 0,
|
||||
@@ -156,14 +133,14 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
pub fn lookahead_char(&mut self) -> Result<char, ParserError> {
|
||||
match self.reader.peek_char() {
|
||||
Some(Ok(c)) => Ok(c),
|
||||
_ => Err(ParserError::unexpected_eof(self.loc_to_err_src())),
|
||||
_ => Err(ParserError::unexpected_eof()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_char(&mut self) -> Result<char, ParserError> {
|
||||
match self.reader.read_char() {
|
||||
Some(Ok(c)) => Ok(c),
|
||||
_ => Err(ParserError::unexpected_eof(self.loc_to_err_src())),
|
||||
_ => Err(ParserError::unexpected_eof()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,7 +215,10 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
match comment_loop() {
|
||||
Err(e) if e.is_unexpected_eof() => {
|
||||
return Err(ParserError::IncompleteReduction(self.loc_to_err_src()));
|
||||
return Err(ParserError::IncompleteReduction(
|
||||
self.line_num,
|
||||
self.col_num,
|
||||
));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
@@ -250,7 +230,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
self.skip_char(c);
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(ParserError::NonPrologChar(self.loc_to_err_src()))
|
||||
Err(ParserError::NonPrologChar(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
self.return_char('/');
|
||||
@@ -267,7 +247,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
if !back_quote_char!(c2) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()))
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char(c2);
|
||||
Ok(c2)
|
||||
@@ -292,7 +272,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
Ok(None)
|
||||
} else {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()))
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
self.get_back_quoted_char().map(Some)
|
||||
@@ -314,10 +294,10 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
self.skip_char(c);
|
||||
Ok(token)
|
||||
} else {
|
||||
Err(ParserError::MissingQuote(self.loc_to_err_src()))
|
||||
Err(ParserError::MissingQuote(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()))
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,7 +328,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
if !single_quote_char!(c2) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()))
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char(c2);
|
||||
Ok(c2)
|
||||
@@ -389,7 +369,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
if !double_quote_char!(c2) {
|
||||
self.return_char(c);
|
||||
Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()))
|
||||
Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num))
|
||||
} else {
|
||||
self.skip_char(c2);
|
||||
Ok(c2)
|
||||
@@ -413,7 +393,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
't' => '\t',
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
c => return Err(ParserError::UnexpectedChar(c, self.loc_to_err_src())),
|
||||
c => return Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num)),
|
||||
};
|
||||
|
||||
self.skip_char(c);
|
||||
@@ -431,7 +411,10 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
if hexadecimal_digit_char!(c) {
|
||||
self.escape_sequence_to_char(|c| hexadecimal_digit_char!(c), 16)
|
||||
} else {
|
||||
Err(ParserError::IncompleteReduction(self.loc_to_err_src()))
|
||||
Err(ParserError::IncompleteReduction(
|
||||
self.line_num,
|
||||
self.col_num,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,11 +440,17 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
if backslash_char!(c) {
|
||||
self.skip_char(c);
|
||||
u32::from_str_radix(&token, radix).map_or_else(
|
||||
|_| Err(ParserError::ParseBigInt(self.loc_to_err_src())),
|
||||
|n| char::try_from(n).map_err(|_| ParserError::Utf8Error(self.loc_to_err_src())),
|
||||
|_| Err(ParserError::ParseBigInt(self.line_num, self.col_num)),
|
||||
|n| {
|
||||
char::try_from(n)
|
||||
.map_err(|_| ParserError::Utf8Error(self.line_num, self.col_num))
|
||||
},
|
||||
)
|
||||
} else {
|
||||
Err(ParserError::IncompleteReduction(self.loc_to_err_src()))
|
||||
Err(ParserError::IncompleteReduction(
|
||||
self.line_num,
|
||||
self.col_num,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -473,7 +462,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
Ok(c)
|
||||
} else {
|
||||
if !backslash_char!(c) {
|
||||
return Err(ParserError::UnexpectedChar(c, self.loc_to_err_src()));
|
||||
return Err(ParserError::UnexpectedChar(c, self.line_num, self.col_num));
|
||||
}
|
||||
|
||||
self.skip_char(c);
|
||||
@@ -504,7 +493,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
self.skip_char(c);
|
||||
Ok(token)
|
||||
} else {
|
||||
Err(ParserError::MissingQuote(self.loc_to_err_src()))
|
||||
Err(ParserError::MissingQuote(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,7 +518,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
.map(NumberToken::Number)
|
||||
} else {
|
||||
self.return_char(start);
|
||||
Err(ParserError::ParseBigInt(self.loc_to_err_src()))
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -554,7 +543,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
.map(NumberToken::Number)
|
||||
} else {
|
||||
self.return_char(start);
|
||||
Err(ParserError::ParseBigInt(self.loc_to_err_src()))
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,7 +568,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
.map(NumberToken::Number)
|
||||
} else {
|
||||
self.return_char(start);
|
||||
Err(ParserError::ParseBigInt(self.loc_to_err_src()))
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -646,42 +635,37 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
if !token.is_empty() && token.chars().nth(1).is_none() {
|
||||
if let Some(c) = token.chars().next() {
|
||||
return Ok(Token::Literal(char_as_cell!(c)));
|
||||
return Ok(Token::Literal(Literal::Atom(
|
||||
AtomCell::new_char_inlined(c).get_name(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ParserError::InvalidSingleQuotedCharacter(
|
||||
self.loc_to_err_src(),
|
||||
));
|
||||
return Err(ParserError::InvalidSingleQuotedCharacter(c));
|
||||
}
|
||||
} else {
|
||||
match self.get_back_quoted_string() {
|
||||
Ok(_) => return Err(ParserError::BackQuotedString(self.loc_to_err_src())),
|
||||
Ok(_) => return Err(ParserError::BackQuotedString(self.line_num, self.col_num)),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
if token.as_str() == "[]" {
|
||||
Ok(Token::Literal(empty_list_as_cell!()))
|
||||
Ok(Token::Literal(Literal::Atom(atom!("[]"))))
|
||||
} else {
|
||||
Ok(Token::Literal(atom_as_cell!(AtomTable::build_with(
|
||||
Ok(Token::Literal(Literal::Atom(AtomTable::build_with(
|
||||
&self.machine_st.atom_tbl,
|
||||
&token,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_lossy_wrapper<T: FromLexical>(&self, token: &str) -> Result<T, ParserError> {
|
||||
match parse::<T, _>(token.as_bytes()) {
|
||||
Ok(n) => Ok(n),
|
||||
Err(_) => return Err(ParserError::LexicalError(self.loc_to_err_src())),
|
||||
}
|
||||
}
|
||||
|
||||
fn vacate_with_float(&mut self, mut token: String) -> Result<Token, ParserError> {
|
||||
self.return_char(token.pop().unwrap());
|
||||
let n = self.parse_lossy_wrapper::<f64>(&token)?;
|
||||
Ok(Token::Literal(HeapCellValue::from(float_alloc!(
|
||||
|
||||
let n = parse_float_lossy(&token)?;
|
||||
|
||||
Ok(Token::Literal(Literal::from(float_alloc!(
|
||||
n,
|
||||
self.machine_st.arena
|
||||
))))
|
||||
@@ -698,7 +682,7 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
if decimal_digit_char!(c) {
|
||||
Ok(c)
|
||||
} else {
|
||||
Err(ParserError::ParseBigInt(self.loc_to_err_src()))
|
||||
Err(ParserError::ParseBigInt(self.line_num, self.col_num))
|
||||
}
|
||||
} else {
|
||||
Ok(c)
|
||||
@@ -810,8 +794,8 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
}
|
||||
}
|
||||
|
||||
let n = self.parse_lossy_wrapper::<f64>(&token)?;
|
||||
Ok(Token::Literal(HeapCellValue::from(float_alloc!(
|
||||
let n = parse_float_lossy(&token)?;
|
||||
Ok(Token::Literal(Literal::from(float_alloc!(
|
||||
n,
|
||||
self.machine_st.arena
|
||||
))))
|
||||
@@ -819,8 +803,8 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
return self.vacate_with_float(token).map(NumberToken::Number);
|
||||
}
|
||||
} else {
|
||||
let n = self.parse_lossy_wrapper::<f64>(&token)?;
|
||||
Ok(Token::Literal(HeapCellValue::from(float_alloc!(
|
||||
let n = parse_float_lossy(&token)?;
|
||||
Ok(Token::Literal(Literal::from(float_alloc!(
|
||||
n,
|
||||
self.machine_st.arena
|
||||
))))
|
||||
@@ -1057,14 +1041,14 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
|
||||
return if let DoubleQuotes::Atom = self.machine_st.flags.double_quotes {
|
||||
let atom = AtomTable::build_with(&self.machine_st.atom_tbl, &s);
|
||||
Ok(Token::Literal(atom_as_cell!(atom)))
|
||||
Ok(Token::Literal(Literal::Atom(atom)))
|
||||
} else {
|
||||
Ok(Token::String(s))
|
||||
};
|
||||
}
|
||||
|
||||
if c == '\u{0}' {
|
||||
return Err(ParserError::unexpected_eof(self.loc_to_err_src()));
|
||||
return Err(ParserError::unexpected_eof());
|
||||
}
|
||||
|
||||
self.name_token(c)
|
||||
@@ -1073,3 +1057,13 @@ impl<'a, R: CharRead> LexerParser<'a, R> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_float_lossy(token: &str) -> Result<f64, ParserError> {
|
||||
const FORMAT: u128 = lexical::format::STANDARD;
|
||||
let options = lexical::ParseFloatOptions::builder()
|
||||
.lossy(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
let n = lexical::parse_with_options::<f64, _, FORMAT>(token.as_bytes(), &options)?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user