ty_ree/expression.rs
1//! Expressions, statements, and blocks.
2
3pub use t_ree::expression::{Binding, Literal, MatchPattern};
4pub use t_ree::operator;
5
6use crate::declaration::Parameter;
7use crate::types::{ConstExpression, GenericArgument, Type};
8use t_ree::operator::{BinaryOperator, UnaryOperator};
9
10/// A typed expression node.
11#[derive(Clone, Debug)]
12pub struct Expression {
13 /// The expression variant.
14 pub kind: ExpressionKind,
15 /// Resolved (or inferred) type of this expression.
16 pub resolved_type: Type,
17}
18
19impl Expression {
20 /// Creates a new expression with the given kind and type.
21 pub fn new(kind: ExpressionKind, resolved_type: Type) -> Self {
22 Self {
23 kind,
24 resolved_type,
25 }
26 }
27}
28
29/// A sequence of statements with an optional trailing result expression.
30#[derive(Clone, Debug)]
31pub struct Block {
32 /// Statements executed in order.
33 pub statements: Vec<Statement>,
34 /// Optional result expression (determines block value).
35 pub result: Option<Box<Expression>>,
36}
37
38impl Block {
39 /// Creates an empty block with no statements or result.
40 pub fn empty() -> Self {
41 Self {
42 statements: Vec::new(),
43 result: None,
44 }
45 }
46
47 /// Creates a block containing only a result expression.
48 pub fn expression(expression: Expression) -> Self {
49 Self {
50 statements: Vec::new(),
51 result: Some(Box::new(expression)),
52 }
53 }
54}
55
56/// A single arm in a match expression.
57#[derive(Clone, Debug)]
58pub struct MatchArm {
59 /// Pattern to match against.
60 pub pattern: MatchPattern,
61 /// Body executed when the pattern matches.
62 pub body: Block,
63}
64
65/// A statement (no value produced).
66#[derive(Clone, Debug)]
67pub enum Statement {
68 /// Expression evaluated for side effects.
69 Expression(Expression),
70 /// Variable binding (`let`/`ref`/`var`).
71 Let {
72 /// Variable name.
73 name: String,
74 /// Binding kind (value, ref, mutable ref).
75 binding: Binding,
76 /// Optional explicit type annotation.
77 declared_type: Option<Type>,
78 /// Initializer expression.
79 value: Expression,
80 /// Source line number (0 if unavailable).
81 line: u32,
82 },
83 /// Assignment (`target := value`).
84 Assign(Expression, Expression),
85 /// Early return with optional value.
86 Return(Option<Expression>),
87 /// Label definition for structured jumps.
88 Label {
89 /// Label name.
90 name: String,
91 /// Parameters bound on each jump.
92 parameters: Vec<Parameter>,
93 /// Initial argument values.
94 initial_arguments: Vec<Expression>,
95 },
96 /// Jump to a label with arguments.
97 Jump {
98 /// Target label name.
99 label: String,
100 /// Arguments passed to the label.
101 arguments: Vec<Expression>,
102 },
103 /// While loop.
104 While {
105 /// Loop condition.
106 condition: Expression,
107 /// Loop body.
108 body: Block,
109 },
110 /// Deferred statement executed on scope exit.
111 Defer(Box<Self>),
112}
113
114/// The kind of an expression (untyped variant).
115#[derive(Clone, Debug)]
116pub enum ExpressionKind {
117 /// Literal value (integer, float, string, bool).
118 Literal(Literal),
119 /// Variable reference by name.
120 Variable(String),
121
122 /// Binary operation (e.g. `a + b`).
123 BinaryOperation(BinaryOperator, Box<Expression>, Box<Expression>),
124 /// Unary operation (e.g. `-x`).
125 UnaryOperation(UnaryOperator, Box<Expression>),
126
127 /// `expression(arguments)` — callable tuple construction + evaluation.
128 Call(Box<Expression>, Vec<Expression>),
129 /// Field access by name.
130 Field(Box<Expression>, String),
131 /// Index access (`collection@index`).
132 Index(Box<Expression>, Box<Expression>),
133
134 /// `expression[T, N, ...]` — generic specialization.
135 Specialize(Box<Expression>, Vec<GenericArgument>),
136
137 /// Pointer dereference.
138 Dereference(Box<Expression>),
139
140 /// Newtype conversion (`Type: value` or `.Type` downcast).
141 Convert(Box<Expression>, Type),
142 /// Size of a type in bytes.
143 SizeOf(Type),
144
145 /// `Name { field: value, ... }` — type construction (data only, no eval).
146 TypeConstruction(String, Vec<GenericArgument>, Vec<(String, Expression)>),
147 /// `expression!` — explicit evaluation of a constructed value.
148 Eval(Box<Expression>),
149 /// Array literal (`[a, b, c]`).
150 ArrayLiteral(Vec<Expression>),
151 /// Array repeat (`[value]count` where count is a const expression).
152 ArrayRepeat(Box<Expression>, ConstExpression),
153 /// Tuple literal (`(a, b, c)`).
154 TupleLiteral(Vec<Expression>),
155
156 /// Block expression.
157 Block(Block),
158 /// Conditional expression.
159 If {
160 /// Condition to evaluate.
161 condition: Box<Expression>,
162 /// Branch taken when true.
163 then_branch: Block,
164 /// Optional branch taken when false.
165 else_branch: Option<Block>,
166 },
167 /// Pattern matching expression.
168 Match {
169 /// Value being matched.
170 value: Box<Expression>,
171 /// Match arms evaluated in order.
172 arms: Vec<MatchArm>,
173 },
174
175 /// Replace expression (`:=`, returns old value).
176 Replace(Box<Expression>, Box<Expression>),
177 /// Print expression for debugging.
178 Print(Vec<Expression>),
179}