ty-ree 0.1.0

AST definitions for the Ty programming language
Documentation
//! Expressions, statements, and blocks.

pub use t_ree::expression::{Binding, Literal, MatchPattern};
pub use t_ree::operator;

use crate::declaration::Parameter;
use crate::types::{ConstExpression, GenericArgument, Type};
use t_ree::operator::{BinaryOperator, UnaryOperator};

/// A typed expression node.
#[derive(Clone, Debug)]
pub struct Expression {
    /// The expression variant.
    pub kind: ExpressionKind,
    /// Resolved (or inferred) type of this expression.
    pub resolved_type: Type,
}

impl Expression {
    /// Creates a new expression with the given kind and type.
    pub fn new(kind: ExpressionKind, resolved_type: Type) -> Self {
        Self {
            kind,
            resolved_type,
        }
    }
}

/// A sequence of statements with an optional trailing result expression.
#[derive(Clone, Debug)]
pub struct Block {
    /// Statements executed in order.
    pub statements: Vec<Statement>,
    /// Optional result expression (determines block value).
    pub result: Option<Box<Expression>>,
}

impl Block {
    /// Creates an empty block with no statements or result.
    pub fn empty() -> Self {
        Self {
            statements: Vec::new(),
            result: None,
        }
    }

    /// Creates a block containing only a result expression.
    pub fn expression(expression: Expression) -> Self {
        Self {
            statements: Vec::new(),
            result: Some(Box::new(expression)),
        }
    }
}

/// A single arm in a match expression.
#[derive(Clone, Debug)]
pub struct MatchArm {
    /// Pattern to match against.
    pub pattern: MatchPattern,
    /// Body executed when the pattern matches.
    pub body: Block,
}

/// A statement (no value produced).
#[derive(Clone, Debug)]
pub enum Statement {
    /// Expression evaluated for side effects.
    Expression(Expression),
    /// Variable binding (`let`/`ref`/`var`).
    Let {
        /// Variable name.
        name: String,
        /// Binding kind (value, ref, mutable ref).
        binding: Binding,
        /// Optional explicit type annotation.
        declared_type: Option<Type>,
        /// Initializer expression.
        value: Expression,
        /// Source line number (0 if unavailable).
        line: u32,
    },
    /// Assignment (`target := value`).
    Assign(Expression, Expression),
    /// Early return with optional value.
    Return(Option<Expression>),
    /// Label definition for structured jumps.
    Label {
        /// Label name.
        name: String,
        /// Parameters bound on each jump.
        parameters: Vec<Parameter>,
        /// Initial argument values.
        initial_arguments: Vec<Expression>,
    },
    /// Jump to a label with arguments.
    Jump {
        /// Target label name.
        label: String,
        /// Arguments passed to the label.
        arguments: Vec<Expression>,
    },
    /// While loop.
    While {
        /// Loop condition.
        condition: Expression,
        /// Loop body.
        body: Block,
    },
    /// Deferred statement executed on scope exit.
    Defer(Box<Self>),
}

/// The kind of an expression (untyped variant).
#[derive(Clone, Debug)]
pub enum ExpressionKind {
    /// Literal value (integer, float, string, bool).
    Literal(Literal),
    /// Variable reference by name.
    Variable(String),

    /// Binary operation (e.g. `a + b`).
    BinaryOperation(BinaryOperator, Box<Expression>, Box<Expression>),
    /// Unary operation (e.g. `-x`).
    UnaryOperation(UnaryOperator, Box<Expression>),

    /// `expression(arguments)` — callable tuple construction + evaluation.
    Call(Box<Expression>, Vec<Expression>),
    /// Field access by name.
    Field(Box<Expression>, String),
    /// Index access (`collection@index`).
    Index(Box<Expression>, Box<Expression>),

    /// `expression[T, N, ...]` — generic specialization.
    Specialize(Box<Expression>, Vec<GenericArgument>),

    /// Pointer dereference.
    Dereference(Box<Expression>),

    /// Newtype conversion (`Type: value` or `.Type` downcast).
    Convert(Box<Expression>, Type),
    /// Size of a type in bytes.
    SizeOf(Type),

    /// `Name { field: value, ... }` — type construction (data only, no eval).
    TypeConstruction(String, Vec<GenericArgument>, Vec<(String, Expression)>),
    /// `expression!` — explicit evaluation of a constructed value.
    Eval(Box<Expression>),
    /// Array literal (`[a, b, c]`).
    ArrayLiteral(Vec<Expression>),
    /// Array repeat (`[value]count` where count is a const expression).
    ArrayRepeat(Box<Expression>, ConstExpression),
    /// Tuple literal (`(a, b, c)`).
    TupleLiteral(Vec<Expression>),

    /// Block expression.
    Block(Block),
    /// Conditional expression.
    If {
        /// Condition to evaluate.
        condition: Box<Expression>,
        /// Branch taken when true.
        then_branch: Block,
        /// Optional branch taken when false.
        else_branch: Option<Block>,
    },
    /// Pattern matching expression.
    Match {
        /// Value being matched.
        value: Box<Expression>,
        /// Match arms evaluated in order.
        arms: Vec<MatchArm>,
    },

    /// Replace expression (`:=`, returns old value).
    Replace(Box<Expression>, Box<Expression>),
    /// Print expression for debugging.
    Print(Vec<Expression>),
}