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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// AST Core Types - Circular dependencies
//
// This file contains types with circular dependencies that must stay together:
// Expression ↔ Statement ↔ Pattern
//
// Independent types have been extracted to separate modules.

// Import types from extracted modules
use crate::parser::ast::literals::{Literal, MacroDelimiter};
use crate::parser::ast::operators::{BinaryOp, CompoundOp, UnaryOp};
use crate::parser::ast::ownership::OwnershipHint;
use crate::parser::ast::types::{AssociatedType, SourceLocation, Type, TypeParam};

// ============================================================================
// PARAMETERS (depends on Pattern - circular)
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Parameter<'ast> {
    pub name: String, // For simple parameters and backward compatibility
    pub pattern: Option<Pattern<'ast>>, // For pattern matching parameters
    pub type_: Type,
    pub ownership: OwnershipHint,
    pub is_mutable: bool, // Whether parameter is declared with 'mut' keyword
    pub decorators: Vec<Decorator<'ast>>, // GPU builtins, etc.
}

// ============================================================================
// DECORATORS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Decorator<'ast> {
    pub name: String,
    pub arguments: Vec<(String, &'ast Expression<'ast>)>, // Named arguments
}

// ============================================================================
// FUNCTIONS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FunctionDecl<'ast> {
    pub name: String,
    pub is_pub: bool,                // Whether this function has pub visibility
    pub is_extern: bool,             // Whether this is an extern function (FFI)
    pub type_params: Vec<TypeParam>, // Generic type parameters with optional bounds: <T: Display, U>
    pub where_clause: Vec<(String, Vec<String>)>, // Where clause: [(type_param, [trait_bounds])]
    pub decorators: Vec<Decorator<'ast>>,
    pub is_async: bool,
    pub parameters: Vec<Parameter<'ast>>,
    pub return_type: Option<Type>,
    pub return_decorators: Vec<Decorator<'ast>>, // Decorators on return type (e.g., @location(0))
    pub body: Vec<&'ast Statement<'ast>>,        // Empty for extern functions
    pub parent_type: Option<String>, // The type name if this function is in an impl block
    /// `None` for inherent `impl Type`; `Some(trait)` for `impl Trait for Type` (disambiguates codegen).
    pub impl_trait: Option<String>,
    pub doc_comment: Option<String>, // Documentation comment (/// lines)
}

// ============================================================================
// STRUCTS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StructField<'ast> {
    pub name: String,
    pub field_type: Type,
    pub decorators: Vec<Decorator<'ast>>,
    pub is_pub: bool,
    pub doc_comment: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StructDecl<'ast> {
    pub name: String,
    pub is_pub: bool,                // Whether this struct has pub visibility
    pub is_extern: bool,             // `extern struct` — opaque / linked type
    pub type_params: Vec<TypeParam>, // Generic type parameters with optional bounds: <T: Clone>
    pub where_clause: Vec<(String, Vec<String>)>, // Where clause: [(type_param, [trait_bounds])]
    pub fields: Vec<StructField<'ast>>,
    pub tuple_fields: Option<Vec<Type>>, // Tuple struct fields: struct Point(i32, i32)
    pub decorators: Vec<Decorator<'ast>>,
    pub doc_comment: Option<String>, // Documentation comment (/// lines)
}

// ============================================================================
// ENUMS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumVariant {
    pub name: String,
    pub data: EnumVariantData,
    pub doc_comment: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EnumVariantData {
    Unit,                        // Variant
    Tuple(Vec<Type>),            // Variant(T1, T2)
    Struct(Vec<(String, Type)>), // Variant { field1: T1, field2: T2 }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumDecl {
    pub name: String,
    pub is_pub: bool,                // Whether this enum has pub visibility
    pub type_params: Vec<TypeParam>, // Generic type parameters: enum Option<T>, enum Result<T, E>
    pub variants: Vec<EnumVariant>,
    pub doc_comment: Option<String>, // Documentation comment (/// lines)
}

// ============================================================================
// STATEMENTS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Statement<'ast> {
    Let {
        pattern: Pattern<'ast>,
        mutable: bool,
        type_: Option<Type>,
        value: &'ast Expression<'ast>,
        /// Optional else block for let-else patterns (e.g., `let Some(x) = opt else { return }`)
        else_block: Option<Vec<&'ast Statement<'ast>>>,
        location: SourceLocation,
    },
    Const {
        name: String,
        type_: Type,
        value: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Static {
        name: String,
        mutable: bool,
        type_: Type,
        value: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Assignment {
        target: &'ast Expression<'ast>,
        value: &'ast Expression<'ast>,
        compound_op: Option<CompoundOp>,
        location: SourceLocation,
    },
    Return {
        value: Option<&'ast Expression<'ast>>,
        location: SourceLocation,
    },
    Expression {
        expr: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    If {
        condition: &'ast Expression<'ast>,
        then_block: Vec<&'ast Statement<'ast>>,
        else_block: Option<Vec<&'ast Statement<'ast>>>,
        location: SourceLocation,
    },
    Match {
        value: &'ast Expression<'ast>,
        arms: Vec<MatchArm<'ast>>,
        location: SourceLocation,
    },
    For {
        pattern: Pattern<'ast>,
        iterable: &'ast Expression<'ast>,
        body: Vec<&'ast Statement<'ast>>,
        location: SourceLocation,
    },
    Loop {
        body: Vec<&'ast Statement<'ast>>,
        location: SourceLocation,
    },
    While {
        condition: &'ast Expression<'ast>,
        body: Vec<&'ast Statement<'ast>>,
        location: SourceLocation,
    },
    Thread {
        body: Vec<&'ast Statement<'ast>>,
        location: SourceLocation,
    },
    Async {
        body: Vec<&'ast Statement<'ast>>,
        location: SourceLocation,
    },
    Defer {
        statement: &'ast Statement<'ast>,
        location: SourceLocation,
    },
    Break {
        location: SourceLocation,
    },
    Continue {
        location: SourceLocation,
    },
    Use {
        path: Vec<String>,
        alias: Option<String>,
        is_pub: bool, // THE WINDJAMMER WAY: Track pub use for re-exports
        location: SourceLocation,
    },
}

impl<'ast> Statement<'ast> {
    /// Get the source location of this statement (if available)
    pub fn location(&self) -> SourceLocation {
        match self {
            Statement::Let { location, .. } => location.clone(),
            Statement::Const { location, .. } => location.clone(),
            Statement::Static { location, .. } => location.clone(),
            Statement::Assignment { location, .. } => location.clone(),
            Statement::Return { location, .. } => location.clone(),
            Statement::Expression { location, .. } => location.clone(),
            Statement::If { location, .. } => location.clone(),
            Statement::Match { location, .. } => location.clone(),
            Statement::For { location, .. } => location.clone(),
            Statement::Loop { location, .. } => location.clone(),
            Statement::While { location, .. } => location.clone(),
            Statement::Thread { location, .. } => location.clone(),
            Statement::Async { location, .. } => location.clone(),
            Statement::Defer { location, .. } => location.clone(),
            Statement::Break { location, .. } => location.clone(),
            Statement::Continue { location, .. } => location.clone(),
            Statement::Use { location, .. } => location.clone(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MatchArm<'ast> {
    pub pattern: Pattern<'ast>,
    pub guard: Option<&'ast Expression<'ast>>, // Optional guard: if condition
    pub body: &'ast Expression<'ast>,
}

// ============================================================================
// PATTERNS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EnumPatternBinding<'ast> {
    None,                                       // No parentheses: None, Empty
    Wildcard,                                   // Parentheses with wildcard: Some(_)
    Single(String),                             // Single binding: Some(x)
    Tuple(Vec<Pattern<'ast>>),                  // Multiple bindings: Rgb(r, g, b)
    Struct(Vec<(String, Pattern<'ast>)>, bool), // Struct pattern: Box { width: w, height: h }, bool=has_wildcard (..)
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Pattern<'ast> {
    Wildcard,
    Identifier(String),
    EnumVariant(String, EnumPatternBinding<'ast>), // Enum name, binding type
    Literal(Literal),
    Tuple(Vec<Pattern<'ast>>),      // Tuple pattern: (a, b, c)
    Or(Vec<Pattern<'ast>>),         // Or pattern: pattern1 | pattern2 | pattern3
    Reference(&'ast Pattern<'ast>), // Reference pattern: &x
    Ref(String),                    // Ref binding: ref x (borrows without moving)
    RefMut(String),                 // RefMut binding: ref mut x (mutable borrow)
    MutBinding(String),             // Mut binding: mut x (takes ownership, allows mutation)
}

// ============================================================================
// EXPRESSIONS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Expression<'ast> {
    Literal {
        value: Literal,
        location: SourceLocation,
    },
    Identifier {
        name: String,
        location: SourceLocation,
    },
    Binary {
        left: &'ast Expression<'ast>,
        op: BinaryOp,
        right: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Unary {
        op: UnaryOp,
        operand: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Call {
        function: &'ast Expression<'ast>,
        arguments: Vec<(Option<String>, &'ast Expression<'ast>)>, // (label, expr)
        location: SourceLocation,
    },
    MethodCall {
        object: &'ast Expression<'ast>,
        method: String,
        type_args: Option<Vec<Type>>, // Turbofish: Vec::<int>::new()
        arguments: Vec<(Option<String>, &'ast Expression<'ast>)>, // (label, expr)
        location: SourceLocation,
    },
    FieldAccess {
        object: &'ast Expression<'ast>,
        field: String,
        location: SourceLocation,
    },
    StructLiteral {
        name: String,
        fields: Vec<(String, &'ast Expression<'ast>)>,
        location: SourceLocation,
    },
    MapLiteral {
        pairs: Vec<(&'ast Expression<'ast>, &'ast Expression<'ast>)>, // {key: value, ...}
        location: SourceLocation,
    },
    Range {
        start: &'ast Expression<'ast>,
        end: &'ast Expression<'ast>,
        inclusive: bool,
        location: SourceLocation,
    },
    Closure {
        parameters: Vec<String>,
        body: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Cast {
        expr: &'ast Expression<'ast>,
        type_: Type,
        location: SourceLocation,
    },
    Index {
        object: &'ast Expression<'ast>,
        index: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Tuple {
        elements: Vec<&'ast Expression<'ast>>, // Tuple expression: (a, b, c)
        location: SourceLocation,
    },
    Array {
        elements: Vec<&'ast Expression<'ast>>, // Array expression: [a, b, c]
        location: SourceLocation,
    },
    MacroInvocation {
        name: String,
        args: Vec<&'ast Expression<'ast>>,
        delimiter: MacroDelimiter, // (), [], or {}
        is_repeat: bool,           // true for vec![x; n], false for vec![x, y]
        location: SourceLocation,
    },
    TryOp {
        expr: &'ast Expression<'ast>, // The ? operator
        location: SourceLocation,
    },
    Await {
        expr: &'ast Expression<'ast>, // The .await
        location: SourceLocation,
    },
    ChannelSend {
        channel: &'ast Expression<'ast>,
        value: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    ChannelRecv {
        channel: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Block {
        statements: Vec<&'ast Statement<'ast>>,
        is_unsafe: bool,
        location: SourceLocation,
    },
}

impl<'ast> Expression<'ast> {
    /// Get the source location of this expression (if available)
    pub fn location(&self) -> SourceLocation {
        match self {
            Expression::Literal { location, .. } => location.clone(),
            Expression::Identifier { location, .. } => location.clone(),
            Expression::Binary { location, .. } => location.clone(),
            Expression::Unary { location, .. } => location.clone(),
            Expression::Call { location, .. } => location.clone(),
            Expression::MethodCall { location, .. } => location.clone(),
            Expression::FieldAccess { location, .. } => location.clone(),
            Expression::StructLiteral { location, .. } => location.clone(),
            Expression::MapLiteral { location, .. } => location.clone(),
            Expression::Range { location, .. } => location.clone(),
            Expression::Closure { location, .. } => location.clone(),
            Expression::Cast { location, .. } => location.clone(),
            Expression::Index { location, .. } => location.clone(),
            Expression::Tuple { location, .. } => location.clone(),
            Expression::Array { location, .. } => location.clone(),
            Expression::MacroInvocation { location, .. } => location.clone(),
            Expression::TryOp { location, .. } => location.clone(),
            Expression::Await { location, .. } => location.clone(),
            Expression::ChannelSend { location, .. } => location.clone(),
            Expression::ChannelRecv { location, .. } => location.clone(),
            Expression::Block { location, .. } => location.clone(),
        }
    }
}

// Manual Hash implementation for Literal (needed because f64 doesn't implement Hash)
impl std::hash::Hash for Literal {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            Literal::Int(i) => {
                0u8.hash(state);
                i.hash(state);
            }
            Literal::IntSuffixed(i, suffix) => {
                5u8.hash(state);
                i.hash(state);
                suffix.hash(state);
            }
            Literal::Float(f) => {
                1u8.hash(state);
                // Hash the bit representation of the float
                f.to_bits().hash(state);
            }
            Literal::String(s) => {
                2u8.hash(state);
                s.hash(state);
            }
            Literal::Char(c) => {
                3u8.hash(state);
                c.hash(state);
            }
            Literal::Bool(b) => {
                4u8.hash(state);
                b.hash(state);
            }
        }
    }
}

// ============================================================================
// TRAITS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TraitDecl<'ast> {
    pub name: String,
    pub generics: Vec<String>,    // Generic parameters like <T, U>
    pub supertraits: Vec<String>, // Supertrait bounds: trait Manager: Employee
    pub associated_types: Vec<AssociatedType>, // Associated type declarations: type Item;
    pub methods: Vec<TraitMethod<'ast>>,
    pub doc_comment: Option<String>, // Documentation comment (/// lines)
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TraitMethod<'ast> {
    pub name: String,
    pub parameters: Vec<Parameter<'ast>>,
    pub return_type: Option<Type>,
    pub is_async: bool,
    pub body: Option<Vec<&'ast Statement<'ast>>>, // None for trait definitions, Some for default impls
    pub doc_comment: Option<String>,              // Documentation comment (/// lines)
}

// ============================================================================
// IMPL BLOCKS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImplBlock<'ast> {
    pub type_name: String,
    pub type_params: Vec<TypeParam>, // Generic type parameters with optional bounds: impl<T: Display> Box<T>
    pub where_clause: Vec<(String, Vec<String>)>, // Where clause: [(type_param, [trait_bounds])]
    pub trait_name: Option<String>, // None for inherent impl, Some for trait impl (without type args)
    pub trait_type_args: Option<Vec<Type>>, // Type arguments for generic trait impl: From<int> -> Some([Type::Int])
    pub associated_types: Vec<AssociatedType>, // Associated type implementations: type Item = i32;
    pub functions: Vec<FunctionDecl<'ast>>,
    pub decorators: Vec<Decorator<'ast>>,
    /// `extern impl` — signatures for FFI/linked code; same codegen shape as normal impl
    pub is_extern: bool,
}

// ============================================================================
// TOP-LEVEL ITEMS
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Item<'ast> {
    Function {
        decl: FunctionDecl<'ast>,
        location: SourceLocation,
    },
    Struct {
        decl: StructDecl<'ast>,
        location: SourceLocation,
    },
    Enum {
        decl: EnumDecl,
        location: SourceLocation,
    },
    Trait {
        decl: TraitDecl<'ast>,
        location: SourceLocation,
    },
    Impl {
        block: ImplBlock<'ast>,
        location: SourceLocation,
    },
    Const {
        name: String,
        is_pub: bool,
        type_: Type,
        value: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    Static {
        name: String,
        mutable: bool,
        type_: Type,
        value: &'ast Expression<'ast>,
        location: SourceLocation,
    },
    ExternLet {
        name: String,
        type_: Type,
        decorators: Vec<Decorator<'ast>>,
        is_pub: bool,
        location: SourceLocation,
    },
    Use {
        path: Vec<String>,
        alias: Option<String>,
        is_pub: bool, // THE WINDJAMMER WAY: Track pub use for re-exports
        location: SourceLocation,
    }, // use std::fs as fs -> path=["std", "fs"], alias=Some("fs")
    Mod {
        name: String,
        items: Vec<Item<'ast>>,
        is_public: bool,
        location: SourceLocation,
    }, // mod ffi { ... }
    BoundAlias {
        name: String,
        traits: Vec<String>,
        location: SourceLocation,
    }, // bound Printable = Display + Debug
    TypeAlias {
        name: String,
        target: Type,
        is_pub: bool,
        location: SourceLocation,
    }, // pub type QuestStatus = QuestState
}

impl<'ast> Item<'ast> {
    /// Get the source location of this item (if available)
    pub fn location(&self) -> SourceLocation {
        match self {
            Item::Function { location, .. } => location.clone(),
            Item::Struct { location, .. } => location.clone(),
            Item::Enum { location, .. } => location.clone(),
            Item::Trait { location, .. } => location.clone(),
            Item::Impl { location, .. } => location.clone(),
            Item::Const { location, .. } => location.clone(),
            Item::Static { location, .. } => location.clone(),
            Item::ExternLet { location, .. } => location.clone(),
            Item::Use { location, .. } => location.clone(),
            Item::Mod { location, .. } => location.clone(),
            Item::BoundAlias { location, .. } => location.clone(),
            Item::TypeAlias { location, .. } => location.clone(),
        }
    }
}

// ============================================================================
// PROGRAM
// ============================================================================

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Program<'ast> {
    pub items: Vec<Item<'ast>>,
}