t-ree 0.1.0

AST definitions for the T programming language
Documentation
use crate::expression::{Block, Expression, Span};
use crate::types::Type;

/// A named function parameter.
#[derive(Clone, Debug)]
pub struct Parameter {
    /// Parameter name.
    pub name: String,
    /// Parameter type (`None` for label parameters with inferred types).
    pub parameter_type: Option<Type>,
}

/// Parameter list, either fixed or variadic.
#[derive(Clone, Debug)]
pub enum ParameterList {
    /// Fixed number of parameters.
    Fixed(Vec<Parameter>),
    /// Variadic (last parameter accepts additional arguments).
    Variadic(Vec<Parameter>),
}

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

/// Function definition with body.
#[derive(Clone, Debug)]
pub struct FunctionDefinition {
    /// Function name.
    pub name: String,
    /// Function parameters.
    pub parameters: Vec<Parameter>,
    /// Return type.
    pub return_type: Type,
    /// Function body.
    pub body: Block,
    /// Whether the function is publicly visible (not `static` in C).
    pub public: bool,
    /// Source location.
    pub span: Span,
}

/// External function declaration (no body).
#[derive(Clone, Debug)]
pub struct ExternFunction {
    /// Function name.
    pub name: String,
    /// Parameter list (may be variadic).
    pub parameters: ParameterList,
    /// Return type.
    pub return_type: Type,
}

/// Newtype declaration: creates a distinct type wrapping another.
///
/// Implicit downcast (newtype → underlying) is allowed,
/// but upcast (underlying → newtype) requires explicit construction.
#[derive(Clone, Debug)]
pub struct Newtype {
    /// Newtype name.
    pub name: String,
    /// The underlying type being wrapped.
    pub inner_type: Type,
    /// Whether the type is publicly visible (for future header generation).
    pub public: bool,
}

/// Compile-time constant declaration.
#[derive(Clone, Debug)]
pub struct Constant {
    /// Constant name.
    pub name: String,
    /// Constant type.
    pub constant_type: Type,
    /// Constant value expression.
    pub value: Expression,
    /// Whether the constant is publicly visible (not `static` in C).
    pub public: bool,
}

/// Top-level declaration.
#[derive(Clone, Debug)]
pub enum Declaration {
    /// Newtype definition.
    Type(Newtype),
    /// Function definition.
    Function(FunctionDefinition),
    /// External function declaration.
    Extern(ExternFunction),
    /// Constant definition.
    Constant(Constant),
    /// Module import.
    Import(String),
}

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