rumoca 0.7.28

Modelica compiler written in RUST
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
//! Operator record expansion
//!
//! This module expands operator record arithmetic (like Complex +, -, *, /)
//! into field-wise operations. For Complex numbers:
//!   c = a + b  becomes  c.re = a.re + b.re; c.im = a.im + b.im
//!   c = a * b  becomes  c.re = a.re*b.re - a.im*b.im; c.im = a.re*b.im + a.im*b.re

use crate::ir::ast::{
    ClassDefinition, ClassType, ComponentRefPart, ComponentReference, Equation, Expression,
    OpBinary, Subscript, Token,
};
use indexmap::IndexMap;

/// Information about an operator record type
#[derive(Debug, Clone)]
pub struct OperatorRecordInfo {
    /// The record's field names (in order)
    pub fields: Vec<String>,
    /// Whether this is specifically the Complex type
    pub is_complex: bool,
}

/// Build a map of operator record types from the class dictionary
///
/// This always includes the built-in Complex type with `re` and `im` fields,
/// even if it's not explicitly defined in the class dictionary.
pub fn build_operator_record_map(
    class_dict: &IndexMap<String, ClassDefinition>,
) -> IndexMap<String, OperatorRecordInfo> {
    let mut result = IndexMap::new();

    // Always include the built-in Complex type
    result.insert(
        "Complex".to_string(),
        OperatorRecordInfo {
            fields: vec!["re".to_string(), "im".to_string()],
            is_complex: true,
        },
    );

    for (name, class) in class_dict {
        // Check if this is an operator record
        if matches!(class.class_type, ClassType::Record) {
            // Collect field names
            let fields: Vec<String> = class.components.keys().cloned().collect();

            // Check if this looks like Complex (has re and im fields)
            let is_complex =
                fields.contains(&"re".to_string()) && fields.contains(&"im".to_string());

            if !fields.is_empty() {
                result.insert(name.clone(), OperatorRecordInfo { fields, is_complex });
            }
        }
    }

    result
}

/// Build a map from variable name to its type name
///
/// This function detects Complex variables by checking if both `.re` and `.im`
/// components exist in the flattened class. After flattening, a Complex variable
/// `y` becomes `y.re` and `y.im`, so we infer that `y` is Complex.
pub fn build_type_map(
    flat_class: &ClassDefinition,
    _class_dict: &IndexMap<String, ClassDefinition>,
) -> IndexMap<String, String> {
    let mut result = IndexMap::new();
    let mut complex_candidates: std::collections::HashSet<String> =
        std::collections::HashSet::new();

    // First pass: add all components and collect potential Complex variables
    for (comp_name, comp) in &flat_class.components {
        let type_name = comp.type_name.to_string();
        result.insert(comp_name.clone(), type_name);

        // If this is a `.re` or `.im` component, record the parent as a candidate
        if let Some(parent) = comp_name.strip_suffix(".re") {
            complex_candidates.insert(parent.to_string());
        }
        if let Some(parent) = comp_name.strip_suffix(".im") {
            complex_candidates.insert(parent.to_string());
        }
    }

    // Second pass: check candidates - if both .re and .im exist, mark as Complex
    for candidate in complex_candidates {
        let re_name = format!("{}.re", candidate);
        let im_name = format!("{}.im", candidate);
        if result.contains_key(&re_name) && result.contains_key(&im_name) {
            result.insert(candidate, "Complex".to_string());
        }
    }

    result
}

/// Expand Complex arithmetic in equations
///
/// Given an equation like `c = a + b` where a, b, c are Complex,
/// this expands it into:
///   c.re = a.re + b.re
///   c.im = a.im + b.im
pub fn expand_complex_equations(
    equations: &[Equation],
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Vec<Equation> {
    expand_complex_equations_impl(equations, type_map, operator_records, false)
}

/// Verbose version for debugging
pub fn expand_complex_equations_verbose(
    equations: &[Equation],
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Vec<Equation> {
    expand_complex_equations_impl(equations, type_map, operator_records, true)
}

fn expand_complex_equations_impl(
    equations: &[Equation],
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
    verbose: bool,
) -> Vec<Equation> {
    let mut result = Vec::new();

    for eq in equations {
        if verbose {
            eprintln!("Processing equation: {:?}", eq);
        }
        match expand_single_equation(eq, type_map, operator_records) {
            Some(expanded) => {
                if verbose {
                    eprintln!("  -> Expanded to {} equations", expanded.len());
                }
                result.extend(expanded);
            }
            None => {
                if verbose {
                    eprintln!("  -> Not expanded");
                }
                result.push(eq.clone());
            }
        }
    }

    result
}

/// Extract the base name from a component reference (without subscripts)
fn get_base_name(comp_ref: &ComponentReference) -> String {
    comp_ref
        .parts
        .iter()
        .map(|p| p.ident.text.clone())
        .collect::<Vec<_>>()
        .join(".")
}

/// Get subscripts from the last part of a component reference
fn get_subscripts(comp_ref: &ComponentReference) -> Option<Vec<Subscript>> {
    comp_ref.parts.last().and_then(|p| p.subs.clone())
}

/// Try to expand a single equation if it involves Complex arithmetic
fn expand_single_equation(
    eq: &Equation,
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Option<Vec<Equation>> {
    // Check if this is a simple equation with a Complex variable on the LHS
    let Equation::Simple {
        lhs: Expression::ComponentReference(lhs_ref),
        rhs,
    } = eq
    else {
        return None;
    };

    // Get the base name without subscripts
    let base_name = get_base_name(lhs_ref);
    let subscripts = get_subscripts(lhs_ref);

    // Try looking up with the full name first (e.g., "y[1]"), then the base name
    let lhs_name = lhs_ref.to_string();
    let lhs_type = type_map
        .get(&lhs_name)
        .or_else(|| type_map.get(&base_name))?;
    let record_info = operator_records.get(lhs_type)?;

    if record_info.is_complex {
        // This is a Complex assignment, try to expand the RHS
        expand_complex_assignment(
            &base_name,
            subscripts.as_ref(),
            rhs,
            type_map,
            operator_records,
        )
    } else {
        None
    }
}

/// Expand a Complex assignment into field-wise equations
fn expand_complex_assignment(
    lhs_name: &str,
    subscripts: Option<&Vec<Subscript>>,
    rhs: &Expression,
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Option<Vec<Equation>> {
    // Check if the RHS involves Complex values
    if !expr_involves_complex(rhs, type_map, operator_records) {
        return None;
    }

    // Recursively transform the RHS expression to extract .re and .im parts
    let re_expr = transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
    let im_expr = transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;

    Some(vec![
        Equation::Simple {
            lhs: make_field_ref(lhs_name, "re", subscripts),
            rhs: re_expr,
        },
        Equation::Simple {
            lhs: make_field_ref(lhs_name, "im", subscripts),
            rhs: im_expr,
        },
    ])
}

/// Check if a function name returns Complex values
fn is_complex_function(name: &str) -> bool {
    // Complex constructor
    if name == "Complex" {
        return true;
    }
    // Modelica.ComplexMath functions that return Complex
    if name.contains("ComplexMath") {
        // Most ComplexMath functions return Complex (conj, exp, log, sin, cos, etc.)
        // Functions like abs, arg, real, imag return Real, not Complex
        let func_name = name.rsplit('.').next().unwrap_or(name);
        !matches!(
            func_name,
            "abs" | "arg" | "real" | "imag" | "'abs'" | "'arg'"
        )
    } else {
        false
    }
}

/// Check if an expression involves Complex values (recursively)
fn expr_involves_complex(
    expr: &Expression,
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> bool {
    match expr {
        Expression::ComponentReference(comp) => {
            let name = comp.to_string();
            let base_name = get_base_name(comp);
            if let Some(type_name) = type_map.get(&name).or_else(|| type_map.get(&base_name)) {
                return operator_records
                    .get(type_name)
                    .is_some_and(|r| r.is_complex);
            }
            false
        }
        Expression::FunctionCall { comp, args } => {
            let func_name = comp.to_string();
            // Check if function returns Complex
            if is_complex_function(&func_name) {
                return true;
            }
            // Otherwise check if any argument involves Complex
            args.iter()
                .any(|arg| expr_involves_complex(arg, type_map, operator_records))
        }
        Expression::Binary { lhs, rhs, .. } => {
            expr_involves_complex(lhs, type_map, operator_records)
                || expr_involves_complex(rhs, type_map, operator_records)
        }
        Expression::Unary { rhs, .. } => expr_involves_complex(rhs, type_map, operator_records),
        Expression::If {
            branches,
            else_branch,
        } => {
            // Check if any branch expression involves Complex
            branches
                .iter()
                .any(|(_, expr)| expr_involves_complex(expr, type_map, operator_records))
                || expr_involves_complex(else_branch, type_map, operator_records)
        }
        Expression::Parenthesized { inner } => {
            expr_involves_complex(inner, type_map, operator_records)
        }
        _ => false,
    }
}

/// Transform an expression to get its real part
fn transform_to_real_part(
    expr: &Expression,
    subscripts: Option<&Vec<Subscript>>,
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Option<Expression> {
    match expr {
        Expression::ComponentReference(comp) => {
            // Get the base name and check if it's Complex
            let base_name = get_base_name(comp);
            let full_name = comp.to_string();
            let comp_subs = get_subscripts(comp);

            if let Some(type_name) = type_map
                .get(&full_name)
                .or_else(|| type_map.get(&base_name))
                && operator_records
                    .get(type_name)
                    .is_some_and(|r| r.is_complex)
            {
                // Use the component's own subscripts if it has them
                Some(make_field_ref(&base_name, "re", comp_subs.as_ref()))
            } else {
                Some(expr.clone())
            }
        }
        Expression::FunctionCall { comp, args } => {
            let func_name = comp.to_string();
            if func_name == "Complex" {
                // Complex(re, im) -> re
                Some(args.first().cloned().unwrap_or(make_zero()))
            } else if is_complex_function(&func_name) {
                // Handle ComplexMath functions
                let short_name = func_name.rsplit('.').next().unwrap_or(&func_name);
                match short_name {
                    "conj" => {
                        // conj(z).re = z.re
                        if let Some(arg) = args.first() {
                            transform_to_real_part(arg, subscripts, type_map, operator_records)
                        } else {
                            Some(make_zero())
                        }
                    }
                    _ => {
                        // For other Complex-returning functions, we can't easily expand
                        // Just return the expression as-is (will fail to fully expand)
                        Some(expr.clone())
                    }
                }
            } else {
                Some(expr.clone())
            }
        }
        Expression::Binary { op, lhs, rhs } => {
            match op {
                OpBinary::Add(_) | OpBinary::Sub(_) => {
                    // (a + b).re = a.re + b.re
                    let lhs_re =
                        transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                    let rhs_re =
                        transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                    Some(Expression::Binary {
                        op: op.clone(),
                        lhs: Box::new(lhs_re),
                        rhs: Box::new(rhs_re),
                    })
                }
                OpBinary::Mul(_) => {
                    // Check if one operand is Real (scalar multiplication)
                    let lhs_is_complex = expr_involves_complex(lhs, type_map, operator_records);
                    let rhs_is_complex = expr_involves_complex(rhs, type_map, operator_records);

                    if lhs_is_complex && rhs_is_complex {
                        // (a * b).re = a.re*b.re - a.im*b.im
                        let a_re =
                            transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                        let a_im =
                            transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                        let b_re =
                            transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                        let b_im =
                            transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Sub(Token::default()),
                            lhs: Box::new(Expression::Binary {
                                op: OpBinary::Mul(Token::default()),
                                lhs: Box::new(a_re),
                                rhs: Box::new(b_re),
                            }),
                            rhs: Box::new(Expression::Binary {
                                op: OpBinary::Mul(Token::default()),
                                lhs: Box::new(a_im),
                                rhs: Box::new(b_im),
                            }),
                        })
                    } else if lhs_is_complex {
                        // scalar * complex: (k * c).re = k * c.re
                        let c_re =
                            transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: rhs.clone(),
                            rhs: Box::new(c_re),
                        })
                    } else {
                        // complex * scalar: (c * k).re = c.re * k
                        let c_re =
                            transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: lhs.clone(),
                            rhs: Box::new(c_re),
                        })
                    }
                }
                OpBinary::Div(_) => {
                    // (a / b).re = (a.re*b.re + a.im*b.im) / (b.re^2 + b.im^2)
                    let a_re = transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                    let a_im = transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                    let b_re = transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                    let b_im = transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                    let num = Expression::Binary {
                        op: OpBinary::Add(Token::default()),
                        lhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(a_re),
                            rhs: Box::new(b_re.clone()),
                        }),
                        rhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(a_im),
                            rhs: Box::new(b_im.clone()),
                        }),
                    };
                    let denom = Expression::Binary {
                        op: OpBinary::Add(Token::default()),
                        lhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(b_re.clone()),
                            rhs: Box::new(b_re),
                        }),
                        rhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(b_im.clone()),
                            rhs: Box::new(b_im),
                        }),
                    };
                    Some(Expression::Binary {
                        op: OpBinary::Div(Token::default()),
                        lhs: Box::new(num),
                        rhs: Box::new(denom),
                    })
                }
                _ => Some(expr.clone()),
            }
        }
        Expression::Unary { op, rhs } => {
            let inner_re = transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
            Some(Expression::Unary {
                op: op.clone(),
                rhs: Box::new(inner_re),
            })
        }
        Expression::If {
            branches,
            else_branch,
        } => {
            // Transform each branch: if cond then expr.re elseif ... else expr.re
            let transformed_branches: Option<Vec<_>> = branches
                .iter()
                .map(|(cond, branch_expr)| {
                    transform_to_real_part(branch_expr, subscripts, type_map, operator_records)
                        .map(|transformed| (cond.clone(), transformed))
                })
                .collect();
            let transformed_else =
                transform_to_real_part(else_branch, subscripts, type_map, operator_records)?;
            Some(Expression::If {
                branches: transformed_branches?,
                else_branch: Box::new(transformed_else),
            })
        }
        Expression::Parenthesized { inner } => {
            let inner_re = transform_to_real_part(inner, subscripts, type_map, operator_records)?;
            Some(Expression::Parenthesized {
                inner: Box::new(inner_re),
            })
        }
        _ => Some(expr.clone()),
    }
}

/// Transform an expression to get its imaginary part
fn transform_to_imag_part(
    expr: &Expression,
    subscripts: Option<&Vec<Subscript>>,
    type_map: &IndexMap<String, String>,
    operator_records: &IndexMap<String, OperatorRecordInfo>,
) -> Option<Expression> {
    match expr {
        Expression::ComponentReference(comp) => {
            // Get the base name and check if it's Complex
            let base_name = get_base_name(comp);
            let full_name = comp.to_string();
            let comp_subs = get_subscripts(comp);

            if let Some(type_name) = type_map
                .get(&full_name)
                .or_else(|| type_map.get(&base_name))
                && operator_records
                    .get(type_name)
                    .is_some_and(|r| r.is_complex)
            {
                // Use the component's own subscripts if it has them
                Some(make_field_ref(&base_name, "im", comp_subs.as_ref()))
            } else {
                Some(make_zero()) // Real values have im = 0
            }
        }
        Expression::FunctionCall { comp, args } => {
            let func_name = comp.to_string();
            if func_name == "Complex" {
                // Complex(re, im) -> im
                Some(args.get(1).cloned().unwrap_or(make_zero()))
            } else if is_complex_function(&func_name) {
                // Handle ComplexMath functions
                let short_name = func_name.rsplit('.').next().unwrap_or(&func_name);
                match short_name {
                    "conj" => {
                        // conj(z).im = -z.im
                        if let Some(arg) = args.first() {
                            let inner_im = transform_to_imag_part(
                                arg,
                                subscripts,
                                type_map,
                                operator_records,
                            )?;
                            Some(Expression::Unary {
                                op: crate::ir::ast::OpUnary::Minus(Token::default()),
                                rhs: Box::new(inner_im),
                            })
                        } else {
                            Some(make_zero())
                        }
                    }
                    _ => {
                        // For other Complex-returning functions, we can't easily expand
                        Some(make_zero())
                    }
                }
            } else {
                Some(make_zero())
            }
        }
        Expression::Binary { op, lhs, rhs } => {
            match op {
                OpBinary::Add(_) | OpBinary::Sub(_) => {
                    // (a + b).im = a.im + b.im
                    let lhs_im =
                        transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                    let rhs_im =
                        transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                    Some(Expression::Binary {
                        op: op.clone(),
                        lhs: Box::new(lhs_im),
                        rhs: Box::new(rhs_im),
                    })
                }
                OpBinary::Mul(_) => {
                    // Check if one operand is Real (scalar multiplication)
                    let lhs_is_complex = expr_involves_complex(lhs, type_map, operator_records);
                    let rhs_is_complex = expr_involves_complex(rhs, type_map, operator_records);

                    if lhs_is_complex && rhs_is_complex {
                        // (a * b).im = a.re*b.im + a.im*b.re
                        let a_re =
                            transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                        let a_im =
                            transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                        let b_re =
                            transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                        let b_im =
                            transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Add(Token::default()),
                            lhs: Box::new(Expression::Binary {
                                op: OpBinary::Mul(Token::default()),
                                lhs: Box::new(a_re),
                                rhs: Box::new(b_im),
                            }),
                            rhs: Box::new(Expression::Binary {
                                op: OpBinary::Mul(Token::default()),
                                lhs: Box::new(a_im),
                                rhs: Box::new(b_re),
                            }),
                        })
                    } else if lhs_is_complex {
                        // scalar * complex: (k * c).im = k * c.im
                        let c_im =
                            transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: rhs.clone(),
                            rhs: Box::new(c_im),
                        })
                    } else {
                        // complex * scalar: (c * k).im = c.im * k
                        let c_im =
                            transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                        Some(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: lhs.clone(),
                            rhs: Box::new(c_im),
                        })
                    }
                }
                OpBinary::Div(_) => {
                    // (a / b).im = (a.im*b.re - a.re*b.im) / (b.re^2 + b.im^2)
                    let a_re = transform_to_real_part(lhs, subscripts, type_map, operator_records)?;
                    let a_im = transform_to_imag_part(lhs, subscripts, type_map, operator_records)?;
                    let b_re = transform_to_real_part(rhs, subscripts, type_map, operator_records)?;
                    let b_im = transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
                    let num = Expression::Binary {
                        op: OpBinary::Sub(Token::default()),
                        lhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(a_im),
                            rhs: Box::new(b_re.clone()),
                        }),
                        rhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(a_re),
                            rhs: Box::new(b_im.clone()),
                        }),
                    };
                    let denom = Expression::Binary {
                        op: OpBinary::Add(Token::default()),
                        lhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(b_re.clone()),
                            rhs: Box::new(b_re),
                        }),
                        rhs: Box::new(Expression::Binary {
                            op: OpBinary::Mul(Token::default()),
                            lhs: Box::new(b_im.clone()),
                            rhs: Box::new(b_im),
                        }),
                    };
                    Some(Expression::Binary {
                        op: OpBinary::Div(Token::default()),
                        lhs: Box::new(num),
                        rhs: Box::new(denom),
                    })
                }
                _ => Some(make_zero()),
            }
        }
        Expression::Unary { op, rhs } => {
            let inner_im = transform_to_imag_part(rhs, subscripts, type_map, operator_records)?;
            Some(Expression::Unary {
                op: op.clone(),
                rhs: Box::new(inner_im),
            })
        }
        Expression::If {
            branches,
            else_branch,
        } => {
            // Transform each branch: if cond then expr.im elseif ... else expr.im
            let transformed_branches: Option<Vec<_>> = branches
                .iter()
                .map(|(cond, branch_expr)| {
                    transform_to_imag_part(branch_expr, subscripts, type_map, operator_records)
                        .map(|transformed| (cond.clone(), transformed))
                })
                .collect();
            let transformed_else =
                transform_to_imag_part(else_branch, subscripts, type_map, operator_records)?;
            Some(Expression::If {
                branches: transformed_branches?,
                else_branch: Box::new(transformed_else),
            })
        }
        Expression::Parenthesized { inner } => {
            let inner_im = transform_to_imag_part(inner, subscripts, type_map, operator_records)?;
            Some(Expression::Parenthesized {
                inner: Box::new(inner_im),
            })
        }
        _ => Some(make_zero()),
    }
}

/// Create a field reference expression: name.field or name.field[subs]
fn make_field_ref(name: &str, field: &str, subscripts: Option<&Vec<Subscript>>) -> Expression {
    Expression::ComponentReference(ComponentReference {
        local: false,
        parts: vec![ComponentRefPart {
            ident: Token {
                text: format!("{}.{}", name, field),
                ..Default::default()
            },
            subs: subscripts.cloned(),
        }],
    })
}

/// Create a zero expression
fn make_zero() -> Expression {
    Expression::Terminal {
        terminal_type: crate::ir::ast::TerminalType::UnsignedReal,
        token: Token {
            text: "0".to_string(),
            ..Default::default()
        },
    }
}

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

    #[test]
    fn test_complex_detection() {
        let mut class_dict = IndexMap::new();

        // Create a Complex-like record
        let mut complex_class = ClassDefinition {
            class_type: ClassType::Record,
            ..Default::default()
        };
        complex_class.components.insert(
            "re".to_string(),
            crate::ir::ast::Component {
                name: "re".to_string(),
                ..Default::default()
            },
        );
        complex_class.components.insert(
            "im".to_string(),
            crate::ir::ast::Component {
                name: "im".to_string(),
                ..Default::default()
            },
        );

        class_dict.insert("Complex".to_string(), complex_class);

        let op_records = build_operator_record_map(&class_dict);

        assert!(op_records.contains_key("Complex"));
        assert!(op_records["Complex"].is_complex);
        assert_eq!(op_records["Complex"].fields.len(), 2);
    }
}