1#[derive(Debug, Clone, Copy)]
3pub struct Span {
4 pub line: usize,
5 pub col: usize,
6}
7
8#[derive(Debug, Clone)]
10pub struct Program {
11 pub stmts: Vec<Stmt>,
12}
13
14#[derive(Debug, Clone)]
15pub struct Stmt {
16 pub kind: StmtKind,
17 pub span: Span,
18}
19
20#[derive(Debug, Clone)]
21pub enum StmtKind {
22 Let {
24 mutable: bool,
25 pattern: Pattern,
26 type_ann: Option<TypeAnn>,
27 value: Expr,
28 },
29 ExprStmt { expr: Expr, has_semi: bool },
31 FnDecl {
33 name: String,
34 params: Vec<Param>,
35 body: Vec<Stmt>,
36 },
37 For {
39 label: Option<String>,
40 pattern: Pattern,
41 iter: Expr,
42 body: Vec<Stmt>,
43 },
44 While {
46 label: Option<String>,
47 cond: Expr,
48 body: Vec<Stmt>,
49 },
50 WhileLet {
52 label: Option<String>,
53 pattern: Pattern,
54 expr: Expr,
55 body: Vec<Stmt>,
56 },
57 Loop {
59 label: Option<String>,
60 body: Vec<Stmt>,
61 },
62 Break {
64 label: Option<String>,
65 value: Option<Expr>,
66 },
67 Continue { label: Option<String> },
69 Use {
71 path: Vec<String>,
72 imports: UseImports,
73 },
74 Return { value: Option<Expr> },
76 Assign {
78 target: AssignTarget,
79 op: AssignOp,
80 value: Expr,
81 },
82}
83
84#[derive(Debug, Clone)]
85pub struct Param {
86 pub name: String,
87 pub default: Option<Expr>,
88}
89
90#[derive(Debug, Clone)]
91pub enum AssignTarget {
92 Ident(String),
93 Index(Box<Expr>, Box<Expr>),
94 Field(Box<Expr>, String),
95}
96
97#[derive(Debug, Clone, Copy)]
98pub enum AssignOp {
99 Eq,
100 PlusEq,
101 MinusEq,
102 StarEq,
103 SlashEq,
104}
105
106#[derive(Debug, Clone)]
107pub struct Expr {
108 pub kind: ExprKind,
109 pub span: Span,
110}
111
112#[derive(Debug, Clone)]
113pub enum ExprKind {
114 Int(i64),
116 Float(f64),
117 Bool(bool),
118 Str(String),
119 FStr(Vec<FStrPart>),
121 Bytes(Vec<u8>),
122 None,
123 Unit,
124
125 Ident(String),
127 ModulePath(Vec<String>),
129
130 SomeExpr(Box<Expr>),
133 OkExpr(Box<Expr>),
135 ErrExpr(Box<Expr>),
137
138 List(Vec<ListEntry>),
141 Dict(Vec<DictEntry>),
143 Tuple(Vec<Expr>),
145 ListComp {
147 expr: Box<Expr>,
148 pattern: Pattern,
149 iter: Box<Expr>,
150 cond: Option<Box<Expr>>,
151 },
152 DictComp {
154 key: Box<Expr>,
155 value: Box<Expr>,
156 pattern: Pattern,
157 iter: Box<Expr>,
158 cond: Option<Box<Expr>>,
159 },
160
161 BinOp {
163 left: Box<Expr>,
164 op: BinOp,
165 right: Box<Expr>,
166 },
167 UnaryOp {
168 op: UnaryOp,
169 expr: Box<Expr>,
170 },
171 Try(Box<Expr>),
173 PipeOp {
175 left: Box<Expr>,
176 right: Box<Expr>,
177 },
178
179 FieldAccess {
182 expr: Box<Expr>,
183 field: String,
184 },
185 Index {
187 expr: Box<Expr>,
188 index: Box<Expr>,
189 },
190 Slice {
192 expr: Box<Expr>,
193 start: Option<Box<Expr>>,
194 end: Option<Box<Expr>>,
195 inclusive: bool,
196 },
197 MethodCall {
199 expr: Box<Expr>,
200 method: String,
201 args: Vec<CallArg>,
202 },
203
204 Call {
207 func: Box<Expr>,
208 args: Vec<CallArg>,
209 },
210 Lambda {
212 params: Vec<String>,
213 body: Box<Expr>,
214 },
215
216 If {
219 cond: Box<Expr>,
220 then_body: Vec<Stmt>,
221 else_body: Option<Vec<Stmt>>,
222 },
223 IfLet {
225 pattern: Pattern,
226 expr: Box<Expr>,
227 then_body: Vec<Stmt>,
228 else_body: Option<Vec<Stmt>>,
229 },
230 Match {
232 expr: Box<Expr>,
233 arms: Vec<MatchArm>,
234 },
235 Block(Vec<Stmt>),
237 LoopExpr(Vec<Stmt>),
239 TryCatch {
241 body: Vec<Stmt>,
242 var: String,
243 handler: Vec<Stmt>,
244 },
245
246 StructConstruct {
248 name: String,
249 fields: Vec<(String, Expr)>,
250 spread: Option<Box<Expr>>,
251 },
252
253 EnumVariant {
255 enum_name: String,
256 variant: String,
257 },
258 EnumVariantCall {
259 enum_name: String,
260 variant: String,
261 args: Vec<Expr>,
262 },
263
264 Range {
266 start: Box<Expr>,
267 end: Box<Expr>,
268 inclusive: bool,
269 },
270
271 AsyncBlock(Vec<Stmt>),
274 SpawnExpr(Box<Expr>),
276 AwaitExpr(Box<Expr>),
278 SelectExpr(Vec<SelectBranch>),
280}
281
282#[derive(Debug, Clone)]
284pub enum ListEntry {
285 Elem(Expr),
286 Spread(Expr),
287}
288
289#[derive(Debug, Clone)]
291pub enum DictEntry {
292 KeyValue(Expr, Expr),
293 Spread(Expr),
294}
295
296#[derive(Debug, Clone)]
297pub enum FStrPart {
298 Literal(String),
299 Expr(Expr),
300}
301
302#[derive(Debug, Clone)]
303pub struct CallArg {
304 pub name: Option<String>,
305 pub value: Expr,
306}
307
308#[derive(Debug, Clone)]
310pub struct SelectBranch {
311 pub pattern: Pattern,
312 pub future_expr: Expr,
313 pub body: Expr,
314}
315
316#[derive(Debug, Clone)]
317pub struct MatchArm {
318 pub pattern: Pattern,
319 pub guard: Option<Expr>,
320 pub body: Expr,
321}
322
323#[derive(Debug, Clone)]
324pub enum Pattern {
325 Wildcard,
326 Ident(String),
327 Int(i64),
328 Float(f64),
329 Bool(bool),
330 Str(String),
331 Bytes(Vec<u8>),
332 None,
333 Some(Box<Pattern>),
335 Ok(Box<Pattern>),
337 Err(Box<Pattern>),
339 Tuple(Vec<Pattern>),
341 List(Vec<Pattern>, Option<Box<Pattern>>),
343 EnumVariant {
345 enum_name: String,
346 variant: String,
347 fields: EnumPatternFields,
348 },
349 Struct {
351 name: String,
352 fields: Vec<(String, Option<Pattern>)>,
353 },
354}
355
356#[derive(Debug, Clone)]
357pub enum EnumPatternFields {
358 None,
359 Positional(Vec<Pattern>),
360 Named(Vec<(String, Option<Pattern>)>),
361}
362
363#[derive(Debug, Clone, Copy)]
364pub enum BinOp {
365 Add,
366 Sub,
367 Mul,
368 Div,
369 Mod,
370 Eq,
371 Ne,
372 Lt,
373 Gt,
374 Le,
375 Ge,
376 And,
377 Or,
378 BitAnd,
379 BitOr,
380 BitXor,
381 Shl,
382 Shr,
383}
384
385#[derive(Debug, Clone)]
389pub enum TypeAnn {
390 Simple(String), Option(Box<TypeAnn>), Result(Box<TypeAnn>, Box<TypeAnn>), List(Box<TypeAnn>), Dict(Box<TypeAnn>, Box<TypeAnn>), }
396
397#[derive(Debug, Clone, Copy)]
398pub enum UnaryOp {
399 Neg,
400 Not,
401}
402
403#[derive(Debug, Clone)]
405pub enum UseImports {
406 Glob,
408 Names(Vec<String>),
410 Single(String),
412}