Skip to main content

oxilean_codegen/lcnf/
lcnfexpr_traits.rs

1//! # LcnfExpr - Trait Implementations
2//!
3//! This module contains trait implementations for `LcnfExpr`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use super::types::LcnfExpr;
12use std::fmt;
13
14impl fmt::Display for LcnfExpr {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            LcnfExpr::Let {
18                id,
19                name,
20                ty,
21                value,
22                body,
23            } => {
24                writeln!(f, "let {} ({}) : {} := {};", id, name, ty, value)?;
25                write!(f, "{}", body)
26            }
27            LcnfExpr::Case {
28                scrutinee,
29                alts,
30                default,
31                ..
32            } => {
33                writeln!(f, "case {} of", scrutinee)?;
34                for alt in alts {
35                    write!(f, "  | {}", alt.ctor_name)?;
36                    for p in &alt.params {
37                        write!(f, " {}", p.id)?;
38                    }
39                    writeln!(f, " => {}", alt.body)?;
40                }
41                if let Some(def) = default {
42                    writeln!(f, "  | _ => {}", def)?;
43                }
44                Ok(())
45            }
46            LcnfExpr::Return(arg) => write!(f, "return {}", arg),
47            LcnfExpr::Unreachable => write!(f, "unreachable"),
48            LcnfExpr::TailCall(func, args) => {
49                write!(f, "tailcall {}(", func)?;
50                for (i, a) in args.iter().enumerate() {
51                    if i > 0 {
52                        write!(f, ", ")?;
53                    }
54                    write!(f, "{}", a)?;
55                }
56                write!(f, ")")
57            }
58        }
59    }
60}