blr-lang 0.1.0

A language implementation that provides type safe dataframes
Documentation
//! Air is the top layer of the compiler and represents the syntax of the language itself.
//!
//! This layer prioritizes the syntax of the language above all else. It preserves the position
//! information and the exact syntax used to build the syntax tree so that it can be recreated with
//! fidelity from the tree.

#[cfg(test)]
mod parser_tests;
pub mod sexpr;

lalrpop_util::lalrpop_mod!(
    #[allow(clippy::all, missing_debug_implementations, dead_code)]
    pub parser,
    "/compiler/air/parser.rs"
);

#[derive(PartialEq)]
pub struct Module {
    pub imports: Vec<Import>,
    pub items: Vec<Item>,
}

#[derive(PartialEq, Eq)]
pub struct Import {
    pub path: String,
}

#[derive(PartialEq)]
pub enum Item {
    Native(NativeItem),
    External(ExternalItem),
}

impl Item {
    pub fn symbol(&self) -> &Symbol {
        match self {
            Item::Native(item) => &item.symbol,
            Item::External(item) => &item.symbol,
        }
    }
    pub fn typ(&self) -> &FunctionType {
        match self {
            Item::Native(item) => &item.typ,
            Item::External(item) => &item.typ,
        }
    }
}

#[derive(PartialEq)]
pub struct NativeItem {
    pub symbol: Symbol,
    pub function: Function,
    pub typ: FunctionType,
}

#[derive(PartialEq)]
pub struct ExternalItem {
    pub symbol: Symbol,
    pub typ: FunctionType,
}

#[derive(PartialEq)]
pub enum Expr {
    Unit,
    Integer(i64),
    Float(f64),
    Variable(String),
    String(String),
    Variant(Label, Box<Self>),
    Match(Box<Self>, Vec<(Pattern, Self)>),
    Let {
        // TODO Make ident a Pattern
        pattern: Pattern,
        init: Box<Self>,
        body: Box<Self>,
    },
    Function(Function),
    Binary {
        left: Box<Self>,
        op: BinaryOperator,
        right: Box<Self>,
    },
    Forward {
        parameter: Box<Self>,
        call: Box<Self>,
    },
    Application {
        function: Box<Self>,
        parameters: Vec<Self>,
    },
    RecordSelect {
        record: Box<Self>,
        label: String,
    },
    Record {
        fields: Vec<(String, Self)>,
    },
    Parenthesized(Box<Self>),
    Item(Symbol),
}

#[derive(PartialEq)]
pub struct Function {
    pub parameters: Vec<String>,
    pub body: Box<Expr>,
}

#[derive(PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
pub struct Symbol {
    // Local path to module
    pub module: String,
    pub field: String,
}

#[derive(PartialEq, Debug)]
pub enum BinaryOperator {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    Concat,
}

#[derive(Clone, PartialEq)]
pub enum TypeExpr {
    Unit,
    Int,
    Float,
    String,
    Var(String),
    Fun(FunctionType),
    Prod(Row),
    Sum(Row),
    Nominal(String, Box<Self>),
    DataFrame,
}

#[derive(Clone, PartialEq)]
pub struct FunctionType {
    pub parameter_names: Vec<String>,
    pub parameter_typs: Vec<TypeExpr>,
    pub ret: Box<TypeExpr>,
}

pub type Label = String;

#[derive(Clone, PartialEq)]
pub struct Row {
    pub fields: Vec<(Label, TypeExpr)>,
}

#[derive(Clone, PartialEq)]
pub enum Pattern {
    Identifier(String),
    Variant(Label, Box<Self>),
}