Skip to content

Commit

Permalink
Some tail recursion
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17584 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 7, 2013
1 parent b70897a commit 4b41ce4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 101 deletions.
54 changes: 24 additions & 30 deletions Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2781,29 +2781,16 @@ algorithm
end match;
end optPathString;

public function pathString2 "function:
Helper function to pathString"
input Path inPath;
input String inString;
public function pathString2 "Tail-recursive version, with string builder (stringDelimitList is optimised)"
input Path path;
input String delimiter;
output String outString;
algorithm
outString:=
match (inPath,inString)
local
Ident s,ns,s1,ss,str;
Path n;
case (IDENT(name = s),_) then s;
case (QUALIFIED(name = s,path = n),str)
equation
ns = pathString2(n, str);
s1 = stringAppend(s, str);
ss = stringAppend(s1, ns);
then
ss;
case(FULLYQUALIFIED(path=n),str)
equation
ss = "." +& pathString2(n,str);
then ss;
outString := match (path,delimiter)
case (FULLYQUALIFIED(path=_),_)
then "." +& stringDelimitList(pathToStringList(path),delimiter);
else
then stringDelimitList(pathToStringList(path),delimiter);
end match;
end pathString2;

Expand Down Expand Up @@ -3054,20 +3041,27 @@ end pathSuffixOfr;
public function pathToStringList
input Path path;
output list<String> outPaths;
algorithm outPaths := match(path)
algorithm
outPaths := listReverse(pathToStringListWork(path,{}));
end pathToStringList;

protected function pathToStringListWork
input Path path;
input list<String> acc;
output list<String> outPaths;
algorithm
outPaths := match(path,acc)
local
String n;
Path p;
list<String> strings;

case (IDENT(name = n)) then {n};
case (FULLYQUALIFIED(path = p)) then pathToStringList(p);
case (QUALIFIED(name = n,path = p))
equation
strings = pathToStringList(p);
then n::strings;
end match;
end pathToStringList;
case (IDENT(name = n),_) then n::acc;
case (FULLYQUALIFIED(path = p),_) then pathToStringListWork(p,acc);
case (QUALIFIED(name = n,path = p),_)
then pathToStringListWork(p,n::acc);
end match;
end pathToStringListWork;

public function pathReplaceFirstIdent "
Replaces the first part of a path with a replacement path:
Expand Down
18 changes: 14 additions & 4 deletions Compiler/FrontEnd/ComponentReference.mo
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ algorithm
equation
ns = stringAppendList({inPreString, s, inNameSeparator});
ss = crefToStr(ns,n,inNameSeparator);
then
ss;
then ss;
end match;
end crefToStr;

Expand Down Expand Up @@ -2892,10 +2891,21 @@ protected
DAE.ComponentRef first;
list<DAE.ComponentRef> rest;
algorithm
first :: rest := listReverse(inParts);
outCref := implode_tail(rest, first);
outCref := implode_reverse(listReverse(inParts));
end implode;

public function implode_reverse
"Constructs a cref from a reversed list of CREF_IDENTs."
input list<DAE.ComponentRef> inParts;
output DAE.ComponentRef outCref;
protected
DAE.ComponentRef first;
list<DAE.ComponentRef> rest;
algorithm
first :: rest := inParts;
outCref := implode_tail(rest, first);
end implode_reverse;

protected function implode_tail
input list<DAE.ComponentRef> inParts;
input DAE.ComponentRef inAccumCref;
Expand Down
8 changes: 4 additions & 4 deletions Compiler/FrontEnd/Env.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1626,21 +1626,21 @@ protected
Env rest;
Absyn.Path p;
algorithm
outPath := matchcontinue(inEnv)
outPath := match (inEnv)
case (FRAME(name = SOME(id)) :: rest)
equation
outPath = getEnvName2(rest, Absyn.IDENT(id));
then
outPath;
end matchcontinue;
end match;
end getEnvName;

public function getEnvName2
input Env inEnv;
input Absyn.Path inPath;
output Absyn.Path outPath;
algorithm
outPath := matchcontinue(inEnv, inPath)
outPath := match (inEnv, inPath)
local
Ident id;
Env rest, env;
Expand All @@ -1650,7 +1650,7 @@ algorithm
then getEnvName2(rest, Absyn.QUALIFIED(id, inPath));

else inPath;
end matchcontinue;
end match;
end getEnvName2;

public function getEnvPath "This function returns all partially instantiated parents as an Absyn.Path
Expand Down
63 changes: 38 additions & 25 deletions Compiler/FrontEnd/PrefixUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function prefixPath "Prefix a Path variable by adding the supplied
input Prefix.Prefix inPrefix;
output Absyn.Path outPath;
algorithm
outPath := matchcontinue (inPath,inPrefix)
outPath := match (inPath,inPrefix)
local
Absyn.Path p,p_1;
String s;
Expand All @@ -282,14 +282,12 @@ algorithm
case (p,Prefix.PREFIX(Prefix.PRE(prefix = s,next = Prefix.NOCOMPPRE()),cp))
equation
p_1 = Absyn.QUALIFIED(s,p);
then
p_1;
then p_1;
case (p,Prefix.PREFIX(Prefix.PRE(prefix = s,next = ss),cp))
equation
p_1 = prefixPath(Absyn.QUALIFIED(s,p), Prefix.PREFIX(ss,cp));
then
p_1;
end matchcontinue;
then p_1;
end match;
end prefixPath;

public function prefixToPath "Convert a Prefix to a Path"
Expand Down Expand Up @@ -471,27 +469,42 @@ protected function prefixSubscriptsInCref "help function to prefixToCrefOpt2, de
output Env.Cache outCache;
output DAE.ComponentRef outCr;
algorithm
(outCache,outCr) := match (inCache,inEnv,inIH,pre,inCr)
local
DAE.Ident id;
DAE.Type tp;
list<DAE.Subscript> subs;
Env.Cache cache;
Env.Env env;
DAE.ComponentRef cr;


case(cache,env,_,_,DAE.CREF_IDENT(id,tp,subs)) equation
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
then (cache,ComponentReference.makeCrefIdent(id,tp,subs));
case(cache,env,_,_,DAE.CREF_QUAL(id,tp,subs,cr)) equation
(cache,cr) = prefixSubscriptsInCref(cache,env,inIH,pre,cr);
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
then (cache,ComponentReference.makeCrefQual(id,tp,subs,cr));
case(cache,_,_,_,DAE.WILD()) then (cache,DAE.WILD());
end match;
(outCache,outCr) := prefixSubscriptsInCrefWork(inCache,inEnv,inIH,pre,inCr,{});
end prefixSubscriptsInCref;

protected function prefixSubscriptsInCrefWork "help function to prefixToCrefOpt2, deals with prefixing expressions in subscripts"
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix pre;
input DAE.ComponentRef inCr;
input list<DAE.ComponentRef> acc;
output Env.Cache outCache;
output DAE.ComponentRef outCr;
algorithm
(outCache,outCr) := match (inCache,inEnv,inIH,pre,inCr,acc)
local
DAE.Ident id;
DAE.Type tp;
list<DAE.Subscript> subs;
Env.Cache cache;
Env.Env env;
DAE.ComponentRef cr,crid;
case(cache,env,_,_,DAE.CREF_IDENT(id,tp,subs),_)
equation
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
cr = ComponentReference.makeCrefIdent(id,tp,subs);
then (cache,ComponentReference.implode(listReverse(cr::acc)));
case(cache,env,_,_,DAE.CREF_QUAL(id,tp,subs,cr),_)
equation
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
crid = ComponentReference.makeCrefIdent(id,tp,subs);
(cache,cr) = prefixSubscriptsInCrefWork(cache,env,inIH,pre,cr,crid::acc);
then (cache,cr);
case(cache,_,_,_,DAE.WILD(),_) then (cache,DAE.WILD());
end match;
end prefixSubscriptsInCrefWork;

protected function prefixSubscripts "help function to prefixSubscriptsInCref, adds prefix to subscripts"
input Env.Cache inCache;
input Env.Env inEnv;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Global/Global.mo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ encapsulated package Global
The Global package contains structures that are available globally."


constant Integer recursionDepthLimit = 1000;
constant Integer recursionDepthLimit = 256;
constant Integer maxFunctionFileLength = 50;

constant Integer instHashIndex = 0;
Expand Down

0 comments on commit 4b41ce4

Please sign in to comment.