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 pattern: Pattern,
40 iter: Expr,
41 body: Vec<Stmt>,
42 },
43 While { cond: Expr, body: Vec<Stmt> },
45 WhileLet {
47 pattern: Pattern,
48 expr: Expr,
49 body: Vec<Stmt>,
50 },
51 Loop { body: Vec<Stmt> },
53 Break { value: Option<Expr> },
55 Continue,
57 Use {
59 path: Vec<String>,
60 imports: UseImports,
61 },
62 Return { value: Option<Expr> },
64 Assign {
66 target: AssignTarget,
67 op: AssignOp,
68 value: Expr,
69 },
70}
71
72#[derive(Debug, Clone)]
73pub struct Param {
74 pub name: String,
75 pub default: Option<Expr>,
76}
77
78#[derive(Debug, Clone)]
79pub enum AssignTarget {
80 Ident(String),
81 Index(Box<Expr>, Box<Expr>),
82 Field(Box<Expr>, String),
83}
84
85#[derive(Debug, Clone, Copy)]
86pub enum AssignOp {
87 Eq,
88 PlusEq,
89 MinusEq,
90 StarEq,
91 SlashEq,
92}
93
94#[derive(Debug, Clone)]
95pub struct Expr {
96 pub kind: ExprKind,
97 pub span: Span,
98}
99
100#[derive(Debug, Clone)]
101pub enum ExprKind {
102 Int(i64),
104 Float(f64),
105 Bool(bool),
106 Str(String),
107 FStr(Vec<FStrPart>),
109 Bytes(Vec<u8>),
110 None,
111 Unit,
112
113 Ident(String),
115 ModulePath(Vec<String>),
117
118 SomeExpr(Box<Expr>),
121 OkExpr(Box<Expr>),
123 ErrExpr(Box<Expr>),
125
126 List(Vec<ListEntry>),
129 Dict(Vec<DictEntry>),
131 Tuple(Vec<Expr>),
133 ListComp {
135 expr: Box<Expr>,
136 pattern: Pattern,
137 iter: Box<Expr>,
138 cond: Option<Box<Expr>>,
139 },
140 DictComp {
142 key: Box<Expr>,
143 value: Box<Expr>,
144 pattern: Pattern,
145 iter: Box<Expr>,
146 cond: Option<Box<Expr>>,
147 },
148
149 BinOp {
151 left: Box<Expr>,
152 op: BinOp,
153 right: Box<Expr>,
154 },
155 UnaryOp {
156 op: UnaryOp,
157 expr: Box<Expr>,
158 },
159 Try(Box<Expr>),
161 PipeOp {
163 left: Box<Expr>,
164 right: Box<Expr>,
165 },
166
167 FieldAccess {
170 expr: Box<Expr>,
171 field: String,
172 },
173 Index {
175 expr: Box<Expr>,
176 index: Box<Expr>,
177 },
178 Slice {
180 expr: Box<Expr>,
181 start: Option<Box<Expr>>,
182 end: Option<Box<Expr>>,
183 inclusive: bool,
184 },
185 MethodCall {
187 expr: Box<Expr>,
188 method: String,
189 args: Vec<CallArg>,
190 },
191
192 Call {
195 func: Box<Expr>,
196 args: Vec<CallArg>,
197 },
198 Lambda {
200 params: Vec<String>,
201 body: Box<Expr>,
202 },
203
204 If {
207 cond: Box<Expr>,
208 then_body: Vec<Stmt>,
209 else_body: Option<Vec<Stmt>>,
210 },
211 IfLet {
213 pattern: Pattern,
214 expr: Box<Expr>,
215 then_body: Vec<Stmt>,
216 else_body: Option<Vec<Stmt>>,
217 },
218 Match {
220 expr: Box<Expr>,
221 arms: Vec<MatchArm>,
222 },
223 Block(Vec<Stmt>),
225 LoopExpr(Vec<Stmt>),
227 TryCatch {
229 body: Vec<Stmt>,
230 var: String,
231 handler: Vec<Stmt>,
232 },
233
234 StructConstruct {
236 name: String,
237 fields: Vec<(String, Expr)>,
238 spread: Option<Box<Expr>>,
239 },
240
241 EnumVariant {
243 enum_name: String,
244 variant: String,
245 },
246 EnumVariantCall {
247 enum_name: String,
248 variant: String,
249 args: Vec<Expr>,
250 },
251
252 Range {
254 start: Box<Expr>,
255 end: Box<Expr>,
256 inclusive: bool,
257 },
258
259 AsyncBlock(Vec<Stmt>),
262 SpawnExpr(Box<Expr>),
264 AwaitExpr(Box<Expr>),
266 SelectExpr(Vec<SelectBranch>),
268}
269
270#[derive(Debug, Clone)]
272pub enum ListEntry {
273 Elem(Expr),
274 Spread(Expr),
275}
276
277#[derive(Debug, Clone)]
279pub enum DictEntry {
280 KeyValue(Expr, Expr),
281 Spread(Expr),
282}
283
284#[derive(Debug, Clone)]
285pub enum FStrPart {
286 Literal(String),
287 Expr(Expr),
288}
289
290#[derive(Debug, Clone)]
291pub struct CallArg {
292 pub name: Option<String>,
293 pub value: Expr,
294}
295
296#[derive(Debug, Clone)]
298pub struct SelectBranch {
299 pub pattern: Pattern,
300 pub future_expr: Expr,
301 pub body: Expr,
302}
303
304#[derive(Debug, Clone)]
305pub struct MatchArm {
306 pub pattern: Pattern,
307 pub guard: Option<Expr>,
308 pub body: Expr,
309}
310
311#[derive(Debug, Clone)]
312pub enum Pattern {
313 Wildcard,
314 Ident(String),
315 Int(i64),
316 Float(f64),
317 Bool(bool),
318 Str(String),
319 Bytes(Vec<u8>),
320 None,
321 Some(Box<Pattern>),
323 Ok(Box<Pattern>),
325 Err(Box<Pattern>),
327 Tuple(Vec<Pattern>),
329 List(Vec<Pattern>, Option<Box<Pattern>>),
331 EnumVariant {
333 enum_name: String,
334 variant: String,
335 fields: EnumPatternFields,
336 },
337 Struct {
339 name: String,
340 fields: Vec<(String, Option<Pattern>)>,
341 },
342}
343
344#[derive(Debug, Clone)]
345pub enum EnumPatternFields {
346 None,
347 Positional(Vec<Pattern>),
348 Named(Vec<(String, Option<Pattern>)>),
349}
350
351#[derive(Debug, Clone, Copy)]
352pub enum BinOp {
353 Add,
354 Sub,
355 Mul,
356 Div,
357 Mod,
358 Eq,
359 Ne,
360 Lt,
361 Gt,
362 Le,
363 Ge,
364 And,
365 Or,
366 BitAnd,
367 BitOr,
368 BitXor,
369 Shl,
370 Shr,
371}
372
373#[derive(Debug, Clone)]
377pub enum TypeAnn {
378 Simple(String), Option(Box<TypeAnn>), Result(Box<TypeAnn>, Box<TypeAnn>), List(Box<TypeAnn>), Dict(Box<TypeAnn>, Box<TypeAnn>), }
384
385#[derive(Debug, Clone, Copy)]
386pub enum UnaryOp {
387 Neg,
388 Not,
389}
390
391#[derive(Debug, Clone)]
393pub enum UseImports {
394 Glob,
396 Names(Vec<String>),
398 Single(String),
400}