Merge branch 'dashu' of https://github.com/coasys/scryer-prolog into coasys-dashu

This commit is contained in:
Mark
2023-07-24 11:47:57 -06:00
19 changed files with 279 additions and 149 deletions

View File

@@ -1,7 +1,7 @@
use crate::arena::*;
use crate::atom_table::*;
use crate::parser::ast::*;
use crate::parser::rug::{Integer, Rational};
use crate::parser::dashu::{Integer, Rational};
use crate::{
alpha_numeric_char, capital_letter_char, cut_char, decimal_digit_char, graphic_token_char,
is_fx, is_infix, is_postfix, is_prefix, is_xf, is_xfx, is_xfy, is_yfx, semicolon_char,
@@ -18,6 +18,8 @@ use crate::machine::stack::*;
use crate::machine::streams::*;
use crate::types::*;
use dashu::base::DivRem;
use dashu::base::DivRemEuclid;
use ordered_float::OrderedFloat;
use indexmap::IndexMap;
@@ -205,7 +207,7 @@ impl NumberFocus {
fn is_negative(&self) -> bool {
match self {
NumberFocus::Unfocused(n) => n.is_negative(),
NumberFocus::Denominator(r) | NumberFocus::Numerator(r) => **r < 0,
NumberFocus::Denominator(r) | NumberFocus::Numerator(r) => **r < Rational::from(0),
}
}
}
@@ -411,8 +413,8 @@ fn negated_op_needs_bracketing(
&& iter.leftmost_leaf_has_property(op_dir, |addr| match Number::try_from(addr) {
Ok(Number::Fixnum(n)) => n.get_num() > 0,
Ok(Number::Float(f)) => f > OrderedFloat(0f64),
Ok(Number::Integer(n)) => &*n > &0,
Ok(Number::Rational(n)) => &*n > &0,
Ok(Number::Integer(n)) => &*n > &Integer::from(0),
Ok(Number::Rational(n)) => &*n > &Rational::from(0),
_ => false,
})
} else {
@@ -520,11 +522,13 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
let i = n.mod_u(26) as usize;
let j = n.div_rem_floor(Integer::from(26));
let n_clone: Integer = n.clone();
let i = n.div_rem_euclid(Integer::from(26)).1.to_f32().value() as usize;
let j = n_clone.div_rem(Integer::from(26));
let j = <(Integer, Integer)>::from(j).0;
if j == 0 {
if j == Integer::from(0) {
CHAR_CODES[i].to_string()
} else {
format!("{}{}", CHAR_CODES[i], j)
@@ -540,7 +544,7 @@ pub(crate) fn numbervar(offset: &Integer, addr: HeapCellValue) -> Option<String>
}
}
Ok(Number::Integer(n)) => {
if &*n >= &0 {
if &*n >= &Integer::from(0) {
Some(numbervar(Integer::from(offset + &*n)))
} else {
None
@@ -987,14 +991,14 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
}
},
NumberFocus::Denominator(r) => {
let output_str = format!("{}", r.denom());
let output_str = format!("{}", r.denominator());
push_space_if_amb!(self, &output_str, {
append_str!(self, &output_str);
});
}
NumberFocus::Numerator(r) => {
let output_str = format!("{}", r.numer());
let output_str = format!("{}", r.numerator());
push_space_if_amb!(self, &output_str, {
append_str!(self, &output_str);
@@ -1359,10 +1363,10 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> {
if self.numbervars && arity == 1 && name == atom!("$VAR") {
!self.iter.immediate_leaf_has_property(|addr| {
match Number::try_from(addr) {
Ok(Number::Integer(n)) => &*n >= &0,
Ok(Number::Integer(n)) => &*n >= &Integer::from(0),
Ok(Number::Fixnum(n)) => n.get_num() >= 0,
Ok(Number::Float(f)) => f >= OrderedFloat(0f64),
Ok(Number::Rational(r)) => &*r >= &0,
Ok(Number::Rational(r)) => &*r >= &Integer::from(0),
_ => false,
}
}) && needs_bracketing(op_desc, op)