mux-lang 0.3.2

The Mux Programming Language Compiler
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//! Binary and logical operator generation for the code generator.
//!
//! This module handles:
//! - Short-circuit logical operators (&&, ||)
//! - Binary arithmetic operators (+, -, *, /, %, **)
//! - Comparison operators (==, !=, <, >, <=, >=)
//! - The 'in' operator for containment checks

use inkwell::values::{BasicValueEnum, IntValue, PointerValue};

use crate::ast::{BinaryOp, ExpressionKind, ExpressionNode, LiteralNode, PrimitiveType};
use crate::semantics::Type;

use super::CodeGenerator;

impl<'a> CodeGenerator<'a> {
    fn infer_method_return_type(&self, receiver_type: &Type, method_name: &str) -> Option<Type> {
        self.analyzer
            .get_method_sig(receiver_type, method_name)
            .map(|sig| sig.return_type)
    }

    /// Ensure a value is a pointer, boxing it if necessary.
    fn ensure_pointer(&mut self, val: BasicValueEnum<'a>) -> PointerValue<'a> {
        if val.is_pointer_value() {
            val.into_pointer_value()
        } else {
            self.box_value(val)
        }
    }

    /// Generate a numeric comparison (int or float) with the given predicates.
    fn generate_numeric_compare(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
        int_pred: inkwell::IntPredicate,
        float_pred: inkwell::FloatPredicate,
        label: &str,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            self.builder
                .build_int_compare(int_pred, left_int, right_int, label)
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else if let (Ok(left_float), Ok(right_float)) = (
            self.get_raw_float_value(left),
            self.get_raw_float_value(right),
        ) {
            let flabel = format!("f{}", label);
            self.builder
                .build_float_compare(float_pred, left_float, right_float, &flabel)
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else {
            Err(format!("Unsupported {} operands", label))
        }
    }

    /// Call a runtime comparison function on two pointer values and convert
    /// the i32 result to an i1 bool.
    fn call_comparison_runtime(
        &mut self,
        left: PointerValue<'a>,
        right: PointerValue<'a>,
        func_name: &str,
        label: &str,
    ) -> Result<BasicValueEnum<'a>, String> {
        let func = self
            .runtime_function(func_name)
            .ok_or_else(|| format!("{} not found", func_name))?;
        let result = self
            .builder
            .build_call(func, &[left.into(), right.into()], label)
            .map_err(|e| e.to_string())?
            .try_as_basic_value()
            .left()
            .ok_or("Call returned no value")?;

        let result_i32 = result.into_int_value();
        self.i32_to_bool(result_i32)
    }

    fn i32_to_bool(&self, int_val: IntValue<'a>) -> Result<BasicValueEnum<'a>, String> {
        let zero = self.context.i32_type().const_zero();
        self.builder
            .build_int_compare(inkwell::IntPredicate::NE, int_val, zero, "to_bool")
            .map_err(|e| e.to_string())
            .map(|v| v.into())
    }

    /// Resolve an expression type during codegen.
    /// Falls back to codegen variable tables when semantic scopes are no longer available.
    pub(super) fn resolve_expression_type_with_fallback(
        &mut self,
        expr: &ExpressionNode,
    ) -> Result<Type, String> {
        match &expr.kind {
            ExpressionKind::Identifier(_) => self.resolve_identifier_type(expr),
            ExpressionKind::ListAccess { .. } => self.resolve_list_access_type(expr),
            ExpressionKind::Call { .. } => self.resolve_call_type(expr),
            ExpressionKind::Binary { .. } => self.resolve_binary_type(expr),
            _ => self
                .analyzer
                .get_expression_type(expr)
                .map_err(|e| e.to_string()),
        }
    }

    fn resolve_identifier_type(&mut self, expr: &ExpressionNode) -> Result<Type, String> {
        let name = match &expr.kind {
            ExpressionKind::Identifier(name) => name,
            _ => return Err("Expected identifier expression".to_string()),
        };
        if let Some((_, _, ty)) = self
            .variables
            .get(name)
            .or_else(|| self.global_variables.get(name))
        {
            return Ok(ty.clone());
        }

        if let Ok(ty) = self.analyzer.get_expression_type(expr) {
            return Ok(ty);
        }

        if let Some(func_node) = self.function_nodes.get(name) {
            let mut param_types = Vec::with_capacity(func_node.params.len());
            for param in &func_node.params {
                let param_type = self
                    .analyzer
                    .resolve_type(&param.type_)
                    .map_err(|e| e.to_string())?;
                param_types.push(param_type);
            }
            let return_type = self
                .analyzer
                .resolve_type(&func_node.return_type)
                .map_err(|e| e.to_string())?;
            return Ok(Type::Function {
                params: param_types,
                returns: Box::new(return_type),
                default_count: 0,
            });
        }

        if let Some(symbol) = self.analyzer.symbol_table().lookup(name)
            && let Some(ty) = &symbol.type_
        {
            return Ok(ty.clone());
        }

        Err(format!("Undefined variable '{}'", name))
    }

    fn resolve_list_access_type(&mut self, expr: &ExpressionNode) -> Result<Type, String> {
        match &expr.kind {
            ExpressionKind::ListAccess {
                expr: container,
                index,
            } => {
                let container_type = self.resolve_expression_type_with_fallback(container)?;
                match container_type {
                    Type::List(inner) => Ok(*inner),
                    Type::Map(_, value) => Ok(*value),
                    Type::Tuple(left, right) => match &index.kind {
                        ExpressionKind::Literal(LiteralNode::Integer(0)) => Ok(*left),
                        ExpressionKind::Literal(LiteralNode::Integer(1)) => Ok(*right),
                        _ => Err("Tuple index must be a literal 0 or 1".to_string()),
                    },
                    _ => Err(format!("Cannot index into type: {:?}", container_type)),
                }
            }
            _ => Err("Expected list access expression".to_string()),
        }
    }

    fn resolve_call_type(&mut self, expr: &ExpressionNode) -> Result<Type, String> {
        match &expr.kind {
            ExpressionKind::Call { func, .. } => {
                if let Ok(ty) = self.analyzer.get_expression_type(expr) {
                    return Ok(ty);
                }

                if let ExpressionKind::FieldAccess {
                    expr: receiver_expr,
                    field,
                } = &func.kind
                {
                    let receiver_type =
                        self.resolve_expression_type_with_fallback(receiver_expr)?;
                    if let Some(return_type) = self.infer_method_return_type(&receiver_type, field)
                    {
                        return Ok(return_type);
                    }
                }

                let func_type = self.resolve_expression_type_with_fallback(func)?;
                match func_type {
                    Type::Function { returns, .. } => Ok(*returns),
                    other => Err(format!("Cannot call non-function type: {:?}", other)),
                }
            }
            _ => Err("Expected call expression".to_string()),
        }
    }

    fn resolve_binary_type(&mut self, expr: &ExpressionNode) -> Result<Type, String> {
        match &expr.kind {
            ExpressionKind::Binary {
                left, op, right, ..
            } => {
                let left_type = self.resolve_expression_type_with_fallback(left)?;
                let right_type = self.resolve_expression_type_with_fallback(right)?;
                match op {
                    BinaryOp::Add
                    | BinaryOp::Subtract
                    | BinaryOp::Multiply
                    | BinaryOp::Divide
                    | BinaryOp::Modulo
                    | BinaryOp::Exponent => {
                        if left_type == Type::Primitive(PrimitiveType::Float)
                            || right_type == Type::Primitive(PrimitiveType::Float)
                        {
                            Ok(Type::Primitive(PrimitiveType::Float))
                        } else {
                            Ok(left_type)
                        }
                    }
                    BinaryOp::Less
                    | BinaryOp::Greater
                    | BinaryOp::LessEqual
                    | BinaryOp::GreaterEqual
                    | BinaryOp::Equal
                    | BinaryOp::NotEqual
                    | BinaryOp::LogicalAnd
                    | BinaryOp::LogicalOr
                    | BinaryOp::In => Ok(Type::Primitive(PrimitiveType::Bool)),
                    BinaryOp::Assign
                    | BinaryOp::AddAssign
                    | BinaryOp::SubtractAssign
                    | BinaryOp::MultiplyAssign
                    | BinaryOp::DivideAssign
                    | BinaryOp::ModuloAssign => Ok(left_type),
                }
            }
            _ => Err("Expected binary expression".to_string()),
        }
    }

    pub(super) fn generate_short_circuit_logical_op(
        &mut self,
        left_expr: &ExpressionNode,
        op: &BinaryOp,
        right_expr: &ExpressionNode,
    ) -> Result<BasicValueEnum<'a>, String> {
        // Get the current function from the current basic block
        let current_bb = self
            .builder
            .get_insert_block()
            .ok_or("No current basic block for short-circuit logical operation")?;
        let current_fn = current_bb
            .get_parent()
            .ok_or("No current function for short-circuit logical operation")?;

        match op {
            BinaryOp::LogicalAnd => {
                // Create basic blocks for control flow
                let eval_right_bb = self
                    .context
                    .append_basic_block(current_fn, "and_eval_right");
                let merge_bb = self.context.append_basic_block(current_fn, "and_merge");

                // Evaluate left operand
                let left_val = self.generate_expression(left_expr)?;
                let left_bool = self.get_raw_bool_value(left_val)?;
                let left_bb = self
                    .builder
                    .get_insert_block()
                    .ok_or("No insert block after evaluating left operand")?;

                // If left is false, skip to merge with false result
                // If left is true, evaluate right operand
                self.builder
                    .build_conditional_branch(left_bool, eval_right_bb, merge_bb)
                    .map_err(|e| e.to_string())?;

                // eval_right_bb: evaluate right operand
                self.builder.position_at_end(eval_right_bb);
                let right_val = self.generate_expression(right_expr)?;
                let right_bool = self.get_raw_bool_value(right_val)?;
                let right_bb = self
                    .builder
                    .get_insert_block()
                    .ok_or("No insert block after evaluating right operand")?;
                self.builder
                    .build_unconditional_branch(merge_bb)
                    .map_err(|e| e.to_string())?;

                // merge_bb: phi node combines results
                self.builder.position_at_end(merge_bb);
                let phi = self
                    .builder
                    .build_phi(self.context.bool_type(), "and_result")
                    .map_err(|e| e.to_string())?;

                let false_val = self.context.bool_type().const_zero();
                phi.add_incoming(&[
                    (&false_val, left_bb),   // Left was false, return false
                    (&right_bool, right_bb), // Left was true, return right
                ]);

                Ok(phi.as_basic_value())
            }
            BinaryOp::LogicalOr => {
                // Create basic blocks for control flow
                let eval_right_bb = self.context.append_basic_block(current_fn, "or_eval_right");
                let merge_bb = self.context.append_basic_block(current_fn, "or_merge");

                // Evaluate left operand
                let left_val = self.generate_expression(left_expr)?;
                let left_bool = self.get_raw_bool_value(left_val)?;
                let left_bb = self
                    .builder
                    .get_insert_block()
                    .ok_or("No insert block after evaluating left operand")?;

                // If left is true, skip to merge with true result
                // If left is false, evaluate right operand
                self.builder
                    .build_conditional_branch(left_bool, merge_bb, eval_right_bb)
                    .map_err(|e| e.to_string())?;

                // eval_right_bb: evaluate right operand
                self.builder.position_at_end(eval_right_bb);
                let right_val = self.generate_expression(right_expr)?;
                let right_bool = self.get_raw_bool_value(right_val)?;
                let right_bb = self
                    .builder
                    .get_insert_block()
                    .ok_or("No insert block after evaluating right operand")?;
                self.builder
                    .build_unconditional_branch(merge_bb)
                    .map_err(|e| e.to_string())?;

                // merge_bb: phi node combines results
                self.builder.position_at_end(merge_bb);
                let phi = self
                    .builder
                    .build_phi(self.context.bool_type(), "or_result")
                    .map_err(|e| e.to_string())?;

                let true_val = self.context.bool_type().const_int(1, false);
                phi.add_incoming(&[
                    (&true_val, left_bb),    // Left was true, return true
                    (&right_bool, right_bb), // Left was false, return right
                ]);

                Ok(phi.as_basic_value())
            }
            _ => Err(
                "generate_short_circuit_logical_op called with non-logical operator".to_string(),
            ),
        }
    }

    fn generate_add_op(
        &mut self,
        left_expr: &ExpressionNode,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        let left_type = self
            .resolve_expression_type_with_fallback(left_expr)
            .map_err(|e| format!("Failed to get left operand type for '+': {}", e))?;

        match &left_type {
            Type::Primitive(PrimitiveType::Str) => {
                let left_ptr = self.ensure_pointer(left);
                let right_ptr = self.ensure_pointer(right);
                let left_cstr = self.extract_c_string_from_value(left_ptr)?;
                let right_cstr = self.extract_c_string_from_value(right_ptr)?;

                let concat_fn = self
                    .runtime_function("mux_string_concat")
                    .ok_or("mux_string_concat not found")?;
                let result = self
                    .builder
                    .build_call(
                        concat_fn,
                        &[left_cstr.into(), right_cstr.into()],
                        "string_concat",
                    )
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?
                    .into_pointer_value();

                self.box_string_value(result)
            }
            Type::List(_) => {
                let left_list = self.extract_list_from_value(left.into_pointer_value())?;
                let right_list = self.extract_list_from_value(right.into_pointer_value())?;

                let concat_fn = self
                    .runtime_function("mux_list_concat")
                    .ok_or("mux_list_concat not found")?;
                let result_list = self
                    .builder
                    .build_call(
                        concat_fn,
                        &[left_list.into(), right_list.into()],
                        "list_concat",
                    )
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?
                    .into_pointer_value();

                let free_list_fn = self
                    .runtime_function("mux_free_list")
                    .ok_or("mux_free_list not found")?;
                self.builder
                    .build_call(free_list_fn, &[left_list.into()], "free_list")
                    .map_err(|e| e.to_string())?;
                self.builder
                    .build_call(free_list_fn, &[right_list.into()], "free_list")
                    .map_err(|e| e.to_string())?;

                let list_value_fn = self
                    .runtime_function("mux_list_value")
                    .ok_or("mux_list_value not found")?;
                let result = self
                    .builder
                    .build_call(list_value_fn, &[result_list.into()], "list_value")
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?;

                Ok(result)
            }
            Type::Map(_, _) => {
                let left_map = self.extract_map_from_value(left.into_pointer_value())?;
                let right_map = self.extract_map_from_value(right.into_pointer_value())?;

                let merge_fn = self
                    .runtime_function("mux_map_merge")
                    .ok_or("mux_map_merge not found")?;
                let result_map = self
                    .builder
                    .build_call(merge_fn, &[left_map.into(), right_map.into()], "map_merge")
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?
                    .into_pointer_value();

                let free_map_fn = self
                    .runtime_function("mux_free_map")
                    .ok_or("mux_free_map not found")?;
                self.builder
                    .build_call(free_map_fn, &[left_map.into()], "free_map")
                    .map_err(|e| e.to_string())?;
                self.builder
                    .build_call(free_map_fn, &[right_map.into()], "free_map")
                    .map_err(|e| e.to_string())?;

                let map_value_fn = self
                    .runtime_function("mux_map_value")
                    .ok_or("mux_map_value not found")?;
                let result = self
                    .builder
                    .build_call(map_value_fn, &[result_map.into()], "map_value")
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?;

                Ok(result)
            }
            Type::Set(_) => {
                let left_set = self.extract_set_from_value(left.into_pointer_value())?;
                let right_set = self.extract_set_from_value(right.into_pointer_value())?;

                let union_fn = self
                    .runtime_function("mux_set_union")
                    .ok_or("mux_set_union not found")?;
                let result_set = self
                    .builder
                    .build_call(union_fn, &[left_set.into(), right_set.into()], "set_union")
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?
                    .into_pointer_value();

                let free_set_fn = self
                    .runtime_function("mux_free_set")
                    .ok_or("mux_free_set not found")?;
                self.builder
                    .build_call(free_set_fn, &[left_set.into()], "free_set")
                    .map_err(|e| e.to_string())?;
                self.builder
                    .build_call(free_set_fn, &[right_set.into()], "free_set")
                    .map_err(|e| e.to_string())?;

                let set_value_fn = self
                    .runtime_function("mux_set_value")
                    .ok_or("mux_set_value not found")?;
                let result = self
                    .builder
                    .build_call(set_value_fn, &[result_set.into()], "set_value")
                    .map_err(|e| e.to_string())?
                    .try_as_basic_value()
                    .left()
                    .ok_or("Call returned no value")?;

                Ok(result)
            }
            Type::Primitive(PrimitiveType::Int) => {
                let left_int = self.get_raw_int_value(left)?;
                let right_int = self.get_raw_int_value(right)?;
                self.builder
                    .build_int_add(left_int, right_int, "add")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::Primitive(PrimitiveType::Float) => {
                let left_float = self.get_raw_float_value(left)?;
                let right_float = self.get_raw_float_value(right)?;
                self.builder
                    .build_float_add(left_float, right_float, "fadd")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            _ => Err(format!(
                "Add operation not supported for type: {:?}",
                left_type
            )),
        }
    }

    fn generate_subtract_op(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            self.builder
                .build_int_sub(left_int, right_int, "sub")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else if let (Ok(left_float), Ok(right_float)) = (
            self.get_raw_float_value(left),
            self.get_raw_float_value(right),
        ) {
            self.builder
                .build_float_sub(left_float, right_float, "fsub")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else {
            Err("Unsupported sub operands".to_string())
        }
    }

    fn generate_multiply_op(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            self.builder
                .build_int_mul(left_int, right_int, "mul")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else if let (Ok(left_float), Ok(right_float)) = (
            self.get_raw_float_value(left),
            self.get_raw_float_value(right),
        ) {
            self.builder
                .build_float_mul(left_float, right_float, "fmul")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else {
            Err("Unsupported mul operands".to_string())
        }
    }

    fn generate_divide_op(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            self.builder
                .build_int_signed_div(left_int, right_int, "div")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else if let (Ok(left_float), Ok(right_float)) = (
            self.get_raw_float_value(left),
            self.get_raw_float_value(right),
        ) {
            self.builder
                .build_float_div(left_float, right_float, "fdiv")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else {
            Err("Unsupported div operands".to_string())
        }
    }

    fn generate_exponent_op(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            let pow_fn = self
                .runtime_function("mux_int_pow")
                .ok_or("mux_int_pow not found")?;
            let result = self
                .builder
                .build_call(pow_fn, &[left_int.into(), right_int.into()], "pow")
                .map_err(|e| e.to_string())?
                .try_as_basic_value()
                .left()
                .ok_or("Call returned no value")?;
            Ok(result)
        } else if let (Ok(left_float), Ok(right_float)) = (
            self.get_raw_float_value(left),
            self.get_raw_float_value(right),
        ) {
            let pow_fn = self
                .runtime_function("mux_math_pow")
                .ok_or("mux_math_pow not found")?;
            let result = self
                .builder
                .build_call(pow_fn, &[left_float.into(), right_float.into()], "pow")
                .map_err(|e| e.to_string())?
                .try_as_basic_value()
                .left()
                .ok_or("Call returned no value")?;
            Ok(result)
        } else {
            Err("Unsupported pow operands".to_string())
        }
    }

    fn generate_modulo_op(
        &mut self,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        if let (Ok(left_int), Ok(right_int)) =
            (self.get_raw_int_value(left), self.get_raw_int_value(right))
        {
            self.builder
                .build_int_signed_rem(left_int, right_int, "mod")
                .map_err(|e| e.to_string())
                .map(|v| v.into())
        } else {
            Err("Unsupported mod operands".to_string())
        }
    }

    fn generate_equal_op(
        &mut self,
        left_expr: &ExpressionNode,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        let left_type = self
            .resolve_expression_type_with_fallback(left_expr)
            .map_err(|e| format!("Failed to get left operand type for '==': {}", e))?;

        match &left_type {
            Type::Primitive(PrimitiveType::Str) => {
                let left_ptr = self.ensure_pointer(left);
                let right_ptr = self.ensure_pointer(right);
                let left_cstr = self.extract_c_string_from_value(left_ptr)?;
                let right_cstr = self.extract_c_string_from_value(right_ptr)?;
                self.call_comparison_runtime(
                    left_cstr,
                    right_cstr,
                    "mux_string_equal",
                    "string_equal",
                )
            }
            Type::Primitive(PrimitiveType::Int) | Type::Primitive(PrimitiveType::Char) => {
                let left_int = self.get_raw_int_value(left)?;
                let right_int = self.get_raw_int_value(right)?;
                self.builder
                    .build_int_compare(inkwell::IntPredicate::EQ, left_int, right_int, "eq")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::Primitive(PrimitiveType::Bool) => {
                let left_bool = self.get_raw_bool_value(left)?;
                let right_bool = self.get_raw_bool_value(right)?;
                self.builder
                    .build_int_compare(inkwell::IntPredicate::EQ, left_bool, right_bool, "eq")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::Primitive(PrimitiveType::Float) => {
                let left_float = self.get_raw_float_value(left)?;
                let right_float = self.get_raw_float_value(right)?;
                self.builder
                    .build_float_compare(
                        inkwell::FloatPredicate::OEQ,
                        left_float,
                        right_float,
                        "feq",
                    )
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::List(_)
            | Type::Map(_, _)
            | Type::Set(_)
            | Type::Tuple(_, _)
            | Type::EmptyList
            | Type::EmptyMap
            | Type::EmptySet => {
                let left_ptr = self.ensure_pointer(left);
                let right_ptr = self.ensure_pointer(right);
                self.call_comparison_runtime(left_ptr, right_ptr, "mux_value_equal", "value_equal")
            }
            _ => Err(format!(
                "Equality comparison not supported for type: {:?}",
                left_type
            )),
        }
    }

    fn generate_not_equal_op(
        &mut self,
        left_expr: &ExpressionNode,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        let left_type = self
            .resolve_expression_type_with_fallback(left_expr)
            .map_err(|e| format!("Failed to get left operand type for '!=': {}", e))?;

        match &left_type {
            Type::Primitive(PrimitiveType::Str) => {
                let left_ptr = self.ensure_pointer(left);
                let right_ptr = self.ensure_pointer(right);
                let left_cstr = self.extract_c_string_from_value(left_ptr)?;
                let right_cstr = self.extract_c_string_from_value(right_ptr)?;
                self.call_comparison_runtime(
                    left_cstr,
                    right_cstr,
                    "mux_string_not_equal",
                    "string_not_equal",
                )
            }
            Type::Primitive(PrimitiveType::Int) | Type::Primitive(PrimitiveType::Char) => {
                let left_int = self.get_raw_int_value(left)?;
                let right_int = self.get_raw_int_value(right)?;
                self.builder
                    .build_int_compare(inkwell::IntPredicate::NE, left_int, right_int, "ne")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::Primitive(PrimitiveType::Bool) => {
                let left_bool = self.get_raw_bool_value(left)?;
                let right_bool = self.get_raw_bool_value(right)?;
                self.builder
                    .build_int_compare(inkwell::IntPredicate::NE, left_bool, right_bool, "ne")
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::Primitive(PrimitiveType::Float) => {
                let left_float = self.get_raw_float_value(left)?;
                let right_float = self.get_raw_float_value(right)?;
                self.builder
                    .build_float_compare(
                        inkwell::FloatPredicate::ONE,
                        left_float,
                        right_float,
                        "fne",
                    )
                    .map_err(|e| e.to_string())
                    .map(|v| v.into())
            }
            Type::List(_)
            | Type::Map(_, _)
            | Type::Set(_)
            | Type::EmptyList
            | Type::EmptyMap
            | Type::EmptySet => {
                let left_ptr = self.ensure_pointer(left);
                let right_ptr = self.ensure_pointer(right);
                self.call_comparison_runtime(
                    left_ptr,
                    right_ptr,
                    "mux_value_not_equal",
                    "value_not_equal",
                )
            }
            _ => Err(format!(
                "Inequality comparison not supported for type: {:?}",
                left_type
            )),
        }
    }

    fn generate_in_op(
        &mut self,
        left_expr: &ExpressionNode,
        right_expr: &ExpressionNode,
        left: BasicValueEnum<'a>,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        let right_type = self
            .resolve_expression_type_with_fallback(right_expr)
            .map_err(|e| format!("Failed to get right operand type for 'in': {}", e))?;

        match right_type {
            Type::List(_) | Type::EmptyList => {
                let raw_list = self.extract_list_from_value(right.into_pointer_value())?;
                let item_ptr = self.ensure_pointer(left);
                let result = self
                    .generate_runtime_call("mux_list_contains", &[raw_list.into(), item_ptr.into()])
                    .ok_or("mux_list_contains returned no value")?;
                let free_fn = self
                    .runtime_function("mux_free_list")
                    .ok_or("mux_free_list not found")?;
                self.builder
                    .build_call(free_fn, &[raw_list.into()], "free_list")
                    .map_err(|e| e.to_string())?;
                Ok(result)
            }
            Type::Set(_) | Type::EmptySet => {
                let raw_set = self.extract_set_from_value(right.into_pointer_value())?;
                let item_ptr = self.ensure_pointer(left);
                let result = self
                    .generate_runtime_call("mux_set_contains", &[raw_set.into(), item_ptr.into()])
                    .ok_or("mux_set_contains returned no value")?;
                let free_fn = self
                    .runtime_function("mux_free_set")
                    .ok_or("mux_free_set not found")?;
                self.builder
                    .build_call(free_fn, &[raw_set.into()], "free_set")
                    .map_err(|e| e.to_string())?;
                Ok(result)
            }
            Type::Primitive(PrimitiveType::Str) => {
                let left_type = self
                    .resolve_expression_type_with_fallback(left_expr)
                    .map_err(|e| format!("Failed to get left operand type for 'in': {}", e))?;
                let string_ptr = right.into_pointer_value();

                match left_type {
                    Type::Primitive(PrimitiveType::Char) => {
                        let char_i64 = left.into_int_value();
                        let contains_fn = self
                            .runtime_function("mux_string_contains_char")
                            .ok_or("mux_string_contains_char not found")?;
                        let result = self
                            .builder
                            .build_call(
                                contains_fn,
                                &[string_ptr.into(), char_i64.into()],
                                "string_contains_char",
                            )
                            .map_err(|e| e.to_string())?
                            .try_as_basic_value()
                            .left()
                            .expect("mux_string_contains_char should return a basic value");
                        Ok(result)
                    }
                    Type::Primitive(PrimitiveType::Str) => {
                        let substring_ptr = left.into_pointer_value();
                        let contains_fn = self
                            .runtime_function("mux_string_contains")
                            .ok_or("mux_string_contains not found")?;
                        let result = self
                            .builder
                            .build_call(
                                contains_fn,
                                &[string_ptr.into(), substring_ptr.into()],
                                "string_contains",
                            )
                            .map_err(|e| e.to_string())?
                            .try_as_basic_value()
                            .left()
                            .expect("mux_string_contains should return a basic value");
                        Ok(result)
                    }
                    _ => Err(format!(
                        "Invalid left operand type for 'in' operator with string: {:?}",
                        left_type
                    )),
                }
            }
            _ => Err(format!(
                "'in' operator not supported for type: {:?}",
                right_type
            )),
        }
    }

    pub(super) fn generate_binary_op(
        &mut self,
        left_expr: &ExpressionNode,
        left: BasicValueEnum<'a>,
        op: &BinaryOp,
        right_expr: &ExpressionNode,
        right: BasicValueEnum<'a>,
    ) -> Result<BasicValueEnum<'a>, String> {
        match op {
            BinaryOp::Add => self.generate_add_op(left_expr, left, right),
            BinaryOp::Subtract => self.generate_subtract_op(left, right),
            BinaryOp::Multiply => self.generate_multiply_op(left, right),
            BinaryOp::Divide => self.generate_divide_op(left, right),
            BinaryOp::Exponent => self.generate_exponent_op(left, right),
            BinaryOp::Equal => self.generate_equal_op(left_expr, left, right),
            BinaryOp::Less => self.generate_numeric_compare(
                left,
                right,
                inkwell::IntPredicate::SLT,
                inkwell::FloatPredicate::OLT,
                "lt",
            ),
            BinaryOp::Greater => self.generate_numeric_compare(
                left,
                right,
                inkwell::IntPredicate::SGT,
                inkwell::FloatPredicate::OGT,
                "gt",
            ),
            BinaryOp::LessEqual => self.generate_numeric_compare(
                left,
                right,
                inkwell::IntPredicate::SLE,
                inkwell::FloatPredicate::OLE,
                "le",
            ),
            BinaryOp::GreaterEqual => self.generate_numeric_compare(
                left,
                right,
                inkwell::IntPredicate::SGE,
                inkwell::FloatPredicate::OGE,
                "ge",
            ),
            BinaryOp::NotEqual => self.generate_not_equal_op(left_expr, left, right),
            BinaryOp::LogicalAnd | BinaryOp::LogicalOr => {
                // These should be handled by generate_short_circuit_logical_op
                // and should not reach here
                Err("Logical AND/OR should use short-circuit evaluation".to_string())
            }
            BinaryOp::Modulo => self.generate_modulo_op(left, right),
            BinaryOp::In => self.generate_in_op(left_expr, right_expr, left, right),
            _ => Err("Binary op not implemented".to_string()),
        }
    }
}