Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion Compiler/NFFrontEnd/NFSimplifyExp.mo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ import Operator = NFOperator;
import Type = NFType;
import NFCall.Call;

protected

import Dimension = NFDimension;
import ExpressionSimplify;

public

function simplifyExp
input output Expression exp;
algorithm
Expand Down Expand Up @@ -274,6 +281,9 @@ algorithm
case Expression.IF(condition=Expression.BOOLEAN(value=b1))
then if b1 then exp.trueBranch else exp.falseBranch;

case Expression.RANGE()
then simplifyRange(exp.start, exp.step, exp.stop, exp.ty);

case Expression.CAST()
then simplifyCast(exp.ty, exp.exp);

Expand All @@ -300,11 +310,61 @@ algorithm

else
algorithm
Error.assertion(false, getInstanceName() + " failed on " + Expression.toString(exp), sourceInfo());
Error.assertion(false, getInstanceName() + " failed on " + Expression.toString(exp) + " to type: " + Type.toString(ty), sourceInfo());
then
fail();
end match;
end simplifyCast;

function simplifyRange
input Expression start;
input Option<Expression> optStep;
input Expression stop;
input Type ty;
output Expression exp;
protected
Expression step;
Integer i1, i2, i3;
Real r1, r2, r3;
Boolean b1, b2;
list<Expression> exps;
list<String> literals;
Type enumType;
algorithm
step := match (optStep, ty)
case (SOME(step),_) then step;
case (_,Type.ARRAY(elementType=Type.INTEGER())) then Expression.INTEGER(1);
case (_,Type.ARRAY(elementType=Type.REAL())) then Expression.REAL(1.0);
case (_,Type.ARRAY(elementType=Type.BOOLEAN())) then Expression.INTEGER(1); // dummy
case (_,Type.ARRAY(elementType=Type.ENUMERATION())) then Expression.INTEGER(1); // dummy
else
algorithm
Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo());
then fail();
end match;
exp := match (start, step, stop)
case (Expression.INTEGER(i1),Expression.INTEGER(i2),Expression.INTEGER(i3))
algorithm
exps := list(Expression.INTEGER(i) for i in ExpressionSimplify.simplifyRange(i1, i2, i3));
then Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(listLength(exps))}), exps);
case (Expression.REAL(r1),Expression.REAL(r2),Expression.REAL(r3))
algorithm
exps := list(Expression.REAL(r) for r in ExpressionSimplify.simplifyRangeReal(r1, r2, r3));
then Expression.ARRAY(Type.ARRAY(Type.REAL(), {Dimension.fromInteger(listLength(exps))}), exps);
case (Expression.BOOLEAN(b1),_,Expression.BOOLEAN(b2))
algorithm
exps := list(Expression.BOOLEAN(b) for b in ExpressionSimplify.simplifyRangeBool(b1, b2));
then Expression.ARRAY(Type.ARRAY(Type.BOOLEAN(), {Dimension.fromInteger(listLength(exps))}), exps);
case (Expression.ENUM_LITERAL(ty=enumType as Type.ENUMERATION(literals=literals), index=i1),_,Expression.ENUM_LITERAL(index=i2))
algorithm
exps := list(Expression.ENUM_LITERAL(enumType, listGet(literals, i), i) for i in i1:i2);
then Expression.ARRAY(Type.ARRAY(enumType, {Dimension.fromInteger(listLength(exps))}), exps);
else
algorithm
Error.assertion(false, getInstanceName() + " failed to type: " + Type.toString(ty), sourceInfo());
then fail();
end match;
end simplifyRange;

annotation(__OpenModelica_Interface="frontend");
end NFSimplifyExp;