microcad_lang/syntax/statement/
if_statement.rs1use crate::{src_ref::*, syntax::*};
7
8#[derive(Clone)]
10pub struct IfStatement {
11 pub cond: Expression,
13 pub body: Body,
15 pub body_else: Option<Body>,
17 pub next_if: Option<Box<IfStatement>>,
19 pub src_ref: SrcRef,
21}
22
23impl SrcReferrer for IfStatement {
24 fn src_ref(&self) -> SrcRef {
25 self.src_ref.clone()
26 }
27}
28
29impl std::fmt::Display for IfStatement {
30 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
31 writeln!(f, "if {cond} {body}", cond = self.cond, body = self.body)?;
32 if let Some(body) = &self.body_else {
33 writeln!(f, "else {body}")?;
34 }
35 Ok(())
36 }
37}
38
39impl std::fmt::Debug for IfStatement {
40 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41 writeln!(
42 f,
43 "if {cond:?} {body:?}",
44 cond = self.cond,
45 body = self.body
46 )?;
47 if let Some(body) = &self.body_else {
48 writeln!(f, "else {body:?}")?;
49 }
50 Ok(())
51 }
52}
53
54impl TreeDisplay for IfStatement {
55 fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
56 writeln!(f, "{:depth$}IfStatement:", "")?;
57 depth.indent();
58 writeln!(f, "{:depth$}Condition:", "")?;
59 self.cond.tree_print(f, depth.indented())?;
60 writeln!(f, "{:depth$}If:", "")?;
61 self.body.tree_print(f, depth.indented())?;
62 if let Some(body_else) = &self.body_else {
63 writeln!(f, "{:depth$}Else:", "")?;
64 body_else.tree_print(f, depth.indented())?;
65 }
66 Ok(())
67 }
68}