Skip to content

Commit

Permalink
rescursion -> loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hkiel authored and OpenModelica-Hudson committed Feb 26, 2016
1 parent a3605ed commit e5a26e4
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 236 deletions.
4 changes: 2 additions & 2 deletions Compiler/FFrontEnd/FNode.mo
Expand Up @@ -2792,7 +2792,7 @@ algorithm
str = r(a);
then
str;
case (NONE(),_) then "";
else "";
end match;
end getOptionStr;

Expand Down Expand Up @@ -2850,8 +2850,8 @@ protected function getHeight "Retrieve the height of a node"
output Integer height;
algorithm
height := match (bt)
case(NONE()) then 0;
case(SOME(FCore.CAVLTREENODE(height = height))) then height;
else 0;
end match;
end getHeight;

Expand Down
52 changes: 20 additions & 32 deletions Compiler/FrontEnd/NFSCodeEnv.mo
Expand Up @@ -2040,38 +2040,26 @@ public function avlTreeGet
output AvlValue outValue;
protected
AvlKey rkey;
algorithm
AVLTREENODE(value = SOME(AVLTREEVALUE(key = rkey))) := inAvlTree;
outValue := avlTreeGet2(inAvlTree, stringCompare(inKey, rkey), inKey);
Integer sc;
AvlTree tree = inAvlTree;
algorithm
while true loop
AVLTREENODE(value = SOME(AVLTREEVALUE(key = rkey))) := tree;
sc := stringCompare(inKey, rkey);
if sc == 0 then
// Found match.
AVLTREENODE(value = SOME(AVLTREEVALUE(value = outValue))) := tree;
return;
elseif sc > 0 then
// Search to the right.
AVLTREENODE(right = SOME(tree)) := tree;
else
// Search to the left.
AVLTREENODE(left = SOME(tree)) := tree;
end if;
end while;
end avlTreeGet;

protected function avlTreeGet2
"Helper function to avlTreeGet."
input AvlTree inAvlTree;
input Integer inKeyComp;
input AvlKey inKey;
output AvlValue outValue;
algorithm
outValue := match(inAvlTree, inKeyComp, inKey)
local
AvlKey key;
AvlValue rval;
AvlTree left, right;

// Found match.
case (AVLTREENODE(value = SOME(AVLTREEVALUE(value = rval))), 0, _)
then rval;

// Search to the right.
case (AVLTREENODE(right = SOME(right)), 1, key)
then avlTreeGet(right, key);

// Search to the left.
case (AVLTREENODE(left = SOME(left)), -1, key)
then avlTreeGet(left, key);
end match;
end avlTreeGet2;

public function avlTreeReplace
"Replaces the value of an already existing node in the tree with a new value."
input AvlTree inAvlTree;
Expand Down Expand Up @@ -2143,8 +2131,8 @@ protected function createEmptyAvlIfNone
output AvlTree outT;
algorithm
outT := match(t)
case (NONE()) then avlTreeNew();
case (SOME(outT)) then outT;
else avlTreeNew();
end match;
end createEmptyAvlIfNone;

Expand Down Expand Up @@ -2350,8 +2338,8 @@ protected function getHeight
output Integer height;
algorithm
height := match(bt)
case(NONE()) then 0;
case(SOME(AVLTREENODE(height = height))) then height;
else 0;
end match;
end getHeight;

Expand Down
102 changes: 35 additions & 67 deletions Compiler/Script/Interactive.mo
Expand Up @@ -562,26 +562,14 @@ protected function evaluateForStmt
input list<Absyn.AlgorithmItem> algItemList;
input GlobalScript.SymbolTable inSymbolTable;
output GlobalScript.SymbolTable outSymbolTable;
protected GlobalScript.SymbolTable st = inSymbolTable;
algorithm
outSymbolTable:=
match (iter,valList,algItemList, inSymbolTable)
local
Values.Value val;
list<Values.Value> vallst;
list<Absyn.AlgorithmItem> algItems;
GlobalScript.SymbolTable st1,st2,st3,st4,st5;
case (_, val::vallst, algItems, st1)
equation
st2 = GlobalScriptUtil.appendVarToSymboltable(iter, val, Types.typeOfValue(val), st1);
st3 = evaluateAlgStmtLst(algItems, st2);
st4 = GlobalScriptUtil.deleteVarFromSymboltable(iter, st3);
st5 = evaluateForStmt(iter, vallst, algItems, st4);
then
st5;
case (_, {}, _, st1)
then
st1;
end match;
for val in valList loop
st := GlobalScriptUtil.appendVarToSymboltable(iter, val, Types.typeOfValue(val), st);
st := evaluateAlgStmtLst(algItemList, st);
st := GlobalScriptUtil.deleteVarFromSymboltable(iter, st);
end for;
outSymbolTable := st;
end evaluateForStmt;

protected function evaluateForStmtRangeOpt
Expand Down Expand Up @@ -745,21 +733,13 @@ protected function evaluateAlgStmtLst
input list<Absyn.AlgorithmItem> inAbsynAlgorithmItemLst;
input GlobalScript.SymbolTable inSymbolTable;
output GlobalScript.SymbolTable outSymbolTable;
protected
GlobalScript.SymbolTable st = inSymbolTable;
algorithm
outSymbolTable:=
match (inAbsynAlgorithmItemLst,inSymbolTable)
local
GlobalScript.SymbolTable st,st_1,st_2;
Absyn.AlgorithmItem algitem;
list<Absyn.AlgorithmItem> algrest;
case ({},st) then st;
case ((algitem :: algrest),st)
equation
(_,st_1) = evaluateAlgStmt(algitem, st);
st_2 = evaluateAlgStmtLst(algrest, st_1);
then
st_2;
end match;
for algitem in inAbsynAlgorithmItemLst loop
(_,st) := evaluateAlgStmt(algitem, st);
end for;
outSymbolTable := st;
end evaluateAlgStmtLst;

protected function evaluateExpr
Expand Down Expand Up @@ -914,26 +894,19 @@ public function getTypeOfVariable
input Absyn.Ident inIdent;
input list<GlobalScript.Variable> inVariableLst;
output DAE.Type outType;
protected
String id;
DAE.Type tp;
algorithm
outType := matchcontinue (inIdent,inVariableLst)
local
String id,varid;
DAE.Type tp;
list<GlobalScript.Variable> rest;

case (_,{}) then fail();
case (varid,(GlobalScript.IVAR(varIdent = id,type_ = tp) :: _))
equation
true = stringEq(varid, id);
then
tp;
case (varid,(GlobalScript.IVAR(varIdent = id) :: rest))
equation
false = stringEq(varid, id);
tp = getTypeOfVariable(varid, rest);
then
tp;
end matchcontinue;
for var in inVariableLst loop
GlobalScript.IVAR(varIdent = id,type_ = tp) := var;
if stringEq(inIdent, id) then
outType := tp;
return;
end if;
end for;
// did not find a type
fail();
end getTypeOfVariable;

protected function getApiFunctionNameInfo
Expand Down Expand Up @@ -2792,21 +2765,16 @@ protected function renameComponentInIterators
input Absyn.ComponentRef newComp;
output Absyn.ForIterators iteratorsRenamed;
algorithm
iteratorsRenamed := match(iterators, oldComp, newComp)
local
Absyn.ForIterators rest, restNew;
Absyn.Exp exp, expNew; String i;
case ({}, _, _) then {};
case (Absyn.ITERATOR(i, NONE(), SOME(exp))::rest, _, _)
equation
expNew = renameComponentInExp(exp, oldComp, newComp);
restNew = renameComponentInIterators(rest, oldComp, newComp);
then Absyn.ITERATOR(i, NONE(), SOME(expNew))::restNew;
case (Absyn.ITERATOR(i, NONE(), NONE())::rest, _, _)
equation
restNew = renameComponentInIterators(rest, oldComp, newComp);
then Absyn.ITERATOR(i, NONE(), NONE())::restNew;
end match;
iteratorsRenamed := list(match (it)
local
Absyn.Exp exp; String i;
case (Absyn.ITERATOR(i, NONE(), SOME(exp)))
equation
exp = renameComponentInExp(exp, oldComp, newComp);
then Absyn.ITERATOR(i, NONE(), SOME(exp));
case (Absyn.ITERATOR(i, NONE(), NONE()))
then Absyn.ITERATOR(i, NONE(), NONE());
end match for it in iterators);
end renameComponentInIterators;

protected function renameComponentInNamedArgs
Expand Down
18 changes: 9 additions & 9 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -514,10 +514,10 @@ template functionPrototype(String fname, list<Variable> fargs, list<Variable> ou
else
(fargs |> var => ", " + funArgDefinition(var) )
let outarg = (match outVars
case {} then "void"
case var::_ then (match var
case VARIABLE(__) then if boxed then varTypeBoxed(var) else varType(var)
case FUNCTION_PTR(__) then "modelica_fnptr"))
case VARIABLE(__) then if boxed then varTypeBoxed(var) else varType(var)
case FUNCTION_PTR(__) then "modelica_fnptr")
else "void")
let boxPtrStr = if boxed then "boxptr" else "omc"
if outVars then
let outargs = List.rest(outVars) |> var => ", " + (match var
Expand Down Expand Up @@ -990,8 +990,8 @@ case FUNCTION(__) then
'/* Free GPU/OpenCL CPU memory */<%\n%><%varFrees%>'%>
<%freeConstructedExternalObjects%>
<%match outVars
case {} then 'return;'
case v::_ then 'return <%funArgName(v)%>;'
else 'return;'
%>
}
<% if inFunc then generateInFunc(fname,functionArguments,outVars) %>
Expand All @@ -1014,7 +1014,7 @@ template generateInFunc(Text fname, list<Variable> functionArguments, list<Varia
case v::_ then '<%funArgName(v)%> = '
%>omc_<%fname%>(threadData<%functionArguments |> var => (", " + funArgName(var) )%><%List.restOrEmpty(outVars) |> var => (", &" + funArgName(var) )%>);
MMC_CATCH_TOP(return 1)
<% match outVars case {} then "write_noretcall(outVar);" case first::_ then writeOutVar(first) %>
<% match outVars case first::_ then writeOutVar(first) else "write_noretcall(outVar);" %>
<% List.restOrEmpty(outVars) |> var => writeOutVar(var) ;separator="\n"; empty %>
fflush(NULL);
return 0;
Expand Down Expand Up @@ -1158,8 +1158,8 @@ case PARALLEL_FUNCTION(__) then
<%outVarAssign%>
<%match outVars
case {} then 'return;'
case v::_ then 'return <%funArgName(v)%>;'
else 'return;'
%>
}
>>
Expand Down Expand Up @@ -1229,8 +1229,8 @@ case KERNEL_FUNCTION(__) then
// return
<%match outVars
case {} then 'return;'
case var::_ then 'return <%funArgName(var)%>;'
else 'return;'
%>
}

Expand Down Expand Up @@ -1317,8 +1317,8 @@ case efn as EXTERNAL_FUNCTION(__) then
<%callPart%>
<%outVarAssign%>
<%match outVars
case {} then 'return;'
case v::_ then 'return <%funArgName(v)%>;'
else 'return;'
%>
}
>>
Expand Down Expand Up @@ -4317,13 +4317,13 @@ template daeExpListToCons(list<Exp> listItems, Context context, Text &preExp,
"Helper to daeExpList."
::=
match listItems
case {} then "MMC_REFSTRUCTLIT(mmc_nil)"
case e :: rest then
let expPart = daeExp(e, context, &preExp, &varDecls, &auxFunction)
let restList = daeExpListToCons(rest, context, &preExp, &varDecls, &auxFunction)
<<
mmc_mk_cons(<%expPart%>, <%restList%>)
>>
else "MMC_REFSTRUCTLIT(mmc_nil)"
end daeExpListToCons;


Expand Down

0 comments on commit e5a26e4

Please sign in to comment.