diff --git a/calyx-frontend/src/parser.rs b/calyx-frontend/src/parser.rs index 63d680d714..f7c64d297a 100644 --- a/calyx-frontend/src/parser.rs +++ b/calyx-frontend/src/parser.rs @@ -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 { + 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 { let span = Self::get_span(&input); @@ -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 + } + }, ); diff --git a/calyx-frontend/src/syntax.pest b/calyx-frontend/src/syntax.pest index 90b85263f6..d01f1a136c 100644 --- a/calyx-frontend/src/syntax.pest +++ b/calyx-frontend/src/syntax.pest @@ -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). @@ -26,7 +27,8 @@ num_lit = ${ ~ ( "d" ~ decimal | "b" ~ binary | "x" ~ hex - | "o" ~ octal ) + | "o" ~ octal + | "f" ~ float) } char = { !"\"" ~ ANY }