Skip to content

Commit

Permalink
Rename Float's decimal field to fractional. (#1887)
Browse files Browse the repository at this point in the history
This avoids the ambiguity with "decimal" meaning base-10.
  • Loading branch information
sunfishcode authored Oct 31, 2024
1 parent d498715 commit 5a7419d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
34 changes: 17 additions & 17 deletions crates/wast/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ pub enum Float<'a> {
},
/// A parsed and separated floating point value
Val {
/// Whether or not the `integral` and `decimal` are specified in hex
/// Whether or not the `integral` and `fractional` are specified in hex
hex: bool,
/// The float parts before the `.`
integral: Cow<'a, str>,
/// The float parts after the `.`
decimal: Option<Cow<'a, str>>,
/// The exponent to multiple this `integral.decimal` portion of the
fractional: Option<Cow<'a, str>>,
/// The exponent to multiple this `integral.fractional` portion of the
/// float by. If `hex` is true this is `2^exponent` and otherwise it's
/// `10^exponent`
exponent: Option<Cow<'a, str>>,
Expand Down Expand Up @@ -673,7 +673,7 @@ impl<'a> Lexer<'a> {
}
}

// A number can optionally be after the decimal so only actually try to
// A number can optionally be after the dot so only actually try to
// parse one if it's there.
if it.clone().next() == Some(&b'.') {
it.next();
Expand Down Expand Up @@ -1079,7 +1079,7 @@ impl Token {
hex,
} => {
let src = self.src(s);
let (integral, decimal, exponent) = match src.find('.') {
let (integral, fractional, exponent) = match src.find('.') {
Some(i) => {
let integral = &src[..i];
let rest = &src[i + 1..];
Expand All @@ -1106,7 +1106,7 @@ impl Token {
}
};
let mut integral = Cow::Borrowed(integral.strip_prefix('+').unwrap_or(integral));
let mut decimal = decimal.and_then(|s| {
let mut fractional = fractional.and_then(|s| {
if s.is_empty() {
None
} else {
Expand All @@ -1117,8 +1117,8 @@ impl Token {
exponent.map(|s| Cow::Borrowed(s.strip_prefix('+').unwrap_or(s)));
if has_underscores {
*integral.to_mut() = integral.replace("_", "");
if let Some(decimal) = &mut decimal {
*decimal.to_mut() = decimal.replace("_", "");
if let Some(fractional) = &mut fractional {
*fractional.to_mut() = fractional.replace("_", "");
}
if let Some(exponent) = &mut exponent {
*exponent.to_mut() = exponent.replace("_", "");
Expand All @@ -1130,7 +1130,7 @@ impl Token {
Float::Val {
hex,
integral,
decimal,
fractional,
exponent,
}
}
Expand Down Expand Up @@ -1501,7 +1501,7 @@ mod tests {
get_float("1.2"),
Float::Val {
integral: "1".into(),
decimal: Some("2".into()),
fractional: Some("2".into()),
exponent: None,
hex: false,
},
Expand All @@ -1510,7 +1510,7 @@ mod tests {
get_float("1.2e3"),
Float::Val {
integral: "1".into(),
decimal: Some("2".into()),
fractional: Some("2".into()),
exponent: Some("3".into()),
hex: false,
},
Expand All @@ -1519,7 +1519,7 @@ mod tests {
get_float("-1_2.1_1E+0_1"),
Float::Val {
integral: "-12".into(),
decimal: Some("11".into()),
fractional: Some("11".into()),
exponent: Some("01".into()),
hex: false,
},
Expand All @@ -1528,7 +1528,7 @@ mod tests {
get_float("+1_2.1_1E-0_1"),
Float::Val {
integral: "12".into(),
decimal: Some("11".into()),
fractional: Some("11".into()),
exponent: Some("-01".into()),
hex: false,
},
Expand All @@ -1537,7 +1537,7 @@ mod tests {
get_float("0x1_2.3_4p5_6"),
Float::Val {
integral: "12".into(),
decimal: Some("34".into()),
fractional: Some("34".into()),
exponent: Some("56".into()),
hex: true,
},
Expand All @@ -1546,7 +1546,7 @@ mod tests {
get_float("+0x1_2.3_4P-5_6"),
Float::Val {
integral: "12".into(),
decimal: Some("34".into()),
fractional: Some("34".into()),
exponent: Some("-56".into()),
hex: true,
},
Expand All @@ -1555,7 +1555,7 @@ mod tests {
get_float("1."),
Float::Val {
integral: "1".into(),
decimal: None,
fractional: None,
exponent: None,
hex: false,
},
Expand All @@ -1564,7 +1564,7 @@ mod tests {
get_float("0x1p-24"),
Float::Val {
integral: "1".into(),
decimal: None,
fractional: None,
exponent: Some("-24".into()),
hex: true,
},
Expand Down
32 changes: 16 additions & 16 deletions crates/wast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ macro_rules! float {
$parse(&Float::Val {
hex: base == 16,
integral: s.into(),
decimal: None,
fractional: None,
exponent: None,
}),
rest,
Expand All @@ -436,7 +436,7 @@ macro_rules! float {
let signif_mask = (1 << exp_offset) - 1;
let bias = (1 << ($exp_bits - 1)) - 1;

let (hex, integral, decimal, exponent_str) = match val {
let (hex, integral, fractional, exponent_str) = match val {
// Infinity is when the exponent bits are all set and
// the significand is zero.
Float::Inf { negative } => {
Expand Down Expand Up @@ -471,18 +471,18 @@ macro_rules! float {
}

// This is trickier, handle this below
Float::Val { hex, integral, decimal, exponent } => {
(hex, integral, decimal, exponent)
Float::Val { hex, integral, fractional, exponent } => {
(hex, integral, fractional, exponent)
}
};

// Rely on Rust's standard library to parse base 10 floats
// correctly.
if !*hex {
let mut s = integral.to_string();
if let Some(decimal) = decimal {
if let Some(fractional) = fractional {
s.push_str(".");
s.push_str(&decimal);
s.push_str(&fractional);
}
if let Some(exponent) = exponent_str {
s.push_str("e");
Expand All @@ -501,7 +501,7 @@ macro_rules! float {
// this below does. It was copied from Gecko's implementation in
// `WasmTextToBinary.cpp`. Would love comments on this if you have
// them!
let decimal = decimal.as_ref().map(|s| &**s).unwrap_or("");
let fractional = fractional.as_ref().map(|s| &**s).unwrap_or("");
let negative = integral.starts_with('-');
let integral = integral.trim_start_matches('-').trim_start_matches('0');

Expand All @@ -510,15 +510,15 @@ macro_rules! float {
// adjustments depending on where the digit was found, but the
// general idea here is that I'm not really sure why things are
// calculated the way they are but it should match Gecko.
let decimal_no_leading = decimal.trim_start_matches('0');
let decimal_iter = if integral.is_empty() {
decimal_no_leading.chars()
let fractional_no_leading = fractional.trim_start_matches('0');
let fractional_iter = if integral.is_empty() {
fractional_no_leading.chars()
} else {
decimal.chars()
fractional.chars()
};
let mut digits = integral.chars()
.map(|c| (to_hex(c) as $int, false))
.chain(decimal_iter.map(|c| (to_hex(c) as $int, true)));
.chain(fractional_iter.map(|c| (to_hex(c) as $int, true)));
let lead_nonzero_digit = match digits.next() {
Some((c, _)) => c,
// No digits? Must be `+0` or `-0`, being careful to handle the
Expand All @@ -530,7 +530,7 @@ macro_rules! float {
let mut exponent = if !integral.is_empty() {
1
} else {
-((decimal.len() - decimal_no_leading.len() + 1) as i32) + 1
-((fractional.len() - fractional_no_leading.len() + 1) as i32) + 1
};
let lz = (lead_nonzero_digit as u8).leading_zeros() as i32 - 4;
exponent = exponent.checked_mul(4)?.checked_sub(lz + 1)?;
Expand All @@ -542,8 +542,8 @@ macro_rules! float {
// digits. Again, not entirely sure why everything is the way it is
// here! This is copied frmo gecko.
let mut discarded_extra_nonzero = false;
for (digit, decimal) in digits {
if !decimal {
for (digit, is_fractional) in digits {
if !is_fractional {
exponent += 4;
}
if significand_pos > -4 {
Expand Down Expand Up @@ -699,7 +699,7 @@ mod tests {
(@mk $a:tt, $b:expr, $e:expr) => (crate::lexer::Float::Val {
hex: true,
integral: $a.into(),
decimal: $b,
fractional: $b,
exponent: $e
});
}
Expand Down

0 comments on commit 5a7419d

Please sign in to comment.