Skip to main content

oxilean_codegen/scala_backend/
scalaexpr_traits.rs

1//! # ScalaExpr - Trait Implementations
2//!
3//! This module contains trait implementations for `ScalaExpr`.
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::{ScalaEnumerator, ScalaExpr};
14use std::fmt;
15
16impl fmt::Display for ScalaExpr {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            ScalaExpr::Lit(lit) => write!(f, "{}", lit),
20            ScalaExpr::Var(v) => write!(f, "{}", v),
21            ScalaExpr::App(func, args) => {
22                write!(f, "{}(", func)?;
23                for (i, a) in args.iter().enumerate() {
24                    if i > 0 {
25                        write!(f, ", ")?;
26                    }
27                    write!(f, "{}", a)?;
28                }
29                write!(f, ")")
30            }
31            ScalaExpr::Infix(lhs, op, rhs) => write!(f, "({} {} {})", lhs, op, rhs),
32            ScalaExpr::Prefix(op, operand) => write!(f, "{}({})", op, operand),
33            ScalaExpr::If(cond, then_e, else_e) => {
34                write!(f, "if {} then {} else {}", cond, then_e, else_e)
35            }
36            ScalaExpr::Match(scrut, arms) => {
37                writeln!(f, "{} match {{", scrut)?;
38                for arm in arms {
39                    write!(f, "  case {}", arm.pattern)?;
40                    if let Some(guard) = &arm.guard {
41                        write!(f, " if {}", guard)?;
42                    }
43                    writeln!(f, " => {}", arm.body)?;
44                }
45                write!(f, "}}")
46            }
47            ScalaExpr::For(enumerators, body) => {
48                writeln!(f, "for {{")?;
49                for e in enumerators {
50                    match e {
51                        ScalaEnumerator::Generator(v, expr) => {
52                            writeln!(f, "  {} <- {}", v, expr)?;
53                        }
54                        ScalaEnumerator::Guard(expr) => {
55                            writeln!(f, "  if {}", expr)?;
56                        }
57                        ScalaEnumerator::Definition(v, expr) => {
58                            writeln!(f, "  {} = {}", v, expr)?;
59                        }
60                    }
61                }
62                write!(f, "}} yield {}", body)
63            }
64            ScalaExpr::Try(body, catches, finally) => {
65                write!(f, "try {{ {} }}", body)?;
66                if !catches.is_empty() {
67                    writeln!(f, " catch {{")?;
68                    for c in catches {
69                        writeln!(f, "  case {} => {}", c.pattern, c.body)?;
70                    }
71                    write!(f, "}}")?;
72                }
73                if let Some(fin) = finally {
74                    write!(f, " finally {{ {} }}", fin)?;
75                }
76                Ok(())
77            }
78            ScalaExpr::Lambda(params, body) => {
79                if params.len() == 1 {
80                    write!(f, "{} => {}", params[0], body)
81                } else {
82                    write!(f, "(")?;
83                    for (i, p) in params.iter().enumerate() {
84                        if i > 0 {
85                            write!(f, ", ")?;
86                        }
87                        write!(f, "{}", p)?;
88                    }
89                    write!(f, ") => {}", body)
90                }
91            }
92            ScalaExpr::Block(stmts, last) => {
93                writeln!(f, "{{")?;
94                for s in stmts {
95                    writeln!(f, "  {}", s)?;
96                }
97                write!(f, "  {}\n}}", last)
98            }
99            ScalaExpr::New(cls, args) => {
100                write!(f, "new {}(", cls)?;
101                for (i, a) in args.iter().enumerate() {
102                    if i > 0 {
103                        write!(f, ", ")?;
104                    }
105                    write!(f, "{}", a)?;
106                }
107                write!(f, ")")
108            }
109            ScalaExpr::This => write!(f, "this"),
110            ScalaExpr::Super => write!(f, "super"),
111            ScalaExpr::Assign(name, val) => write!(f, "{} = {}", name, val),
112            ScalaExpr::TypeAnnotation(expr, ty) => write!(f, "({}: {})", expr, ty),
113            ScalaExpr::Throw(expr) => write!(f, "throw {}", expr),
114        }
115    }
116}