diff options
author | JJ | 2023-10-26 22:04:37 +0000 |
---|---|---|
committer | JJ | 2023-10-26 22:06:51 +0000 |
commit | 2ea4fd4c09ad71c4ac648cf3645426476dd7521f (patch) | |
tree | c4bf3f7a4fd43a25508231ee21202bb3a573c8b2 /src | |
parent | 0609270d01b8f87e2c7579e91b43a6d0c2862f49 (diff) |
compiler: clean up the abstract syntax tree
Diffstat (limited to 'src')
-rw-r--r-- | src/ast.rs | 38 |
1 files changed, 24 insertions, 14 deletions
@@ -38,18 +38,18 @@ pub enum Pattern { Ident(Id), // type aliases, union variants, calls... Number(i64), Float(f64), Char(char), String(String), - Struct(Vec<(Id, Expr)>), - Tuple(Vec<(Option<Id>, Expr)>), + Struct(Vec<StructPattern>), + Tuple(Vec<TuplePattern>), List(Vec<Expr>), // arrays, slices, lists } +pub struct StructPattern { field: Id, value: Expr } +pub struct TuplePattern { field: Option<Id>, value: Expr } + /// Expressions introduce a new binding or bindings, in some regard. pub enum Binding { Let { - // Most usages of Let, Var, Const have the id as just an Ident. - // But making it a Pattern supports ex. `let (a, b) = ...` - // And also unpacking: ex. `let Object(a, b) = ...` - id: Pattern, + id: Pattern, // id: Pattern supports ex. `let (a, b) = ...` kind: Option<Type>, value: Box<Expr> }, @@ -67,36 +67,46 @@ pub enum Binding { public: bool, effect: Option<Id>, id: Id, - generics: Vec<(Id, Option<Type>)>, - params: Vec<(Id, Type)>, + generics: Vec<GenericDecl>, + params: Vec<ParamDecl>, kind: Type, - body: Vec<Expr>, + body: Vec<Expr> }, TypeDecl { id: Id, generics: Vec<Id>, alias: Type }, Import { from: Option<Id>, imports: Vec<Id>, alias: Option<Id> }, Module { id: Id, body: Vec<Expr> }, } +pub struct GenericDecl { id: Id, kind: Option<Type> } +pub struct ParamDecl { id: Id, kind: Type } + /// Expressions related to control flow. pub enum Control { Call { id: Id, params: Vec<Expr> }, // function calls, macro invocations, field access... Cond { - // cond, body - branches: Vec<(Expr, Vec<Expr>)>, + branches: Vec<CondBranch>, else_body: Option<Vec<Expr>> }, + Try { + body: Vec<Expr>, + catches: Vec<CatchBranch>, + finally: Option<Vec<Expr>> + }, Match { item: Box<Expr>, - // pattern, guard, branch - branches: Vec<(Pattern, Option<Expr>, Expr)> // todo + branches: Vec<MatchBranch> }, Block { id: Option<Id>, body: Vec<Expr> }, Static { body: Vec<Expr> }, For { binding: Pattern, range: Box<Expr>, body: Vec<Expr> }, - While { cond: Box<Expr>, body: Vec<Expr>, }, + While { cond: Box<Expr>, body: Vec<Expr> }, Loop { body: Vec<Expr> }, } +pub struct CondBranch { cond: Expr, body: Vec<Expr> } +pub struct CatchBranch { exceptions: Vec<Id>, binding: Option<Id>, body: Vec<Expr> } +pub struct MatchBranch { pattern: Pattern, guard: Option<Expr>, body: Vec<Expr> } + /// Expressions are either Patterns, Bindings, or Control flow constructs. pub enum Expr { Pattern(Pattern), |