oxilean_codegen/matlab_backend/
matlabexpr_traits.rs1use super::types::MatlabExpr;
12use std::fmt;
13
14impl std::fmt::Display for MatlabExpr {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 match self {
17 MatlabExpr::Lit(lit) => write!(f, "{}", lit),
18 MatlabExpr::Var(name) => write!(f, "{}", name),
19 MatlabExpr::MatrixLit(rows) => {
20 write!(f, "[")?;
21 for (i, row) in rows.iter().enumerate() {
22 if i > 0 {
23 write!(f, "; ")?;
24 }
25 let elems: Vec<String> = row.iter().map(|e| e.to_string()).collect();
26 write!(f, "{}", elems.join(", "))?;
27 }
28 write!(f, "]")
29 }
30 MatlabExpr::CellLit(rows) => {
31 write!(f, "{{")?;
32 for (i, row) in rows.iter().enumerate() {
33 if i > 0 {
34 write!(f, "; ")?;
35 }
36 let elems: Vec<String> = row.iter().map(|e| e.to_string()).collect();
37 write!(f, "{}", elems.join(", "))?;
38 }
39 write!(f, "}}")
40 }
41 MatlabExpr::ColonRange { start, step, end } => {
42 if let Some(step_expr) = step {
43 write!(f, "{}:{}:{}", start, step_expr, end)
44 } else {
45 write!(f, "{}:{}", start, end)
46 }
47 }
48 MatlabExpr::Call(func, args) => {
49 let args_str: Vec<String> = args.iter().map(|a| a.to_string()).collect();
50 write!(f, "{}({})", func, args_str.join(", "))
51 }
52 MatlabExpr::Index {
53 obj,
54 indices,
55 cell_index,
56 } => {
57 let idx_str: Vec<String> = indices.iter().map(|i| i.to_string()).collect();
58 let (open, close) = if *cell_index { ("{", "}") } else { ("(", ")") };
59 write!(f, "{}{}{}{}", obj, open, idx_str.join(", "), close)
60 }
61 MatlabExpr::FieldAccess(obj, field) => write!(f, "{}.{}", obj, field),
62 MatlabExpr::BinaryOp(op, lhs, rhs) => write!(f, "{} {} {}", lhs, op, rhs),
63 MatlabExpr::UnaryOp(op, operand, postfix) => {
64 if *postfix {
65 write!(f, "{}{}", operand, op)
66 } else {
67 write!(f, "{}{}", op, operand)
68 }
69 }
70 MatlabExpr::IfExpr(cond, then_expr, else_expr) => {
71 write!(f, "({{{0}; {1}}}{{{2}+1}})", else_expr, then_expr, cond)
72 }
73 MatlabExpr::AnonFunc(params, body) => {
74 write!(f, "@({}) {}", params.join(", "), body)
75 }
76 MatlabExpr::End => write!(f, "end"),
77 MatlabExpr::Colon => write!(f, ":"),
78 MatlabExpr::Nargin => write!(f, "nargin"),
79 MatlabExpr::Nargout => write!(f, "nargout"),
80 }
81 }
82}