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
//! Array comprehension expander
//!
//! This module expands array comprehensions like `{expr for i in 1:n}` into
//! explicit arrays `{expr[i=1], expr[i=2], ..., expr[i=n]}`.
//!
//! For example:
//! - `{i*2 for i in 1:3}` becomes `{2, 4, 6}`
//! - `{A[i,j]*x[j] for j in 1:n}` becomes `{A[i,1]*x[1], A[i,2]*x[2], ..., A[i,n]*x[n]}`

use crate::ir::ast::{
    ClassDefinition, Component, ComponentRefPart, ComponentReference, Expression, ForIndex,
    OpBinary, OpUnary, Subscript, TerminalType, Token,
};
use crate::ir::visitor::{MutVisitable, MutVisitor};
use indexmap::IndexMap;

/// Maximum recursion depth for evaluation to prevent stack overflow
const MAX_EVAL_DEPTH: usize = 100;

// =============================================================================
// Array Comprehension Expander Visitor
// =============================================================================

/// Visitor that expands array comprehensions into explicit arrays.
///
/// This replaces manual recursion through equations and expressions with
/// the standard MutVisitor pattern for cleaner, more maintainable code.
struct ComprehensionExpander<'a> {
    params: &'a IndexMap<String, Component>,
}

impl<'a> ComprehensionExpander<'a> {
    fn new(params: &'a IndexMap<String, Component>) -> Self {
        Self { params }
    }
}

impl MutVisitor for ComprehensionExpander<'_> {
    fn exit_expression(&mut self, expr: &mut Expression) {
        // After children have been processed, check if this is an ArrayComprehension
        // that can be expanded
        let expanded = match expr {
            Expression::ArrayComprehension {
                expr: inner_expr,
                indices,
            } => try_expand_comprehension(inner_expr, indices, self.params),
            _ => None,
        };
        if let Some(expanded) = expanded {
            *expr = expanded;
        }
    }
}

/// Expand all array comprehensions in a class definition.
///
/// This function uses the visitor pattern to walk through all expressions
/// in the class and replace ArrayComprehension expressions with expanded
/// Array expressions.
pub fn expand_array_comprehensions(class: &mut ClassDefinition) {
    // Build parameter map for evaluation
    let params: IndexMap<String, Component> = class
        .components
        .iter()
        .filter(|(_, c)| matches!(c.variability, crate::ir::ast::Variability::Parameter(..)))
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();

    // Use visitor pattern to expand comprehensions
    let mut expander = ComprehensionExpander::new(&params);
    class.accept_mut(&mut expander);
}

/// Try to expand an array comprehension into an explicit array
fn try_expand_comprehension(
    expr: &Expression,
    indices: &[ForIndex],
    params: &IndexMap<String, Component>,
) -> Option<Expression> {
    if indices.is_empty() {
        return None;
    }

    // For now, handle single-index comprehensions
    // Multi-index (cartesian product) support can be added later
    if indices.len() == 1 {
        let idx = &indices[0];
        let var_name = &idx.ident.text;
        let range = &idx.range;

        // Try to evaluate the range to get start and end values
        let (start, end) = try_evaluate_range(range, params)?;

        // Generate elements by substituting the index variable
        let mut elements = Vec::new();
        for i in start..=end {
            let substituted = substitute_variable(expr, var_name, i);
            elements.push(substituted);
        }

        Some(Expression::Array {
            elements,
            is_matrix: false,
        })
    } else {
        // Multi-index comprehension: expand nested
        // {expr for i in 1:n, j in 1:m} becomes
        // {{expr[i=1,j=1], expr[i=1,j=2], ...}, {expr[i=2,j=1], ...}, ...}
        let first_idx = &indices[0];
        let remaining_indices: Vec<ForIndex> = indices[1..].to_vec();

        let var_name = &first_idx.ident.text;
        let (start, end) = try_evaluate_range(&first_idx.range, params)?;

        let mut elements = Vec::new();
        for i in start..=end {
            // Substitute first variable
            let substituted_expr = substitute_variable(expr, var_name, i);
            // Recursively expand remaining indices
            if let Some(inner) =
                try_expand_comprehension(&substituted_expr, &remaining_indices, params)
            {
                elements.push(inner);
            } else {
                // Can't expand remaining - return nested comprehension
                elements.push(Expression::ArrayComprehension {
                    expr: Box::new(substituted_expr),
                    indices: remaining_indices.clone(),
                });
            }
        }

        Some(Expression::Array {
            elements,
            is_matrix: false,
        })
    }
}

/// Try to evaluate a range expression to get (start, end) integer values
fn try_evaluate_range(
    expr: &Expression,
    params: &IndexMap<String, Component>,
) -> Option<(i64, i64)> {
    match expr {
        Expression::Range { start, step, end } => {
            // For now, only handle simple ranges without step
            if step.is_some() {
                return None; // TODO: handle stepped ranges
            }
            let start_val = try_evaluate_integer(start, params, 0)?;
            let end_val = try_evaluate_integer(end, params, 0)?;
            Some((start_val, end_val))
        }
        _ => {
            // Single value - treat as 1:value range
            let val = try_evaluate_integer(expr, params, 0)?;
            Some((1, val))
        }
    }
}

/// Try to evaluate an expression as an integer
fn try_evaluate_integer(
    expr: &Expression,
    params: &IndexMap<String, Component>,
    depth: usize,
) -> Option<i64> {
    if depth > MAX_EVAL_DEPTH {
        return None;
    }

    match expr {
        Expression::Terminal { token, .. } => token.text.parse::<i64>().ok(),
        Expression::ComponentReference(cref) => {
            let name = cref.to_string();
            if let Some(param) = params.get(&name) {
                try_evaluate_integer(&param.start, params, depth + 1)
            } else {
                None
            }
        }
        Expression::Unary { op, rhs } => match op {
            OpUnary::Minus(_) => try_evaluate_integer(rhs, params, depth + 1).map(|v| -v),
            OpUnary::Plus(_) => try_evaluate_integer(rhs, params, depth + 1),
            _ => None,
        },
        Expression::Binary { lhs, op, rhs } => {
            let l = try_evaluate_integer(lhs, params, depth + 1)?;
            let r = try_evaluate_integer(rhs, params, depth + 1)?;
            match op {
                OpBinary::Add(_) => Some(l + r),
                OpBinary::Sub(_) => Some(l - r),
                OpBinary::Mul(_) => Some(l * r),
                OpBinary::Div(_) if r != 0 => Some(l / r),
                _ => None,
            }
        }
        Expression::Parenthesized { inner } => try_evaluate_integer(inner, params, depth + 1),
        Expression::FunctionCall { comp, args } => {
            let func_name = comp.to_string();
            if func_name == "size" && !args.is_empty() {
                // Handle size(array, dim) function
                if let Expression::ComponentReference(array_ref) = &args[0] {
                    let array_name = array_ref.to_string();
                    let dim_index = if args.len() >= 2 {
                        try_evaluate_integer(&args[1], params, depth + 1)? as usize
                    } else {
                        1
                    };

                    if let Some(array_comp) = params.get(&array_name) {
                        // Try evaluated shape first
                        if !array_comp.shape.is_empty()
                            && dim_index >= 1
                            && dim_index <= array_comp.shape.len()
                        {
                            return Some(array_comp.shape[dim_index - 1] as i64);
                        }
                        // Try shape_expr
                        if !array_comp.shape_expr.is_empty()
                            && dim_index >= 1
                            && dim_index <= array_comp.shape_expr.len()
                            && let crate::ir::ast::Subscript::Expression(expr) =
                                &array_comp.shape_expr[dim_index - 1]
                        {
                            return try_evaluate_integer(expr, params, depth + 1);
                        }
                        // Subscript::Range (`:`) can't be evaluated directly
                        // Try to infer from start expression (array literal)
                        if let Expression::Array { elements, .. } = &array_comp.start
                            && dim_index == 1
                        {
                            return Some(elements.len() as i64);
                        }
                    }
                }
            }
            None
        }
        _ => None,
    }
}

/// Substitute all occurrences of a variable with an integer value in an expression
fn substitute_variable(expr: &Expression, var_name: &str, value: i64) -> Expression {
    match expr {
        Expression::ComponentReference(cref) => {
            // Check if this is just the variable name
            if cref.parts.len() == 1 && cref.parts[0].ident.text == var_name {
                // Replace with integer literal
                Expression::Terminal {
                    terminal_type: TerminalType::UnsignedInteger,
                    token: Token {
                        text: value.to_string(),
                        ..Default::default()
                    },
                }
            } else {
                // Check subscripts for the variable
                let new_parts: Vec<ComponentRefPart> = cref
                    .parts
                    .iter()
                    .map(|part| {
                        let new_subs = part.subs.as_ref().map(|subs| {
                            subs.iter()
                                .map(|sub| substitute_in_subscript(sub, var_name, value))
                                .collect()
                        });
                        ComponentRefPart {
                            ident: part.ident.clone(),
                            subs: new_subs,
                        }
                    })
                    .collect();
                Expression::ComponentReference(ComponentReference {
                    local: cref.local,
                    parts: new_parts,
                })
            }
        }
        Expression::Binary { lhs, op, rhs } => Expression::Binary {
            op: op.clone(),
            lhs: Box::new(substitute_variable(lhs, var_name, value)),
            rhs: Box::new(substitute_variable(rhs, var_name, value)),
        },
        Expression::Unary { op, rhs } => Expression::Unary {
            op: op.clone(),
            rhs: Box::new(substitute_variable(rhs, var_name, value)),
        },
        Expression::FunctionCall { comp, args } => Expression::FunctionCall {
            comp: comp.clone(),
            args: args
                .iter()
                .map(|a| substitute_variable(a, var_name, value))
                .collect(),
        },
        Expression::Array {
            elements,
            is_matrix,
        } => Expression::Array {
            elements: elements
                .iter()
                .map(|e| substitute_variable(e, var_name, value))
                .collect(),
            is_matrix: *is_matrix,
        },
        Expression::Tuple { elements } => Expression::Tuple {
            elements: elements
                .iter()
                .map(|e| substitute_variable(e, var_name, value))
                .collect(),
        },
        Expression::If {
            branches,
            else_branch,
        } => Expression::If {
            branches: branches
                .iter()
                .map(|(c, t)| {
                    (
                        substitute_variable(c, var_name, value),
                        substitute_variable(t, var_name, value),
                    )
                })
                .collect(),
            else_branch: Box::new(substitute_variable(else_branch, var_name, value)),
        },
        Expression::Range { start, step, end } => Expression::Range {
            start: Box::new(substitute_variable(start, var_name, value)),
            step: step
                .as_ref()
                .map(|s| Box::new(substitute_variable(s, var_name, value))),
            end: Box::new(substitute_variable(end, var_name, value)),
        },
        Expression::Parenthesized { inner } => Expression::Parenthesized {
            inner: Box::new(substitute_variable(inner, var_name, value)),
        },
        Expression::ArrayComprehension {
            expr: inner,
            indices,
        } => {
            // Check if the variable is shadowed by the comprehension's own indices
            let is_shadowed = indices.iter().any(|idx| idx.ident.text == var_name);
            if is_shadowed {
                // Don't substitute - the variable is local to this comprehension
                expr.clone()
            } else {
                Expression::ArrayComprehension {
                    expr: Box::new(substitute_variable(inner, var_name, value)),
                    indices: indices
                        .iter()
                        .map(|idx| ForIndex {
                            ident: idx.ident.clone(),
                            range: substitute_variable(&idx.range, var_name, value),
                        })
                        .collect(),
                }
            }
        }
        Expression::Terminal { .. } | Expression::Empty => expr.clone(),
    }
}

/// Substitute a variable in a subscript
fn substitute_in_subscript(sub: &Subscript, var_name: &str, value: i64) -> Subscript {
    match sub {
        Subscript::Expression(expr) => {
            Subscript::Expression(substitute_variable(expr, var_name, value))
        }
        Subscript::Range { token } => Subscript::Range {
            token: token.clone(),
        },
        Subscript::Empty => Subscript::Empty,
    }
}

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

    #[test]
    fn test_substitute_simple() {
        // Test substituting i with 5 in expression "i"
        let expr = Expression::ComponentReference(ComponentReference {
            local: false,
            parts: vec![ComponentRefPart {
                ident: Token {
                    text: "i".to_string(),
                    ..Default::default()
                },
                subs: None,
            }],
        });

        let result = substitute_variable(&expr, "i", 5);

        match result {
            Expression::Terminal { token, .. } => {
                assert_eq!(token.text, "5");
            }
            _ => panic!("Expected Terminal expression"),
        }
    }

    #[test]
    fn test_evaluate_simple_range() {
        let params = IndexMap::new();
        let range = Expression::Range {
            start: Box::new(Expression::Terminal {
                terminal_type: TerminalType::UnsignedInteger,
                token: Token {
                    text: "1".to_string(),
                    ..Default::default()
                },
            }),
            step: None,
            end: Box::new(Expression::Terminal {
                terminal_type: TerminalType::UnsignedInteger,
                token: Token {
                    text: "3".to_string(),
                    ..Default::default()
                },
            }),
        };

        let result = try_evaluate_range(&range, &params);
        assert_eq!(result, Some((1, 3)));
    }
}