Skip to content

Commit

Permalink
support floating point parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Jul 3, 2024
1 parent 92e5b26 commit ce90f1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ impl CalyxParser {
u64::from_str_radix(input.as_str(), 2)
.map_err(|_| input.error("Expected binary number"))
}
fn float(input: Node) -> ParseResult<f64> {
let float_str = input.as_str();
let float_val: f64 = float_str
.parse()
.map_err(|_| input.error("Expected valid floating-point number"))?;
Ok(float_val)
}

fn num_lit(input: Node) -> ParseResult<BitNum> {
let span = Self::get_span(&input);
Expand Down Expand Up @@ -322,6 +329,21 @@ impl CalyxParser {
val,
span
},
[bitwidth(width), float(val)] => {
let bit_pattern = if width == 32 {
(val as f32).to_bits() as u64
} else if width == 64 {
val.to_bits()
} else {
return Err(input.error("Unsupported bitwidth for floating-point number"));
};
BitNum {
width,
num_type: NumType::Hex,
val: bit_pattern,
span
}
},

);

Expand Down
4 changes: 3 additions & 1 deletion calyx-frontend/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ binary = @{ ASCII_HEX_DIGIT+ }
decimal = @{ ASCII_HEX_DIGIT+ }
octal = @{ ASCII_HEX_DIGIT+ }
hex = @{ ASCII_HEX_DIGIT+ }
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

// `$` creates a compound rule which ignores whitespace while allowing for
// inner rules (`@` makes inner rules silent).
Expand All @@ -26,7 +27,8 @@ num_lit = ${
~ ( "d" ~ decimal
| "b" ~ binary
| "x" ~ hex
| "o" ~ octal )
| "o" ~ octal
| "f" ~ float)
}

char = { !"\"" ~ ANY }
Expand Down

0 comments on commit ce90f1d

Please sign in to comment.