aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/ast.rs
diff options
context:
space:
mode:
authorJJ2024-04-06 23:48:19 +0000
committerJJ2024-04-07 17:43:08 +0000
commit51c8b349a77a8d8b1b34ce8e03518dad6e3cba00 (patch)
treeef42507e7eca659272d7c702d66945d8c5a1c335 /src/frontend/ast.rs
parent22c4776f9fddef47a6ce3f309e4eafa2fbdc3a65 (diff)
compiler: rewrite lexer, clean up ast
Diffstat (limited to 'src/frontend/ast.rs')
-rw-r--r--src/frontend/ast.rs65
1 files changed, 38 insertions, 27 deletions
diff --git a/src/frontend/ast.rs b/src/frontend/ast.rs
index ed44712..b6ae7be 100644
--- a/src/frontend/ast.rs
+++ b/src/frontend/ast.rs
@@ -4,24 +4,25 @@ pub type Id = String;
/// Puck's fundamental types.
#[derive(Clone, PartialEq)]
pub enum Type {
- Void, Never,
+ Void, Never, Any,
Integer, Float, String, // char et al are defined later
- Func { // todo: multiple params, effects
- from: Box<Type>,
- to: Box<Type>
+ Func { // future: effects
+ from: Vec<Type>,
+ to: Vec<Type>
},
- Struct(Vec<(Id, Box<Type>)>),
- Tuple(Vec<(Option<Id>, Box<Type>)>),
- Union(Vec<(Id, Box<Type>)>),
+ Struct(Vec<(Id, Type)>),
+ Tuple(Vec<(Option<Id>, Type)>),
+ Union(Vec<(Id, Type)>),
Interface(Vec<Sig>),
- Array{size: usize, kind: Box<Type>},
+ Array {
+ size: usize,
+ kind: Box<Type>
+ },
List(Box<Type>),
Slice(Box<Type>), // todo: plus ownership
- Reference(Box<Type>),
- Pointer(Box<Type>),
+ Reference(Box<Type>), Pointer(Box<Type>),
Distinct(Box<Type>), // todo: not sure
- Mutable(Box<Type>), // parameters only
- Static(Box<Type>), // parameters only
+ Mutable(Box<Type>), Static(Box<Type>), // parameters only
Alias { // todo: this is wrong
id: Id,
generics: Vec<Type>
@@ -31,11 +32,10 @@ pub enum Type {
/// Function signatures.
#[derive(Clone, PartialEq)]
pub struct Sig {
- pub effect: Option<Id>,
pub id: Id,
pub generics: Vec<(Id, Option<Type>)>,
pub parameters: Vec<Type>,
- pub kind: Option<Type>
+ pub kind: Type
}
/// Patterns are recognizable given zero context.
@@ -50,8 +50,8 @@ pub enum Pattern {
List(Vec<Expr>), // arrays, slices, lists
}
-/// Expressions introduce a new binding or bindings, in some regard.
-pub enum Binding {
+/// Expressions introduce a new binding or bindings.
+pub enum Binding { // todo: excessive use of Option<Type>
Let {
id: Pattern, // id: Pattern supports ex. `let (a, b) = ...`
kind: Option<Type>,
@@ -70,23 +70,22 @@ pub enum Binding {
},
Func {
public: bool,
- effect: Option<Id>,
id: Id,
- generics: Vec<(Id, Option<Type>)>, // id, kind
- parameters: Vec<(Id, Type)>, // id, kind
+ generics: Vec<(Id, Option<Type>)>,
+ parameters: Vec<(Id, Type)>,
kind: Type,
body: Vec<Expr>
},
Macro {
public: bool,
id: Id,
- generics: Vec<(Id, Option<Type>)>, // id, kind
- parameters: Vec<(Id, Option<Type>)>, // id, kind
+ generics: Vec<(Id, Option<Type>)>,
+ parameters: Vec<(Id, Option<Type>)>,
kind: Option<Type>,
body: Vec<Expr>
},
TypeDecl { id: Id, generics: Vec<(Id, Option<Type>)>, alias: Type },
- Import { from: Option<Id>, imports: Vec<Id>, alias: Option<Id> },
+ Use { modules: Vec<Id> }, // todo: aliases? anything else fancy?
Module { id: Id, body: Vec<Expr> },
}
@@ -95,12 +94,12 @@ pub enum Control {
Call { id: Id, params: Vec<Expr> }, // function calls, macro invocations, field access...
If {
branches: Vec<CondBranch>,
- else_body: Option<Vec<Expr>>
+ else_body: Vec<Expr>
},
Try {
body: Vec<Expr>,
catches: Vec<CatchBranch>,
- finally: Option<Vec<Expr>>
+ finally: Vec<Expr>
},
Match {
item: Pattern,
@@ -113,9 +112,19 @@ pub enum Control {
Loop { body: Vec<Expr> },
}
-pub struct CondBranch { pub cond: Expr, pub body: Vec<Expr> }
-pub struct CatchBranch { pub exceptions: Vec<(Id, Option<Id>)>, pub body: Vec<Expr> }
-pub struct MatchBranch { pub patterns: Vec<Pattern>, pub guard: Option<Expr>, pub body: Vec<Expr> }
+pub struct CondBranch {
+ pub cond: Expr,
+ pub body: Vec<Expr>
+}
+pub struct CatchBranch {
+ pub exceptions: Vec<(Id, Option<Id>)>,
+ pub body: Vec<Expr>
+}
+pub struct MatchBranch {
+ pub patterns: Vec<Pattern>,
+ pub guard: Option<Expr>,
+ pub body: Vec<Expr>
+}
/// Expressions are either Patterns, Bindings, or Control flow constructs.
pub enum Expr {
@@ -123,3 +132,5 @@ pub enum Expr {
Binding(Binding),
Control(Control),
}
+
+pub type Ast = Vec<Expr>;