Skip to content

Commit

Permalink
[NF] Improve subscripting in EvalFunction.
Browse files Browse the repository at this point in the history
- Implement handling of scalar subscripts on the lhs of assignments.

Belonging to [master]:
  - OpenModelica/OMCompiler#2496
  - OpenModelica/OpenModelica-testsuite#971
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jun 4, 2018
1 parent f430883 commit 9d7f5d5
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions Compiler/NFFrontEnd/NFEvalFunction.mo
Expand Up @@ -452,8 +452,7 @@ algorithm

case (Expression.SUBSCRIPTED_EXP(exp = Expression.MUTABLE(exp = var_ptr)), _)
algorithm
Error.assertion(false, getInstanceName() +
": missing handling of subscripted assignment", sourceInfo());
assignSubscriptedVariable(var_ptr, variable.subscripts, value);
then
();

Expand All @@ -467,6 +466,55 @@ algorithm
end match;
end assignVariable;

function assignSubscriptedVariable
input Mutable<Expression> variable;
input list<Expression> subscripts;
input Expression value;
protected
list<Expression> subs;
algorithm
subs := list(Ceval.evalExp(s) for s in subscripts);
Mutable.update(variable, assignArrayElement(Mutable.access(variable), subs, value));
end assignSubscriptedVariable;

function assignArrayElement
input Expression arrayExp;
input list<Expression> subscripts;
input Expression value;
output Expression result;
protected
Expression sub;
list<Expression> rest_subs;
Integer idx;
algorithm
result := match (arrayExp, subscripts)
case (Expression.ARRAY(), {sub}) guard Expression.isScalarLiteral(sub)
algorithm
idx := Expression.toInteger(sub);
arrayExp.elements := List.set(arrayExp.elements, idx, value);
then
arrayExp;

case (Expression.ARRAY(), sub :: rest_subs) guard Expression.isScalarLiteral(sub)
algorithm
idx := Expression.toInteger(sub);
arrayExp.elements := List.set(arrayExp.elements, idx,
assignArrayElement(listGet(arrayExp.elements, idx), rest_subs, value));
then
arrayExp;

else
algorithm
Error.assertion(false, getInstanceName() + ": unimplemented case for " +
Expression.toString(arrayExp) +
List.toString(subscripts, Expression.toString, "", "[", ", ", "]", false) + " = " +
Expression.toString(value), sourceInfo());
then
fail();

end match;
end assignArrayElement;

function assignExp
input Expression lhs;
input Expression rhs;
Expand Down

0 comments on commit 9d7f5d5

Please sign in to comment.