ruchy 4.1.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//! WASM Compiler for Ruchy AST
//!
//! Compiles Ruchy AST to WebAssembly bytecode.

use crate::frontend::ast::{Expr, ExprKind, Literal};
use crate::wasm::component::ComponentConfig;
use anyhow::Result;
use wasm_encoder::{
    CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction, Module,
    TypeSection, ValType,
};

/// High-level WASM compiler for Ruchy AST
pub struct WasmCompiler {
    optimization_level: u8,
    config: ComponentConfig,
}

impl WasmCompiler {
    /// Create a new WASM compiler
    pub fn new() -> Self {
        Self {
            optimization_level: 0,
            config: ComponentConfig::default(),
        }
    }

    /// Set optimization level (0-3)
    pub fn set_optimization_level(&mut self, level: u8) {
        self.optimization_level = level.min(3);
    }

    /// Compile AST to WASM module
    pub fn compile(&self, ast: &Expr) -> Result<WasmModule> {
        let mut module = Module::new();
        let mut exports = vec![];

        // Type section - define function signatures
        let mut types = TypeSection::new();

        // Function section - declare functions
        let mut functions = FunctionSection::new();

        // Export section - export functions
        let mut export_section = ExportSection::new();

        // Code section - function bodies
        let mut code = CodeSection::new();

        // Process AST and generate WASM
        match &ast.kind {
            ExprKind::Function {
                name, params, body, ..
            } => {
                // Add function type
                let param_types: Vec<ValType> = params
                    .iter()
                    .map(|_| ValType::I32) // Simplification: all params are i32
                    .collect();
                let result_types = vec![ValType::I32]; // Simplification: returns i32

                types.function(param_types, result_types);
                functions.function(0); // Reference to type 0

                // Generate function body
                let mut func = Function::new(vec![]);
                Self::compile_expr(body, &mut func)?;
                if !self.has_return(body) {
                    func.instruction(&Instruction::I32Const(0));
                }
                func.instruction(&Instruction::End);
                code.function(&func);

                // Export the function
                export_section.export(name, ExportKind::Func, 0);
                exports.push(name.clone());
            }
            ExprKind::Block(exprs) => {
                // Process multiple top-level expressions
                for expr in exprs {
                    if let ExprKind::Function { name, .. } = &expr.kind {
                        exports.push(name.clone());
                    }
                }
            }
            _ => {
                // For other expressions, wrap in a main function
                types.function(vec![], vec![ValType::I32]);
                functions.function(0);

                let mut func = Function::new(vec![]);
                Self::compile_expr(ast, &mut func)?;
                func.instruction(&Instruction::End);
                code.function(&func);
            }
        }

        // Assemble the module
        if !types.is_empty() {
            module.section(&types);
        }
        if !functions.is_empty() {
            module.section(&functions);
        }
        if !export_section.is_empty() {
            module.section(&export_section);
        }
        if !code.is_empty() {
            module.section(&code);
        }

        let bytes = module.finish();

        Ok(WasmModule { bytes, exports })
    }

    /// Compile an expression to WASM instructions
    fn compile_expr(expr: &Expr, func: &mut Function) -> Result<()> {
        match &expr.kind {
            ExprKind::Literal(lit) => match lit {
                Literal::Integer(n, _) => {
                    func.instruction(&Instruction::I32Const(*n as i32));
                }
                Literal::Float(f) => {
                    func.instruction(&Instruction::F64Const(*f));
                }
                Literal::Bool(b) => {
                    func.instruction(&Instruction::I32Const(i32::from(*b)));
                }
                _ => {
                    // Other literals default to 0
                    func.instruction(&Instruction::I32Const(0));
                }
            },
            ExprKind::Binary { left, op, right } => {
                use crate::frontend::ast::BinaryOp;
                Self::compile_expr(left, func)?;
                Self::compile_expr(right, func)?;
                match op {
                    BinaryOp::Add => func.instruction(&Instruction::I32Add),
                    BinaryOp::Subtract => func.instruction(&Instruction::I32Sub),
                    BinaryOp::Multiply => func.instruction(&Instruction::I32Mul),
                    BinaryOp::Divide => func.instruction(&Instruction::I32DivS),
                    _ => func.instruction(&Instruction::I32Add), // Default
                };
            }
            _ => {
                // Default: push 0 on stack
                func.instruction(&Instruction::I32Const(0));
            }
        }
        Ok(())
    }

    /// Check if expression contains a return
    fn has_return(&self, expr: &Expr) -> bool {
        matches!(expr.kind, ExprKind::Return { .. })
    }
}

impl Default for WasmCompiler {
    fn default() -> Self {
        Self::new()
    }
}

/// A compiled WASM module
pub struct WasmModule {
    bytes: Vec<u8>,
    exports: Vec<String>,
}

impl WasmModule {
    /// Get the WASM bytecode
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Check if module has an export
    pub fn has_export(&self, name: &str) -> bool {
        self.exports.contains(&name.to_string())
    }

    /// Validate the module
    pub fn validate(&self) -> Result<()> {
        // Basic validation - check magic number
        if self.bytes.len() >= 4 && self.bytes[0..4] == [0x00, 0x61, 0x73, 0x6d] {
            Ok(())
        } else {
            anyhow::bail!("Invalid WASM module")
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::frontend::ast::{BinaryOp, Span};

    // Helper to create test expressions
    fn make_int(n: i64) -> Expr {
        Expr {
            kind: ExprKind::Literal(Literal::Integer(n, None)),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn make_float(f: f64) -> Expr {
        Expr {
            kind: ExprKind::Literal(Literal::Float(f)),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn make_bool(b: bool) -> Expr {
        Expr {
            kind: ExprKind::Literal(Literal::Bool(b)),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn make_binary(left: Expr, op: BinaryOp, right: Expr) -> Expr {
        Expr {
            kind: ExprKind::Binary {
                left: Box::new(left),
                op,
                right: Box::new(right),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    // =====================================================================
    // WasmCompiler Tests
    // =====================================================================

    #[test]
    fn test_compiler_new() {
        let compiler = WasmCompiler::new();
        assert_eq!(compiler.optimization_level, 0);
    }

    #[test]
    fn test_compiler_default() {
        let compiler = WasmCompiler::default();
        assert_eq!(compiler.optimization_level, 0);
    }

    #[test]
    fn test_set_optimization_level_valid() {
        let mut compiler = WasmCompiler::new();
        compiler.set_optimization_level(2);
        assert_eq!(compiler.optimization_level, 2);
    }

    #[test]
    fn test_set_optimization_level_clamps_high() {
        let mut compiler = WasmCompiler::new();
        compiler.set_optimization_level(10);
        assert_eq!(
            compiler.optimization_level, 3,
            "Should clamp to maximum of 3"
        );
    }

    #[test]
    fn test_set_optimization_level_zero() {
        let mut compiler = WasmCompiler::new();
        compiler.set_optimization_level(0);
        assert_eq!(compiler.optimization_level, 0);
    }

    #[test]
    fn test_compile_integer_literal() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile integer literal");
    }

    #[test]
    fn test_compile_float_literal() {
        let compiler = WasmCompiler::new();
        let ast = make_float(3.15);
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile float literal");
    }

    #[test]
    fn test_compile_bool_literal() {
        let compiler = WasmCompiler::new();
        let ast = make_bool(true);
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile bool literal");
    }

    #[test]
    fn test_compile_binary_add() {
        let compiler = WasmCompiler::new();
        let ast = make_binary(make_int(2), BinaryOp::Add, make_int(3));
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile addition");
    }

    #[test]
    fn test_compile_binary_subtract() {
        let compiler = WasmCompiler::new();
        let ast = make_binary(make_int(5), BinaryOp::Subtract, make_int(2));
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile subtraction");
    }

    #[test]
    fn test_compile_binary_multiply() {
        let compiler = WasmCompiler::new();
        let ast = make_binary(make_int(3), BinaryOp::Multiply, make_int(4));
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile multiplication");
    }

    #[test]
    fn test_compile_binary_divide() {
        let compiler = WasmCompiler::new();
        let ast = make_binary(make_int(10), BinaryOp::Divide, make_int(2));
        let result = compiler.compile(&ast);
        assert!(result.is_ok(), "Should compile division");
    }

    #[test]
    fn test_has_return_false() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        assert!(
            !compiler.has_return(&ast),
            "Integer literal should not have return"
        );
    }

    #[test]
    fn test_has_return_true() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Return {
                value: Some(Box::new(make_int(42))),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(
            compiler.has_return(&ast),
            "Return expression should have return"
        );
    }

    // =====================================================================
    // WasmModule Tests
    // =====================================================================

    #[test]
    fn test_module_bytes() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        let bytes = module.bytes();
        assert!(!bytes.is_empty(), "Module should have bytecode");
    }

    #[test]
    fn test_module_validate_valid() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        let result = module.validate();
        assert!(result.is_ok(), "Valid WASM module should pass validation");
    }

    #[test]
    fn test_module_validate_invalid() {
        let module = WasmModule {
            bytes: vec![0xFF, 0xFF, 0xFF, 0xFF],
            exports: vec![],
        };
        let result = module.validate();
        assert!(
            result.is_err(),
            "Invalid WASM module should fail validation"
        );
    }

    #[test]
    fn test_module_validate_empty() {
        let module = WasmModule {
            bytes: vec![],
            exports: vec![],
        };
        let result = module.validate();
        assert!(result.is_err(), "Empty module should fail validation");
    }

    #[test]
    fn test_module_has_magic_number() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        let bytes = module.bytes();
        assert!(bytes.len() >= 4, "Module should have at least 4 bytes");
        assert_eq!(
            &bytes[0..4],
            &[0x00, 0x61, 0x73, 0x6d],
            "Should have WASM magic number"
        );
    }

    #[test]
    fn test_module_has_export_false() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        assert!(
            !module.has_export("nonexistent"),
            "Should not have nonexistent export"
        );
    }

    // =====================================================================
    // Integration Tests
    // =====================================================================

    #[test]
    fn test_compile_nested_arithmetic() {
        let compiler = WasmCompiler::new();
        // (2 + 3) * 4
        let inner = make_binary(make_int(2), BinaryOp::Add, make_int(3));
        let outer = make_binary(inner, BinaryOp::Multiply, make_int(4));
        let result = compiler.compile(&outer);
        assert!(result.is_ok(), "Should compile nested arithmetic");
        let module = result.unwrap();
        assert!(
            module.validate().is_ok(),
            "Nested arithmetic should produce valid WASM"
        );
    }

    #[test]
    fn test_compile_different_optimization_levels() {
        let ast = make_binary(make_int(2), BinaryOp::Add, make_int(3));

        for level in 0..=3 {
            let mut compiler = WasmCompiler::new();
            compiler.set_optimization_level(level);
            let result = compiler.compile(&ast);
            assert!(
                result.is_ok(),
                "Should compile at optimization level {level}"
            );
        }
    }

    #[test]
    fn test_compile_preserves_bytecode() {
        let compiler = WasmCompiler::new();
        let ast = make_int(123);
        let module = compiler.compile(&ast).expect("Should compile");
        let bytes1 = module.bytes();
        let bytes2 = module.bytes();
        assert_eq!(
            bytes1, bytes2,
            "Multiple calls to bytes() should return same data"
        );
    }

    // =====================================================================
    // Property Tests (10,000+ cases for mathematical proof)
    // =====================================================================

    #[cfg(test)]
    mod property_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #![proptest_config(ProptestConfig::with_cases(10000))]

            /// Property 1: Compilation of integer literals never panics
            #[test]
            fn prop_compile_integer_never_panics(n in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_int(i64::from(n));
                let _ = compiler.compile(&ast);
            }

            /// Property 2: Compilation of float literals never panics
            #[test]
            fn prop_compile_float_never_panics(f in any::<f64>()) {
                let compiler = WasmCompiler::new();
                let ast = make_float(f);
                let _ = compiler.compile(&ast);
            }

            /// Property 3: All valid WASM modules have magic number
            #[test]
            fn prop_compiled_modules_have_magic_number(n in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_int(i64::from(n));
                if let Ok(module) = compiler.compile(&ast) {
                    let bytes = module.bytes();
                    prop_assert!(bytes.len() >= 4, "Module should have at least 4 bytes");
                    prop_assert_eq!(&bytes[0..4], &[0x00, 0x61, 0x73, 0x6d], "Should have WASM magic number");
                }
            }

            /// Property 4: Compilation is deterministic
            #[test]
            fn prop_compilation_is_deterministic(n in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_int(i64::from(n));
                if let Ok(module1) = compiler.compile(&ast) {
                    if let Ok(module2) = compiler.compile(&ast) {
                        prop_assert_eq!(module1.bytes(), module2.bytes(), "Same AST should produce same bytecode");
                    }
                }
            }

            /// Property 5: Optimization level always clamped to 0-3
            #[test]
            fn prop_optimization_level_clamped(level in any::<u8>()) {
                let mut compiler = WasmCompiler::new();
                compiler.set_optimization_level(level);
                prop_assert!(compiler.optimization_level <= 3, "Optimization level should be clamped to max 3");
            }

            /// Property 6: Valid modules always pass validation
            #[test]
            fn prop_valid_modules_pass_validation(n in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_int(i64::from(n));
                if let Ok(module) = compiler.compile(&ast) {
                    prop_assert!(module.validate().is_ok(), "Valid module should pass validation");
                }
            }

            /// Property 7: Binary addition is commutative in compilation
            #[test]
            fn prop_binary_add_compiles_consistently(a in any::<i32>(), b in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_binary(make_int(i64::from(a)), BinaryOp::Add, make_int(i64::from(b)));
                if let Ok(module) = compiler.compile(&ast) {
                    prop_assert!(module.validate().is_ok(), "Binary operation should produce valid WASM");
                }
            }

            /// Property 8: Multiple calls to bytes() return same data
            #[test]
            fn prop_bytes_call_idempotent(n in any::<i32>()) {
                let compiler = WasmCompiler::new();
                let ast = make_int(i64::from(n));
                if let Ok(module) = compiler.compile(&ast) {
                    let bytes1 = module.bytes();
                    let bytes2 = module.bytes();
                    let bytes3 = module.bytes();
                    prop_assert_eq!(bytes1, bytes2);
                    prop_assert_eq!(bytes2, bytes3);
                }
            }
        }
    }

    // ===== EXTREME TDD Round 154 - WASM Compiler Tests =====

    #[test]
    fn test_compile_bool_false() {
        let compiler = WasmCompiler::new();
        let ast = make_bool(false);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_string_literal() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Literal(Literal::String("hello".to_string())),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_char_literal() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Literal(Literal::Char('x')),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_unit_literal() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Literal(Literal::Unit),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_negative_integer() {
        let compiler = WasmCompiler::new();
        let ast = make_int(-42);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_large_integer() {
        let compiler = WasmCompiler::new();
        let ast = make_int(i32::MAX as i64);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_small_integer() {
        let compiler = WasmCompiler::new();
        let ast = make_int(i32::MIN as i64);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_negative_float() {
        let compiler = WasmCompiler::new();
        let ast = make_float(-3.14);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_zero_float() {
        let compiler = WasmCompiler::new();
        let ast = make_float(0.0);
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_block_empty() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Block(vec![]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_block_single_expr() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Block(vec![make_int(42)]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_identifier() {
        let compiler = WasmCompiler::new();
        let ast = Expr {
            kind: ExprKind::Identifier("x".to_string()),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_module_has_export_empty_name() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        assert!(!module.has_export(""));
    }

    #[test]
    fn test_set_optimization_level_all_valid() {
        for level in 0..=3 {
            let mut compiler = WasmCompiler::new();
            compiler.set_optimization_level(level);
            assert_eq!(compiler.optimization_level, level);
        }
    }

    #[test]
    fn test_compile_binary_default_op() {
        let compiler = WasmCompiler::new();
        // Use an operator that falls through to default
        let ast = make_binary(make_int(2), BinaryOp::Less, make_int(3));
        let result = compiler.compile(&ast);
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_deeply_nested_binary() {
        let compiler = WasmCompiler::new();
        // ((1 + 2) - 3) * 4 / 5
        let add = make_binary(make_int(1), BinaryOp::Add, make_int(2));
        let sub = make_binary(add, BinaryOp::Subtract, make_int(3));
        let mul = make_binary(sub, BinaryOp::Multiply, make_int(4));
        let div = make_binary(mul, BinaryOp::Divide, make_int(5));
        let result = compiler.compile(&div);
        assert!(result.is_ok());
    }

    #[test]
    fn test_module_bytecode_length() {
        let compiler = WasmCompiler::new();
        let ast = make_int(42);
        let module = compiler.compile(&ast).expect("Should compile");
        let bytes = module.bytes();
        // WASM module should have reasonable minimum size
        assert!(bytes.len() >= 8, "WASM module should have header + section");
    }
}