//! A very naive AST definition using recursive enums //! //! See the parser for implementation use std::rc::Rc; pub type Parent = Vec; /// Entities are functions, classes, and modules #[derive(Debug, PartialEq)] pub enum Entity { Fn(Fn), Class(Class), Module(Module), Static(Let), } /// A module just provides an additional scope /// /// TODO: Add exporting and importing modules #[derive(Debug, PartialEq)] pub struct Module { /// Name of module pub name: Rc, /// Everything inside the module pub children: Vec, } /// Modules contain functions, classes and statements #[derive(Debug, PartialEq)] pub enum ModuleChildren { Fn(Fn), Class(Class), Module(Module), Static(Let), } /// Classes encapsulate functions and definitions. #[derive(Debug, PartialEq)] pub struct Class { /// Name of class pub name: Rc, /// Everything inside the class pub children: Vec, } #[derive(Debug, PartialEq)] pub enum ClassChildren { Fn(Fn), Let(Let), Static(Let), } /// A Function #[derive(Debug, PartialEq)] pub struct Fn { /// Name of the function pub name: Rc, /// Optional return type pub return_ty: Option, /// Parameters pub params: Vec<(Rc, Ty)>, /// The function block pub children: Vec, } /// Statements encapsulate expressions and definitions #[derive(Debug, PartialEq)] pub enum Statement { Static(Let), Let(Let), Expr(Expr), } /// A variable definition #[derive(Debug, PartialEq)] pub struct Let { /// Name of variabe pub name: Rc, /// Type of variable pub ty: Ty, /// Value of variable pub expr: Option, } /// Primitives /// /// TODO: add arrays and pointers maybe #[derive(Debug, PartialEq)] pub enum Ty { Int, Float, Char, } #[derive(Debug, PartialEq)] pub struct If { pub cond: Box, pub then: Vec, pub or: Option>, } #[derive(Debug, PartialEq)] pub enum ElseType { If(If), Else(Vec), } pub(crate) type Op = crate::lexer::TokenSymbol; #[derive(Debug, PartialEq)] pub enum Literal { Int(i32), Float(f32), Char(char), } /// Lowest form of expression /// /// TODO: refine #[derive(Debug, PartialEq)] pub enum Expr { Literal(Literal), Identifier(Rc), Op(Op, Box, Option>), If(If), Block(Vec), Loop(Vec), Break, Continue, Return(Option>), }