Skip to content

Commit

Permalink
- More match, less continue
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7579 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 29, 2010
1 parent 1f6ffe5 commit c6e6d25
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 92 deletions.
10 changes: 5 additions & 5 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -2277,7 +2277,7 @@ algorithm
case (((eqn as BackendDAE.ARRAY_EQUATION(index = indx,crefOrDerCref = expl)) :: rest)) /* array equation */
equation
// fails if not all call results are true
Util.listMapAllValue(expl, isAlgebraic, true);
true = Util.listMapAllValue(expl, isAlgebraic, true);
(resAlgEqn,resDiffEqn,resArrayEqns) = extractAlgebraicAndDifferentialEqn(rest);
then
(resAlgEqn,resDiffEqn,(eqn :: resArrayEqns));
Expand Down Expand Up @@ -3034,9 +3034,9 @@ algorithm
zcl = listAppend(zcl1,zcl2);
then
zcl;
case (_,_,_,_,_)
else
equation
print("BackendDAECreate.traverseAlgStmtsFor failed \n");
Error.addMessage(Error.INTERNAL_ERROR, {"BackendDAECreate.traverseAlgStmtsFor failed\n"});
then
fail();
end matchcontinue;
Expand Down Expand Up @@ -3173,9 +3173,9 @@ algorithm

case ({},{},trueCls,elseCls,nextInd) then ({},nextInd,listAppend(trueCls,elseCls));

case (_,_,_,_,_)
else
equation
print("- BackendDAECreate.mergeClauses: Error in mergeClauses.\n");
Error.addMessage(Error.INTERNAL_ERROR, {"BackendDAECreate.mergeClauses: Error in mergeClauses."});
then fail();
end matchcontinue;
end mergeClauses;
Expand Down
12 changes: 3 additions & 9 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -2233,9 +2233,7 @@ algorithm
crs = Expression.extractCrefsFromExp(e2);
(crVars,_) = Util.listMap12(crs,BackendVariable.getVar,vars);
// fails if not all mapped calls return true
Util.listMapAllValue(Util.listFlatten(crVars),BackendVariable.isStateVar,true);
then
true;
then Util.listMapAllValue(Util.listFlatten(crVars),BackendVariable.isStateVar,true);

case(cr,BackendDAE.EQUATION(exp = e2, scalar = DAE.CREF(cr2,_)),vars)
equation
Expand All @@ -2244,13 +2242,9 @@ algorithm
crs = Expression.extractCrefsFromExp(e2);
(crVars,_) = Util.listMap12(crs,BackendVariable.getVar,vars);
// fails if not all mapped calls return true
Util.listMapAllValue(Util.listFlatten(crVars),BackendVariable.isStateVar,true);
then
true;
then Util.listMapAllValue(Util.listFlatten(crVars),BackendVariable.isStateVar,true);

case(cr,eqn,vars)
then
false;
else false;
end matchcontinue;
end isStateConstraintEquation;

Expand Down
10 changes: 4 additions & 6 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -5941,20 +5941,18 @@ algorithm
list<SimVar> stringParamVars;
list<SimVar> extObjVars;
/* no initial equations so nothing to do */
case (_, {})
then simvarsIn;
case (_, {}) then simvarsIn;
case (SIMVARS(stateVars, derivativeVars, algVars, intAlgVars, boolAlgVars, inputVars,
outputVars, paramVars, intParamVars, boolParamVars, stringAlgVars, stringParamVars,
extObjVars), initCrefs)
equation
Util.listMapAllValue(stateVars, simvarFixed,true);
true = Util.listMapAllValue(stateVars, simvarFixed,true);
stateVars = Util.listMap1(stateVars, nonFixifyIfHasInit, initCrefs);
then SIMVARS(stateVars, derivativeVars, algVars, intAlgVars, boolAlgVars, inputVars,
outputVars, paramVars, intParamVars, boolParamVars, stringAlgVars, stringParamVars,
extObjVars);
/* not all were fixed so nothing to do */
case (_, _)
then simvarsIn;
/* not all were fixed so nothing to do */
else simvarsIn;
end matchcontinue;
end fixInitialThing;

Expand Down
6 changes: 2 additions & 4 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -1045,10 +1045,8 @@ algorithm
((subs as (_ :: _))) = crefLastSubs(cr);
exps = Util.listMap(subs, Expression.subscriptExp);
// fails if any mapped functions returns false
Util.listMapAllValue(exps, Expression.isOne, true);
then
true;
case (_) then false;
then Util.listMapAllValue(exps, Expression.isOne, true);
else false;
end matchcontinue;
end crefIsFirstArrayElt;

Expand Down
66 changes: 17 additions & 49 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4397,7 +4397,7 @@ public function isZero
input DAE.Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inExp)
outBoolean := match (inExp)
local
Integer ival;
Real rzero,rval;
Expand All @@ -4411,32 +4411,22 @@ algorithm

case (DAE.ICONST(integer = ival)) then intEq(ival,0);
case (DAE.RCONST(real = rval)) then realEq(rval,0.0);
case (DAE.CAST(ty = t,exp = e))
equation
res = isZero(e) "Casting to zero is still zero" ;
then
res;
case (DAE.CAST(ty = t,exp = e)) then isZero(e);

case(DAE.UNARY(DAE.UMINUS(_),e)) then isZero(e);
case(DAE.ARRAY(array = ae))
equation
Util.listMapAllValue(ae,isZero,true);
then
true;
case(DAE.ARRAY(array = ae)) then Util.listMapAllValue(ae,isZero,true);

case (DAE.MATRIX(scalar = scalar))
equation
aelstlst = Util.listFlatten(scalar);
ae = Util.listMap(aelstlst,Util.tuple21);
Util.listMapAllValue(ae,isZero,true);
then
true;
then Util.listMapAllValue(ae,isZero,true);

case(DAE.UNARY(DAE.UMINUS_ARR(_),e)) then isZero(e);

case (_) then false;
else false;

end matchcontinue;
end match;
end isZero;

public function isConst
Expand Down Expand Up @@ -4509,73 +4499,51 @@ algorithm
then
true;

case (DAE.ARRAY(array = ae))
equation
Util.listMapAllValue(ae,isConst,true);
then
true;
case (DAE.ARRAY(array = ae)) then Util.listMapAllValue(ae,isConst,true);

case (DAE.MATRIX(scalar = scalar))
equation
aelstlst = Util.listFlatten(scalar);
ae = Util.listMap(aelstlst,Util.tuple21);
Util.listMapAllValue(ae,isConst,true);
then
true;
then Util.listMapAllValue(ae,isConst,true);

case (DAE.RANGE(exp=e,expOption=NONE(),range=e1))
equation
true = isConst(e);
true = isConst(e1);
then
true;
then isConst(e1);

case (DAE.RANGE(exp=e,expOption=SOME(e1),range=e2))
equation
true = isConst(e);
true = isConst(e1);
true = isConst(e2);
then
true;
then isConst(e2);

case (DAE.PARTEVALFUNCTION(expList = ae))
equation
Util.listMapAllValue(ae,isConst,true);
then
true;
then Util.listMapAllValue(ae,isConst,true);

case (DAE.TUPLE(PR = ae))
equation
Util.listMapAllValue(ae,isConst,true);
then
true;
then Util.listMapAllValue(ae,isConst,true);

case (DAE.ASUB(exp=e,sub=ae))
equation
true = isConst(e);
Util.listMapAllValue(ae,isConst,true);
then
true;
then Util.listMapAllValue(ae,isConst,true);

case (DAE.SIZE(exp=e,sz=NONE())) then isConst(e);

case (DAE.SIZE(exp=e,sz=SOME(e1)))
equation
true = isConst(e);
true = isConst(e1);
then
true;
then isConst(e1);

case (DAE.END()) then true;

case (DAE.REDUCTION(expr=e1,range=e2))
equation
true = isConst(e1);
true = isConst(e2);
then
true;
then isConst(e2);

case (_) then false;
else false;

end matchcontinue;
end isConst;
Expand Down Expand Up @@ -5356,7 +5324,7 @@ algorithm
/* For several terms, all must be positive or zero and at least one must be > 0 */
case(e) equation
(terms as _::_) = terms(e);
Util.listMapAllValue(terms,expIsPositiveOrZero,true);
true = Util.listMapAllValue(terms,expIsPositiveOrZero,true);
_::_ = Util.listSelect(terms,expIsPositive);
then
false;
Expand Down
19 changes: 8 additions & 11 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -500,38 +500,35 @@ public function isRecordWithOnlyReals "Returns true if type is a record only con
input Type tp;
output Boolean b;
algorithm
b := matchcontinue(tp)
b := match (tp)
local
list<Boolean> bLst;
list<Var> varLst;

case((DAE.T_COMPLEX(ClassInf.RECORD(_),varLst,_,_),_))
equation
Util.listMapAllValue(Util.listMap(varLst,getVarType),isReal,true);
then
true;
then Util.listMapAllValue(Util.listMap(varLst,getVarType),isReal,true);

// otherwise false
case(_) then false;
end matchcontinue;
else false;
end match;
end isRecordWithOnlyReals;

public function getVarType "Return the Type of a Var"
input Var v;
output Type tp;
algorithm
tp := matchcontinue(v)
tp := match (v)
case(DAE.TYPES_VAR(type_ = tp)) then tp;
end matchcontinue;
end match;
end getVarType;

public function getVarName "Return the name of a Var"
input Var v;
output String name;
algorithm
name := matchcontinue(v)
name := match (v)
case(DAE.TYPES_VAR(name = name)) then name;
end matchcontinue;
end match;
end getVarName;

public function isReal "Returns true if type is Real"
Expand Down
40 changes: 32 additions & 8 deletions Compiler/Util/Util.mo
Expand Up @@ -7094,15 +7094,41 @@ algorithm
end match;
end listConsOption;

function listMapAllValue
public function listMapAllValue
"@author adrpo
applies a function to all elements in the lists and checks if all are the same
as a given value"
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
input list<TypeA> inLst;
input FuncTypeTypeVarToTypeVar fn;
input TypeB value;
input TypeB value;
output Boolean b;
partial function FuncTypeTypeVarToTypeVar
input TypeA inTypeA;
output TypeB outTypeB;
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
end FuncTypeTypeVarToTypeVar;
algorithm
b := matchcontinue (inLst, fn, value)
case (inLst, fn, value)
equation
listMapAllValue2(inLst, fn, value);
then true;
else false;
end matchcontinue;
end listMapAllValue;

protected function listMapAllValue2
"@author adrpo
applies a function to all elements in the lists and checks if all are the same
as a given value"
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
input list<TypeA> inLst;
input FuncTypeTypeVarToTypeVar fn;
input TypeB value;
partial function FuncTypeTypeVarToTypeVar
input TypeA inTypeA;
output TypeB outTypeB;
Expand All @@ -7117,20 +7143,18 @@ algorithm
list<TypeA> rest;
list<TypeB> l, result;


case ({}, _, _) then ();

case (hd::rest, fn, value)
equation
hdChanged = fn(hd);
equality(hdChanged = value);
listMapAllValue(rest, fn, value);
then
();
listMapAllValue2(rest, fn, value);
then ();
end matchcontinue;
end listMapAllValue;
end listMapAllValue2;

function listMap2AllValue
public function listMap2AllValue
"@author adrpo
checks that the mapped function returns the same given value, otherwise fails"
input list<Type_a> inTypeALst;
Expand Down

0 comments on commit c6e6d25

Please sign in to comment.