Skip to content

Commit 5a7419d

Browse files
authored
Rename Float's decimal field to fractional. (bytecodealliance#1887)
This avoids the ambiguity with "decimal" meaning base-10.
1 parent d498715 commit 5a7419d

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

crates/wast/src/lexer.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ pub enum Float<'a> {
261261
},
262262
/// A parsed and separated floating point value
263263
Val {
264-
/// Whether or not the `integral` and `decimal` are specified in hex
264+
/// Whether or not the `integral` and `fractional` are specified in hex
265265
hex: bool,
266266
/// The float parts before the `.`
267267
integral: Cow<'a, str>,
268268
/// The float parts after the `.`
269-
decimal: Option<Cow<'a, str>>,
270-
/// The exponent to multiple this `integral.decimal` portion of the
269+
fractional: Option<Cow<'a, str>>,
270+
/// The exponent to multiple this `integral.fractional` portion of the
271271
/// float by. If `hex` is true this is `2^exponent` and otherwise it's
272272
/// `10^exponent`
273273
exponent: Option<Cow<'a, str>>,
@@ -673,7 +673,7 @@ impl<'a> Lexer<'a> {
673673
}
674674
}
675675

676-
// A number can optionally be after the decimal so only actually try to
676+
// A number can optionally be after the dot so only actually try to
677677
// parse one if it's there.
678678
if it.clone().next() == Some(&b'.') {
679679
it.next();
@@ -1079,7 +1079,7 @@ impl Token {
10791079
hex,
10801080
} => {
10811081
let src = self.src(s);
1082-
let (integral, decimal, exponent) = match src.find('.') {
1082+
let (integral, fractional, exponent) = match src.find('.') {
10831083
Some(i) => {
10841084
let integral = &src[..i];
10851085
let rest = &src[i + 1..];
@@ -1106,7 +1106,7 @@ impl Token {
11061106
}
11071107
};
11081108
let mut integral = Cow::Borrowed(integral.strip_prefix('+').unwrap_or(integral));
1109-
let mut decimal = decimal.and_then(|s| {
1109+
let mut fractional = fractional.and_then(|s| {
11101110
if s.is_empty() {
11111111
None
11121112
} else {
@@ -1117,8 +1117,8 @@ impl Token {
11171117
exponent.map(|s| Cow::Borrowed(s.strip_prefix('+').unwrap_or(s)));
11181118
if has_underscores {
11191119
*integral.to_mut() = integral.replace("_", "");
1120-
if let Some(decimal) = &mut decimal {
1121-
*decimal.to_mut() = decimal.replace("_", "");
1120+
if let Some(fractional) = &mut fractional {
1121+
*fractional.to_mut() = fractional.replace("_", "");
11221122
}
11231123
if let Some(exponent) = &mut exponent {
11241124
*exponent.to_mut() = exponent.replace("_", "");
@@ -1130,7 +1130,7 @@ impl Token {
11301130
Float::Val {
11311131
hex,
11321132
integral,
1133-
decimal,
1133+
fractional,
11341134
exponent,
11351135
}
11361136
}
@@ -1501,7 +1501,7 @@ mod tests {
15011501
get_float("1.2"),
15021502
Float::Val {
15031503
integral: "1".into(),
1504-
decimal: Some("2".into()),
1504+
fractional: Some("2".into()),
15051505
exponent: None,
15061506
hex: false,
15071507
},
@@ -1510,7 +1510,7 @@ mod tests {
15101510
get_float("1.2e3"),
15111511
Float::Val {
15121512
integral: "1".into(),
1513-
decimal: Some("2".into()),
1513+
fractional: Some("2".into()),
15141514
exponent: Some("3".into()),
15151515
hex: false,
15161516
},
@@ -1519,7 +1519,7 @@ mod tests {
15191519
get_float("-1_2.1_1E+0_1"),
15201520
Float::Val {
15211521
integral: "-12".into(),
1522-
decimal: Some("11".into()),
1522+
fractional: Some("11".into()),
15231523
exponent: Some("01".into()),
15241524
hex: false,
15251525
},
@@ -1528,7 +1528,7 @@ mod tests {
15281528
get_float("+1_2.1_1E-0_1"),
15291529
Float::Val {
15301530
integral: "12".into(),
1531-
decimal: Some("11".into()),
1531+
fractional: Some("11".into()),
15321532
exponent: Some("-01".into()),
15331533
hex: false,
15341534
},
@@ -1537,7 +1537,7 @@ mod tests {
15371537
get_float("0x1_2.3_4p5_6"),
15381538
Float::Val {
15391539
integral: "12".into(),
1540-
decimal: Some("34".into()),
1540+
fractional: Some("34".into()),
15411541
exponent: Some("56".into()),
15421542
hex: true,
15431543
},
@@ -1546,7 +1546,7 @@ mod tests {
15461546
get_float("+0x1_2.3_4P-5_6"),
15471547
Float::Val {
15481548
integral: "12".into(),
1549-
decimal: Some("34".into()),
1549+
fractional: Some("34".into()),
15501550
exponent: Some("-56".into()),
15511551
hex: true,
15521552
},
@@ -1555,7 +1555,7 @@ mod tests {
15551555
get_float("1."),
15561556
Float::Val {
15571557
integral: "1".into(),
1558-
decimal: None,
1558+
fractional: None,
15591559
exponent: None,
15601560
hex: false,
15611561
},
@@ -1564,7 +1564,7 @@ mod tests {
15641564
get_float("0x1p-24"),
15651565
Float::Val {
15661566
integral: "1".into(),
1567-
decimal: None,
1567+
fractional: None,
15681568
exponent: Some("-24".into()),
15691569
hex: true,
15701570
},

crates/wast/src/token.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ macro_rules! float {
410410
$parse(&Float::Val {
411411
hex: base == 16,
412412
integral: s.into(),
413-
decimal: None,
413+
fractional: None,
414414
exponent: None,
415415
}),
416416
rest,
@@ -436,7 +436,7 @@ macro_rules! float {
436436
let signif_mask = (1 << exp_offset) - 1;
437437
let bias = (1 << ($exp_bits - 1)) - 1;
438438

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

473473
// This is trickier, handle this below
474-
Float::Val { hex, integral, decimal, exponent } => {
475-
(hex, integral, decimal, exponent)
474+
Float::Val { hex, integral, fractional, exponent } => {
475+
(hex, integral, fractional, exponent)
476476
}
477477
};
478478

479479
// Rely on Rust's standard library to parse base 10 floats
480480
// correctly.
481481
if !*hex {
482482
let mut s = integral.to_string();
483-
if let Some(decimal) = decimal {
483+
if let Some(fractional) = fractional {
484484
s.push_str(".");
485-
s.push_str(&decimal);
485+
s.push_str(&fractional);
486486
}
487487
if let Some(exponent) = exponent_str {
488488
s.push_str("e");
@@ -501,7 +501,7 @@ macro_rules! float {
501501
// this below does. It was copied from Gecko's implementation in
502502
// `WasmTextToBinary.cpp`. Would love comments on this if you have
503503
// them!
504-
let decimal = decimal.as_ref().map(|s| &**s).unwrap_or("");
504+
let fractional = fractional.as_ref().map(|s| &**s).unwrap_or("");
505505
let negative = integral.starts_with('-');
506506
let integral = integral.trim_start_matches('-').trim_start_matches('0');
507507

@@ -510,15 +510,15 @@ macro_rules! float {
510510
// adjustments depending on where the digit was found, but the
511511
// general idea here is that I'm not really sure why things are
512512
// calculated the way they are but it should match Gecko.
513-
let decimal_no_leading = decimal.trim_start_matches('0');
514-
let decimal_iter = if integral.is_empty() {
515-
decimal_no_leading.chars()
513+
let fractional_no_leading = fractional.trim_start_matches('0');
514+
let fractional_iter = if integral.is_empty() {
515+
fractional_no_leading.chars()
516516
} else {
517-
decimal.chars()
517+
fractional.chars()
518518
};
519519
let mut digits = integral.chars()
520520
.map(|c| (to_hex(c) as $int, false))
521-
.chain(decimal_iter.map(|c| (to_hex(c) as $int, true)));
521+
.chain(fractional_iter.map(|c| (to_hex(c) as $int, true)));
522522
let lead_nonzero_digit = match digits.next() {
523523
Some((c, _)) => c,
524524
// No digits? Must be `+0` or `-0`, being careful to handle the
@@ -530,7 +530,7 @@ macro_rules! float {
530530
let mut exponent = if !integral.is_empty() {
531531
1
532532
} else {
533-
-((decimal.len() - decimal_no_leading.len() + 1) as i32) + 1
533+
-((fractional.len() - fractional_no_leading.len() + 1) as i32) + 1
534534
};
535535
let lz = (lead_nonzero_digit as u8).leading_zeros() as i32 - 4;
536536
exponent = exponent.checked_mul(4)?.checked_sub(lz + 1)?;
@@ -542,8 +542,8 @@ macro_rules! float {
542542
// digits. Again, not entirely sure why everything is the way it is
543543
// here! This is copied frmo gecko.
544544
let mut discarded_extra_nonzero = false;
545-
for (digit, decimal) in digits {
546-
if !decimal {
545+
for (digit, is_fractional) in digits {
546+
if !is_fractional {
547547
exponent += 4;
548548
}
549549
if significand_pos > -4 {
@@ -699,7 +699,7 @@ mod tests {
699699
(@mk $a:tt, $b:expr, $e:expr) => (crate::lexer::Float::Val {
700700
hex: true,
701701
integral: $a.into(),
702-
decimal: $b,
702+
fractional: $b,
703703
exponent: $e
704704
});
705705
}

0 commit comments

Comments
 (0)