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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/// Represents a class definition.
///
/// Example:
/// ```text
/// Counter = (
///   | total |
///   new = ( self reset )
///   increment = ( total := total + 1 )
///   get = ( ^ total )
///   reset = ( total := 0 )
/// )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ClassDef {
    /// The name of the class.
    pub name: String,
    /// The name of the superclass.
    pub super_class: Option<String>,
    /// The locals for instances of that class.
    pub instance_locals: Vec<String>,
    /// The methods declared for instances of that class.
    pub instance_methods: Vec<MethodDef>,
    /// The static locals for that class.
    pub static_locals: Vec<String>,
    /// The static methods declared for that class.
    pub static_methods: Vec<MethodDef>,
}

/// Represents a method's kind.
///
/// Example:
/// ```text
/// "unary method"       increment = ( self increment: 1 )
/// "positional method"  increment: value = ( total := total + value )
/// "operator method"    + value = ( self increment: value )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum MethodKind {
    /// A unary method definition.
    Unary,
    /// A positional method definition (keyword-based).
    Positional {
        /// The binding names for the method's parameters.
        parameters: Vec<String>,
    },
    /// A binary operator method definiton.
    Operator {
        /// The binding name for the right-hand side.
        rhs: String,
    },
}

/// Represents a method definition.
///
/// Example:
/// ```text
/// "unary method"       increment = ( self increment: 1 )
/// "positional method"  increment: value = ( total := total + value )
/// "operator method"    + value = ( self increment: value )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct MethodDef {
    /// The method's kind.
    pub kind: MethodKind,
    /// The method's signature (eg. `println`, `at:put:` or `==`).
    pub signature: String,
    /// The method's body.
    pub body: MethodBody,
}

/// Represents a method's body.
///
/// Exemple:
/// ```text
/// "primitive method body"
/// printString: string = primitive
///
/// "actual method body, with a local"
/// double: value = ( |clone| clone := double. ^ (double + clone) )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum MethodBody {
    /// A primitive (meant to be implemented by the VM itself).
    Primitive,
    /// An actual body for the method, with locals.
    Body { locals: Vec<String>, body: Body },
}

/// Represents the contents of a body (within a term or block).
///
/// Exemple:
/// ```text
/// "body within a term"
/// new = (
///     local := counter + 5.
///     (counter get) > 5
/// )
///
/// "body within a block"
/// [ :arg |
///     local := counter + arg.
///     arg * (counter get)
/// ]
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Body {
    /// The expressions in the body.
    pub exprs: Vec<Expression>,
    /// Is the last expression terminated with a period ?
    pub full_stopped: bool,
}

/// Represents an expression.
///
/// Exemple:
/// ```text
/// "reference"          counter
/// "assignment"         counter := 10
/// "messsage send"      counter incrementBy: 5
/// "binary operation"   counter <= 5
/// "exit operation"     ^counter
/// "literal"            'foo'
/// "block"              [ :value | counter incrementBy: value ]
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    /// A reference to a binding (eg. `counter`).
    Reference(String),
    /// An assignment to a binding (eg. `counter := 10`).
    Assignment(String, Box<Expression>),
    /// A message send (eg. `counter incrementBy: 5`).
    Message(Message),
    /// A binary operation (eg. `counter <= 5`).
    BinaryOp(BinaryOp),
    /// An exit operation (eg. `^counter`).
    Exit(Box<Expression>),
    /// A literal (eg. `'foo'`, `10`, `#foo`, ...).
    Literal(Literal),
    /// A block (eg. `[ :value | counter incrementBy: value ]`).
    Block(Block),
}

/// Represents a message send.
///
/// Exemple:
/// ```text
/// "unary message send"
/// 'hello, world' println
///
/// "positional message send"
/// range from: 0 to: 10
///
/// "binary operator message send"
/// value == 3
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
    /// The object to which the message is sent to.
    pub receiver: Box<Expression>,
    /// The signature of the message (eg. "ifTrue:ifFalse:").
    pub signature: String,
    /// The list of dynamic values that are passed.
    pub values: Vec<Expression>,
}

/// Represents a binary operation.
///
/// Exemple:
/// ```text
/// counter <= 2
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryOp {
    /// Represents the operator symbol.
    pub op: String,
    /// Represents the left-hand side.
    pub lhs: Box<Expression>,
    /// Represents the right-hand side.
    pub rhs: Box<Expression>,
}

/// Represents a block.
///
/// Exemple:
/// ```text
/// "simple block"
/// [ 'hello, world' println ]
///
/// "block with parameter"
/// [ :value | value * 2 ]
///
/// "block with parameter and local"
/// [ :value | |serialized| serialized := value asString. serialized println ]
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Block {
    /// Represents the parameters' names.
    pub parameters: Vec<String>,
    /// The names of the locals.
    pub locals: Vec<String>,
    /// Represents the block's body.
    pub body: Body,
}

/// Represents a term.
///
/// Exemple:
/// ```text
/// "simple term"
/// ( 1 + 1 )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Term {
    /// The body of the term.
    pub body: Body,
}

/// Represents a literal.
///
/// Exemple:
/// ```text
/// #foo     "symbol literal"
/// 'hello'  "string literal"
/// 3.14     "double literal"
/// 42       "integer literal"
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
    /// Represents a symbol literal (eg. `#foo`).
    Symbol(String),
    /// Represents a string literal (eg. `'hello'`).
    String(String),
    /// Represents a decimal number literal (eg. `3.14`).
    Double(f64),
    /// Represents a integer number literal (eg. `42`).
    Integer(i64),
    /// Represents a big integer (bigger than a 64-bit signed integer can represent).
    BigInteger(String),
    /// Represents an array literal (eg. `$(1 2 3)`)
    Array(Vec<Literal>),
}