rustleaf 0.1.0

A simple programming language interpreter written in Rust
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# 6. Statements

Statements are syntactic constructs that either execute expressions for their side effects or perform special operations that cannot be expressions. This chapter defines the various statement types in RustLeaf, their syntax, and execution semantics.

### 6.1. Statement Grammar

The statement grammar follows a unified structure where most constructs are expressions, but certain operations are statement-only.

**Statement Syntax:**
```
Statement = Expression ";" Statement?
          | Assignment ";" Statement?
          | Declaration ";" Statement?
          | ControlFlow ";" Statement?
          | Import ";" Statement?
          | Expression  // final expression value (no semicolon)

Assignment = LValue AssignOp Expression
Declaration = "var" Pattern ("=" Expression)?
ControlFlow = ReturnStmt | BreakStmt | ContinueStmt
Import = "use" ModulePath ImportList
```

**Statement Execution Rules:**
- Statements execute in the order they appear
- Statements are separated by semicolons, except for the final expression in a block
- The final expression in a block (without semicolon) becomes the block's value
- Module-level statements execute when the module is loaded
- Control flow statements (break, continue, return) alter execution flow

**Statement Categories:**
1. **Expression statements** - Expressions executed for side effects (followed by semicolon)
2. **Assignment** - Update variables or object properties (statement-only, not expressions)
3. **Declaration** - Introduce new variables (statement-only)
4. **Control flow** - Alter execution flow (statement-only)
5. **Import** - Import modules and symbols (statement-only)

**Semicolon Rules:**
```
// Semicolons separate statements
fn example() {
    print("first");   // Semicolon required
    print("second");  // Semicolon required
    print("third")    // No semicolon - this is the return value
}

// Error: missing semicolon
fn bad_example() {
    print("first")    // Parse error: missing semicolon
    print("second")   // Parser stops at previous error
}

// Block with multiple statements
{
    var x = 10;       // Semicolon required
    x = x + 1;        // Semicolon required
    x * 2             // No semicolon - block returns x * 2
}
```

### 6.2. Expression Statements

Any expression can be used as a statement by following it with a semicolon.

**Syntax:**
```
ExpressionStatement = Expression ";"
```

**Expression Statement Rules:**
- The expression is evaluated for its side effects
- The resulting value is discarded
- Commonly used for function calls and method calls
- Pure expressions without side effects are allowed but not useful
- Assignment is **not** an expression and therefore cannot appear in expression statements

**Examples:**
```
// Function call statements
print("Hello, World!");
list.append(42);
file.close();

// Method calls
list.sort();
dict.clear();

// Pure expressions (legal but not useful)
42;              // Evaluates to 42, then discards it
x + y;           // Computes sum, then discards it
true and false;  // Evaluates to false, then discards it
```

### 6.3. Declaration Statements

Variables are declared using the `var` keyword, optionally with initialization.

**Syntax:**
```
Declaration = "var" Pattern ("=" Expression)?
Pattern = Identifier 
        | ListPattern
        | DictPattern
```

**Declaration Rules:**
- Creates new variables in the current scope
- Uninitialized variables default to `null`
- Destructuring patterns can declare multiple variables
- Pattern must match the initializer structure

**Examples:**
```
// Simple declarations
var x;                    // x is null
var y = 42;              // y is 42
var name = "Alice";      // name is "Alice"

// Destructuring declarations
var [a, b, c] = [1, 2, 3];           // a=1, b=2, c=3
var [first, *rest] = [1, 2, 3, 4];   // first=1, rest=[2,3,4]
var {x, y} = {x: 10, y: 20, z: 30};  // x=10, y=20

// Nested destructuring
var [[a, b], {name}] = [[1, 2], {name: "Bob", age: 30}];

// Pattern mismatch error
var [x, y] = [1];        // Error: Pattern mismatch
var {foo} = {bar: 1};    // Error: Key 'foo' not found
```

### 6.4. Assignment Statements

Assignment statements update the value stored in a variable or mutable location.

**Syntax:**
```
Assignment = LValue AssignOp Expression
LValue = Identifier
       | Expression "." Identifier  
       | Expression "[" Expression "]"
AssignOp = "=" | "+=" | "-=" | "*=" | "/=" | "%="
```

**Assignment Rules:**
- Assignment is a statement, not an expression
- Assignment does not return a value
- Left-hand side must be a valid assignment target
- Right-hand side is evaluated before the assignment
- Cannot be used in conditional contexts or embedded in expressions

**Examples:**
```
// Simple assignment
x = 10;
y = "hello";

// Property assignment
obj.field = "value";
point.x = 100;

// Index assignment  
arr[0] = 42;
dict["key"] = "value";

// Compound assignment
count += 1;
total *= 2;
name += " Smith";

// Nested assignment targets
matrix[0][1] = 99;
obj.nested.field = "deep";

// Invalid: assignment is not an expression
// var result = (x = 5);    // Error
// if x = 10 { }            // Error
```

### 6.5. Control Flow Statements

Control flow statements alter the sequential execution of statements.

**Syntax:**
```
ControlFlow = ReturnStmt | BreakStmt | ContinueStmt

ReturnStmt = "return" Expression?
BreakStmt = "break" Expression?
ContinueStmt = "continue"
```

**Control Flow Rules:**
- These constructs are statements only, not expressions
- They immediately alter execution flow
- Cannot be used in expression contexts
- Must appear in appropriate scopes (return in functions, break/continue in loops)

### 6.6. Loop Statements

Loop constructs (`while`, `for`, `loop`) use identical syntax in both statement and expression contexts. When used as statements, any return value is discarded. When used as expressions, they can return values via `break` statements.

#### 6.6.1. While Statements

While statements repeatedly execute a block while a condition is true.

**Syntax:**
```
WhileStatement = "while" Expression Block
```

**Semantics:**
- Evaluates condition before each iteration
- Executes block if condition is truthy
- Continues until condition is falsy
- Body may contain break/continue statements
- When used as a statement (not assigned to variable), the return value is discarded
- See Section 5.14.1 for while expressions that return values

**Examples:**
```
// Basic while loop
var i = 0;
while i < 10 {
    print(i);
    i += 1;
}

// While with break
while true {
    var input = read_line();
    if input == "quit" {
        break;
    }
    process(input);
}

// While with continue  
var i = 0;
while i < 100 {
    i += 1;
    if i % 2 == 0 {
        continue;  // Skip even numbers
    }
    print(i);  // Only prints odd numbers
}
```

#### 6.6.2. For Statements

For statements iterate over sequences using the iterator protocol.

**Syntax:**
```
ForStatement = "for" Pattern "in" Expression Block
Pattern = Identifier | ListPattern | DictPattern
```

**Semantics:**
- Iterates over the collection using iterator protocol
- Pattern binds values on each iteration
- When used as a statement (not assigned to variable), the return value is discarded
- See Section 5.14.2 for for expressions that return values

**Iterator Protocol:**
For statements work with any object that implements the iterator protocol. See Section 12.5 for complete iterator protocol specification, including requirements for `op_iter()` and `op_next()` methods. Iterator completion is detected using `is_unit()` since unit values cannot be used in boolean contexts. Built-in types (list, dict, string) implement this protocol.

**Examples:**
```
// Simple iteration
for x in [1, 2, 3, 4, 5] {
    print(x);
}

// String iteration (iterates over characters)
for ch in "hello" {
    print(ch);  // Prints: h, e, l, l, o
}

// Dict iteration (yields [key, value] pairs)
for key, value in {a: 1, b: 2, c: 3} {
    print("${key} = ${value}");
}

// Destructuring in for loop
var points = [[0, 0], [1, 1], [2, 4]];
for [x, y] in points {
    print("Point at (${x}, ${y})");
}

// Using custom iterator
class Range {
    var start;
    var end;
    
    static fn new(start, end) {
        var r = Range();
        r.start = start;
        r.end = end;
        r
    }
    
    fn op_iter() {
        var iter = RangeIterator();
        iter.current = self.start;
        iter.end = self.end;
        iter
    }
}

class RangeIterator {
    var current;
    var end;
    
    fn op_next() {
        if self.current >= self.end {
            return;  // Returns unit - iteration complete
        }
        var value = self.current;
        self.current += 1;
        value
    }
}

for i in Range.new(0, 5) {
    print(i);  // Prints: 0, 1, 2, 3, 4
}
```

**Unit Type:**
The unit type represents "no meaningful value" and is used consistently throughout RustLeaf for:

**Unit Value Properties:**
- Cannot be written as a literal in source code
- Has type name `"unit"` when checked with `type()`
- Can be checked with `is_unit(value)` built-in function
- All unit values are equal to each other

**When Unit is Returned:**
- Functions that end with a statement rather than an expression
- `return;` statements (return without a value)
- `op_next()` when iteration is complete
- Any control flow that doesn't explicitly produce a value

**Statement vs Expression Semantics:**
- Statements are executed for their side effects and don't produce values
- Expression statements execute an expression for side effects but discard the result
- Functions return the value of their last expression, or unit if they end with a statement

### 6.7. Try-Catch Statements

Try-catch statements handle errors that may occur during execution.

**Syntax:**
```
TryStatement = "try" Block "catch" Pattern Block
Pattern = Identifier | DictPattern
```

**Error Handling:**
- Try block executes normally until an error occurs
- On error, control transfers to catch block
- Catch pattern binds the error value
- No finally clause (use `with` statement for cleanup)
- Try-catch uses identical syntax in both statement and expression forms (see Section 5.8)
- Parser distinguishes based on context (whether result is used)

**Examples:**
```
// Basic error handling
try {
    var result = risky_operation();
    process(result);
} catch e {
    print("Error occurred: ${e}");
}

// Pattern matching on error
try {
    validate_input(data);
} catch {type, message} {
    print("${type} error: ${message}");
}

// Re-raising errors
try {
    database_operation();
} catch e {
    log_error(e);
    raise(e);  // Re-raise the same error
}

// Nested try-catch
try {
    try {
        inner_operation();
    } catch e {
        print("Inner error: ${e}");
        raise("Failed in inner block");
    }
} catch e {
    print("Outer error: ${e}");
}
```

### 6.8. With Statements

With statements ensure resources are properly cleaned up after use.

**Syntax:**
```
WithStatement = "with" ResourceList Block
ResourceList = Resource ("," Resource)*
Resource = Identifier "=" Expression
```

**Resource Management:**
- Each expression is evaluated and bound to its identifier
- Block executes with resources available
- After block (or on error), resources are cleaned up
- Cleanup calls `close()` method on each resource
- Cleanup happens in reverse order of acquisition
- If resource lacks `close()` method, no cleanup occurs

**Examples:**
```
// Single resource
with file = open("data.txt") {
    var contents = file.read();
    process(contents);
}  // file.close() called automatically

// Multiple resources
with input = open("input.txt"), output = create("output.txt") {
    output.write(input.read());
}  // output.close() then input.close() called

// With error handling
try {
    with conn = connect_database() {
        conn.query("SELECT * FROM users");
    }  // conn.close() called even if query fails
} catch e {
    print("Database error: ${e}");
}

// Custom resource with close method
class TempDirectory {
    var path;
    
    static fn new(path) {
        var td = TempDirectory();
        td.path = path;
        create_directory(td.path);
        td
    }
    
    fn close() {
        remove_directory(self.path);
    }
}

with temp = TempDirectory.new("/tmp/work") {
    // Use temporary directory
}  // Directory automatically removed
```

**Note:** Break, continue, and return statements are now covered in Section 6.5 (Control Flow Statements) since they follow the unified grammar structure.

### 6.9. Import Statements

Import statements bring symbols from other modules into scope.

**Syntax:**
```
Import = "use" ModulePath ImportList
ModulePath = Identifier ("::" Identifier)*
ImportList = "::" "*"
           | "::" "{" ImportItem ("," ImportItem)* "}"
           | "::" Identifier

ImportItem = Identifier ("as" Identifier)?
```

**Import Rules:**
- Modules correspond to `.rustleaf` files
- Path separator is `::`
- Can import all public symbols with `*`
- Can import specific symbols with `{...}`
- Can rename imports with `as`
- Imported symbols must be marked `pub` in source module

**Module Resolution:**
- Paths resolve relative to current module's directory
- Use `super::` to access parent directory
- See Chapter 10 for complete module resolution specification

**Examples:**
```
// Import everything from a module
use std::collections::*;

// Import specific items
use std::io::{read_file, write_file};

// Import with renaming
use std::math::{sqrt as square_root, pi};

// Import a single item
use graphics::Color;

// Nested module paths
use game::entities::player::Player;

// Multiple imports from same module
use std::string::{
    split,
    join,
    trim as strip_whitespace
};

// Error: importing private symbols
// In other.rustleaf: fn helper() { }  // Not pub
use other::helper;  // Error: helper is not public
```

**Module Example:**
```
// File: utils/math.rustleaf
pub fn add(a, b) {
    a + b
}

pub fn multiply(a, b) {
    a * b
}

fn internal_helper() {  // Not accessible outside module
    // ...
}

// File: main.rustleaf
use utils::math::{add, multiply};

var sum = add(2, 3);
var product = multiply(4, 5);
```

---