From af3e9f67fad68c6000ee5c02433edd005381b071 Mon Sep 17 00:00:00 2001 From: Lawrence Bethlenfalvy Date: Wed, 11 Oct 2023 20:32:04 +0100 Subject: [PATCH] Fixed float parsing and improved printing --- src/parse/numeric.rs | 16 +++++++++++++--- src/rule/repository.rs | 11 ++--------- src/systems/stl/functional.orc | 12 +++++------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/parse/numeric.rs b/src/parse/numeric.rs index cef05b8..26f636e 100644 --- a/src/parse/numeric.rs +++ b/src/parse/numeric.rs @@ -62,7 +62,7 @@ pub fn parse_num(string: &str) -> Result { let (base, exponent) = match noprefix.split_once('p') { Some((b, e)) => { 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), }; @@ -142,7 +142,17 @@ mod test { #[must_use] pub fn print_nat16(num: NotNan) -> 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 man = num / 16_f64.powf(exp); - format!("{man}p{exp:.0}") + let man = *num / 16_f64.powf(exp); + format!("0x{man}p{exp:.0}") } diff --git a/src/rule/repository.rs b/src/rule/repository.rs index 609dc89..fe789d5 100644 --- a/src/rule/repository.rs +++ b/src/rule/repository.rs @@ -1,5 +1,4 @@ use std::fmt::{Debug, Display}; -use std::format; use std::rc::Rc; use hashbrown::HashSet; @@ -13,6 +12,7 @@ use super::{update_first_seq, RuleError, VectreeMatcher}; use crate::ast::Rule; use crate::interner::Interner; use crate::Sym; +use crate::parse::print_nat16; #[derive(Debug)] pub struct CachedRule { @@ -139,18 +139,11 @@ impl Debug for Repository { } } -#[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 Display for Repository { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "Repository[")?; 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(", "); writeln!(f, " priority: {prio}\tdependencies: [{deps}]")?; writeln!(f, " {rule}")?; diff --git a/src/systems/stl/functional.orc b/src/systems/stl/functional.orc index 4ff9ef3..5547b49 100644 --- a/src/systems/stl/functional.orc +++ b/src/systems/stl/functional.orc @@ -18,11 +18,9 @@ export const pass2 := \a. \b. \cont. cont a b ]-- 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) -macro ...$prefix |> $fn ..$suffix:1 =0x2p32=> $fn (...$prefix) ..$suffix - -macro ($name) => ...$body =0x2p129=> (\$name. ...$body) -macro ($name, ...$argv) => ...$body =0x2p129=> (\$name. (...$argv) => ...$body) -macro $name => ...$body =0x1p129=> (\$name. ...$body) +export macro ($name) => ...$body =0x2p127=> (\$name. ...$body) +export macro ($name, ...$argv) => ...$body =0x2p127=> (\$name. (...$argv) => ...$body) +export macro $name => ...$body =0x1p127=> (\$name. ...$body)