Skip to content

Commit 9657994

Browse files
Change Expr type to have Binary and Unary variants
1 parent 2dc4040 commit 9657994

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/ir.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,33 @@ pub enum Stmt {
181181
pub struct ExprId(u32);
182182
entity_impl!(ExprId, "expr");
183183

184+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
185+
pub enum BinOp {
186+
Equal,
187+
And,
188+
}
189+
190+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
191+
pub enum UnaryOp {
192+
Not,
193+
}
194+
184195
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
185196
pub enum Expr {
186197
// nullary
187198
Const(BitVecValue),
188199
Sym(SymbolId),
189200
DontCare,
190201
// unary
191-
Not(ExprId),
202+
Binary(BinOp, ExprId, ExprId),
192203
// binary
193-
And(ExprId, ExprId),
194-
Equal(ExprId, ExprId),
204+
Unary(UnaryOp, ExprId),
195205
}
196206

207+
208+
209+
// add further bin ops
210+
197211
#[derive(Clone, Copy, Hash, PartialEq, Eq, Default)]
198212
pub struct StructId(u32);
199213
entity_impl!(StructId, "struct");
@@ -337,7 +351,7 @@ impl Index<SymbolId> for SymbolTable {
337351
fn index(&self, index: SymbolId) -> &Self::Output {
338352
&self.entries[index]
339353
}
340-
}
354+
}
341355

342356
impl Index<&SymbolId> for SymbolTable {
343357
type Output = SymbolTableEntry;
@@ -468,8 +482,8 @@ mod tests {
468482
}
469483

470484
#[test]
471-
fn serialize_calyx_go_down_transaction() {
472-
// Manually create the expected result of parsing `calyx_go_down`.
485+
fn serialize_calyx_go_done_transaction() {
486+
// Manually create the expected result of parsing `calyx_go_done`.
473487
// Note that the order in which things are created will be different in the parser.
474488

475489
// 1) declare symbols
@@ -508,8 +522,8 @@ mod tests {
508522
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
509523
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
510524
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
511-
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
512-
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
525+
let cond_expr = calyx_go_done.e(Expr::Binary(BinOp::Equal, dut_done_expr, one_expr));
526+
let not_expr = calyx_go_done.e(Expr::Unary(UnaryOp::Not, cond_expr));
513527

514528
// 4) create statements
515529
let while_body = vec![calyx_go_done.s(Stmt::Step)];

src/serialize.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pub fn serialize_expr(tr: &Transaction, st: &SymbolTable, expr_id: &ExprId) -> S
3737
Expr::Const(val) => val.to_bit_str(),
3838
Expr::Sym(symid) => st[symid].full_name(st),
3939
Expr::DontCare => "X".to_owned(),
40-
Expr::Not(not_exprid) => "!(".to_owned() + &serialize_expr(tr, st, not_exprid) + ")",
41-
Expr::And(lhs, rhs) => serialize_expr(tr, st, lhs) + " && " + &serialize_expr(tr, st, rhs),
42-
Expr::Equal(lhs, rhs) => {
40+
Expr::Unary(UnaryOp::Not, not_exprid) => "!(".to_owned() + &serialize_expr(tr, st, not_exprid) + ")",
41+
Expr::Binary(BinOp::And,lhs, rhs) => serialize_expr(tr, st, lhs) + " && " + &serialize_expr(tr, st, rhs),
42+
Expr::Binary(BinOp::Equal, lhs, rhs) => {
4343
serialize_expr(tr, st, lhs) + " == " + &serialize_expr(tr, st, rhs)
4444
}
4545
}
@@ -239,8 +239,8 @@ pub mod tests {
239239
}
240240

241241
#[test]
242-
fn serialize_calyx_go_down_transaction() {
243-
// Manually create the expected result of parsing `calyx_go_down`.
242+
fn serialize_calyx_go_done_transaction() {
243+
// Manually create the expected result of parsing `calyx_go_done`.
244244
// Note that the order in which things are created will be different in the parser.
245245

246246
// TODO: create this into function that factors our the code to put src code into IR
@@ -281,8 +281,8 @@ pub mod tests {
281281
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
282282
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
283283
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
284-
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
285-
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
284+
let cond_expr = calyx_go_done.e(Expr::Binary(BinOp::Equal, dut_done_expr, one_expr));
285+
let not_expr = calyx_go_done.e(Expr::Unary(UnaryOp::Not, cond_expr));
286286

287287
// 4) create statements
288288
let while_body = vec![calyx_go_done.s(Stmt::Step)];
@@ -338,7 +338,7 @@ pub mod tests {
338338
let a_expr = easycond.e(Expr::Sym(a));
339339
let dut_a_expr = easycond.e(Expr::Sym(dut_a));
340340
let one_expr = easycond.e(Expr::Const(BitVecValue::from_u64(1, 1)));
341-
let cond_expr = easycond.e(Expr::Equal(dut_a_expr, one_expr));
341+
let cond_expr = easycond.e(Expr::Binary(BinOp::Equal, dut_a_expr, one_expr));
342342

343343
// 4) create statements
344344
let if_body = vec![easycond.s(Stmt::Step)];

src/typecheck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn check_expr_types(
1616
Expr::Const(_) => Ok(Type::BitVec(1)),
1717
Expr::Sym(symid) => Ok(st[symid].tpe()),
1818
Expr::DontCare => Ok(Type::Unknown),
19-
Expr::Not(not_exprid) => {
19+
Expr::Unary(UnaryOp::Not, not_exprid) => {
2020
let inner_type = check_expr_types(tr, st, handler, not_exprid)?;
2121
if let Type::BitVec(_) = inner_type {
2222
Ok(inner_type)
@@ -30,7 +30,7 @@ fn check_expr_types(
3030
Ok(inner_type)
3131
}
3232
}
33-
Expr::And(lhs, rhs) | Expr::Equal(lhs, rhs) => {
33+
Expr::Binary(BinOp::And, lhs, rhs) | Expr::Binary(BinOp::Equal, lhs, rhs) => {
3434
let lhs_type = check_expr_types(tr, st, handler, lhs)?;
3535
let rhs_type = check_expr_types(tr, st, handler, rhs)?;
3636
if lhs_type.is_equivalent(&rhs_type) {
@@ -190,9 +190,9 @@ mod tests {
190190
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
191191
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
192192
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
193-
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
193+
let cond_expr = calyx_go_done.e(Expr::Binary(BinOp::Equal, dut_done_expr, one_expr));
194194
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
195-
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
195+
let not_expr = calyx_go_done.e(Expr::Unary(UnaryOp::Not, cond_expr));
196196
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
197197

198198
// 4) create statements

0 commit comments

Comments
 (0)