Skip to content

Commit

Permalink
[NB] fix pre() variable detection (#12417)
Browse files Browse the repository at this point in the history
- collect pre(not b) calls
 - report and fail on all pre calls that are not pre(b) or pre(not b). Future work needed here?
 - swap DetectStates and Events module so that pre() variables are correctly parsed before collecting the events
  • Loading branch information
kabdelhak committed May 14, 2024
1 parent 5ed0b9c commit 6fd3a50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public
(simplify, "simplify1"),
(Alias.main, "Alias"),
(simplify, "simplify2"), // TODO simplify in Alias only
(Events.main, "Events"),
(DetectStates.main, "Detect States")
(DetectStates.main, "Detect States"),
(Events.main, "Events")
};

mainModules := {
Expand Down
26 changes: 20 additions & 6 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBDetectStates.mo
Original file line number Diff line number Diff line change
Expand Up @@ -347,20 +347,34 @@ protected
algorithm
exp := match exp
local
list<Expression> args;
ComponentRef state_cref, pre_cref;
Pointer<Variable> state_var, pre_var;
Boolean negated;
Expression new_exp;

case Expression.CALL(call = Call.TYPED_CALL(fn = Function.FUNCTION(path = Absyn.IDENT(name = "pre")),
arguments = {Expression.CREF(cref = state_cref)}))
case Expression.CALL(call = Call.TYPED_CALL(fn = Function.FUNCTION(path = Absyn.IDENT(name = "pre")), arguments = args))
algorithm
state_var := BVariable.getVarPointer(state_cref);
(state_var, negated) := match args
// ToDo! General expressions inside pre call!
// ToDo! edge and change replacement!
case {Expression.CREF(cref = state_cref)} then (BVariable.getVarPointer(state_cref), false);
case {Expression.LUNARY(exp = Expression.CREF(cref = state_cref))} then (BVariable.getVarPointer(state_cref), true);
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of unexpected expression pre("
+ List.toString(args, Expression.toString, "", ", ", "") + ")."});
then fail();
end match;
pre_cref := getPreVar(state_cref, state_var, acc_previous, scalarized);
if not scalarized then
pre_cref := ComponentRef.setSubscriptsList(listReverse(ComponentRef.subscriptsAll(state_cref)), pre_cref);
end if;
then Expression.fromCref(pre_cref);
// ToDo! General expressions inside pre call!
// ToDo! edge and change replacement!
new_exp := Expression.fromCref(pre_cref);
if negated then
new_exp := Expression.logicNegate(new_exp);
end if;
then new_exp;

else exp;
end match;
end collectPreAndPrevious;
Expand Down

0 comments on commit 6fd3a50

Please sign in to comment.