Fixed float parsing and improved printing

This commit is contained in:
2023-10-11 20:32:04 +01:00
parent 86e520e8b8
commit af3e9f67fa
3 changed files with 20 additions and 19 deletions

View File

@@ -62,7 +62,7 @@ pub fn parse_num(string: &str) -> Result<Numeric, NumError> {
let (base, exponent) = match noprefix.split_once('p') { let (base, exponent) = match noprefix.split_once('p') {
Some((b, e)) => { Some((b, e)) => {
let (s, d, len) = e.strip_prefix('-').map_or((1, e, 0), |ue| (-1, ue, 1)); let (s, d, len) = e.strip_prefix('-').map_or((1, e, 0), |ue| (-1, ue, 1));
(b, s * int_parse(d, radix, pos + b.len() + 1 + len)? as i32) (b, s * int_parse(d, 10, pos + b.len() + 1 + len)? as i32)
}, },
None => (noprefix, 0), None => (noprefix, 0),
}; };
@@ -142,7 +142,17 @@ mod test {
#[must_use] #[must_use]
pub fn print_nat16(num: NotNan<f64>) -> String { pub fn print_nat16(num: NotNan<f64>) -> String {
if *num == 0.0 {
return "0x0".to_string()
} else if num.is_infinite() {
return match num.is_sign_positive() {
true => "Infinity".to_string(),
false => "-Infinity".to_string(),
}
} else if num.is_nan() {
return "NaN".to_string()
}
let exp = num.log(16.0).floor(); let exp = num.log(16.0).floor();
let man = num / 16_f64.powf(exp); let man = *num / 16_f64.powf(exp);
format!("{man}p{exp:.0}") format!("0x{man}p{exp:.0}")
} }

View File

@@ -1,5 +1,4 @@
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::format;
use std::rc::Rc; use std::rc::Rc;
use hashbrown::HashSet; use hashbrown::HashSet;
@@ -13,6 +12,7 @@ use super::{update_first_seq, RuleError, VectreeMatcher};
use crate::ast::Rule; use crate::ast::Rule;
use crate::interner::Interner; use crate::interner::Interner;
use crate::Sym; use crate::Sym;
use crate::parse::print_nat16;
#[derive(Debug)] #[derive(Debug)]
pub struct CachedRule<M: Matcher> { pub struct CachedRule<M: Matcher> {
@@ -139,18 +139,11 @@ impl<M: Debug + Matcher> Debug for Repository<M> {
} }
} }
#[must_use]
fn fmt_hex(num: f64) -> String {
let exponent = (num.log2() / 4_f64).floor();
let mantissa = num / 16_f64.powf(exponent);
format!("0x{:x}p{}", mantissa as i64, exponent as i64)
}
impl<M: Display + Matcher> Display for Repository<M> { impl<M: Display + Matcher> Display for Repository<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Repository[")?; writeln!(f, "Repository[")?;
for (rule, deps, p) in self.cache.iter() { for (rule, deps, p) in self.cache.iter() {
let prio = fmt_hex(f64::from(*p)); let prio = print_nat16(*p);
let deps = deps.iter().map(|t| t.extern_vec().join("::")).join(", "); let deps = deps.iter().map(|t| t.extern_vec().join("::")).join(", ");
writeln!(f, " priority: {prio}\tdependencies: [{deps}]")?; writeln!(f, " priority: {prio}\tdependencies: [{deps}]")?;
writeln!(f, " {rule}")?; writeln!(f, " {rule}")?;

View File

@@ -18,11 +18,9 @@ export const pass2 := \a. \b. \cont. cont a b
]-- ]--
export const return := \a. \b.a export const return := \a. \b.a
export ::($, |>, =>) export macro ...$prefix $ ...$suffix:1 =0x1p38=> ...$prefix (...$suffix)
export macro ...$prefix |> $fn ..$suffix:1 =0x2p32=> $fn (...$prefix) ..$suffix
macro ...$prefix $ ...$suffix:1 =0x1p38=> ...$prefix (...$suffix) export macro ($name) => ...$body =0x2p127=> (\$name. ...$body)
macro ...$prefix |> $fn ..$suffix:1 =0x2p32=> $fn (...$prefix) ..$suffix export macro ($name, ...$argv) => ...$body =0x2p127=> (\$name. (...$argv) => ...$body)
export macro $name => ...$body =0x1p127=> (\$name. ...$body)
macro ($name) => ...$body =0x2p129=> (\$name. ...$body)
macro ($name, ...$argv) => ...$body =0x2p129=> (\$name. (...$argv) => ...$body)
macro $name => ...$body =0x1p129=> (\$name. ...$body)