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
//! Expression evaluation utilities for compile-time constant folding.
//!
//! This module provides functions to evaluate expressions to concrete values
//! when the operands are known at compile time (parameters, constants, etc.).
//!
//! # Usage
//!
//! ```ignore
//! use rumoca::ir::transform::eval::{eval_integer, eval_real, eval_boolean};
//!
//! // Evaluate an expression using known component values
//! if let Some(value) = eval_integer(&expr, &components) {
//!     println!("Expression evaluates to: {}", value);
//! }
//! ```

use crate::ir::ast::{Component, Expression, OpBinary, OpUnary, TerminalType, Variability};
use indexmap::IndexMap;

/// Configuration for expression evaluation behavior.
#[derive(Debug, Clone, Copy, Default)]
pub struct EvalConfig {
    /// If true, only evaluate Parameter components.
    /// If false, evaluate any component with a start value.
    pub parameters_only: bool,
}

impl EvalConfig {
    /// Create config that only evaluates parameters
    pub fn parameters_only() -> Self {
        Self {
            parameters_only: true,
        }
    }

    /// Create config that evaluates all components
    pub fn all_components() -> Self {
        Self {
            parameters_only: false,
        }
    }
}

/// Evaluate an expression to an integer value.
///
/// Handles:
/// - Integer and real literals (if whole number)
/// - Component references (with optional parameter-only restriction)
/// - Unary +/- operators
/// - Binary +, -, *, / operators
/// - size(array, dim) function
/// - integer(), mod(), div() functions
/// - If-then-else expressions
/// - Parenthesized expressions
pub fn eval_integer(expr: &Expression, components: &IndexMap<String, Component>) -> Option<i64> {
    eval_integer_with_config(expr, components, EvalConfig::default())
}

/// Evaluate an expression to an integer with configuration.
pub fn eval_integer_with_config(
    expr: &Expression,
    components: &IndexMap<String, Component>,
    config: EvalConfig,
) -> Option<i64> {
    match expr {
        Expression::Terminal {
            terminal_type: TerminalType::UnsignedInteger,
            token,
        } => token.text.parse().ok(),

        Expression::Terminal {
            terminal_type: TerminalType::UnsignedReal,
            token,
        } => {
            let f: f64 = token.text.parse().ok()?;
            if f.fract() == 0.0 {
                Some(f as i64)
            } else {
                None
            }
        }

        Expression::ComponentReference(comp_ref) => {
            // Handle both simple names and dotted names
            if comp_ref.parts.iter().all(|p| p.subs.is_none()) {
                let name = build_component_name(comp_ref);

                if let Some(comp) = components.get(&name) {
                    // Check if we should evaluate this component
                    if !config.parameters_only
                        || matches!(comp.variability, Variability::Parameter(_))
                    {
                        return eval_integer_with_config(&comp.start, components, config);
                    }
                }
            }
            None
        }

        Expression::Unary { op, rhs } => {
            let val = eval_integer_with_config(rhs, components, config)?;
            match op {
                OpUnary::Minus(_) => Some(-val),
                OpUnary::Plus(_) => Some(val),
                _ => None,
            }
        }

        Expression::Binary { op, lhs, rhs } => {
            let l = eval_integer_with_config(lhs, components, config)?;
            let r = eval_integer_with_config(rhs, components, config)?;
            match op {
                OpBinary::Add(_) => Some(l.saturating_add(r)),
                OpBinary::Sub(_) => Some(l.saturating_sub(r)),
                OpBinary::Mul(_) => Some(l.saturating_mul(r)),
                OpBinary::Div(_) => {
                    if r != 0 {
                        Some(l / r)
                    } else {
                        None
                    }
                }
                _ => None,
            }
        }

        Expression::FunctionCall { comp, args } => {
            eval_integer_function(comp, args, components, config)
        }

        Expression::If {
            branches,
            else_branch,
        } => {
            // Evaluate if-then-else expression for conditional values
            for (condition, result) in branches {
                if let Some(cond_val) = eval_boolean_with_config(condition, components, config) {
                    if cond_val {
                        return eval_integer_with_config(result, components, config);
                    }
                } else {
                    // Condition can't be evaluated at compile time
                    return None;
                }
            }
            // All conditions were false, evaluate else branch
            eval_integer_with_config(else_branch, components, config)
        }

        Expression::Parenthesized { inner } => eval_integer_with_config(inner, components, config),

        _ => None,
    }
}

/// Evaluate integer-returning function calls
fn eval_integer_function(
    comp: &crate::ir::ast::ComponentReference,
    args: &[Expression],
    components: &IndexMap<String, Component>,
    config: EvalConfig,
) -> Option<i64> {
    let first_part = comp.parts.first()?;
    let func_name = first_part.ident.text.as_str();

    match func_name {
        // Handle size(array, dim) function
        "size" if !args.is_empty() => {
            if let Expression::ComponentReference(array_ref) = &args[0]
                && array_ref.parts.iter().all(|p| p.subs.is_none())
            {
                let array_name = build_component_name(array_ref);

                if let Some(array_comp) = components.get(&array_name) {
                    let dim_index = if args.len() >= 2 {
                        eval_integer_with_config(&args[1], components, config).unwrap_or(1) as usize
                    } else {
                        1
                    };

                    // First check evaluated shape
                    if !array_comp.shape.is_empty() && dim_index <= array_comp.shape.len() {
                        return Some(array_comp.shape[dim_index - 1] as i64);
                    }

                    // Check if it's an array literal in start
                    if let Expression::Array { elements, .. } = &array_comp.start
                        && dim_index == 1
                    {
                        return Some(elements.len() as i64);
                    }
                }
            }
            None
        }

        // Handle integer(x) - truncation toward zero
        "integer" if args.len() == 1 => {
            eval_real_with_config(&args[0], components, config).map(|val| val.trunc() as i64)
        }

        // Handle mod(x, y) - modulo operation
        "mod" if args.len() == 2 => {
            let x = eval_integer_with_config(&args[0], components, config)?;
            let y = eval_integer_with_config(&args[1], components, config)?;
            if y != 0 {
                // Modelica mod is floored division remainder
                Some(((x % y) + y) % y)
            } else {
                None
            }
        }

        // Handle div(x, y) - integer division (truncated toward zero)
        "div" if args.len() == 2 => {
            let x = eval_integer_with_config(&args[0], components, config)?;
            let y = eval_integer_with_config(&args[1], components, config)?;
            if y != 0 { Some(x / y) } else { None }
        }

        _ => None,
    }
}

/// Evaluate an expression to a real (f64) value.
pub fn eval_real(expr: &Expression, components: &IndexMap<String, Component>) -> Option<f64> {
    eval_real_with_config(expr, components, EvalConfig::default())
}

/// Evaluate an expression to a real with configuration.
pub fn eval_real_with_config(
    expr: &Expression,
    components: &IndexMap<String, Component>,
    config: EvalConfig,
) -> Option<f64> {
    match expr {
        Expression::Terminal {
            terminal_type: TerminalType::UnsignedReal,
            token,
        } => token.text.parse().ok(),

        Expression::Terminal {
            terminal_type: TerminalType::UnsignedInteger,
            token,
        } => token.text.parse::<i64>().ok().map(|v| v as f64),

        Expression::ComponentReference(comp_ref) => {
            if comp_ref.parts.iter().all(|p| p.subs.is_none()) {
                let name = build_component_name(comp_ref);

                if let Some(comp) = components.get(&name)
                    && (!config.parameters_only
                        || matches!(comp.variability, Variability::Parameter(_)))
                {
                    return eval_real_with_config(&comp.start, components, config);
                }
            }
            None
        }

        Expression::Unary { op, rhs } => {
            let val = eval_real_with_config(rhs, components, config)?;
            match op {
                OpUnary::Minus(_) => Some(-val),
                OpUnary::Plus(_) => Some(val),
                _ => None,
            }
        }

        Expression::Binary { op, lhs, rhs } => {
            let l = eval_real_with_config(lhs, components, config)?;
            let r = eval_real_with_config(rhs, components, config)?;
            match op {
                OpBinary::Add(_) => Some(l + r),
                OpBinary::Sub(_) => Some(l - r),
                OpBinary::Mul(_) => Some(l * r),
                OpBinary::Div(_) => {
                    if r != 0.0 {
                        Some(l / r)
                    } else {
                        None
                    }
                }
                _ => None,
            }
        }

        Expression::Parenthesized { inner } => eval_real_with_config(inner, components, config),

        _ => None,
    }
}

/// Evaluate an expression to a boolean value.
pub fn eval_boolean(expr: &Expression, components: &IndexMap<String, Component>) -> Option<bool> {
    eval_boolean_with_config(expr, components, EvalConfig::default())
}

/// Evaluate an expression to a boolean with configuration.
pub fn eval_boolean_with_config(
    expr: &Expression,
    components: &IndexMap<String, Component>,
    config: EvalConfig,
) -> Option<bool> {
    match expr {
        Expression::Terminal {
            terminal_type: TerminalType::Bool,
            token,
        } => Some(token.text == "true"),

        Expression::ComponentReference(comp_ref) => {
            if comp_ref.parts.len() == 1 && comp_ref.parts[0].subs.is_none() {
                let name = &comp_ref.parts[0].ident.text;
                if let Some(comp) = components.get(name) {
                    // Evaluate if it's a parameter or has a boolean start value
                    let should_eval = !config.parameters_only
                        || matches!(comp.variability, Variability::Parameter(_))
                        || matches!(
                            &comp.start,
                            Expression::Terminal {
                                terminal_type: TerminalType::Bool,
                                ..
                            }
                        );

                    if should_eval {
                        return eval_boolean_with_config(&comp.start, components, config);
                    }
                }
            }
            None
        }

        Expression::Unary { op, rhs } => {
            let val = eval_boolean_with_config(rhs, components, config)?;
            match op {
                OpUnary::Not(_) => Some(!val),
                _ => None,
            }
        }

        Expression::Binary { op, lhs, rhs } => {
            match op {
                OpBinary::And(_) => {
                    let l = eval_boolean_with_config(lhs, components, config)?;
                    let r = eval_boolean_with_config(rhs, components, config)?;
                    Some(l && r)
                }
                OpBinary::Or(_) => {
                    let l = eval_boolean_with_config(lhs, components, config)?;
                    let r = eval_boolean_with_config(rhs, components, config)?;
                    Some(l || r)
                }
                // Comparison operators
                OpBinary::Eq(_) => {
                    // Try integer comparison
                    if let (Some(l), Some(r)) = (
                        eval_integer_with_config(lhs, components, config),
                        eval_integer_with_config(rhs, components, config),
                    ) {
                        return Some(l == r);
                    }
                    // Try boolean comparison
                    if let (Some(l), Some(r)) = (
                        eval_boolean_with_config(lhs, components, config),
                        eval_boolean_with_config(rhs, components, config),
                    ) {
                        return Some(l == r);
                    }
                    None
                }
                OpBinary::Neq(_) => {
                    if let (Some(l), Some(r)) = (
                        eval_integer_with_config(lhs, components, config),
                        eval_integer_with_config(rhs, components, config),
                    ) {
                        return Some(l != r);
                    }
                    if let (Some(l), Some(r)) = (
                        eval_boolean_with_config(lhs, components, config),
                        eval_boolean_with_config(rhs, components, config),
                    ) {
                        return Some(l != r);
                    }
                    None
                }
                OpBinary::Lt(_) => {
                    let l = eval_integer_with_config(lhs, components, config)?;
                    let r = eval_integer_with_config(rhs, components, config)?;
                    Some(l < r)
                }
                OpBinary::Le(_) => {
                    let l = eval_integer_with_config(lhs, components, config)?;
                    let r = eval_integer_with_config(rhs, components, config)?;
                    Some(l <= r)
                }
                OpBinary::Gt(_) => {
                    let l = eval_integer_with_config(lhs, components, config)?;
                    let r = eval_integer_with_config(rhs, components, config)?;
                    Some(l > r)
                }
                OpBinary::Ge(_) => {
                    let l = eval_integer_with_config(lhs, components, config)?;
                    let r = eval_integer_with_config(rhs, components, config)?;
                    Some(l >= r)
                }
                _ => None,
            }
        }

        _ => None,
    }
}

/// Build a full component name from a component reference.
fn build_component_name(comp_ref: &crate::ir::ast::ComponentReference) -> String {
    if comp_ref.parts.len() == 1 {
        comp_ref.parts[0].ident.text.clone()
    } else {
        comp_ref
            .parts
            .iter()
            .map(|p| p.ident.text.as_str())
            .collect::<Vec<_>>()
            .join(".")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::ast::{ComponentRefPart, ComponentReference, Token};

    fn make_int_terminal(value: i64) -> Expression {
        Expression::Terminal {
            terminal_type: TerminalType::UnsignedInteger,
            token: Token {
                text: value.to_string(),
                ..Default::default()
            },
        }
    }

    fn make_real_terminal(value: f64) -> Expression {
        Expression::Terminal {
            terminal_type: TerminalType::UnsignedReal,
            token: Token {
                text: value.to_string(),
                ..Default::default()
            },
        }
    }

    #[test]
    fn test_eval_integer_literal() {
        let components = IndexMap::new();
        let expr = make_int_terminal(42);
        assert_eq!(eval_integer(&expr, &components), Some(42));
    }

    #[test]
    fn test_eval_real_as_integer() {
        let components = IndexMap::new();
        let expr = make_real_terminal(42.0);
        assert_eq!(eval_integer(&expr, &components), Some(42));

        // Non-integer real should return None
        let expr = make_real_terminal(42.5);
        assert_eq!(eval_integer(&expr, &components), None);
    }

    #[test]
    fn test_eval_binary_ops() {
        let components = IndexMap::new();

        let add = Expression::Binary {
            op: OpBinary::Add(Token::default()),
            lhs: Box::new(make_int_terminal(10)),
            rhs: Box::new(make_int_terminal(5)),
        };
        assert_eq!(eval_integer(&add, &components), Some(15));

        let sub = Expression::Binary {
            op: OpBinary::Sub(Token::default()),
            lhs: Box::new(make_int_terminal(10)),
            rhs: Box::new(make_int_terminal(5)),
        };
        assert_eq!(eval_integer(&sub, &components), Some(5));

        let mul = Expression::Binary {
            op: OpBinary::Mul(Token::default()),
            lhs: Box::new(make_int_terminal(10)),
            rhs: Box::new(make_int_terminal(5)),
        };
        assert_eq!(eval_integer(&mul, &components), Some(50));

        let div = Expression::Binary {
            op: OpBinary::Div(Token::default()),
            lhs: Box::new(make_int_terminal(10)),
            rhs: Box::new(make_int_terminal(5)),
        };
        assert_eq!(eval_integer(&div, &components), Some(2));
    }

    #[test]
    fn test_eval_division_by_zero() {
        let components = IndexMap::new();
        let div = Expression::Binary {
            op: OpBinary::Div(Token::default()),
            lhs: Box::new(make_int_terminal(10)),
            rhs: Box::new(make_int_terminal(0)),
        };
        assert_eq!(eval_integer(&div, &components), None);
    }

    #[test]
    fn test_eval_unary() {
        let components = IndexMap::new();

        let neg = Expression::Unary {
            op: OpUnary::Minus(Token::default()),
            rhs: Box::new(make_int_terminal(42)),
        };
        assert_eq!(eval_integer(&neg, &components), Some(-42));
    }

    #[test]
    fn test_eval_boolean() {
        let components = IndexMap::new();

        let true_expr = Expression::Terminal {
            terminal_type: TerminalType::Bool,
            token: Token {
                text: "true".to_string(),
                ..Default::default()
            },
        };
        assert_eq!(eval_boolean(&true_expr, &components), Some(true));

        let false_expr = Expression::Terminal {
            terminal_type: TerminalType::Bool,
            token: Token {
                text: "false".to_string(),
                ..Default::default()
            },
        };
        assert_eq!(eval_boolean(&false_expr, &components), Some(false));
    }

    #[test]
    fn test_eval_component_reference() {
        let mut components = IndexMap::new();
        components.insert(
            "n".to_string(),
            Component {
                name: "n".to_string(),
                variability: Variability::Parameter(Token::default()),
                start: make_int_terminal(10),
                ..Default::default()
            },
        );

        let comp_ref = Expression::ComponentReference(ComponentReference {
            local: false,
            parts: vec![ComponentRefPart {
                ident: Token {
                    text: "n".to_string(),
                    ..Default::default()
                },
                subs: None,
            }],
        });

        // With default config (parameters_only = false), should evaluate
        assert_eq!(eval_integer(&comp_ref, &components), Some(10));

        // With parameters_only = true, should also evaluate (it is a parameter)
        assert_eq!(
            eval_integer_with_config(&comp_ref, &components, EvalConfig::parameters_only()),
            Some(10)
        );
    }
}