Skip to main content

oxilean_codegen/chisel_backend/
chiselexpr_traits.rs

1//! # ChiselExpr - Trait Implementations
2//!
3//! This module contains trait implementations for `ChiselExpr`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use super::types::ChiselExpr;
12use std::fmt;
13
14impl fmt::Display for ChiselExpr {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            ChiselExpr::ULit(v, w) => write!(f, "{v}.U({w}.W)"),
18            ChiselExpr::SLit(v, w) => write!(f, "{v}.S({w}.W)"),
19            ChiselExpr::BoolLit(b) => {
20                write!(f, "{}.B", if *b { "true" } else { "false" })
21            }
22            ChiselExpr::Var(s) => write!(f, "{s}"),
23            ChiselExpr::Io(name) => write!(f, "io.{name}"),
24            ChiselExpr::RegField(n) => write!(f, "reg_{n}"),
25            ChiselExpr::BinOp(l, op, r) => write!(f, "({l} {op} {r})"),
26            ChiselExpr::UnOp(op, e) => write!(f, "({op}({e}))"),
27            ChiselExpr::Mux(c, t, e) => write!(f, "Mux({c}, {t}, {e})"),
28            ChiselExpr::BitSlice(e, hi, lo) => write!(f, "{e}({hi}, {lo})"),
29            ChiselExpr::Cat(parts) => {
30                write!(f, "Cat(")?;
31                for (i, p) in parts.iter().enumerate() {
32                    if i > 0 {
33                        write!(f, ", ")?;
34                    }
35                    write!(f, "{p}")?;
36                }
37                write!(f, ")")
38            }
39            ChiselExpr::MethodCall(recv, method, args) => {
40                write!(f, "{recv}.{method}(")?;
41                for (i, a) in args.iter().enumerate() {
42                    if i > 0 {
43                        write!(f, ", ")?;
44                    }
45                    write!(f, "{a}")?;
46                }
47                write!(f, ")")
48            }
49        }
50    }
51}