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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Control Flow Compilation
//!
//! Handles: if/else, while, loop, for, break, continue
use syn::{Expr, Block};
use super::{Compiler, CompileError, LoopContext};
use crate::opcodes::control;
impl Compiler {
/// Compile if expression
/// if cond { then_block } else { else_block }
pub(crate) fn compile_if(&mut self, cond: &Expr, then_block: &Block, else_branch: Option<&Expr>) -> Result<(), CompileError> {
let else_label = self.unique_label("if_else");
let end_label = self.unique_label("if_end");
// Compile condition
self.compile_expr(cond)?;
// Test condition: XOR with 0 sets ZF if value is 0
self.emit_dup();
self.emit_zero();
self.emit_xor();
self.emit_drop(); // Drop XOR result, keep original condition
self.emit_drop(); // Drop original condition
// Jump to else if condition is false (zero)
self.emit_jump(control::JZ, &else_label);
// Then block
self.compile_block(then_block)?;
self.emit_jump(control::JMP, &end_label);
// Else block
self.mark_label(&else_label);
if let Some(else_expr) = else_branch {
match else_expr {
Expr::Block(block) => {
self.compile_block(&block.block)?;
}
Expr::If(else_if) => {
// else if ...
let else_expr = else_if.else_branch.as_ref().map(|(_, e)| e.as_ref());
self.compile_if(&else_if.cond, &else_if.then_branch, else_expr)?;
}
_ => {
self.compile_expr(else_expr)?;
}
}
} else {
// No else: push unit (0)
self.emit_zero();
}
self.mark_label(&end_label);
Ok(())
}
/// Compile while loop
/// while cond { body }
pub(crate) fn compile_while(&mut self, cond: &Expr, body: &Block) -> Result<(), CompileError> {
let loop_start = self.unique_label("while_start");
let loop_end = self.unique_label("while_end");
// Push loop context for break/continue
self.loop_stack.push(LoopContext {
continue_label: loop_start.clone(),
break_label: loop_end.clone(),
});
// Loop start
self.mark_label(&loop_start);
// Compile condition
self.compile_expr(cond)?;
self.emit_dup();
self.emit_zero();
self.emit_xor();
self.emit_drop();
self.emit_drop();
// Exit if condition is false
self.emit_jump(control::JZ, &loop_end);
// Body (as statements - drop any expression result)
self.compile_block_stmt(body)?;
// Jump back to condition
self.emit_jump(control::JMP, &loop_start);
// Loop end
self.mark_label(&loop_end);
// Pop loop context
self.loop_stack.pop();
// While loops produce unit
self.emit_zero();
Ok(())
}
/// Compile infinite loop
/// loop { body }
pub(crate) fn compile_loop(&mut self, body: &Block) -> Result<(), CompileError> {
let loop_start = self.unique_label("loop_start");
let loop_end = self.unique_label("loop_end");
// Push loop context
self.loop_stack.push(LoopContext {
continue_label: loop_start.clone(),
break_label: loop_end.clone(),
});
// Loop start
self.mark_label(&loop_start);
// Body
self.compile_block_stmt(body)?;
// Jump back
self.emit_jump(control::JMP, &loop_start);
// Loop end (reached via break)
self.mark_label(&loop_end);
// Pop loop context
self.loop_stack.pop();
// Infinite loops produce unit (unless break with value, not supported)
self.emit_zero();
Ok(())
}
/// Compile for loop
/// for i in start..end { body }
pub(crate) fn compile_for_loop(&mut self, for_loop: &syn::ExprForLoop) -> Result<(), CompileError> {
// Push a scope for the for loop (loop variable lives here)
self.push_scope();
// Extract loop variable name
let var_name = Self::extract_pat_name(&for_loop.pat)?;
// Define loop variable in scope (Integer type, unsigned for range)
let loop_var_reg = self.define_var(&var_name, super::VarType::Integer, false)?;
// Parse range expression
let (start, end, inclusive) = self.parse_range_expr(&for_loop.expr)?;
// Labels
let loop_start = self.unique_label("for_start");
let loop_continue = self.unique_label("for_continue");
let loop_end = self.unique_label("for_end");
// Push loop context (continue jumps to increment, not condition)
self.loop_stack.push(LoopContext {
continue_label: loop_continue.clone(),
break_label: loop_end.clone(),
});
// Initialize loop variable
self.compile_expr(&start)?;
self.emit_pop_reg(loop_var_reg);
// Compile end value and store in temp register
let end_reg = self.next_local_reg;
self.next_local_reg += 1;
self.compile_expr(&end)?;
self.emit_pop_reg(end_reg);
// Loop start (condition check)
self.mark_label(&loop_start);
// Compare: loop_var < end (or <= for inclusive)
self.emit_push_reg(loop_var_reg);
self.emit_push_reg(end_reg);
self.emit_cmp();
self.emit_drop();
self.emit_drop();
if inclusive {
// Exit if loop_var > end
self.emit_jump(control::JGT, &loop_end);
} else {
// Exit if loop_var >= end
self.emit_jump(control::JGE, &loop_end);
}
// Body
self.compile_block_stmt(&for_loop.body)?;
// Continue label (increment)
self.mark_label(&loop_continue);
// Increment loop variable
self.emit_push_reg(loop_var_reg);
self.emit_inc();
self.emit_pop_reg(loop_var_reg);
// Jump back to condition
self.emit_jump(control::JMP, &loop_start);
// Loop end
self.mark_label(&loop_end);
// Pop loop context
self.loop_stack.pop();
// Pop scope (for loop variable cleanup)
self.pop_scope();
// For loops produce unit
self.emit_zero();
Ok(())
}
/// Parse a range expression (start..end or start..=end)
pub(crate) fn parse_range_expr(&self, expr: &Expr) -> Result<(Expr, Expr, bool), CompileError> {
match expr {
Expr::Range(range) => {
let start = range.start.as_ref()
.ok_or_else(|| CompileError("Range must have start".to_string()))?;
let end = range.end.as_ref()
.ok_or_else(|| CompileError("Range must have end".to_string()))?;
// Check if inclusive (..=)
let inclusive = matches!(range.limits, syn::RangeLimits::Closed(_));
Ok((*start.clone(), *end.clone(), inclusive))
}
_ => Err(CompileError("For loop requires range expression".to_string())),
}
}
/// Compile break statement
pub(crate) fn compile_break(&mut self) -> Result<(), CompileError> {
let ctx = self.loop_stack.last()
.ok_or_else(|| CompileError("break outside of loop".to_string()))?;
self.emit_jump(control::JMP, &ctx.break_label.clone());
Ok(())
}
/// Compile continue statement
pub(crate) fn compile_continue(&mut self) -> Result<(), CompileError> {
let ctx = self.loop_stack.last()
.ok_or_else(|| CompileError("continue outside of loop".to_string()))?;
self.emit_jump(control::JMP, &ctx.continue_label.clone());
Ok(())
}
/// Compile a block as statements (for loop bodies)
/// Unlike compile_block, this drops ALL expression results
pub(crate) fn compile_block_stmt(&mut self, block: &Block) -> Result<(), CompileError> {
// Push new scope for this block
self.push_scope();
for stmt in &block.stmts {
match stmt {
syn::Stmt::Expr(expr, _) => {
self.compile_expr(expr)?;
self.emit_drop(); // Always drop expression result
}
syn::Stmt::Local(local) => {
self.compile_local(local)?;
}
_ => return Err(CompileError("Unsupported statement type".to_string())),
}
}
// Pop scope and cleanup heap variables
self.pop_scope();
Ok(())
}
/// Compile a block as expression (returns value on stack)
pub(crate) fn compile_block(&mut self, block: &Block) -> Result<(), CompileError> {
// Push new scope for this block
self.push_scope();
let stmts = &block.stmts;
let len = stmts.len();
// Empty block produces unit (0)
if len == 0 {
self.pop_scope();
self.emit_zero();
return Ok(());
}
// Process all statements except the last
for stmt in stmts.iter().take(len.saturating_sub(1)) {
match stmt {
syn::Stmt::Expr(expr, Some(_)) => {
// Expression with semicolon - drop result
self.compile_expr(expr)?;
self.emit_drop();
}
syn::Stmt::Expr(expr, None) => {
// Expression without semicolon in middle - drop anyway
self.compile_expr(expr)?;
self.emit_drop();
}
syn::Stmt::Local(local) => {
self.compile_local(local)?;
}
_ => return Err(CompileError("Unsupported statement type".to_string())),
}
}
// Last statement determines block value
if let Some(last) = stmts.last() {
match last {
syn::Stmt::Expr(expr, None) => {
// Expression without semicolon - keep result
self.compile_expr(expr)?;
}
syn::Stmt::Expr(expr, Some(_)) => {
// Expression with semicolon - drop and push unit
self.compile_expr(expr)?;
self.emit_drop();
self.emit_zero();
}
syn::Stmt::Local(local) => {
self.compile_local(local)?;
self.emit_zero(); // let binding produces unit
}
_ => return Err(CompileError("Unsupported statement type".to_string())),
}
}
// Pop scope and cleanup heap variables
self.pop_scope();
Ok(())
}
}