windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
// Larger primary-expression forms: `match`, closures (`|_|`, `||`), `if` / `if let`, `unsafe` blocks.

use crate::lexer::Token;
use crate::parser::ast::*;
use crate::parser_impl::Parser;

impl Parser {
    pub(in crate::parser) fn parse_primary_match(
        &mut self,
    ) -> Result<&'static Expression<'static>, String> {
        // Match expression
        self.advance();
        // Parse the value to match on, but don't allow struct literals here
        // (since we need to see the { for the match arms)
        let value = self.parse_match_value()?;

        self.expect(Token::LBrace)?;

        let mut arms = Vec::new();
        while self.current_token() != &Token::RBrace {
            let pattern = self.parse_pattern_with_or()?;

            // Parse optional guard: if condition
            let guard = if self.current_token() == &Token::If {
                self.advance();
                Some(self.parse_expression()?)
            } else {
                None
            };

            self.expect(Token::FatArrow)?;

            // TDD: Match arms can contain assignments (statements), not just expressions
            // Check if this is a block or a single statement/expression
            let (body, is_block) = if self.current_token() == &Token::LBrace {
                // Block expression: match x { Pattern => { ... } }
                self.advance();
                let statements = self.parse_block_statements()?;
                self.expect(Token::RBrace)?;
                let block = self.alloc_expr(Expression::Block {
                    statements,
                    is_unsafe: false,
                    location: self.current_location(),
                });
                (block, true)
            } else {
                // Try to parse as statement first (for assignments), then as expression
                let _checkpoint = self.position;

                // Peek ahead to see if this looks like an assignment
                let is_assignment = if let Token::Ident(_) = self.current_token() {
                    // Check for identifier followed by = (or .field = for field assignment)
                    let mut ahead = 1;
                    loop {
                        match self.peek(ahead) {
                            Some(Token::Assign) => break true,
                            Some(Token::Dot) | Some(Token::LBracket) => {
                                ahead += 1;
                                if let Some(Token::Ident(_)) = self.peek(ahead) {
                                    ahead += 1;
                                } else {
                                    break false;
                                }
                            }
                            _ => break false,
                        }
                    }
                } else {
                    false
                };

                // TDD: break/continue/return in match arm (e.g. None => break) must be parsed
                // as statements, not expressions. Expression parser doesn't handle them.
                let is_control_flow = matches!(
                    self.current_token(),
                    Token::Break | Token::Continue | Token::Return
                );

                if is_assignment || is_control_flow {
                    // Parse as statement (assignment or break/continue/return)
                    let stmt = self.parse_statement()?;
                    // Wrap in block expression
                    let block = self.alloc_expr(Expression::Block {
                        statements: vec![stmt],
                        is_unsafe: false,
                        location: self.current_location(),
                    });
                    (block, false)
                } else {
                    // Parse as expression
                    (self.parse_expression()?, false)
                }
            };

            arms.push(MatchArm {
                pattern,
                guard,
                body,
            });

            // TDD: Match arms must be comma-separated
            // Exception: Commas are optional after block expressions (Rust-style)
            if self.current_token() == &Token::Comma {
                self.advance();
                // Allow trailing comma before closing brace
                if self.current_token() == &Token::RBrace {
                    break;
                }
            } else if self.current_token() == &Token::RBrace {
                // End of match arms
                break;
            } else if !is_block {
                // No comma after a non-block expression (and not at end) - this is an error
                return Err(format!(
                    "Expected ',' or '}}' after match arm, got {:?}",
                    self.current_token()
                ));
            }
            // If is_block is true and no comma, continue to next arm (comma is optional)
        }

        self.expect(Token::RBrace)?;

        // Convert match arms into a match expression
        // For now, wrap in a block expression
        let match_stmt = self.alloc_stmt(Statement::Match {
            value,
            arms,
            location: self.current_location(),
        });
        Ok(self.alloc_expr(Expression::Block {
            statements: vec![match_stmt],
            is_unsafe: false,
            location: self.current_location(),
        }))
    }

    pub(in crate::parser) fn parse_primary_closure_pipe(
        &mut self,
    ) -> Result<&'static Expression<'static>, String> {
        // Closure: |params| body
        self.advance();
        let mut parameters = Vec::new();

        while self.current_token() != &Token::Pipe {
            // Handle patterns like &x, &mut x, (a, b), or just x
            let param_name = match self.current_token() {
                Token::LParen => {
                    // Tuple destructuring: |(a, b)| or |(a, b, c)|
                    self.advance();
                    let mut tuple_parts = Vec::new();

                    while self.current_token() != &Token::RParen {
                        if let Token::Ident(name) = self.current_token() {
                            tuple_parts.push(name.clone());
                            self.advance();
                        } else {
                            return Err(format!(
                                "Expected identifier in tuple pattern (at token position {})",
                                self.position
                            ));
                        }

                        if self.current_token() == &Token::Comma {
                            self.advance();
                        } else if self.current_token() != &Token::RParen {
                            return Err(format!(
                                "Expected ',' or ')' in tuple pattern (at token position {})",
                                self.position
                            ));
                        }
                    }

                    self.expect(Token::RParen)?;

                    // Format as tuple pattern: (a, b)
                    format!("({})", tuple_parts.join(", "))
                }
                Token::Ampersand => {
                    self.advance();
                    // Skip optional 'mut'
                    if self.current_token() == &Token::Mut {
                        self.advance();
                    }
                    // Get the identifier
                    if let Token::Ident(name) = self.current_token() {
                        let n = name.clone();
                        self.advance();
                        n
                    } else {
                        return Err("Expected identifier after & in closure parameter".to_string());
                    }
                }
                Token::Ident(name) => {
                    let n = name.clone();
                    self.advance();
                    n
                }
                Token::Underscore => {
                    self.advance();
                    "_".to_string()
                }
                _ => {
                    return Err(format!(
                        "Expected parameter name in closure (at token position {})",
                        self.position
                    ));
                }
            };

            parameters.push(param_name);

            if self.current_token() == &Token::Comma {
                self.advance();
            }
        }

        self.expect(Token::Pipe)?;

        // Parse closure body - can be either an expression or a block
        let body = if self.current_token() == &Token::LBrace {
            // Block closure: |x| { statements }
            self.advance();
            let statements = self.parse_block_statements()?;
            self.expect(Token::RBrace)?;
            self.alloc_expr(Expression::Block {
                statements,
                is_unsafe: false,
                location: self.current_location(),
            })
        } else {
            // Expression closure: |x| expr
            // Check if this looks like a compound assignment (e.g., *c += 1)
            // by peeking ahead for compound assignment operators
            let checkpoint = self.position;

            // Try to parse the left side
            let _left_expr = self.parse_primary_expression()?;

            // Check if followed by a compound assignment operator
            let is_compound_assign = matches!(
                self.current_token(),
                Token::PlusAssign
                    | Token::MinusAssign
                    | Token::StarAssign
                    | Token::SlashAssign
                    | Token::PercentAssign
            );

            if is_compound_assign {
                // Reset and parse as a statement
                self.position = checkpoint;
                let stmt = self.parse_statement()?;
                self.alloc_expr(Expression::Block {
                    statements: vec![stmt],
                    is_unsafe: false,
                    location: self.current_location(),
                })
            } else {
                // Reset and parse as a normal expression
                self.position = checkpoint;
                self.parse_expression()?
            }
        };

        Ok(self.alloc_expr(Expression::Closure {
            parameters,
            body,
            location: self.current_location(),
        }))
    }

    pub(in crate::parser) fn parse_primary_closure_or(
        &mut self,
    ) -> Result<&'static Expression<'static>, String> {
        // Closure with no parameters: || body
        self.advance(); // consume '||'

        // Parse closure body - can be either an expression or a block
        let body = if self.current_token() == &Token::LBrace {
            // Block closure: || { statements }
            self.advance();
            let statements = self.parse_block_statements()?;
            self.expect(Token::RBrace)?;
            self.alloc_expr(Expression::Block {
                statements,
                is_unsafe: false,
                location: self.current_location(),
            })
        } else {
            // Expression closure: || expr
            self.parse_expression()?
        };

        Ok(self.alloc_expr(Expression::Closure {
            parameters: Vec::new(), // No parameters
            body,
            location: self.current_location(),
        }))
    }

    pub(in crate::parser) fn parse_primary_if(
        &mut self,
    ) -> Result<&'static Expression<'static>, String> {
        // If expression: if cond { ... } else { ... }
        // or if let pattern = expr { ... } else { ... }
        self.advance(); // consume 'if'

        // Check for `if let` pattern
        if self.current_token() == &Token::Let {
            self.advance(); // consume 'let'

            // Parse pattern
            let pattern = self.parse_pattern()?;

            self.expect(Token::Assign)?; // '='

            // Parse the expression to match against
            let expr = self.parse_match_value()?;

            // Parse optional guard: `if let Some(x) = opt if x > 0 { ... }`
            let guard = if self.current_token() == &Token::If {
                self.advance();
                Some(self.parse_match_value()?)
            } else {
                None
            };

            self.expect(Token::LBrace)?;
            let then_block = self.parse_block_statements()?;
            self.expect(Token::RBrace)?;

            let else_block = if self.current_token() == &Token::Else {
                self.advance();
                self.expect(Token::LBrace)?;
                let block = self.parse_block_statements()?;
                self.expect(Token::RBrace)?;
                Some(block)
            } else {
                None
            };

            let then_body = self.alloc_expr(Expression::Block {
                statements: then_block,
                is_unsafe: false,
                location: self.current_location(),
            });

            let mut arms = vec![MatchArm {
                pattern,
                guard,
                body: then_body,
            }];

            if let Some(else_block) = else_block {
                let else_body = self.alloc_expr(Expression::Block {
                    statements: else_block,
                    is_unsafe: false,
                    location: self.current_location(),
                });
                arms.push(MatchArm {
                    pattern: Pattern::Wildcard,
                    guard: None,
                    body: else_body,
                });
            }

            let match_stmt = self.alloc_stmt(Statement::Match {
                value: expr,
                arms,
                location: self.current_location(),
            });

            return Ok(self.alloc_expr(Expression::Block {
                statements: vec![match_stmt],
                is_unsafe: false,
                location: self.current_location(),
            }));
        }

        // Regular if expression
        // Use parse_match_value to avoid struct literal ambiguity
        let condition = self.parse_match_value()?;

        self.expect(Token::LBrace)?;
        let then_block = self.parse_block_statements()?;
        self.expect(Token::RBrace)?;

        let else_block = if self.current_token() == &Token::Else {
            self.advance();
            // Check for else if
            if self.current_token() == &Token::If {
                // else if - parse as nested if statement
                let if_stmt = self.parse_if()?;
                Some(vec![if_stmt])
            } else {
                // else - parse block
                self.expect(Token::LBrace)?;
                let block = self.parse_block_statements()?;
                self.expect(Token::RBrace)?;
                Some(block)
            }
        } else {
            None
        };

        // Convert to expression by wrapping in a block with an if statement
        // that returns the value
        let if_stmt = self.alloc_stmt(Statement::If {
            condition,
            then_block,
            else_block,
            location: self.current_location(),
        });

        Ok(self.alloc_expr(Expression::Block {
            statements: vec![if_stmt],
            is_unsafe: false,
            location: self.current_location(),
        }))
    }

    pub(in crate::parser) fn parse_primary_unsafe_block(
        &mut self,
    ) -> Result<&'static Expression<'static>, String> {
        // Unsafe block: unsafe { ... }
        self.advance(); // consume 'unsafe'
        self.expect(Token::LBrace)?;
        let body = self.parse_block_statements()?;
        self.expect(Token::RBrace)?;
        Ok(self.alloc_expr(Expression::Block {
            statements: body,
            is_unsafe: true,
            location: self.current_location(),
        }))
    }
}