aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2023-10-26 21:51:27 +0000
committerJJ2023-10-26 21:51:27 +0000
commit0609270d01b8f87e2c7579e91b43a6d0c2862f49 (patch)
treedf1ea6516e4aa10ef4862ec8ebdb3717e5357f11
parent0fa32e833237affd401ec644704cda4f84d07374 (diff)
compiler: draft of the abstract syntax tree
-rw-r--r--src/ast.rs105
-rw-r--r--src/main.rs1
-rw-r--r--std/fundamental/ast.pk13
3 files changed, 113 insertions, 6 deletions
diff --git a/src/ast.rs b/src/ast.rs
new file mode 100644
index 0000000..92cabac
--- /dev/null
+++ b/src/ast.rs
@@ -0,0 +1,105 @@
+/// Representation of Tokens, function names, the like...
+pub type Id = String;
+
+/// Puck's fundamental types.
+pub enum Type {
+ Void, Never,
+ Integer, Float, String, // char et al are defined later
+ Func{from: Box<Type>, to: Box<Type>}, // todo: multiple params, effects
+ Struct(Vec<(Id, Box<Type>)>),
+ Tuple(Vec<(Option<String>, Box<Type>)>),
+ Union(Vec<(Id, Box<Type>)>),
+ Interface {
+ funcs: Vec<Sig>,
+ for_type: Option<Box<Type>>,
+ },
+ Array{size: usize, kind: Box<Type>},
+ List(Box<Type>),
+ Slice(Box<Type>), // todo: plus ownership
+ Reference(Box<Type>),
+ Mutable(Box<Type>), // parameters only
+ Static(Box<Type>), // parameters only
+ Alias{ id: Id, params: Vec<Type> }, // todo: this is wrong
+}
+
+/// Function signatures.
+pub struct Sig {
+ effect: Option<Id>,
+ id: Id,
+ generics: Vec<(Id, Option<Type>)>,
+ params: Vec<Type>,
+ result: Option<Type>
+}
+
+/// Patterns are recognizable given zero context.
+/// This is why there is a generic Number term and no Bool term.
+/// Also can be considered to be a Term/Value.
+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)>),
+ List(Vec<Expr>), // arrays, slices, lists
+}
+
+/// 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,
+ kind: Option<Type>,
+ value: Box<Expr>
+ },
+ Var {
+ id: Pattern,
+ kind: Option<Type>,
+ value: Option<Box<Expr>> // variable bindings can be delayed
+ },
+ Const {
+ id: Pattern,
+ kind: Option<Type>,
+ value: Box<Expr>
+ },
+ Func {
+ public: bool,
+ effect: Option<Id>,
+ id: Id,
+ generics: Vec<(Id, Option<Type>)>,
+ params: Vec<(Id, Type)>,
+ kind: Type,
+ 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> },
+}
+
+/// 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>)>,
+ else_body: Option<Vec<Expr>>
+ },
+ Match {
+ item: Box<Expr>,
+ // pattern, guard, branch
+ branches: Vec<(Pattern, Option<Expr>, Expr)> // todo
+ },
+ 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>, },
+ Loop { body: Vec<Expr> },
+}
+
+/// Expressions are either Patterns, Bindings, or Control flow constructs.
+pub enum Expr {
+ Pattern(Pattern),
+ Binding(Binding),
+ Control(Control),
+}
diff --git a/src/main.rs b/src/main.rs
index 5cbdf00..837dc1c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,7 @@
#![allow(non_upper_case_globals)]
#![feature(exclusive_range_pattern, let_chains)]
+mod ast;
mod lex;
mod tree;
diff --git a/std/fundamental/ast.pk b/std/fundamental/ast.pk
index 25bdc02..202c7d4 100644
--- a/std/fundamental/ast.pk
+++ b/std/fundamental/ast.pk
@@ -25,8 +25,8 @@ pub type FuncDecl = struct
public: bool
effect: Option[str]
id: str
- generics: list[struct[name: str, kind: Option[Type]]]
- params: list[struct[name: str, kind: Type]]
+ generics: list[struct[id: str, kind: Option[Type]]]
+ params: list[struct[id: str, kind: Type]]
kind: Type
body: list[Expr]
pub type TypeDecl = struct
@@ -38,8 +38,8 @@ pub type Import = struct
imports: list[str]
alias: Option[str]
pub type Module = struct
- name: str
- body: list[ref Expr]
+ id: str
+ body: list[Expr]
pub type Call = struct
id: str
@@ -94,9 +94,10 @@ pub type Signature = struct # todo: generics
kind: Option[Type]
pub type Pattern = union
Ident: str
- Struct: struct[name: str, params: list[]]
+ Number: int, Float: float, Char: char, String: str
+ Struct: struct[name: str, params: list[Pattern]]
Tuple: list[Pattern]
- List
+ List: list[Pattern]
pub type CondBranch = struct
cond: Expr
body: list[Expr]