Skip to content

Commit

Permalink
Fixed float parsing and improved printing
Browse files Browse the repository at this point in the history
  • Loading branch information
lbfalvy committed Oct 11, 2023
1 parent 86e520e commit af3e9f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
16 changes: 13 additions & 3 deletions src/parse/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn parse_num(string: &str) -> Result<Numeric, NumError> {
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),
};
Expand Down Expand Up @@ -142,7 +142,17 @@ mod test {

#[must_use]
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 man = num / 16_f64.powf(exp);
format!("{man}p{exp:.0}")
let man = *num / 16_f64.powf(exp);
format!("0x{man}p{exp:.0}")
}
11 changes: 2 additions & 9 deletions src/rule/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt::{Debug, Display};
use std::format;
use std::rc::Rc;

use hashbrown::HashSet;
Expand All @@ -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<M: Matcher> {
Expand Down Expand Up @@ -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> {
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}")?;
Expand Down
12 changes: 5 additions & 7 deletions src/systems/stl/functional.orc
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit af3e9f6

Please sign in to comment.