Skip to content

Commit

Permalink
trying to parse ieee754 const
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Aug 8, 2024
1 parent 7e60616 commit 269ec2c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
32 changes: 17 additions & 15 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ impl CalyxParser {
Ok(float_val)
}

fn ieee754_const(input: Node) -> ParseResult<BitNum> {
println!("parsing iee754");
let span = Self::get_span(&input);
let val = match_nodes!(
input.clone().into_children();
[float(val)] => val
);
let bit_pattern = val.to_bits();
Ok(BitNum {
width: 64,
num_type: NumType::Hex,
val: bit_pattern,
span,
})
}

fn num_lit(input: Node) -> ParseResult<BitNum> {
let span = Self::get_span(&input);
let num = match_nodes!(
Expand Down Expand Up @@ -330,21 +346,7 @@ 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
}
},
[ieee754_const(val)] => val,

);

Expand Down
9 changes: 6 additions & 3 deletions calyx-frontend/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ octal = @{ ASCII_HEX_DIGIT+ }
hex = @{ ASCII_HEX_DIGIT+ }
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }

ieee754_const = { "ieee754_const(" ~ float ~ ")" }

// `$` creates a compound rule which ignores whitespace while allowing for
// inner rules (`@` makes inner rules silent).
// See: https://pest.rs/book/print.html#atomic
num_lit = ${
bitwidth
(bitwidth
~ "'"
~ ( "d" ~ decimal
| "b" ~ binary
| "x" ~ hex
| "o" ~ octal
| "f" ~ float)
| "o" ~ octal)
)
| ieee754_const
}

char = { !"\"" ~ ANY }
Expand Down
2 changes: 1 addition & 1 deletion tests/correctness/float/float-const.futil
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ component main<"toplevel"=1,>(@clk clk: 1, @reset reset: 1, @go go: 1) -> (@done
wires {
group read {
mem_read.addr0 = 1'b0;
reg0.in = mem_read.read_data;
reg0.in = ieee754_const(1.00);
reg0.write_en = 1'b1;
read[done] = reg0.done;
}
Expand Down

0 comments on commit 269ec2c

Please sign in to comment.