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 + 2 files changed, 106 insertions(+) create mode 100644 src/ast.rs (limited to 'src') 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; -- cgit v1.2.3-70-g09d2