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, Debug, 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}
92
93impl TreeDisplay for Statement {
94    fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
95        // statement is transparent
96        match self {
97            Self::Workbench(w) => w.tree_print(f, depth),
98            Self::Module(m) => m.tree_print(f, depth),
99            Self::Function(func) => func.tree_print(f, depth),
100            Self::Init(i) => i.tree_print(f, depth),
101
102            Self::Use(u) => u.tree_print(f, depth),
103            Self::Return(r) => r.tree_print(f, depth),
104            Self::If(i) => i.tree_print(f, depth),
105            Self::InnerAttribute(i) => i.tree_print(f, depth),
106
107            Self::Assignment(a) => a.tree_print(f, depth),
108            Self::Expression(e) => e.tree_print(f, depth),
109        }
110    }
111}