1#![allow(unused_assignments)]
2
3use enum_as_inner::EnumAsInner;
4
5use crate::Span;
6use crate::pr::{BinOp, Literal, Ty, UnOp};
7
8use super::{Path, TyParam};
9
10#[derive(Debug, Clone, PartialEq)]
13pub struct Expr {
14 pub kind: ExprKind,
15
16 pub span: Option<Span>,
17
18 pub ty: Option<Ty>,
21
22 pub ty_args: Vec<Ty>,
26
27 pub scope_id: Option<usize>,
31
32 pub target: Option<Ref>,
35}
36
37impl Expr {
38 pub fn new<K: Into<ExprKind>>(kind: K) -> Self {
39 Expr {
40 kind: kind.into(),
41 span: None,
42 ty: None,
43 ty_args: Vec::new(),
44 target: None,
45 scope_id: None,
46 }
47 }
48
49 pub fn new_with_span<K: Into<ExprKind>>(kind: K, span: Span) -> Expr {
50 Expr {
51 kind: kind.into(),
52 span: Some(span),
53 ty: None,
54 ty_args: Vec::new(),
55 target: None,
56 scope_id: None,
57 }
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Hash)]
62pub enum Ref {
63 Global(Path),
64 Local {
65 scope: usize,
67
68 offset: usize,
70 },
71}
72
73#[derive(Debug, EnumAsInner, PartialEq, Clone, strum::AsRefStr)]
74pub enum ExprKind {
75 Ident(Path),
76
77 Lookup {
80 base: Box<Expr>,
81 lookup: Lookup,
82 },
83 Literal(Literal),
84 Nested(Box<Expr>),
85 TypeAnnotation(TypeAnnotation),
86
87 Tuple(Vec<TupleField>),
88 Array(Vec<Expr>),
89 Variant(Variant),
90
91 Range(Range),
92 Binary(BinaryExpr),
93 Unary(UnaryExpr),
94 Call(Call),
95 Func(Box<Func>),
96 FuncShort(Box<FuncShort>),
97 FString(Vec<InterpolateItem>),
98 Match(Match),
99 If(If),
100
101 VarBinding(VarBinding),
102}
103
104#[derive(Debug, EnumAsInner, PartialEq, Clone)]
105pub enum Lookup {
106 Name(String),
107 Position(i64),
108}
109
110#[derive(Debug, PartialEq, Clone)]
112pub struct BinaryExpr {
113 pub left: Box<Expr>,
114 pub op: BinOp,
115 pub right: Box<Expr>,
116}
117
118#[derive(Debug, PartialEq, Clone)]
120pub struct UnaryExpr {
121 pub op: UnOp,
122 pub expr: Box<Expr>,
123}
124
125#[derive(Debug, PartialEq, Clone)]
127pub struct Call {
128 pub subject: Box<Expr>,
129 pub args: Vec<CallArg>,
130}
131
132#[derive(Debug, PartialEq, Clone)]
133pub struct CallArg {
134 pub label: Option<String>,
135 pub expr: Expr,
136 pub span: Option<Span>,
137}
138
139impl CallArg {
140 pub fn simple(expr: Expr) -> Self {
141 CallArg {
142 label: None,
143 span: expr.span,
144 expr,
145 }
146 }
147}
148
149#[derive(Debug, PartialEq, Clone)]
151pub struct Func {
152 pub params: Vec<FuncParam>,
154
155 pub return_ty: Option<Ty>,
157
158 pub body: Option<Box<Expr>>,
160
161 pub ty_params: Vec<TyParam>,
163}
164
165#[derive(Debug, PartialEq, Clone)]
167pub struct FuncShort {
168 pub param: FuncParam,
170
171 pub body: Box<Expr>,
173}
174
175#[derive(Debug, PartialEq, Clone)]
176pub struct FuncParam {
177 pub constant: bool,
179
180 pub label: Option<String>,
182
183 pub name: String,
185
186 pub ty: Option<Ty>,
188
189 pub span: Span,
190}
191
192#[derive(Debug, PartialEq, Clone)]
194pub struct Pipeline {
195 pub exprs: Vec<Expr>,
197}
198
199#[derive(Debug, PartialEq, Clone)]
200pub struct TypeAnnotation {
201 pub expr: Box<Expr>,
202 pub ty: Box<Ty>,
203}
204
205#[derive(Debug, PartialEq, Clone)]
207pub struct TupleField {
208 pub name: Option<String>,
209 pub unpack: bool,
210 pub expr: Expr,
211}
212
213#[derive(Debug, PartialEq, Clone)]
215pub struct Variant {
216 pub name: String,
217 pub inner: Option<Box<Expr>>,
218}
219
220#[derive(Debug, Clone, Default, PartialEq)]
223pub struct Range {
224 pub start: Option<Box<Expr>>,
225 pub end: Option<Box<Expr>>,
226}
227
228impl Range {
229 pub const fn unbounded() -> Self {
230 Range {
231 start: None,
232 end: None,
233 }
234 }
235}
236
237#[derive(Debug, Clone, PartialEq)]
238pub enum InterpolateItem {
239 String(String),
240 Expr {
241 expr: Box<Expr>,
242 format: Option<String>,
243 },
244}
245
246#[derive(Debug, Clone, PartialEq)]
247pub struct Match {
248 pub subject: Box<Expr>,
249
250 pub branches: Vec<MatchBranch>,
252}
253
254#[derive(Debug, Clone, PartialEq)]
255pub struct MatchBranch {
256 pub pattern: Pattern,
257 pub value: Box<Expr>,
258}
259
260#[derive(Debug, Clone, PartialEq)]
262pub struct Pattern {
263 pub kind: PatternKind,
264 pub span: Span,
265 pub variant_tag: Option<usize>,
266}
267
268#[derive(Debug, Clone, PartialEq, strum::AsRefStr)]
269pub enum PatternKind {
270 Enum(String, Option<Box<Pattern>>),
272
273 Literal(Literal),
275
276 AnyOf(Vec<Pattern>),
278
279 Bind(String),
281}
282
283impl Pattern {
284 pub fn new_with_span(kind: PatternKind, span: Span) -> Self {
285 Self {
286 kind,
287 span,
288 variant_tag: None,
289 }
290 }
291}
292
293#[derive(Debug, Clone, PartialEq)]
294pub struct If {
295 pub condition: Box<Expr>,
296 pub then: Box<Expr>,
297 pub els: Box<Expr>,
298}
299
300#[derive(Debug, Clone, PartialEq)]
301pub struct VarBinding {
302 pub name: String,
303 pub bound: Box<Expr>,
304 pub main: Box<Expr>,
305}
306
307impl From<Literal> for ExprKind {
308 fn from(value: Literal) -> Self {
309 ExprKind::Literal(value)
310 }
311}
312
313impl From<Func> for ExprKind {
314 fn from(value: Func) -> Self {
315 ExprKind::Func(Box::new(value))
316 }
317}
318
319impl From<Path> for ExprKind {
320 fn from(value: Path) -> Self {
321 ExprKind::Ident(value)
322 }
323}
324
325impl From<Range> for ExprKind {
326 fn from(value: Range) -> Self {
327 ExprKind::Range(value)
328 }
329}
330
331impl From<Variant> for ExprKind {
332 fn from(value: Variant) -> Self {
333 ExprKind::Variant(value)
334 }
335}
336
337impl From<TypeAnnotation> for ExprKind {
338 fn from(value: TypeAnnotation) -> Self {
339 ExprKind::TypeAnnotation(value)
340 }
341}