1use crate::span::Span;
5
6#[derive(Debug, Clone, PartialEq)]
8pub struct SourceFile {
9 pub statements: Vec<Statement>,
10 pub span: Span,
11}
12
13#[derive(Debug, Clone, PartialEq)]
15pub enum Statement {
16 Include { path: String, span: Span },
18 Use { path: String, span: Span },
20 Assignment {
22 name: String,
23 expr: Expr,
24 span: Span,
25 },
26 ModuleDefinition {
28 name: String,
29 params: Vec<Parameter>,
30 body: Vec<Self>,
31 span: Span,
32 },
33 FunctionDefinition {
35 name: String,
36 params: Vec<Parameter>,
37 body: Expr,
38 span: Span,
39 },
40 ModuleInstantiation {
42 name: String,
43 args: Vec<Argument>,
44 children: Vec<Self>,
45 modifiers: Modifiers,
46 span: Span,
47 },
48 IfElse {
50 condition: Expr,
51 then_body: Vec<Self>,
52 else_body: Option<Vec<Self>>,
53 span: Span,
54 },
55 Block { body: Vec<Self>, span: Span },
57 Empty { span: Span },
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
63#[allow(clippy::struct_excessive_bools)]
64pub struct Modifiers {
65 pub root: bool,
67 pub highlight: bool,
69 pub background: bool,
71 pub disable: bool,
73}
74
75#[derive(Debug, Clone, PartialEq)]
77pub struct Expr {
78 pub kind: ExprKind,
79 pub span: Span,
80}
81
82impl Expr {
83 #[must_use]
84 pub const fn new(kind: ExprKind, span: Span) -> Self {
85 Self { kind, span }
86 }
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub enum ExprKind {
91 Number(f64),
93 String(String),
95 BoolTrue,
97 BoolFalse,
99 Undef,
101 Identifier(String),
103
104 UnaryOp { op: UnaryOp, operand: Box<Expr> },
106 BinaryOp {
108 op: BinaryOp,
109 left: Box<Expr>,
110 right: Box<Expr>,
111 },
112 Ternary {
114 condition: Box<Expr>,
115 then_expr: Box<Expr>,
116 else_expr: Box<Expr>,
117 },
118
119 FunctionCall {
121 callee: Box<Expr>,
122 args: Vec<Argument>,
123 },
124 Index { object: Box<Expr>, index: Box<Expr> },
126 MemberAccess { object: Box<Expr>, member: String },
128
129 Vector(Vec<Expr>),
131 Range {
133 start: Box<Expr>,
134 step: Option<Box<Expr>>,
135 end: Box<Expr>,
136 },
137
138 Let {
140 assignments: Vec<Argument>,
141 body: Box<Expr>,
142 },
143 Assert {
145 args: Vec<Argument>,
146 body: Option<Box<Expr>>,
147 },
148 Echo {
150 args: Vec<Argument>,
151 body: Option<Box<Expr>>,
152 },
153
154 AnonymousFunction {
156 params: Vec<Parameter>,
157 body: Box<Expr>,
158 },
159
160 LcFor {
163 assignments: Vec<Argument>,
164 body: Box<Expr>,
165 },
166 LcForC {
168 init: Vec<Argument>,
169 condition: Box<Expr>,
170 update: Vec<Argument>,
171 body: Box<Expr>,
172 },
173 LcIf {
175 condition: Box<Expr>,
176 then_expr: Box<Expr>,
177 else_expr: Option<Box<Expr>>,
178 },
179 LcLet {
181 assignments: Vec<Argument>,
182 body: Box<Expr>,
183 },
184 LcEach { body: Box<Expr> },
186}
187
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub enum UnaryOp {
191 Negate,
192 Not,
193 Plus,
194 BinaryNot,
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
199pub enum BinaryOp {
200 LogicalOr,
202 LogicalAnd,
203 Equal,
205 NotEqual,
206 Less,
208 LessEqual,
209 Greater,
210 GreaterEqual,
211 BitwiseOr,
213 BitwiseAnd,
214 ShiftLeft,
216 ShiftRight,
217 Add,
219 Subtract,
220 Multiply,
221 Divide,
222 Modulo,
223 Exponent,
225}
226
227#[derive(Debug, Clone, PartialEq)]
229pub struct Parameter {
230 pub name: String,
231 pub default: Option<Expr>,
232 pub span: Span,
233}
234
235#[derive(Debug, Clone, PartialEq)]
237pub struct Argument {
238 pub name: Option<String>,
239 pub value: Expr,
240 pub span: Span,
241}
242
243impl Statement {
244 #[must_use]
245 pub const fn span(&self) -> Span {
246 match self {
247 Self::Include { span, .. }
248 | Self::Use { span, .. }
249 | Self::Assignment { span, .. }
250 | Self::ModuleDefinition { span, .. }
251 | Self::FunctionDefinition { span, .. }
252 | Self::ModuleInstantiation { span, .. }
253 | Self::IfElse { span, .. }
254 | Self::Block { span, .. }
255 | Self::Empty { span, .. } => *span,
256 }
257 }
258}