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
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
// Postfix operators: calls, method calls, indexing, slicing, casts, macros.

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

impl Parser {
    pub(in crate::parser) fn parse_postfix_expression(
        &mut self,
        mut expr: &'static Expression<'static>,
    ) -> Result<&'static Expression<'static>, String> {
        loop {
            expr = match self.current_token() {
                Token::Dot => {
                    // Peek ahead to check for .await
                    if self.peek(1) == Some(&Token::Await) {
                        self.advance(); // consume '.'
                        self.advance(); // consume 'await'
                        self.alloc_expr(Expression::Await {
                            expr,
                            location: self.current_location(),
                        })
                    } else {
                        self.advance();
                        // Allow keywords and numeric indices as field names (e.g., std.thread, tuple.0)
                        let field_opt = match self.current_token() {
                            Token::Ident(f) => Some(f.clone()),
                            Token::Thread => Some("thread".to_string()),
                            Token::Async => Some("async".to_string()),
                            Token::IntLiteral(n) | Token::IntLiteralSuffixed(n, _) => {
                                Some(n.to_string())
                            }
                            _ => None,
                        };
                        if let Some(field) = field_opt {
                            self.advance();

                            // Check for turbofish ::<Type>
                            let type_args = if self.current_token() == &Token::ColonColon {
                                // Peek ahead to see if this is turbofish
                                if self.peek(1) == Some(&Token::Lt) {
                                    self.advance(); // consume ::
                                    self.advance(); // consume <
                                    let mut types = vec![self.parse_type()?];
                                    while self.current_token() == &Token::Comma {
                                        self.advance();
                                        if self.current_token() != &Token::Gt
                                            && self.current_token() != &Token::Shr
                                        {
                                            types.push(self.parse_type()?);
                                        }
                                    }
                                    self.expect_gt_or_split_shr()?; // Handle nested generics
                                    Some(types)
                                } else {
                                    // Not turbofish - don't consume ::
                                    None
                                }
                            } else {
                                None
                            };

                            if self.current_token() == &Token::LParen {
                                // Check for newline before LParen (ASI)
                                if self.had_newline_before_current() {
                                    // ASI: This LParen starts a new statement, not a method call
                                    // Create a field access and break
                                    self.alloc_expr(Expression::FieldAccess {
                                        object: expr,
                                        field,
                                        location: self.current_location(),
                                    })
                                } else {
                                    // Method call (possibly with turbofish)
                                    self.advance();
                                    let arguments = self.parse_arguments()?;
                                    self.expect(Token::RParen)?;
                                    self.alloc_expr(Expression::MethodCall {
                                        object: expr,
                                        method: field,
                                        type_args,
                                        arguments,
                                        location: self.current_location(),
                                    })
                                }
                            } else if type_args.is_some() {
                                return Err(
                                    "Turbofish syntax only allowed on method calls".to_string()
                                );
                            } else {
                                // Field access
                                self.alloc_expr(Expression::FieldAccess {
                                    object: expr,
                                    field,
                                    location: self.current_location(),
                                })
                            }
                        } else {
                            return Err(format!(
                                "Expected field or method name (at token position {})",
                                self.position
                            ));
                        }
                    }
                }
                Token::ColonColon => {
                    // Turbofish on function/static method: func::<Type>() or Type::method::<T>()
                    self.advance();
                    if self.current_token() == &Token::Lt {
                        self.advance();
                        let mut types = vec![self.parse_type()?];
                        while self.current_token() == &Token::Comma {
                            self.advance();
                            if self.current_token() != &Token::Gt
                                && self.current_token() != &Token::Shr
                            {
                                types.push(self.parse_type()?);
                            }
                        }
                        self.expect_gt_or_split_shr()?; // Handle nested generics

                        // Now expect either () for call, :: for path continuation, or identifier
                        if self.current_token() == &Token::LParen {
                            self.advance();
                            let arguments = self.parse_arguments()?;
                            self.expect(Token::RParen)?;
                            // Convert to method call with turbofish
                            // For func::<T>(), treat as a special method call on the function
                            self.alloc_expr(Expression::MethodCall {
                                object: expr,
                                method: String::new(), // Empty method name signals turbofish call
                                type_args: Some(types),
                                arguments,
                                location: self.current_location(),
                            })
                        } else if self.current_token() == &Token::ColonColon {
                            // Vec::<int>::new() - another :: after turbofish
                            // Continue parsing in the loop, the :: will be handled on next iteration
                            // We need to represent this as a turbofish-qualified path
                            // For now, convert the types to a string and append to the identifier
                            let mut type_str = String::new();
                            type_str.push_str("::<");
                            for (i, ty) in types.iter().enumerate() {
                                if i > 0 {
                                    type_str.push_str(", ");
                                }
                                type_str.push_str(&self.type_to_string(ty));
                            }
                            type_str.push('>');

                            // Update the expression to include the turbofish
                            if let Expression::Identifier { name, .. } = expr {
                                expr = self.alloc_expr(Expression::Identifier {
                                    name: format!("{}{}", name, type_str),
                                    location: self.current_location(),
                                });
                            } else {
                                return Err(
                                    "Turbofish can only be applied to identifiers".to_string()
                                );
                            }
                            // Continue the loop to handle the next ::
                            continue;
                        } else if let Token::Ident(method) = self.current_token() {
                            // Type::method or module::function continuation
                            let method = method.clone();
                            self.advance();
                            self.alloc_expr(Expression::MethodCall {
                                object: expr,
                                method,
                                type_args: None,
                                arguments: vec![],
                                location: self.current_location(),
                            })
                        } else {
                            return Err(format!(
                                "Expected '(', '::', or identifier after '::<Type>', got {:?}",
                                self.current_token()
                            ));
                        }
                    } else {
                        // Allow keywords as identifiers after :: (e.g., std::thread, std::async)
                        let method = match self.current_token() {
                            Token::Ident(n) => n.clone(),
                            Token::Thread => "thread".to_string(),
                            Token::Async => "async".to_string(),
                            Token::Await => "await".to_string(),
                            _ => {
                                return Err(format!(
                                    "Expected '<' or identifier after '::', got {:?}",
                                    self.current_token()
                                ));
                            }
                        };
                        self.advance();

                        // Check for turbofish on this method
                        let type_args = if self.current_token() == &Token::ColonColon {
                            // Peek ahead to see if this is turbofish
                            if self.peek(1) == Some(&Token::Lt) {
                                self.advance(); // consume ::
                                self.advance(); // consume <
                                let mut types = vec![self.parse_type()?];
                                while self.current_token() == &Token::Comma {
                                    self.advance();
                                    if self.current_token() != &Token::Gt
                                        && self.current_token() != &Token::Shr
                                    {
                                        types.push(self.parse_type()?);
                                    }
                                }
                                self.expect_gt_or_split_shr()?; // Handle nested generics
                                Some(types)
                            } else {
                                // Not turbofish - don't consume ::
                                None
                            }
                        } else {
                            None
                        };

                        if self.current_token() == &Token::LParen {
                            self.advance();
                            let arguments = self.parse_arguments()?;
                            self.expect(Token::RParen)?;
                            self.alloc_expr(Expression::MethodCall {
                                object: expr,
                                method,
                                type_args,
                                arguments,
                                location: self.current_location(),
                            })
                        } else {
                            // Just a path, treat as field access
                            self.alloc_expr(Expression::FieldAccess {
                                object: expr,
                                field: method,
                                location: self.current_location(),
                            })
                        }
                    }
                }
                Token::LParen => {
                    // Check for newline before LParen (automatic semicolon insertion)
                    // If there was a newline, this might be a new statement, not a function call
                    if self.had_newline_before_current() {
                        // ASI: Treat newline as statement terminator
                        // Don't consume the LParen - it belongs to the next statement
                        break;
                    }

                    self.advance();
                    let arguments = self.parse_arguments()?;
                    self.expect(Token::RParen)?;
                    self.alloc_expr(Expression::Call {
                        function: expr,
                        arguments,
                        location: self.current_location(),
                    })
                }
                Token::Question => {
                    // TryOp: expr?
                    // No ambiguity since we removed ternary operator
                    self.advance();
                    self.alloc_expr(Expression::TryOp {
                        expr,
                        location: self.current_location(),
                    })
                }
                Token::LBracket => {
                    self.advance();

                    // Check for slice syntax: [start..end], [start..], [..end]
                    if self.current_token() == &Token::DotDot {
                        // [..end] - slice from beginning
                        self.advance(); // consume '..'
                        let end = if self.current_token() != &Token::RBracket {
                            Some(self.parse_expression()?)
                        } else {
                            None
                        };
                        self.expect(Token::RBracket)?;

                        // Desugar [..end] to .slice(0, end)
                        let end_expr = end.unwrap_or_else(|| {
                            self.alloc_expr(Expression::MethodCall {
                                object: self.alloc_expr(expr.clone()),
                                method: "len".to_string(),
                                type_args: None,
                                arguments: vec![],
                                location: self.current_location(),
                            })
                        });

                        self.alloc_expr(Expression::MethodCall {
                            object: expr,
                            method: "slice".to_string(),
                            type_args: None,
                            arguments: vec![
                                (
                                    None,
                                    self.alloc_expr(Expression::Literal {
                                        value: Literal::Int(0),
                                        location: self.current_location(),
                                    }),
                                ),
                                (None, end_expr),
                            ],
                            location: self.current_location(),
                        })
                    } else {
                        // Parse the first expression
                        // We need to parse without consuming .. as a range operator
                        // So we manually parse a binary expression that stops at ..
                        let start_or_index = self.parse_binary_expression(0)?;

                        // Check if this is a slice or regular index
                        if self.current_token() == &Token::DotDot {
                            // [start..] or [start..end] - slice syntax
                            self.advance(); // consume '..'
                            let end = if self.current_token() != &Token::RBracket {
                                Some(self.parse_expression()?)
                            } else {
                                None
                            };
                            self.expect(Token::RBracket)?;

                            // Desugar [start..end] to .slice(start, end)
                            let end_expr = end.unwrap_or_else(|| {
                                self.alloc_expr(Expression::MethodCall {
                                    object: expr,
                                    method: "len".to_string(),
                                    type_args: None,
                                    arguments: vec![],
                                    location: self.current_location(),
                                })
                            });

                            self.alloc_expr(Expression::MethodCall {
                                object: expr,
                                method: "slice".to_string(),
                                type_args: None,
                                arguments: vec![(None, start_or_index), (None, end_expr)],
                                location: self.current_location(),
                            })
                        } else {
                            // Regular index: [i]
                            self.expect(Token::RBracket)?;
                            self.alloc_expr(Expression::Index {
                                object: expr,
                                index: start_or_index,
                                location: self.current_location(),
                            })
                        }
                    }
                }
                Token::DotDot | Token::DotDotEq => {
                    // Don't parse as range if followed by ] (that's slice syntax)
                    if let Some(next_tok) = self.peek(1) {
                        if next_tok == &Token::RBracket {
                            // This is slice syntax like [1..], not a range
                            // Let the LBracket handler deal with it
                            break;
                        }
                    }

                    let inclusive = self.current_token() == &Token::DotDotEq;
                    self.advance();
                    let end = self.parse_primary_expression()?;
                    self.alloc_expr(Expression::Range {
                        start: expr,
                        end,
                        inclusive,
                        location: self.current_location(),
                    })
                }
                Token::As => {
                    self.advance();
                    let type_ = self.parse_type()?;
                    self.alloc_expr(Expression::Cast {
                        expr,
                        type_,
                        location: self.current_location(),
                    })
                }
                Token::Bang => {
                    // Macro invocation: name!(...) or name![...] or name!{...}
                    if let Expression::Identifier { name, .. } = expr {
                        self.advance(); // consume '!'

                        let (delimiter, end_token) = match self.current_token() {
                            Token::LParen => (MacroDelimiter::Parens, Token::RParen),
                            Token::LBracket => (MacroDelimiter::Brackets, Token::RBracket),
                            Token::LBrace => (MacroDelimiter::Braces, Token::RBrace),
                            _ => return Err("Expected (, [, or { after macro name!".to_string()),
                        };

                        self.advance(); // consume opening delimiter

                        let mut args = Vec::new();

                        // Check for empty macro: vec![], println!()
                        let mut is_repeat_flag = false;
                        if self.current_token() != &end_token {
                            // Parse first argument
                            args.push(self.parse_expression()?);

                            // Check for vec![item; count] repetition syntax
                            if delimiter == MacroDelimiter::Brackets
                                && self.current_token() == &Token::Semicolon
                            {
                                self.advance(); // consume semicolon
                                args.push(self.parse_expression()?);
                                is_repeat_flag = true; // This is vec![x; n] repeat syntax
                            } else {
                                // Parse remaining comma-separated arguments
                                while self.current_token() == &Token::Comma {
                                    self.advance();

                                    // Allow trailing comma
                                    if self.current_token() == &end_token {
                                        break;
                                    }

                                    args.push(self.parse_expression()?);
                                }
                            }
                        }

                        self.expect(end_token)?;

                        if name == "format" {
                            let (file, line) = if let Some(loc) = self.current_location() {
                                (Some(loc.file.display().to_string()), Some(loc.line))
                            } else {
                                (None, None)
                            };
                            self.emit_warning(
                                "format!() is Rust syntax. Use string interpolation instead: \"text ${expr}\"".to_string(),
                                file,
                                line,
                                None,
                            );
                        }

                        self.alloc_expr(Expression::MacroInvocation {
                            name: name.clone(),
                            args,
                            delimiter,
                            is_repeat: is_repeat_flag,
                            location: self.current_location(),
                        })
                    } else {
                        // Not a macro invocation, break out of postfix loop
                        break;
                    }
                }
                _ => break,
            };
        }

        Ok(expr)
    }
}