Skip to content

Commit

Permalink
handle if expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrpo authored and OpenModelica-Hudson committed Dec 19, 2016
1 parent 8ac8faf commit ca147d7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Compiler/NFFrontEnd/NFTypeCheck.mo
Expand Up @@ -2302,5 +2302,51 @@ algorithm
end match;
end getBooleanRangeType;

function checkIfExpression
input DAE.Exp condExp;
input DAE.Type condType;
input DAE.Const condVar;
input DAE.Exp thenExp;
input DAE.Type thenType;
input DAE.Const thenVar;
input DAE.Exp elseExp;
input DAE.Type elseType;
input DAE.Const elseVar;
input SourceInfo info;
output DAE.Exp outExp;
output DAE.Type outType;
output DAE.Const outVar;
protected
DAE.Exp ec, e1, e2;
String s1, s2, s3, s4;
Boolean tyMatch;
algorithm
(ec, _, tyMatch) := Types.matchTypeNoFail(condExp, condType, DAE.T_BOOL_DEFAULT);

// if the condtion is not boolean that's bad :)
if not tyMatch then
s1 := ExpressionDump.printExpStr(condExp);
s2 := Types.unparseType(condType);
Error.addSourceMessageAndFail(Error.IF_CONDITION_TYPE_ERROR , {s1, s2}, info);
end if;

(e1, e2, outType, tyMatch) :=
Types.checkTypeCompat(thenExp, thenType, elseExp, elseType);

// if the types are not matching, print an error and fail.
if not tyMatch then
s1 := ExpressionDump.printExpStr(thenExp);
s2 := ExpressionDump.printExpStr(elseExp);
s3 := Types.unparseTypeNoAttr(thenType);
s4 := Types.unparseTypeNoAttr(elseType);
Error.addSourceMessageAndFail(Error.TYPE_MISMATCH_IF_EXP,
{"", s1, s3, s2, s4}, info);
end if;

outExp := DAE.IFEXP(ec, e1, e2);
outType := thenType;
outVar := Types.constAnd(thenVar, elseVar);
end checkIfExpression;

annotation(__OpenModelica_Interface="frontend");
end NFTypeCheck;
16 changes: 13 additions & 3 deletions Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -670,9 +670,9 @@ algorithm
(typedExp, ty, variability) := match untypedExp
local
DAE.ComponentRef cref;
DAE.Exp e1, e2;
DAE.Type ty1, ty2;
DAE.Const var1, var2;
DAE.Exp e1, e2, e3;
DAE.Type ty1, ty2, ty3;
DAE.Const var1, var2, var3;
DAE.Operator op;

case Absyn.Exp.INTEGER()
Expand Down Expand Up @@ -748,6 +748,16 @@ algorithm
then
(typedExp, ty, Types.constAnd(var1, var2));

case Absyn.Exp.IFEXP()
algorithm
(e1, ty1, var1) := typeExp(untypedExp.ifExp, scope, info);
(e2, ty2, var2) := typeExp(untypedExp.trueBranch, scope, info);
(e3, ty3, var3) := typeExp(untypedExp.elseBranch, scope, info);

(typedExp, ty, variability) := TypeCheck.checkIfExpression(e1, ty1, var1, e2, ty2, var2, e3, ty3, var3, info);
then
(typedExp, ty, variability);

case Absyn.Exp.CALL()
algorithm
(typedExp, ty, variability) := Func.typeFunctionCall(untypedExp.function_, untypedExp.functionArgs, scope, info);
Expand Down

0 comments on commit ca147d7

Please sign in to comment.