eliminate Addr::CharCode (#519), fix atom_code panic (#521), fix failure involving lists of character codes (#520), bump version number

This commit is contained in:
Mark Thom
2020-05-17 15:19:27 -06:00
parent 9f3351469d
commit 5720b7b94c
12 changed files with 75 additions and 243 deletions

6
Cargo.lock generated
View File

@@ -606,9 +606,7 @@ dependencies = [
[[package]]
name = "prolog_parser"
version = "0.8.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2283b7263459418cff68af1923406bd9e4b5b9d2830df665a0fbe22e27a628d"
version = "0.8.58"
dependencies = [
"lexical",
"num-rug-adapter",
@@ -740,7 +738,7 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scryer-prolog"
version = "0.8.121"
version = "0.8.122"
dependencies = [
"cpu-time",
"crossterm",

View File

@@ -1,6 +1,6 @@
[package]
name = "scryer-prolog"
version = "0.8.121"
version = "0.8.122"
authors = ["Mark Thom <markjordanthom@gmail.com>"]
build = "build.rs"
repository = "https://github.com/mthom/scryer-prolog"
@@ -29,7 +29,7 @@ libc = "0.2.62"
nix = "0.15.0"
num-rug-adapter = { optional = true, version = "0.1.3" }
ordered-float = "0.5.0"
prolog_parser = { version = "0.8.57", default-features = false }
prolog_parser = { version = "0.8.58", default-features = false }
ref_thread_local = "0.0.0"
rug = { version = "1.4.0", optional = true }
rustyline = "6.0.0"

View File

@@ -722,9 +722,6 @@ impl<'a> TryFrom<(Addr, &'a Heap)> for Number {
fn try_from((addr, heap): (Addr, &'a Heap)) -> Result<Number, Self::Error> {
match addr {
Addr::CharCode(c) => {
Ok(Number::from(c as isize))
}
Addr::Fixnum(n) => {
Ok(Number::from(n))
}
@@ -755,9 +752,6 @@ impl<'a> TryFrom<&'a HeapCellValue> for Number {
match value {
HeapCellValue::Addr(addr) => {
match addr {
&Addr::CharCode(c) => {
Ok(Number::from(c as isize))
}
&Addr::Fixnum(n) => {
Ok(Number::from(n))
}

View File

@@ -12,7 +12,6 @@ use indexmap::IndexMap;
use std::cell::Cell;
use std::collections::VecDeque;
use std::convert::TryFrom;
use std::path::PathBuf;
use std::rc::Rc;
@@ -649,21 +648,6 @@ impl Into<HeapCellValue> for Number {
impl Number {
#[inline]
pub fn to_u32(&self) -> Option<u32> {
match self {
&Number::Fixnum(n) => u32::try_from(n).ok(),
&Number::Integer(ref n) => n.to_u32(),
&Number::Float(_) => None,
&Number::Rational(ref r) =>
if r.denom() == &1 {
r.numer().to_u32()
} else {
None
}
}
}
#[inline]
pub fn is_positive(&self) -> bool {
match self {

View File

@@ -1469,9 +1469,6 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
});
}
}
&HeapCellValue::Addr(Addr::CharCode(c)) => {
self.append_str(&format!("{}", c as u32));
}
&HeapCellValue::Addr(Addr::Char(c)) => {
self.print_char(self.quoted, c);
}

View File

@@ -223,9 +223,6 @@ impl MachineState {
}
}
}
&HeapCellValue::Addr(Addr::CharCode(n)) => {
interms.push(Number::Integer(Rc::new(Integer::from(n))));
}
&HeapCellValue::Addr(Addr::Fixnum(n)) => {
interms.push(Number::Fixnum(n));
}

View File

@@ -216,9 +216,6 @@ impl<T: RawBlockTraits> HeapTemplate<T> {
Constant::Char(c) => {
Addr::Char(c)
}
Constant::CharCode(c) => {
Addr::CharCode(c)
}
Constant::EmptyList => {
Addr::EmptyList
}

View File

@@ -58,7 +58,6 @@ pub enum TermOrderCategory {
pub enum Addr {
AttrVar(usize),
Char(char),
CharCode(u32),
Con(usize),
CutPoint(usize),
EmptyList,
@@ -160,7 +159,7 @@ impl Addr {
#[inline]
pub fn is_heap_bound(&self) -> bool {
match self {
Addr::Char(_) | Addr::CharCode(_) | Addr::EmptyList |
Addr::Char(_) | Addr::EmptyList |
Addr::CutPoint(_) | Addr::Usize(_) | Addr::Fixnum(_) |
Addr::Float(_) => {
false
@@ -226,7 +225,7 @@ impl Addr {
Addr::Char(_) | Addr::EmptyList => {
Some(TermOrderCategory::Atom)
}
Addr::CharCode(_) | Addr::Fixnum(_) | Addr::Usize(_) => {
Addr::Fixnum(_) | Addr::Usize(_) => {
Some(TermOrderCategory::Integer)
}
Addr::Lis(_) | Addr::PStrLocation(..) | Addr::Str(_) => {
@@ -245,9 +244,6 @@ impl Addr {
&Addr::Char(c) => {
Some(Constant::Char(c))
}
&Addr::CharCode(c) => {
Some(Constant::CharCode(c))
}
&Addr::Con(h) => {
match &machine_st.heap[h] {
&HeapCellValue::Atom(ref name, _) if name.is_char() => {

View File

@@ -1411,7 +1411,7 @@ impl MachineState {
c
}
}
Addr::Char(_) | Addr::CharCode(_) | Addr::Con(_) | Addr::CutPoint(_) |
Addr::Char(_) | Addr::Con(_) | Addr::CutPoint(_) |
Addr::EmptyList | Addr::Fixnum(_) | Addr::Float(_) | Addr::Usize(_) => {
c
}
@@ -1956,17 +1956,6 @@ impl MachineState {
}
}
}
(Addr::CharCode(n1), v2) | (v2, Addr::CharCode(n1)) => {
if let Ok(n2) = Number::try_from((v2, &self.heap)) {
if let Some(n2) = n2.to_u32() {
if n1 == n2 {
continue;
}
}
}
return true;
}
(a1, a2) => {
if let Ok(n1) = Number::try_from((a1, &self.heap)) {
if let Ok(n2) = Number::try_from((a2, &self.heap)) {
@@ -2136,42 +2125,6 @@ impl MachineState {
unreachable!()
}
}
(
Addr::Con(h),
Addr::CharCode(c),
) => {
let c = std::char::from_u32(c).unwrap();
if let HeapCellValue::Atom(ref n1, _) = &self.heap[h] {
if n1.is_char() {
if n1.as_str().chars().next() != Some(c) {
return Some(n1.as_str().chars().next().cmp(&Some(c)));
}
} else {
return Some(Ordering::Greater);
}
} else {
unreachable!()
}
}
(
Addr::CharCode(c),
Addr::Con(h),
) => {
let c = std::char::from_u32(c).unwrap();
if let HeapCellValue::Atom(ref n1, _) = &self.heap[h] {
if n1.is_char() {
if n1.as_str().chars().next() != Some(c) {
return Some(Some(c).cmp(&n1.as_str().chars().next()));
}
} else {
return Some(Ordering::Less);
}
} else {
unreachable!()
}
}
(
Addr::EmptyList,
Addr::Con(h),
@@ -2204,34 +2157,6 @@ impl MachineState {
return Some(c1.cmp(&c2));
}
}
(
Addr::CharCode(c1),
Addr::CharCode(c2),
) => {
if c1 != c2 {
return Some(c1.cmp(&c2));
}
}
(
Addr::Char(c1),
Addr::CharCode(c2),
) => {
let c2 = std::char::from_u32(c2).unwrap();
if c1 != c2 {
return Some(c1.cmp(&c2));
}
}
(
Addr::CharCode(c1),
Addr::Char(c2),
) => {
let c1 = std::char::from_u32(c1).unwrap();
if c1 != c2 {
return Some(c1.cmp(&c2));
}
}
(
Addr::Char(c),
Addr::EmptyList,
@@ -2252,30 +2177,6 @@ impl MachineState {
Some('['.cmp(&c))
};
}
(
Addr::CharCode(c),
Addr::EmptyList,
) => {
let c = std::char::from_u32(c).unwrap();
return if c == '[' {
Some(Ordering::Less)
} else {
Some(c.cmp(&'['))
};
}
(
Addr::EmptyList,
Addr::CharCode(c),
) => {
let c = std::char::from_u32(c).unwrap();
return if c == '[' {
Some(Ordering::Greater)
} else {
Some('['.cmp(&c))
};
}
(
Addr::EmptyList,
Addr::EmptyList,
@@ -2443,7 +2344,6 @@ impl MachineState {
match d {
Addr::Char(_) |
Addr::CharCode(_) |
Addr::Con(_) |
Addr::EmptyList |
Addr::Fixnum(_) |
@@ -2471,9 +2371,6 @@ impl MachineState {
}
_ => {
match d {
Addr::CharCode(_) => {
self.p += 1;
}
Addr::Char(_) if self.flags.double_quotes.is_codes() => {
self.p += 1;
}
@@ -2601,7 +2498,7 @@ impl MachineState {
Addr::Stream(_) => {
self.fail = true;
}
Addr::Char(_) | Addr::CharCode(_) | Addr::Con(_) | Addr::Fixnum(_) |
Addr::Char(_) | Addr::Con(_) | Addr::Fixnum(_) |
Addr::Float(_) | Addr::EmptyList | Addr::Usize(_) => {
self.try_functor_unify_components(a1, 0);
}
@@ -2637,9 +2534,6 @@ impl MachineState {
},
_ =>
match arity {
Addr::CharCode(c) => {
Some(c as isize)
}
arity => {
return Err(
self.error_form(
@@ -2681,7 +2575,7 @@ impl MachineState {
}
match name {
Addr::Char(_) | Addr::CharCode(_) | Addr::Con(_) | Addr::Fixnum(_) | Addr::Float(_) |
Addr::Char(_) | Addr::Con(_) | Addr::Fixnum(_) | Addr::Float(_) |
Addr::EmptyList | Addr::PStrLocation(..) | Addr::Usize(_) if arity == 0 => {
self.unify(a1, name);
}

View File

@@ -508,8 +508,11 @@ impl MachineState {
n: &Integer,
stub: &'static str,
arity: usize,
) -> Result<u32, MachineStub> {
if let Some(c) = n.to_u32() {
) -> Result<char, MachineStub>
{
let c = n.to_u32().and_then(std::char::from_u32);
if let Some(c) = c {
Ok(c)
} else {
let stub = MachineError::functor_stub(clause_name!(stub), arity);
@@ -592,9 +595,6 @@ impl MachineState {
let addr = self.heap.put_constant(Constant::Fixnum(n));
self.unify(nx, addr);
}
Ok(Term::Constant(_, Constant::CharCode(c))) => {
self.unify(nx, Addr::CharCode(c))
}
_ => {
let err = ParserError::ParseBigInt(0, 0);
@@ -909,7 +909,7 @@ impl MachineState {
match self.store(self.deref(a1)) {
Addr::Char(c) => {
let iter = once(Addr::CharCode(c as u32));
let iter = once(Addr::Fixnum(c as isize));
let list_of_codes = Addr::HeapCell(self.heap.to_list(iter));
let a2 = self[temp_v!(2)];
@@ -938,7 +938,7 @@ impl MachineState {
let iter = name
.as_str()
.chars()
.map(|c| Addr::CharCode(c as u32));
.map(|c| Addr::Fixnum(c as isize));
let list_of_codes = Addr::HeapCell(self.heap.to_list(iter));
@@ -951,8 +951,8 @@ impl MachineState {
}
Addr::EmptyList => {
let chars = vec![
Addr::CharCode('[' as u32),
Addr::CharCode(']' as u32),
Addr::Fixnum('[' as isize),
Addr::Fixnum(']' as isize),
];
let list_of_codes = Addr::HeapCell(self.heap.to_list(chars.into_iter()));
@@ -984,7 +984,7 @@ impl MachineState {
2,
)?;
chars.push(std::char::from_u32(c).unwrap());
chars.push(c);
}
}
@@ -992,18 +992,10 @@ impl MachineState {
}
Ok(Number::Integer(n)) => {
let c = self.int_to_char_code(&n, "atom_codes", 2)?;
chars.push(std::char::from_u32(c).unwrap());
chars.push(c);
continue;
}
_ => {
}
}
match addr {
Addr::CharCode(c) => {
chars.push(std::char::from_u32(c).unwrap());
}
_ => {
let stub = MachineError::functor_stub(
clause_name!("atom_codes"),
@@ -1020,11 +1012,6 @@ impl MachineState {
}
}
}
let chars = clause_name!(chars, indices.atom_tbl);
let chars = self.heap.to_unifiable(HeapCellValue::Atom(chars, None));
self.unify(addr, chars);
}
}
}
@@ -1429,14 +1416,15 @@ impl MachineState {
addr if addr.is_ref() => {
addr
}
Addr::CharCode(d) => {
Addr::CharCode(d)
}
addr => {
match Number::try_from((addr, &self.heap)) {
Ok(Number::Integer(n)) => {
if let Some(c) = n.to_u32().and_then(|c| char::try_from(c).ok()) {
Addr::CharCode(c as u32)
let n = n.to_u32().and_then(|n| {
std::char::from_u32(n).and_then(|_| Some(n))
});
if let Some(n) = n {
Addr::Fixnum(n as isize)
} else {
return Err(self.representation_error(
RepFlag::InCharacterCode,
@@ -1446,8 +1434,12 @@ impl MachineState {
}
}
Ok(Number::Fixnum(n)) => {
if let Some(c) = u32::try_from(n).ok().and_then(|c| char::try_from(c).ok()) {
Addr::CharCode(c as u32)
let n = u32::try_from(n).ok().and_then(|n| {
std::char::from_u32(n).and_then(|_| Some(n))
});
if let Some(n) = n {
Addr::Fixnum(n as isize)
} else {
return Err(self.representation_error(
RepFlag::InCharacterCode,
@@ -1474,9 +1466,9 @@ impl MachineState {
match result.map_err(|e| e.kind()) {
Ok(c) => {
if let Some(var) = addr.as_var() {
self.bind(var, Addr::CharCode(c as u32));
self.bind(var, Addr::Fixnum(c as isize));
break;
} else if addr == Addr::CharCode(c as u32) {
} else if addr == Addr::Fixnum(c as isize) {
break;
} else {
self.fail = true;
@@ -1561,7 +1553,7 @@ impl MachineState {
let codes = string
.trim()
.chars()
.map(|c| Addr::CharCode(c as u32));
.map(|c| Addr::Fixnum(c as isize));
let codes_list = Addr::HeapCell(self.heap.to_list(codes));
@@ -1630,11 +1622,11 @@ impl MachineState {
};
let a2 = self[temp_v!(2)];
self.unify(Addr::CharCode(c as u32), a2);
self.unify(Addr::Fixnum(c as isize), a2);
}
Addr::Char(c) => {
let a2 = self[temp_v!(2)];
self.unify(Addr::CharCode(c as u32), a2);
self.unify(Addr::Fixnum(c as isize), a2);
}
addr if addr.is_ref() => {
let a2 = self[temp_v!(2)];
@@ -1647,24 +1639,13 @@ impl MachineState {
Ok(Number::Fixnum(n)) => {
self.int_to_char_code(&Integer::from(n), "char_code", 2)?
}
_ => {
match addr {
Addr::CharCode(c) => {
c
}
_ => {
self.fail = true;
return Ok(());
}
}
}
};
if let Some(c) = std::char::from_u32(c) {
self.unify(Addr::Char(c), addr);
} else {
self.fail = true;
}
}
_ => {
unreachable!();
@@ -1953,10 +1934,6 @@ impl MachineState {
return Err(self.error_form(err, stub));
}
Addr::CharCode(c) => {
let c = char::try_from(c).unwrap();
write!(&mut stream, "{}", c).unwrap();
}
addr => {
match Number::try_from((addr, &self.heap)) {
Ok(Number::Integer(n)) => {
@@ -2389,14 +2366,15 @@ impl MachineState {
addr if addr.is_ref() => {
addr
}
Addr::CharCode(d) => {
Addr::CharCode(d)
}
addr => {
match Number::try_from((addr, &self.heap)) {
Ok(Number::Integer(n)) => {
if let Some(c) = n.to_u32().and_then(|c| char::try_from(c).ok()) {
Addr::CharCode(c as u32)
let n = n.to_u32().and_then(|n| {
std::char::from_u32(n).and_then(|_| Some(n))
});
if let Some(n) = n {
Addr::Fixnum(n as isize)
} else {
return Err(self.representation_error(
RepFlag::InCharacterCode,
@@ -2406,8 +2384,12 @@ impl MachineState {
}
}
Ok(Number::Fixnum(n)) => {
if let Some(c) = u32::try_from(n).ok().and_then(|c| char::try_from(c).ok()) {
Addr::CharCode(c as u32)
let n = u32::try_from(n).ok().and_then(|n| {
std::char::from_u32(n).and_then(|_| Some(n))
});
if let Some(n) = n {
Addr::Fixnum(n as isize)
} else {
return Err(self.representation_error(
RepFlag::InCharacterCode,
@@ -2440,9 +2422,9 @@ impl MachineState {
match result {
Some(Ok(c)) => {
if let Some(var) = addr.as_var() {
self.bind(var, Addr::CharCode(c as u32));
self.bind(var, Addr::Fixnum(c as isize));
break;
} else if addr == Addr::CharCode(c as u32) {
} else if addr == Addr::Fixnum(c as isize) {
break;
} else {
self.fail = true;
@@ -4258,8 +4240,11 @@ impl MachineState {
let addr = self.store(self.deref(self[temp_v!(1)]));
match addr {
Addr::CharCode(c) => {
self.fail = match std::char::from_u32(c) {
Addr::Fixnum(n) => {
let n = u32::try_from(n).ok();
let n = n.and_then(std::char::from_u32);
self.fail = match n {
Some(c) => {
non_quoted_token(once(c))
}
@@ -4373,11 +4358,7 @@ impl MachineState {
&SystemClauseType::SetSeed => {
let seed = self.store(self.deref(self[temp_v!(1)]));
let seed = match seed {
Addr::CharCode(c) => {
Integer::from(c)
}
_ => {
let seed =
match Number::try_from((seed, &self.heap)) {
Ok(Number::Fixnum(n)) => {
Integer::from(n)
@@ -4393,8 +4374,6 @@ impl MachineState {
self.fail = true;
return Ok(());
}
}
}
};
let mut rand = RANDOM_STATE.borrow_mut();

View File

@@ -150,9 +150,6 @@ macro_rules! from_constant {
&Constant::Char(c) => {
HeapCellValue::Addr(Addr::Char(c))
}
&Constant::CharCode(c) => {
HeapCellValue::Addr(Addr::CharCode(c))
}
&Constant::Fixnum(n) => {
HeapCellValue::Addr(Addr::Fixnum(n))
}

View File

@@ -203,7 +203,6 @@ impl fmt::Display for Addr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Addr::Char(c) => write!(f, "Addr::Char({})", c),
&Addr::CharCode(c) => write!(f, "Addr::CharCode({})", c),
&Addr::EmptyList => write!(f, "Addr::EmptyList"),
&Addr::Fixnum(n) => write!(f, "Addr::Fixnum({})", n),
&Addr::Float(fl) => write!(f, "Addr::Float({})", fl),