variable revision
This commit is contained in:
@@ -9,12 +9,10 @@ use crate::machine::machine_indices::*;
|
||||
use crate::machine::machine_state::*;
|
||||
use crate::types::*;
|
||||
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::hash::Hash;
|
||||
use std::io::{Error as IOError, ErrorKind};
|
||||
use std::ops::{Deref, Neg, RangeBounds};
|
||||
use std::ops::Neg;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::vec::Vec;
|
||||
@@ -311,26 +309,11 @@ macro_rules! temp_v {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum GenContext {
|
||||
Head,
|
||||
Mid(usize),
|
||||
Last(usize), // Mid & Last: chunk_num
|
||||
}
|
||||
|
||||
impl GenContext {
|
||||
#[inline]
|
||||
pub fn chunk_num(self) -> usize {
|
||||
match self {
|
||||
GenContext::Head => 0,
|
||||
GenContext::Mid(cn) | GenContext::Last(cn) => cn,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_last(self) -> bool {
|
||||
matches!(self, GenContext::Last(_))
|
||||
}
|
||||
#[macro_export]
|
||||
macro_rules! perm_v {
|
||||
($x:expr) => {
|
||||
$crate::parser::ast::RegType::Perm($x)
|
||||
};
|
||||
}
|
||||
|
||||
#[bitfield]
|
||||
@@ -426,7 +409,7 @@ pub fn default_op_dir() -> OpDir {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ArithmeticError {
|
||||
NonEvaluableFunctor(Literal, usize),
|
||||
NonEvaluableFunctor(HeapCellValue, usize),
|
||||
UninstantiatedVar,
|
||||
}
|
||||
|
||||
@@ -685,111 +668,7 @@ impl Literal {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct VarPtr(Rc<RefCell<Var>>);
|
||||
|
||||
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]
|
||||
pub(crate) fn is_anon(&self) -> bool {
|
||||
match *self.borrow() {
|
||||
Var::Anon | Var::Generated { is_anon: true, .. } => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[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 {
|
||||
Anon,
|
||||
Generated { is_anon: bool, var_num: usize },
|
||||
InSitu(usize),
|
||||
Named(String),
|
||||
}
|
||||
|
||||
impl From<String> for Var {
|
||||
#[inline(always)]
|
||||
fn from(value: String) -> Var {
|
||||
Var::Named(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Var {
|
||||
#[inline(always)]
|
||||
fn from(value: &str) -> Var {
|
||||
Var::Named(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl Var {
|
||||
#[allow(clippy::inherent_to_string)]
|
||||
#[inline(always)]
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
Var::Anon => "_".to_owned(),
|
||||
Var::InSitu(var_num) | Var::Generated { var_num, .. } => format!("_{}", var_num),
|
||||
Var::Named(value) => value.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub type Var = Rc<String>;
|
||||
|
||||
pub(crate) fn subterm_index(heap: &[HeapCellValue], subterm_loc: usize) -> (usize, HeapCellValue) {
|
||||
let subterm = heap[subterm_loc];
|
||||
@@ -1050,7 +929,7 @@ pub fn term_arity(heap: &[HeapCellValue], mut term_loc: usize) -> usize {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn var_locs_from_iter<I: Iterator<Item = HeapCellValue>>(iter: I) -> VarLocs {
|
||||
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());
|
||||
|
||||
@@ -1061,21 +940,20 @@ pub fn var_locs_from_iter<I: Iterator<Item = HeapCellValue>>(iter: I) -> VarLocs
|
||||
}
|
||||
}
|
||||
|
||||
VarLocs(
|
||||
occurrence_set
|
||||
.into_iter()
|
||||
.map(|(var, count)| {
|
||||
let key = var.get_value() as usize;
|
||||
let queue = if count > 1 {
|
||||
(0 .. count).map(|_| VarPtr::from(format!("_{}", key))).collect()
|
||||
} else {
|
||||
(0 .. count).map(|_| VarPtr::from(Var::Anon)).collect()
|
||||
};
|
||||
let mut inverse_var_locs = InverseVarLocs::default();
|
||||
|
||||
(key, queue)
|
||||
})
|
||||
.collect()
|
||||
)
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1137,82 +1015,14 @@ pub fn term_nth_arg(heap: &[HeapCellValue], mut term_loc: usize, n: usize) -> Op
|
||||
}
|
||||
}
|
||||
|
||||
pub type VarNamesToLocs = IndexMap<String, HeapCellValue, FxBuildHasher>;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct VarLocs(IndexMap<usize, VecDeque<VarPtr>, FxBuildHasher>);
|
||||
|
||||
impl VarLocs {
|
||||
pub fn get(&self, key: usize) -> Option<&VarPtr> {
|
||||
self.0.get(&key)
|
||||
.and_then(|queue| {
|
||||
queue.front()
|
||||
})
|
||||
}
|
||||
|
||||
// if a queue of VarPtr's is stored at location key, pop the front
|
||||
// if it exists and pass it along to wrapper, returning a value of
|
||||
// type R. A return value of None indicates that the key doesn't
|
||||
// exist (the map containing a key necessarily means its queue
|
||||
// value is non-empty).
|
||||
fn rotate_latest_mut<R>(
|
||||
&mut self,
|
||||
key: usize,
|
||||
wrapper: impl FnOnce(&VarPtr) -> R,
|
||||
) -> Option<R> {
|
||||
self.0.get_mut(&key)
|
||||
.and_then(move |queue| {
|
||||
if let Some(var_ptr) = queue.pop_front() {
|
||||
let result = wrapper(&var_ptr);
|
||||
queue.push_back(var_ptr);
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn peek_next_var_ptr_at_key(&self, key: usize) -> Option<&VarPtr> {
|
||||
self.0.get(&key).and_then(|queue| queue.front())
|
||||
}
|
||||
|
||||
pub fn read_next_var_ptr_at_key(&mut self, key: usize) -> Option<VarPtr> {
|
||||
self.rotate_latest_mut(key, VarPtr::clone)
|
||||
}
|
||||
|
||||
pub fn push_at_key(&mut self, key: usize, var_ptr: VarPtr) {
|
||||
let entry = self.0.entry(key).or_default();
|
||||
entry.push_back(var_ptr);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iter(&self) -> impl Iterator<Item = (usize, &VecDeque<VarPtr>)> {
|
||||
self.0.iter().map(|(&k, v)| (k, v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn drain<R>(&mut self, range: R) -> indexmap::map::Drain<usize, VecDeque<VarPtr>>
|
||||
where R: RangeBounds<usize>
|
||||
{
|
||||
self.0.drain(range)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert(&mut self, key: usize, var_ptrs: VecDeque<VarPtr>) {
|
||||
self.0.insert(key, var_ptrs);
|
||||
}
|
||||
}
|
||||
pub type VarLocs = IndexMap<Var, HeapCellValue, FxBuildHasher>;
|
||||
pub type InverseVarLocs = IndexMap<usize, Var, FxBuildHasher>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FocusedHeap {
|
||||
pub heap: Vec<HeapCellValue>,
|
||||
pub focus: usize,
|
||||
pub var_locs: VarLocs,
|
||||
pub inverse_var_locs: InverseVarLocs,
|
||||
}
|
||||
|
||||
impl FocusedHeap {
|
||||
@@ -1220,7 +1030,7 @@ impl FocusedHeap {
|
||||
Self {
|
||||
heap: vec![],
|
||||
focus: 0,
|
||||
var_locs: VarLocs::default(),
|
||||
inverse_var_locs: InverseVarLocs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1251,7 +1061,6 @@ impl FocusedHeap {
|
||||
FocusedHeapRefMut {
|
||||
heap: &mut self.heap,
|
||||
focus,
|
||||
// var_locs: &self.var_locs,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,9 @@ use crate::parser::char_reader::*;
|
||||
use crate::parser::lexer::*;
|
||||
use crate::types::*;
|
||||
|
||||
use fxhash::FxBuildHasher;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use std::mem;
|
||||
use std::ops::Neg;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum TokenType {
|
||||
@@ -184,7 +182,7 @@ pub struct Parser<'a, R> {
|
||||
stack: Vec<TokenDesc>,
|
||||
terms: Vec<HeapCellValue>,
|
||||
var_locs: VarLocs,
|
||||
var_names_to_locs: VarNamesToLocs,
|
||||
inverse_var_locs: InverseVarLocs,
|
||||
}
|
||||
|
||||
fn read_tokens<R: CharRead>(lexer: &mut Lexer<R>) -> Result<Vec<Token>, ParserError> {
|
||||
@@ -326,7 +324,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
|
||||
stack: vec![],
|
||||
terms: vec![],
|
||||
var_locs: VarLocs::default(),
|
||||
var_names_to_locs: IndexMap::with_hasher(FxBuildHasher::default()),
|
||||
inverse_var_locs: InverseVarLocs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,7 +335,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
|
||||
stack: vec![],
|
||||
terms: vec![],
|
||||
var_locs: VarLocs::default(),
|
||||
var_names_to_locs: IndexMap::with_hasher(FxBuildHasher::default()),
|
||||
inverse_var_locs: InverseVarLocs::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,32 +499,28 @@ impl<'a, R: CharRead> Parser<'a, R> {
|
||||
self.terms.push(HeapCellValue::from(c));
|
||||
TokenType::Term { heap_loc }
|
||||
}
|
||||
Token::Var(var_string) => match self.var_names_to_locs.get(&var_string).cloned() {
|
||||
Some(heap_loc) => {
|
||||
let heap_idx = heap_loc.get_value() as usize;
|
||||
Token::Var(var_string) => {
|
||||
let var = Rc::new(var_string);
|
||||
|
||||
self.var_locs.push_at_key(heap_idx, VarPtr::from(var_string));
|
||||
self.terms.push(heap_loc);
|
||||
|
||||
TokenType::Term { heap_loc }
|
||||
}
|
||||
None => {
|
||||
self.terms.push(heap_loc);
|
||||
|
||||
if var_string.trim() != "_" {
|
||||
self.var_names_to_locs.insert(var_string.clone(), heap_loc);
|
||||
match self.var_locs.get(&var).cloned() {
|
||||
Some(heap_loc) => {
|
||||
self.terms.push(heap_loc);
|
||||
TokenType::Term { heap_loc }
|
||||
}
|
||||
None => {
|
||||
self.terms.push(heap_loc);
|
||||
|
||||
self.var_locs.push_at_key(
|
||||
heap_loc.get_value() as usize,
|
||||
if var_string.trim() == "_" {
|
||||
VarPtr::from(Var::Anon)
|
||||
} else {
|
||||
VarPtr::from(var_string)
|
||||
},
|
||||
);
|
||||
// if var_string == "_", it not being present
|
||||
// as a key of self.var_locs means it is
|
||||
// anonymous.
|
||||
|
||||
TokenType::Term { heap_loc }
|
||||
if var.trim() != "_" {
|
||||
self.var_locs.insert(var.clone(), heap_loc);
|
||||
self.inverse_var_locs.insert(heap_loc.get_value() as usize, var);
|
||||
}
|
||||
|
||||
TokenType::Term { heap_loc }
|
||||
}
|
||||
}
|
||||
},
|
||||
Token::Comma => TokenType::Comma,
|
||||
@@ -755,7 +749,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.stack.clear();
|
||||
self.var_names_to_locs.clear();
|
||||
self.var_locs.clear();
|
||||
}
|
||||
|
||||
fn expand_comma_compacted_terms(&mut self, index: usize) -> usize {
|
||||
@@ -1356,7 +1350,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
|
||||
}) => Ok(FocusedHeap {
|
||||
heap: mem::replace(&mut self.terms, vec![]),
|
||||
focus: heap_loc.get_value() as usize,
|
||||
var_locs: mem::replace(&mut self.var_locs, VarLocs::default()),
|
||||
inverse_var_locs: mem::replace(&mut self.inverse_var_locs, InverseVarLocs::default()),
|
||||
}),
|
||||
_ => Err(ParserError::IncompleteReduction(
|
||||
self.lexer.loc_to_err_src(),
|
||||
|
||||
Reference in New Issue
Block a user