Skip to content

Commit

Permalink
mc: fix translation of local variable initializers
Browse files Browse the repository at this point in the history
variable initializers were erroneously not executed if they were not a
proc call.
  • Loading branch information
Philipp15b committed Jun 21, 2024
1 parent 0e47be9 commit ff82939
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/mc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn translate_variables(
let decl = var_ref.borrow();
let mut comment = None;
let initial_value = if let Some(initial_expr) = &decl.init {
if matches!(initial_expr.kind, ExprKind::Lit(_)) {
if is_constant(initial_expr) {
Some(Box::new(translate_expr(initial_expr)?))
} else {
let builder = ExprBuilder::new(Span::dummy_span());
Expand Down Expand Up @@ -418,3 +418,12 @@ fn extract_embed(expr: &Expr) -> Option<Expr> {
}
None
}

/// Whether an expression is a constant value and can be evaluated without any
/// other variables.
///
/// Right now, we just check directly that it is a literal. In the future, one
/// could allow more complex (constant) expressions as well.
fn is_constant(expr: &Expr) -> bool {
matches!(expr.kind, ExprKind::Lit(_))
}
10 changes: 4 additions & 6 deletions src/mc/opsem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::{
tyctx::TyCtx,
};

use super::{specs::SpecAutomaton, translate_expr, translate_ident, JaniConversionError};
use super::{
is_constant, specs::SpecAutomaton, translate_expr, translate_ident, JaniConversionError,
};

/// Intermediate structure to build the JANI automaton for pGCL semantics with
/// expected rewards.
Expand Down Expand Up @@ -86,7 +88,7 @@ fn translate_stmt(
StmtKind::Var(decl_ref) => {
let decl = decl_ref.borrow();
match &decl.init {
Some(init) if !is_pure(init) => {
Some(init) if !is_constant(init) => {
translate_assign(automaton, span, translate_ident(decl.name), init, next)
}
Some(_) => Ok(next),
Expand Down Expand Up @@ -339,10 +341,6 @@ pub fn translate_block(
translate_stmts(automaton, &block.node, next)
}

fn is_pure(expr: &Expr) -> bool {
!matches!(expr.kind, ExprKind::Call(_, _))
}

fn translate_assign(
automaton: &mut OpAutomaton,
span: Span,
Expand Down

0 comments on commit ff82939

Please sign in to comment.