Skip to content

Commit

Permalink
chore: Add display for binaryop (noir-lang#839)
Browse files Browse the repository at this point in the history
* add display for binaryop

* remove display import
Ethan-000 authored Feb 14, 2023

Unverified

This user has not yet uploaded their public signing key.
1 parent 06f058b commit 766b57d
Showing 2 changed files with 34 additions and 28 deletions.
29 changes: 1 addition & 28 deletions crates/noirc_evaluator/src/ssa/context.rs
Original file line number Diff line number Diff line change
@@ -157,35 +157,8 @@ impl SsaContext {
fn binary_to_string(&self, binary: &node::Binary) -> String {
let lhs = self.id_to_string(binary.lhs);
let rhs = self.id_to_string(binary.rhs);
let op = match &binary.operator {
BinaryOp::Add => "add",
BinaryOp::SafeAdd => "safe_add",
BinaryOp::Sub { .. } => "sub",
BinaryOp::SafeSub { .. } => "safe_sub",
BinaryOp::Mul => "mul",
BinaryOp::SafeMul => "safe_mul",
BinaryOp::Udiv => "udiv",
BinaryOp::Sdiv => "sdiv",
BinaryOp::Urem => "urem",
BinaryOp::Srem => "srem",
BinaryOp::Div => "div",
BinaryOp::Eq => "eq",
BinaryOp::Ne => "ne",
BinaryOp::Ult => "ult",
BinaryOp::Ule => "ule",
BinaryOp::Slt => "slt",
BinaryOp::Sle => "sle",
BinaryOp::Lt => "lt",
BinaryOp::Lte => "lte",
BinaryOp::And => "and",
BinaryOp::Or => "or",
BinaryOp::Xor => "xor",
BinaryOp::Assign => "assign",
BinaryOp::Shl => "shl",
BinaryOp::Shr => "shr",
};

format!("{op} {lhs}, {rhs}")
format!("{} {lhs}, {rhs}", binary.operator)
}

pub fn operation_to_string(&self, op: &Operation) -> String {
33 changes: 33 additions & 0 deletions crates/noirc_evaluator/src/ssa/node.rs
Original file line number Diff line number Diff line change
@@ -620,6 +620,39 @@ pub enum BinaryOp {
Assign,
}

impl std::fmt::Display for BinaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let op = match &self {
BinaryOp::Add => "add",
BinaryOp::SafeAdd => "safe_add",
BinaryOp::Sub { .. } => "sub",
BinaryOp::SafeSub { .. } => "safe_sub",
BinaryOp::Mul => "mul",
BinaryOp::SafeMul => "safe_mul",
BinaryOp::Udiv => "udiv",
BinaryOp::Sdiv => "sdiv",
BinaryOp::Urem => "urem",
BinaryOp::Srem => "srem",
BinaryOp::Div => "div",
BinaryOp::Eq => "eq",
BinaryOp::Ne => "ne",
BinaryOp::Ult => "ult",
BinaryOp::Ule => "ule",
BinaryOp::Slt => "slt",
BinaryOp::Sle => "sle",
BinaryOp::Lt => "lt",
BinaryOp::Lte => "lte",
BinaryOp::And => "and",
BinaryOp::Or => "or",
BinaryOp::Xor => "xor",
BinaryOp::Assign => "assign",
BinaryOp::Shl => "shl",
BinaryOp::Shr => "shr",
};
write!(f, "{op}")
}
}

impl Binary {
fn new(operator: BinaryOp, lhs: NodeId, rhs: NodeId) -> Binary {
Binary { operator, lhs, rhs, predicate: None }

0 comments on commit 766b57d

Please sign in to comment.