ty-ree 0.1.0

AST definitions for the Ty programming language
Documentation
//! Declarations: types, impl blocks, imports, constants, and extern functions.

use crate::expression::{Block, Expression};
use crate::types::{GenericParameter, Type};

/// A named, typed function parameter.
#[derive(Clone, Debug)]
pub struct Parameter {
    /// Parameter name.
    pub name: String,
    /// Parameter type.
    pub parameter_type: Type,
}

/// Fixed-arity or variadic parameter list.
#[derive(Clone, Debug)]
pub enum ParameterList {
    /// Fixed number of parameters.
    Fixed(Vec<Parameter>),
    /// Variadic (last parameter repeats).
    Variadic(Vec<Parameter>),
}

impl ParameterList {
    /// Returns the parameter slice regardless of arity kind.
    pub fn parameters(&self) -> &[Parameter] {
        match self {
            Self::Fixed(parameters) | Self::Variadic(parameters) => parameters,
        }
    }
}

/// Makes a type callable: `impl Type -> R { body }`.
///
/// Constructing the type auto-evaluates the body.
/// Each type has at most one impl block.
#[derive(Clone, Debug)]
pub struct ImplBlock {
    /// Generic type/const parameters.
    pub generic_parameters: Vec<GenericParameter>,
    /// The type being made callable.
    pub target_type: Type,
    /// Return type and body.
    pub callable: Callable,
    /// Whether this impl is evaluated at compile time (`const fn`).
    pub const_fn: bool,
}

/// Return type and body of a callable impl.
#[derive(Clone, Debug)]
pub struct Callable {
    /// Return type of the callable.
    pub return_type: Type,
    /// Body executed on call.
    pub body: Block,
    /// Original parameter names when desugared from `fn`.
    /// If present, the body uses these directly instead of `it.field`.
    pub parameters: Option<Vec<Parameter>>,
}

/// Foreign function declaration (`extern fn`).
#[derive(Clone, Debug)]
pub struct ExternFunction {
    /// Symbol name.
    pub name: String,
    /// Parameter list (may be variadic).
    pub parameters: ParameterList,
    /// Return type.
    pub return_type: Type,
}

/// A newtype declaration (`type Name = Inner`).
#[derive(Clone, Debug)]
pub struct Newtype {
    /// Type name.
    pub name: String,
    /// Generic type/const parameters.
    pub generic_parameters: Vec<GenericParameter>,
    /// Underlying wrapped type.
    pub inner_type: Type,
    /// Whether the type has external linkage.
    pub public: bool,
}

/// A compile-time constant declaration.
#[derive(Clone, Debug)]
pub struct Constant {
    /// Constant name.
    pub name: String,
    /// Type of the constant.
    pub constant_type: Type,
    /// Constant value expression.
    pub value: Expression,
    /// Whether the constant has external linkage.
    pub public: bool,
}

/// A top-level declaration.
#[derive(Clone, Debug)]
pub enum Declaration {
    /// Newtype declaration.
    Type(Newtype),
    /// Impl block making a type callable.
    Impl(ImplBlock),
    /// Foreign function import.
    Extern(ExternFunction),
    /// Compile-time constant.
    Constant(Constant),
    /// Module import by path.
    Import(String),
}

/// A module is a sequence of declarations.
pub type Module = Vec<Declaration>;