Skip to content

Commit b448cdf

Browse files
committed
Add Display for Ops, streamline Expr Debug impl
1 parent 6f94c57 commit b448cdf

File tree

1 file changed

+51
-1
lines changed
  • experiments/analytic-engine/src

1 file changed

+51
-1
lines changed

experiments/analytic-engine/src/core.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,33 @@ pub enum BasicBinOp {
1414
Rem,
1515
}
1616

17+
impl std::fmt::Display for BasicBinOp {
18+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19+
match self {
20+
BasicBinOp::Add => write!(f, "+"),
21+
BasicBinOp::Sub => write!(f, "-"),
22+
BasicBinOp::Mul => write!(f, "*"),
23+
BasicBinOp::Div => write!(f, "/"),
24+
BasicBinOp::Rem => write!(f, "%"),
25+
}
26+
}
27+
}
28+
1729
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1830
pub enum BasicUnaryOp {
1931
Negate,
2032
AbsVal,
2133
}
2234

35+
impl std::fmt::Display for BasicUnaryOp {
36+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37+
match self {
38+
BasicUnaryOp::Negate => write!(f, "~"),
39+
BasicUnaryOp::AbsVal => write!(f, "abs"),
40+
}
41+
}
42+
}
43+
2344
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
2445
pub enum NumRep {
2546
Auto,
@@ -343,6 +364,15 @@ pub struct BinOp {
343364
out_rep: Option<NumRep>,
344365
}
345366

367+
impl std::fmt::Display for BinOp {
368+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
369+
match self.out_rep {
370+
None => write!(f, "{}", self.op),
371+
Some(rep) => write!(f, "{}{}", self.op, rep),
372+
}
373+
}
374+
}
375+
346376
impl BinOp {
347377
pub const fn new(op: BasicBinOp, out_rep: Option<NumRep>) -> Self {
348378
Self { op, out_rep }
@@ -380,6 +410,15 @@ pub struct UnaryOp {
380410
out_rep: Option<NumRep>,
381411
}
382412

413+
impl std::fmt::Display for UnaryOp {
414+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
415+
match self.out_rep {
416+
None => write!(f, "{}", self.op),
417+
Some(rep) => write!(f, "{}{}", self.op, rep),
418+
}
419+
}
420+
}
421+
383422
impl UnaryOp {
384423
pub const fn new(op: BasicUnaryOp, out_rep: Option<NumRep>) -> Self {
385424
Self { op, out_rep }
@@ -406,7 +445,7 @@ impl UnaryOp {
406445
}
407446
}
408447

409-
#[derive(Clone, Debug)]
448+
#[derive(Clone)]
410449
pub enum Expr {
411450
Const(TypedConst),
412451
BinOp(BinOp, Box<Expr>, Box<Expr>),
@@ -415,6 +454,17 @@ pub enum Expr {
415454
// TryUnwrap(Box<Expr>),
416455
}
417456

457+
impl std::fmt::Debug for Expr {
458+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
459+
match self {
460+
Self::Const(arg0) => write!(f, "{}", arg0),
461+
Self::BinOp(op, lhs, rhs) => write!(f, "({:?} {} {:?})", lhs, op, rhs),
462+
Self::UnaryOp(op, inner) => write!(f, "{}({:?})", op, inner),
463+
Self::Cast(rep, inner) => write!(f, "Cast({:?}, {:?})", inner, rep),
464+
}
465+
}
466+
}
467+
418468
impl Expr {
419469
// pub(crate) fn get_rep(&self) -> Option<NumRep> {
420470
// match self {

0 commit comments

Comments
 (0)