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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// AST Builders - Ergonomic construction of AST nodes
//
// Provides builder functions to make test code dramatically more concise:
//
// Before: Type::Reference(Box::new(Type::Vec(Box::new(Type::Int))))
// After:  type_ref(type_vec(Type::Int))
//
// Expected impact: 60-80% reduction in test code lines

use super::core::{Expression, Parameter, Pattern, Statement};
use super::literals::{Literal, MacroDelimiter};
use super::operators::{BinaryOp, CompoundOp, UnaryOp};
use super::ownership::OwnershipHint;
use super::types::Type;

// ============================================================================
// TYPE BUILDERS
// ============================================================================

/// Build primitive type: int
pub fn type_int() -> Type {
    Type::Int
}

/// Build primitive type: float
pub fn type_float() -> Type {
    Type::Float
}

/// Build primitive type: bool
pub fn type_bool() -> Type {
    Type::Bool
}

/// Build primitive type: string
pub fn type_string() -> Type {
    Type::String
}

/// Build custom type: MyType
pub fn type_custom(name: impl Into<String>) -> Type {
    Type::Custom(name.into())
}

/// Build reference type: &T
pub fn type_ref(inner: Type) -> Type {
    Type::Reference(Box::new(inner))
}

/// Build mutable reference type: &mut T
pub fn type_mut_ref(inner: Type) -> Type {
    Type::MutableReference(Box::new(inner))
}

/// Build Vec type: Vec<T>
pub fn type_vec(inner: Type) -> Type {
    Type::Vec(Box::new(inner))
}

/// Build Option type: Option<T>
pub fn type_option(inner: Type) -> Type {
    Type::Option(Box::new(inner))
}

/// Build Result type: Result<T, E>
pub fn type_result(ok: Type, err: Type) -> Type {
    Type::Result(Box::new(ok), Box::new(err))
}

/// Build parameterized type: HashMap<K, V>
pub fn type_parameterized(name: impl Into<String>, args: Vec<Type>) -> Type {
    Type::Parameterized(name.into(), args)
}

/// Build tuple type: (T1, T2, ...)
pub fn type_tuple(elements: Vec<Type>) -> Type {
    Type::Tuple(elements)
}

/// Build array type: [T; N]
pub fn type_array(inner: Type, size: usize) -> Type {
    Type::Array(Box::new(inner), size)
}

/// Build generic type parameter: T
pub fn type_generic(name: impl Into<String>) -> Type {
    Type::Generic(name.into())
}

/// Build associated type: Self::Item
pub fn type_associated(base: impl Into<String>, assoc: impl Into<String>) -> Type {
    Type::Associated(base.into(), assoc.into())
}

/// Build trait object: dyn Trait
pub fn type_trait_object(name: impl Into<String>) -> Type {
    Type::TraitObject(name.into())
}

/// Build type inference placeholder: _
pub fn type_infer() -> Type {
    Type::Infer
}

/// Build int32 type
pub fn type_int32() -> Type {
    Type::Int32
}

/// Build uint type
pub fn type_uint() -> Type {
    Type::Uint
}

// ============================================================================
// PARAMETER BUILDERS
// ============================================================================

/// Build simple parameter with inferred ownership
pub fn param<'ast>(name: impl Into<String>, type_: Type) -> Parameter<'ast> {
    Parameter {
        name: name.into(),
        pattern: None,
        type_,
        ownership: OwnershipHint::Inferred,
        is_mutable: false,
        decorators: Vec::new(),
    }
}

/// Build reference parameter
pub fn param_ref<'ast>(name: impl Into<String>, type_: Type) -> Parameter<'ast> {
    Parameter {
        name: name.into(),
        pattern: None,
        type_,
        ownership: OwnershipHint::Ref,
        is_mutable: false,
        decorators: Vec::new(),
    }
}

/// Build mutable reference parameter
pub fn param_mut<'ast>(name: impl Into<String>, type_: Type) -> Parameter<'ast> {
    Parameter {
        name: name.into(),
        pattern: None,
        type_,
        ownership: OwnershipHint::Mut,
        is_mutable: true,
        decorators: Vec::new(),
    }
}

/// Build owned parameter
pub fn param_owned<'ast>(name: impl Into<String>, type_: Type) -> Parameter<'ast> {
    Parameter {
        name: name.into(),
        pattern: None,
        type_,
        ownership: OwnershipHint::Owned,
        is_mutable: false,
        decorators: Vec::new(),
    }
}

// ============================================================================
// EXPRESSION BUILDERS
// ============================================================================

/// Build integer literal expression
pub fn expr_int<'ast>(value: i64) -> Expression<'ast> {
    Expression::Literal {
        value: Literal::Int(value),
        location: None,
    }
}

/// Build float literal expression
pub fn expr_float<'ast>(value: f64) -> Expression<'ast> {
    Expression::Literal {
        value: Literal::Float(value),
        location: None,
    }
}

/// Build string literal expression
pub fn expr_string<'ast>(value: impl Into<String>) -> Expression<'ast> {
    Expression::Literal {
        value: Literal::String(value.into()),
        location: None,
    }
}

/// Build boolean literal expression
pub fn expr_bool<'ast>(value: bool) -> Expression<'ast> {
    Expression::Literal {
        value: Literal::Bool(value),
        location: None,
    }
}

/// Build character literal expression
pub fn expr_char<'ast>(value: char) -> Expression<'ast> {
    Expression::Literal {
        value: Literal::Char(value),
        location: None,
    }
}

/// Build variable/identifier expression
pub fn expr_var<'ast>(name: impl Into<String>) -> Expression<'ast> {
    Expression::Identifier {
        name: name.into(),
        location: None,
    }
}

/// Build binary operation expression
pub fn expr_binary<'ast>(
    op: BinaryOp,
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    Expression::Binary {
        left,
        op,
        right,
        location: None,
    }
}

/// Build addition expression (convenience)
pub fn expr_add<'ast>(
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    expr_binary(BinaryOp::Add, left, right)
}

/// Build subtraction expression (convenience)
pub fn expr_sub<'ast>(
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    expr_binary(BinaryOp::Sub, left, right)
}

/// Build multiplication expression (convenience)
pub fn expr_mul<'ast>(
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    expr_binary(BinaryOp::Mul, left, right)
}

/// Build division expression (convenience)
pub fn expr_div<'ast>(
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    expr_binary(BinaryOp::Div, left, right)
}

/// Build equality expression (convenience)
pub fn expr_eq<'ast>(
    left: &'ast Expression<'ast>,
    right: &'ast Expression<'ast>,
) -> Expression<'ast> {
    expr_binary(BinaryOp::Eq, left, right)
}

/// Build unary operation expression
pub fn expr_unary<'ast>(op: UnaryOp, operand: &'ast Expression<'ast>) -> Expression<'ast> {
    Expression::Unary {
        op,
        operand,
        location: None,
    }
}

/// Build negation expression (convenience)
pub fn expr_neg<'ast>(operand: &'ast Expression<'ast>) -> Expression<'ast> {
    expr_unary(UnaryOp::Neg, operand)
}

/// Build logical NOT expression (convenience)
pub fn expr_not<'ast>(operand: &'ast Expression<'ast>) -> Expression<'ast> {
    expr_unary(UnaryOp::Not, operand)
}

/// Build function call expression
/// Note: Caller must provide function expression allocated in arena
pub fn expr_call<'ast>(
    function: &'ast Expression<'ast>,
    arguments: Vec<(Option<String>, &'ast Expression<'ast>)>,
) -> Expression<'ast> {
    Expression::Call {
        function,
        arguments,
        location: None,
    }
}

/// Build method call expression
pub fn expr_method<'ast>(
    object: &'ast Expression<'ast>,
    method: impl Into<String>,
    arguments: Vec<(Option<String>, &'ast Expression<'ast>)>,
) -> Expression<'ast> {
    Expression::MethodCall {
        object,
        method: method.into(),
        type_args: None,
        arguments,
        location: None,
    }
}

/// Build macro invocation expression
pub fn expr_macro<'ast>(
    name: impl Into<String>,
    args: Vec<&'ast Expression<'ast>>,
) -> Expression<'ast> {
    Expression::MacroInvocation {
        name: name.into(),
        args,
        delimiter: MacroDelimiter::Parens,
        is_repeat: false, // Builder creates regular macro calls
        location: None,
    }
}

/// Build field access expression
pub fn expr_field<'ast>(
    object: &'ast Expression<'ast>,
    field: impl Into<String>,
) -> Expression<'ast> {
    Expression::FieldAccess {
        object,
        field: field.into(),
        location: None,
    }
}

/// Build array index expression
pub fn expr_index<'ast>(
    array: &'ast Expression<'ast>,
    index: &'ast Expression<'ast>,
) -> Expression<'ast> {
    Expression::Index {
        object: array,
        index,
        location: None,
    }
}

/// Build array literal expression
pub fn expr_array<'ast>(elements: Vec<&'ast Expression<'ast>>) -> Expression<'ast> {
    Expression::Array {
        elements,
        location: None,
    }
}

/// Build tuple expression
pub fn expr_tuple<'ast>(elements: Vec<&'ast Expression<'ast>>) -> Expression<'ast> {
    Expression::Tuple {
        elements,
        location: None,
    }
}

/// Build block expression
pub fn expr_block<'ast>(statements: Vec<&'ast Statement<'ast>>) -> Expression<'ast> {
    Expression::Block {
        statements,
        is_unsafe: false,
        location: None,
    }
}

// ============================================================================
// STATEMENT BUILDERS
// ============================================================================

/// Build let statement (immutable)
pub fn stmt_let<'ast>(
    name: impl Into<String>,
    type_: Option<Type>,
    value: &'ast Expression<'ast>,
) -> Statement<'ast> {
    Statement::Let {
        pattern: Pattern::Identifier(name.into()),
        mutable: false,
        type_,
        value,
        else_block: None,
        location: None,
    }
}

/// Build let mut statement (mutable)
pub fn stmt_let_mut<'ast>(
    name: impl Into<String>,
    type_: Option<Type>,
    value: &'ast Expression<'ast>,
) -> Statement<'ast> {
    Statement::Let {
        pattern: Pattern::Identifier(name.into()),
        mutable: true,
        type_,
        value,
        else_block: None,
        location: None,
    }
}

/// Build assignment statement
pub fn stmt_assign<'ast>(
    target: &'ast Expression<'ast>,
    value: &'ast Expression<'ast>,
) -> Statement<'ast> {
    Statement::Assignment {
        target,
        value,
        compound_op: None,
        location: None,
    }
}

/// Build compound assignment statement (+=, -=, etc.)
pub fn stmt_compound_assign<'ast>(
    op: CompoundOp,
    target: &'ast Expression<'ast>,
    value: &'ast Expression<'ast>,
) -> Statement<'ast> {
    Statement::Assignment {
        target,
        value,
        compound_op: Some(op),
        location: None,
    }
}

/// Build return statement
pub fn stmt_return<'ast>(value: Option<&'ast Expression<'ast>>) -> Statement<'ast> {
    Statement::Return {
        value,
        location: None,
    }
}

/// Build expression statement
pub fn stmt_expr<'ast>(expr: &'ast Expression<'ast>) -> Statement<'ast> {
    Statement::Expression {
        expr,
        location: None,
    }
}

/// Build if statement (no else)
pub fn stmt_if<'ast>(
    condition: &'ast Expression<'ast>,
    then_block: Vec<&'ast Statement<'ast>>,
    else_block: Option<Vec<&'ast Statement<'ast>>>,
) -> Statement<'ast> {
    Statement::If {
        condition,
        then_block,
        else_block,
        location: None,
    }
}

/// Build if-else statement (convenience)
pub fn stmt_if_else<'ast>(
    condition: &'ast Expression<'ast>,
    then_block: Vec<&'ast Statement<'ast>>,
    else_block: Vec<&'ast Statement<'ast>>,
) -> Statement<'ast> {
    Statement::If {
        condition,
        then_block,
        else_block: Some(else_block),
        location: None,
    }
}

/// Build while loop statement
pub fn stmt_while<'ast>(
    condition: &'ast Expression<'ast>,
    body: Vec<&'ast Statement<'ast>>,
) -> Statement<'ast> {
    Statement::While {
        condition,
        body,
        location: None,
    }
}

/// Build infinite loop statement
pub fn stmt_loop<'ast>(body: Vec<&'ast Statement<'ast>>) -> Statement<'ast> {
    Statement::Loop {
        body,
        location: None,
    }
}

/// Build break statement
pub fn stmt_break<'ast>() -> Statement<'ast> {
    Statement::Break { location: None }
}

/// Build continue statement
pub fn stmt_continue<'ast>() -> Statement<'ast> {
    Statement::Continue { location: None }
}

/// Build for loop statement
pub fn stmt_for<'ast>(
    pattern: Pattern<'ast>,
    iterable: &'ast Expression<'ast>,
    body: Vec<&'ast Statement<'ast>>,
) -> Statement<'ast> {
    Statement::For {
        pattern,
        iterable,
        body,
        location: None,
    }
}

/// Build match statement
pub fn stmt_match<'ast>(
    value: &'ast Expression<'ast>,
    arms: Vec<super::core::MatchArm<'ast>>,
) -> Statement<'ast> {
    Statement::Match {
        value,
        arms,
        location: None,
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_type_primitives() {
        assert_eq!(type_int(), Type::Int);
        assert_eq!(type_float(), Type::Float);
        assert_eq!(type_bool(), Type::Bool);
        assert_eq!(type_string(), Type::String);
    }

    #[test]
    fn test_type_custom() {
        assert_eq!(type_custom("MyType"), Type::Custom("MyType".to_string()));
    }

    #[test]
    fn test_type_reference() {
        assert_eq!(type_ref(Type::Int), Type::Reference(Box::new(Type::Int)));
    }

    #[test]
    fn test_type_mut_reference() {
        assert_eq!(
            type_mut_ref(Type::Int),
            Type::MutableReference(Box::new(Type::Int))
        );
    }

    #[test]
    fn test_type_vec() {
        assert_eq!(type_vec(Type::Int), Type::Vec(Box::new(Type::Int)));
    }

    #[test]
    fn test_type_option() {
        assert_eq!(
            type_option(Type::String),
            Type::Option(Box::new(Type::String))
        );
    }

    #[test]
    fn test_type_result() {
        assert_eq!(
            type_result(Type::Int, Type::String),
            Type::Result(Box::new(Type::Int), Box::new(Type::String))
        );
    }

    #[test]
    fn test_type_parameterized() {
        assert_eq!(
            type_parameterized("Vec", vec![Type::Int]),
            Type::Parameterized("Vec".to_string(), vec![Type::Int])
        );
    }

    #[test]
    fn test_type_tuple() {
        assert_eq!(
            type_tuple(vec![Type::Int, Type::String]),
            Type::Tuple(vec![Type::Int, Type::String])
        );
    }

    #[test]
    fn test_type_chained() {
        // Complex: Option<&mut Vec<String>>
        let complex = type_option(type_mut_ref(type_vec(Type::String)));

        assert_eq!(
            complex,
            Type::Option(Box::new(Type::MutableReference(Box::new(Type::Vec(
                Box::new(Type::String)
            )))))
        );
    }

    #[test]
    fn test_param_simple() {
        let p = param("x", Type::Int);

        assert_eq!(p.name, "x");
        assert_eq!(p.type_, Type::Int);
        assert_eq!(p.ownership, OwnershipHint::Inferred);
        assert!(!p.is_mutable);
    }

    #[test]
    fn test_param_ref() {
        let p = param_ref("x", Type::Int);

        assert_eq!(p.ownership, OwnershipHint::Ref);
        assert!(!p.is_mutable);
    }

    #[test]
    fn test_param_mut() {
        let p = param_mut("x", Type::Int);

        assert_eq!(p.ownership, OwnershipHint::Mut);
        assert!(p.is_mutable);
    }

    #[test]
    fn test_param_owned() {
        let p = param_owned("data", Type::String);

        assert_eq!(p.ownership, OwnershipHint::Owned);
        assert!(!p.is_mutable);
    }

    #[test]
    fn test_ergonomic_example() {
        // Before builders (7 lines):
        // Parameter {
        //     name: "data".to_string(),
        //     pattern: None,
        //     type_: Type::Reference(Box::new(Type::Vec(Box::new(Type::Int)))),
        //     ownership: OwnershipHint::Ref,
        //     is_mutable: false,
        // }

        // After builders (1 line):
        let param = param_ref("data", type_vec(Type::Int));

        assert_eq!(param.name, "data");
        assert_eq!(param.type_, Type::Vec(Box::new(Type::Int)));
        assert_eq!(param.ownership, OwnershipHint::Ref);
    }
}