Skip to main content

oxilean_codegen/csharp_backend/
csharpexpr_traits.rs

1//! # CSharpExpr - Trait Implementations
2//!
3//! This module contains trait implementations for `CSharpExpr`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::lcnf::*;
12
13use super::types::{CSharpExpr, CSharpInterpolationPart};
14use std::fmt;
15
16impl fmt::Display for CSharpExpr {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            CSharpExpr::Lit(lit) => write!(f, "{}", lit),
20            CSharpExpr::Var(name) => write!(f, "{}", name),
21            CSharpExpr::Null => write!(f, "null"),
22            CSharpExpr::Default(None) => write!(f, "default"),
23            CSharpExpr::Default(Some(ty)) => write!(f, "default({})", ty),
24            CSharpExpr::NameOf(name) => write!(f, "nameof({})", name),
25            CSharpExpr::TypeOf(ty) => write!(f, "typeof({})", ty),
26            CSharpExpr::BinOp { op, lhs, rhs } => write!(f, "({} {} {})", lhs, op, rhs),
27            CSharpExpr::UnaryOp { op, operand } => write!(f, "{}({})", op, operand),
28            CSharpExpr::Call { callee, args } => {
29                write!(f, "{}(", callee)?;
30                for (i, a) in args.iter().enumerate() {
31                    if i > 0 {
32                        write!(f, ", ")?;
33                    }
34                    write!(f, "{}", a)?;
35                }
36                write!(f, ")")
37            }
38            CSharpExpr::MethodCall {
39                receiver,
40                method,
41                type_args,
42                args,
43            } => {
44                write!(f, "{}.{}", receiver, method)?;
45                if !type_args.is_empty() {
46                    write!(f, "<")?;
47                    for (i, t) in type_args.iter().enumerate() {
48                        if i > 0 {
49                            write!(f, ", ")?;
50                        }
51                        write!(f, "{}", t)?;
52                    }
53                    write!(f, ">")?;
54                }
55                write!(f, "(")?;
56                for (i, a) in args.iter().enumerate() {
57                    if i > 0 {
58                        write!(f, ", ")?;
59                    }
60                    write!(f, "{}", a)?;
61                }
62                write!(f, ")")
63            }
64            CSharpExpr::New { ty, args } => {
65                write!(f, "new {}(", ty)?;
66                for (i, a) in args.iter().enumerate() {
67                    if i > 0 {
68                        write!(f, ", ")?;
69                    }
70                    write!(f, "{}", a)?;
71                }
72                write!(f, ")")
73            }
74            CSharpExpr::Lambda { params, body } => {
75                if params.len() == 1 && params[0].1.is_none() {
76                    write!(f, "{} => {}", params[0].0, body)
77                } else {
78                    write!(f, "(")?;
79                    for (i, (name, ty)) in params.iter().enumerate() {
80                        if i > 0 {
81                            write!(f, ", ")?;
82                        }
83                        if let Some(t) = ty {
84                            write!(f, "{} {}", t, name)?;
85                        } else {
86                            write!(f, "{}", name)?;
87                        }
88                    }
89                    write!(f, ") => {}", body)
90                }
91            }
92            CSharpExpr::Ternary {
93                cond,
94                then_expr,
95                else_expr,
96            } => {
97                write!(f, "({} ? {} : {})", cond, then_expr, else_expr)
98            }
99            CSharpExpr::Await(expr) => write!(f, "await {}", expr),
100            CSharpExpr::Throw(expr) => write!(f, "throw {}", expr),
101            CSharpExpr::Is { expr, pattern } => write!(f, "({} is {})", expr, pattern),
102            CSharpExpr::As { expr, ty } => write!(f, "({} as {})", expr, ty),
103            CSharpExpr::Member(base, field) => write!(f, "{}.{}", base, field),
104            CSharpExpr::Index(base, idx) => write!(f, "{}[{}]", base, idx),
105            CSharpExpr::SwitchExpr { scrutinee, arms } => {
106                write!(f, "{} switch\n    {{", scrutinee)?;
107                for arm in arms {
108                    if let Some(guard) = &arm.guard {
109                        write!(
110                            f,
111                            "\n        {} when {} => {}",
112                            arm.pattern, guard, arm.body
113                        )?;
114                    } else {
115                        write!(f, "\n        {} => {}", arm.pattern, arm.body)?;
116                    }
117                    write!(f, ",")?;
118                }
119                write!(f, "\n    }}")
120            }
121            CSharpExpr::Interpolated(parts) => {
122                write!(f, "$\"")?;
123                for part in parts {
124                    match part {
125                        CSharpInterpolationPart::Text(s) => write!(f, "{}", s)?,
126                        CSharpInterpolationPart::Expr(e) => write!(f, "{{{}}}", e)?,
127                        CSharpInterpolationPart::ExprFmt(e, fmt_spec) => {
128                            write!(f, "{{{}:{}}}", e, fmt_spec)?
129                        }
130                    }
131                }
132                write!(f, "\"")
133            }
134            CSharpExpr::CollectionExpr(elems) => {
135                write!(f, "[")?;
136                for (i, e) in elems.iter().enumerate() {
137                    if i > 0 {
138                        write!(f, ", ")?;
139                    }
140                    write!(f, "{}", e)?;
141                }
142                write!(f, "]")
143            }
144        }
145    }
146}