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 ++++++++++++++++++++++++-------------- std/fundamental/ast.pk | 13 +++---------- 2 files changed, 27 insertions(+), 24 deletions(-) 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), diff --git a/std/fundamental/ast.pk b/std/fundamental/ast.pk index 202c7d4..0529cbd 100644 --- a/std/fundamental/ast.pk +++ b/std/fundamental/ast.pk @@ -45,15 +45,15 @@ pub type Call = struct id: str params: list[Expr] pub type Cond = struct - branches: list[CondBranch] + branches: list[struct[cond: Expr, body: list[Expr]]] else_body: Option[list[Expr]] pub type Try = struct - body: ref Expr + body: list[Expr] catches: list[struct[exceptions: list[str], body: list[Expr]]] finally_body: Option[list[Expr]] pub type Match = struct item: ref Expr - branches: list[MatchBranch] + branches: list[struct[pattern: Pattern, guard: Option[Expr], body: list[Expr]]] pub type Block = struct id: Option[str] body: list[Expr] @@ -98,13 +98,6 @@ pub type Pattern = union Struct: struct[name: str, params: list[Pattern]] Tuple: list[Pattern] List: list[Pattern] -pub type CondBranch = struct - cond: Expr - body: list[Expr] -pub type MatchBranch = struct - pattern: Pattern - guard: Option[Expr] - body: Expr # the second style of union. A union of literal types, no names. pub type Expr = union -- cgit v1.2.3-70-g09d2