aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2023-10-26 22:04:37 +0000
committerJJ2023-10-26 22:06:51 +0000
commit2ea4fd4c09ad71c4ac648cf3645426476dd7521f (patch)
treec4bf3f7a4fd43a25508231ee21202bb3a573c8b2
parent0609270d01b8f87e2c7579e91b43a6d0c2862f49 (diff)
compiler: clean up the abstract syntax tree
-rw-r--r--src/ast.rs38
-rw-r--r--std/fundamental/ast.pk13
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<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),
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