1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::token::Token;
use crate::value::Value;
#[derive(Debug, Clone, PartialEq)]
pub enum TypeExpr {
/// A bare type name: `Int`, `String`, `Bool`, `Float`, `Nil`, or a class name.
Named(String),
/// A parameterized type: `List[Int]`, `Map[String, Bool]`, `Box[T]`.
Apply(String, Vec<TypeExpr>),
/// A literal type: `42`, `"ok"`, `true`, `false`.
Literal(Value),
/// A union of two or more types: `Int | String`. Always has >= 2 arms.
Union(Vec<TypeExpr>),
/// Escape hatch for future use (e.g. unannotated generics, explicit `Any` type).
#[allow(dead_code)]
Any,
}
#[derive(Debug, Clone)]
pub struct ParamDef {
pub name: String,
pub type_ann: Option<TypeExpr>,
}
#[derive(Debug, Clone)]
pub struct Block {
pub params: Vec<String>,
pub body: Vec<Expr>,
}
#[derive(Debug, Clone)]
pub enum StringPart {
Lit(String),
Expr(Box<Expr>),
}
#[derive(Debug, Clone)]
pub struct FieldDef {
pub name: String,
pub type_ann: Option<TypeExpr>,
pub default: Option<Expr>,
}
#[derive(Debug, Clone)]
pub struct MethodDef {
pub name: String,
pub type_params: Vec<String>,
pub params: Vec<ParamDef>,
pub return_type: Option<TypeExpr>,
pub body: Vec<Expr>,
pub private: bool,
pub class_method: bool,
}
#[derive(Debug, Clone)]
pub struct CallArg {
pub name: Option<String>,
pub value: Expr,
}
#[derive(Debug, Clone)]
pub enum Expr {
Literal(Value),
Grouping(Box<Expr>),
SelfExpr,
Unary {
op: Token,
right: Box<Expr>,
},
Binary {
left: Box<Expr>,
op: Token,
right: Box<Expr>,
},
Variable(String),
Assign {
name: String,
value: Box<Expr>,
},
Call {
callee: Box<Expr>,
args: Vec<CallArg>,
block: Option<Block>,
},
Get {
object: Box<Expr>,
name: String,
},
SafeGet {
object: Box<Expr>,
name: String,
},
Set {
object: Box<Expr>,
name: String,
value: Box<Expr>,
},
StringInterp(Vec<StringPart>),
ListLit(Vec<Expr>),
MapLit(Vec<(String, Expr)>),
Super {
method: String,
args: Vec<CallArg>,
block: Option<Block>,
},
Index {
object: Box<Expr>,
index: Box<Expr>,
},
IndexSet {
object: Box<Expr>,
index: Box<Expr>,
value: Box<Expr>,
},
Yield {
args: Vec<CallArg>,
},
Range {
from: Box<Expr>,
to: Box<Expr>,
},
/// `if` / `elsif` / `else` — value is the last expression in the taken branch, or `nil`.
If {
condition: Box<Expr>,
then_branch: Vec<Expr>,
else_branch: Option<Vec<Expr>>,
},
/// `print expr` — evaluates `expr`, prints, value is the printed value.
Print(Box<Expr>),
/// `class Name ...` — defines a class; value is the class object.
Class {
name: String,
/// Generic type parameters: `[T, U]` in `class Foo[T, U]`.
type_params: Vec<String>,
/// Superclass expression: `Name` or `Outer.Inner`. `None` means inherit from Object.
superclass: Option<Box<Expr>>,
fields: Vec<FieldDef>,
methods: Vec<MethodDef>,
/// Nested class definitions — accessible only as `Outer.Inner`.
nested: Vec<Expr>,
/// Class-level constants (`PI = 3.14`) — accessible as `ClassName.PI`.
constants: Vec<(String, Box<Expr>)>,
},
/// Top-level `def name(...)` on `Object`; value is the method name string.
Function {
name: String,
/// Generic type parameters: `[T]` in `def identity[T](x: T) -> T`.
type_params: Vec<String>,
params: Vec<ParamDef>,
return_type: Option<TypeExpr>,
body: Vec<Expr>,
},
/// `begin … rescue … else … end` — value follows Ruby (last expression on taken path).
Begin {
body: Vec<Expr>,
rescue_var: Option<String>,
rescue_body: Vec<Expr>,
else_body: Vec<Expr>,
},
/// `while cond { ... }` — value is `nil` when the loop exits normally (Ruby).
While {
condition: Box<Expr>,
body: Vec<Expr>,
},
Return(Box<Expr>),
Break(Box<Expr>),
Next(Box<Expr>),
Raise(Box<Expr>),
MultiAssign {
names: Vec<String>,
values: Vec<Expr>,
},
/// Anonymous `def(params) { body }` — a first-class lambda value.
Lambda {
params: Vec<String>,
body: Vec<Expr>,
},
/// `import "./path"` — load and execute a relative file in the current scope.
Import {
path: String,
},
/// `type Name = TypeExpr` — declares a type alias.
TypeAlias {
name: String,
type_expr: TypeExpr,
},
}