From 0609270d01b8f87e2c7579e91b43a6d0c2862f49 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 26 Oct 2023 14:51:27 -0700 Subject: compiler: draft of the abstract syntax tree --- src/ast.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + std/fundamental/ast.pk | 13 +++--- 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/ast.rs 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, to: Box}, // todo: multiple params, effects + Struct(Vec<(Id, Box)>), + Tuple(Vec<(Option, Box)>), + Union(Vec<(Id, Box)>), + Interface { + funcs: Vec, + for_type: Option>, + }, + Array{size: usize, kind: Box}, + List(Box), + Slice(Box), // todo: plus ownership + Reference(Box), + Mutable(Box), // parameters only + Static(Box), // parameters only + Alias{ id: Id, params: Vec }, // todo: this is wrong +} + +/// Function signatures. +pub struct Sig { + effect: Option, + id: Id, + generics: Vec<(Id, Option)>, + params: Vec, + result: Option +} + +/// 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, Expr)>), + List(Vec), // 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, + value: Box + }, + Var { + id: Pattern, + kind: Option, + value: Option> // variable bindings can be delayed + }, + Const { + id: Pattern, + kind: Option, + value: Box + }, + Func { + public: bool, + effect: Option, + id: Id, + generics: Vec<(Id, Option)>, + params: Vec<(Id, Type)>, + kind: Type, + body: Vec, + }, + TypeDecl { id: Id, generics: Vec, alias: Type }, + Import { from: Option, imports: Vec, alias: Option }, + Module { id: Id, body: Vec }, +} + +/// 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)>, + else_body: Option> + }, + Match { + item: Box, + // pattern, guard, branch + branches: Vec<(Pattern, Option, Expr)> // todo + }, + Block { id: Option, body: Vec }, + Static { body: Vec }, + For { binding: Pattern, range: Box, body: Vec }, + While { cond: Box, body: Vec, }, + Loop { body: Vec }, +} + +/// 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] -- cgit v1.2.3-70-g09d2