From 2ea4fd4c09ad71c4ac648cf3645426476dd7521f Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 26 Oct 2023 15:04:37 -0700 Subject: compiler: clean up the abstract syntax tree --- src/ast.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ast.rs b/src/ast.rs index 92cabac..71daa9b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -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, Expr)>), + Struct(Vec), + Tuple(Vec), List(Vec), // arrays, slices, lists } +pub struct StructPattern { field: Id, value: Expr } +pub struct TuplePattern { field: Option, 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, value: Box }, @@ -67,36 +67,46 @@ pub enum Binding { public: bool, effect: Option, id: Id, - generics: Vec<(Id, Option)>, - params: Vec<(Id, Type)>, + generics: Vec, + params: Vec, kind: Type, - body: Vec, + body: Vec }, TypeDecl { id: Id, generics: Vec, alias: Type }, Import { from: Option, imports: Vec, alias: Option }, Module { id: Id, body: Vec }, } +pub struct GenericDecl { id: Id, kind: Option } +pub struct ParamDecl { id: Id, kind: Type } + /// Expressions related to control flow. pub enum Control { Call { id: Id, params: Vec }, // function calls, macro invocations, field access... Cond { - // cond, body - branches: Vec<(Expr, Vec)>, + branches: Vec, else_body: Option> }, + Try { + body: Vec, + catches: Vec, + finally: Option> + }, Match { item: Box, - // pattern, guard, branch - branches: Vec<(Pattern, Option, Expr)> // todo + branches: Vec }, Block { id: Option, body: Vec }, Static { body: Vec }, For { binding: Pattern, range: Box, body: Vec }, - While { cond: Box, body: Vec, }, + While { cond: Box, body: Vec }, Loop { body: Vec }, } +pub struct CondBranch { cond: Expr, body: Vec } +pub struct CatchBranch { exceptions: Vec, binding: Option, body: Vec } +pub struct MatchBranch { pattern: Pattern, guard: Option, body: Vec } + /// Expressions are either Patterns, Bindings, or Control flow constructs. pub enum Expr { Pattern(Pattern), -- cgit v1.2.3-70-g09d2