microcad_lang/syntax/statement/
mod.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! statement syntax elements
5
6use crate::{rc::*, src_ref::*, syntax::*};
7
8mod assignment_statement;
9mod expression_statement;
10mod if_statement;
11mod return_statement;
12mod statement_list;
13
14pub use assignment_statement::*;
15pub use expression_statement::*;
16pub use if_statement::*;
17pub use return_statement::*;
18pub use statement_list::*;
19
20/// Any statement.
21#[derive(Clone, strum::IntoStaticStr)]
22pub enum Statement {
23    /// Part definition
24    Workbench(Rc<WorkbenchDefinition>),
25    /// Module definition
26    Module(Rc<ModuleDefinition>),
27    /// Function definition
28    Function(Rc<FunctionDefinition>),
29    /// Init definition
30    Init(Rc<InitDefinition>),
31
32    /// Use statement
33    Use(UseStatement),
34    /// Return statement
35    Return(ReturnStatement),
36    /// If statement
37    If(IfStatement),
38    /// Inner attribute statement: `#![size = std::A4]`.
39    InnerAttribute(Attribute),
40
41    /// Assignment statement.
42    Assignment(AssignmentStatement),
43    /// Expression statement.
44    Expression(ExpressionStatement),
45}
46
47impl SrcReferrer for Statement {
48    fn src_ref(&self) -> SrcRef {
49        match self {
50            Self::Workbench(w) => w.src_ref(),
51            Self::Module(m) => m.src_ref(),
52            Self::Function(fd) => fd.src_ref(),
53            Self::Init(mid) => mid.src_ref(),
54
55            Self::Use(us) => us.src_ref(),
56            Self::Return(r) => r.src_ref(),
57            Self::If(i) => i.src_ref(),
58            Self::InnerAttribute(i) => i.src_ref(),
59
60            Self::Assignment(a) => a.src_ref(),
61            Self::Expression(e) => e.src_ref(),
62        }
63    }
64}
65
66impl std::fmt::Display for Statement {
67    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
68        match self {
69            Self::Workbench(w) => {
70                write!(f, "{w}")
71            }
72            Self::Module(m) => {
73                write!(f, "{}", m.id)
74            }
75            Self::Function(_f) => {
76                write!(f, "{}", _f.id)
77            }
78            Self::Init(mi) => {
79                write!(f, "{mi}")
80            }
81
82            Self::Use(u) => write!(f, "{u};"),
83            Self::Return(r) => write!(f, "{r};"),
84            Self::If(i) => write!(f, "{i}"),
85            Self::InnerAttribute(i) => write!(f, "{i}"),
86
87            Self::Assignment(a) => write!(f, "{a}"),
88            Self::Expression(e) => write!(f, "{e}"),
89        }
90    }
91}
92impl std::fmt::Debug for Statement {
93    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
94        match self {
95            Self::Workbench(w) => {
96                write!(f, "{w:?}")
97            }
98            Self::Module(m) => {
99                write!(f, "{:?}", m.id)
100            }
101            Self::Function(_f) => {
102                write!(f, "{:?}", _f.id)
103            }
104            Self::Init(mi) => {
105                write!(f, "{mi:?}")
106            }
107
108            Self::Use(u) => write!(f, "{u:?};"),
109            Self::Return(r) => write!(f, "{r:?};"),
110            Self::If(i) => write!(f, "{i:?}"),
111            Self::InnerAttribute(i) => write!(f, "{i:?}"),
112
113            Self::Assignment(a) => write!(f, "{a:?}"),
114            Self::Expression(e) => write!(f, "{e:?}"),
115        }
116    }
117}
118
119impl TreeDisplay for Statement {
120    fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
121        // statement is transparent
122        match self {
123            Self::Workbench(w) => w.tree_print(f, depth),
124            Self::Module(m) => m.tree_print(f, depth),
125            Self::Function(func) => func.tree_print(f, depth),
126            Self::Init(i) => i.tree_print(f, depth),
127
128            Self::Use(u) => u.tree_print(f, depth),
129            Self::Return(r) => r.tree_print(f, depth),
130            Self::If(i) => i.tree_print(f, depth),
131            Self::InnerAttribute(i) => i.tree_print(f, depth),
132
133            Self::Assignment(a) => a.tree_print(f, depth),
134            Self::Expression(e) => e.tree_print(f, depth),
135        }
136    }
137}