microcad_lang/syntax/statement/
mod.rs1use std::rc::Rc;
7
8use crate::syntax::*;
9
10mod assignment_statement;
11mod expression_statement;
12mod if_statement;
13mod inner_doc_comment;
14mod return_statement;
15mod statement_list;
16
17pub use assignment_statement::*;
18pub use expression_statement::*;
19pub use if_statement::*;
20pub use inner_doc_comment::*;
21use microcad_lang_base::{SrcRef, SrcReferrer, TreeDisplay, TreeState};
22pub use return_statement::*;
23pub use statement_list::*;
24
25#[derive(Clone, strum::IntoStaticStr)]
27pub enum Statement {
28 Workbench(Rc<WorkbenchDefinition>),
30 Module(Rc<ModuleDefinition>),
32 Function(Rc<FunctionDefinition>),
34 Init(Rc<InitDefinition>),
36
37 Use(UseStatement),
39 Return(ReturnStatement),
41 If(IfStatement),
43 InnerAttribute(Attribute),
45 InnerDocComment(InnerDocComment),
47
48 Assignment(AssignmentStatement),
50 Expression(ExpressionStatement),
52}
53
54impl SrcReferrer for Statement {
55 fn src_ref(&self) -> SrcRef {
56 match self {
57 Self::Workbench(w) => w.src_ref(),
58 Self::Module(m) => m.src_ref(),
59 Self::Function(fd) => fd.src_ref(),
60 Self::Init(mid) => mid.src_ref(),
61
62 Self::Use(us) => us.src_ref(),
63 Self::Return(r) => r.src_ref(),
64 Self::If(i) => i.src_ref(),
65 Self::InnerAttribute(i) => i.src_ref(),
66 Self::InnerDocComment(i) => i.src_ref(),
67
68 Self::Assignment(a) => a.src_ref(),
69 Self::Expression(e) => e.src_ref(),
70 }
71 }
72}
73
74impl std::fmt::Display for Statement {
75 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
76 match self {
77 Self::Workbench(w) => {
78 write!(f, "{w}")
79 }
80 Self::Module(m) => {
81 write!(f, "{}", m.id_ref())
82 }
83 Self::Function(f_) => {
84 write!(f, "{}", f_.id_ref())
85 }
86 Self::Init(mi) => {
87 write!(f, "{mi}")
88 }
89
90 Self::Use(u) => write!(f, "{u};"),
91 Self::Return(r) => write!(f, "{r};"),
92 Self::If(i) => write!(f, "{i}"),
93 Self::InnerAttribute(i) => write!(f, "{i}"),
94 Self::InnerDocComment(i) => write!(f, "{i}"),
95
96 Self::Assignment(a) => write!(f, "{a}"),
97 Self::Expression(e) => write!(f, "{e}"),
98 }
99 }
100}
101impl std::fmt::Debug for Statement {
102 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
103 match self {
104 Self::Workbench(w) => {
105 write!(f, "{w:?}")
106 }
107 Self::Module(m) => {
108 write!(f, "{:?}", m.id_ref())
109 }
110 Self::Function(f_) => {
111 write!(f, "{:?}", f_.id_ref())
112 }
113 Self::Init(mi) => {
114 write!(f, "{mi:?}")
115 }
116
117 Self::Use(u) => write!(f, "{u:?};"),
118 Self::Return(r) => write!(f, "{r:?};"),
119 Self::If(i) => write!(f, "{i:?}"),
120 Self::InnerAttribute(i) => write!(f, "{i:?}"),
121 Self::InnerDocComment(i) => write!(f, "{i:?}"),
122
123 Self::Assignment(a) => write!(f, "{a:?}"),
124 Self::Expression(e) => write!(f, "{e:?}"),
125 }
126 }
127}
128
129impl TreeDisplay for Statement {
130 fn tree_print(&self, f: &mut std::fmt::Formatter, depth: TreeState) -> std::fmt::Result {
131 match self {
133 Self::Workbench(w) => w.tree_print(f, depth),
134 Self::Module(m) => m.tree_print(f, depth),
135 Self::Function(func) => func.tree_print(f, depth),
136 Self::Init(i) => i.tree_print(f, depth),
137
138 Self::Use(u) => u.tree_print(f, depth),
139 Self::Return(r) => r.tree_print(f, depth),
140 Self::If(i) => i.tree_print(f, depth),
141 Self::InnerAttribute(i) => i.tree_print(f, depth),
142 Self::InnerDocComment(i) => i.tree_print(f, depth),
143
144 Self::Assignment(a) => a.tree_print(f, depth),
145 Self::Expression(e) => e.tree_print(f, depth),
146 }
147 }
148}