Skip to content

Commit 4b41ce4

Browse files
committed
Some tail recursion
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17584 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent b70897a commit 4b41ce4

File tree

6 files changed

+111
-101
lines changed

6 files changed

+111
-101
lines changed

Compiler/FrontEnd/Absyn.mo

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,29 +2781,16 @@ algorithm
27812781
end match;
27822782
end optPathString;
27832783

2784-
public function pathString2 "function:
2785-
Helper function to pathString"
2786-
input Path inPath;
2787-
input String inString;
2784+
public function pathString2 "Tail-recursive version, with string builder (stringDelimitList is optimised)"
2785+
input Path path;
2786+
input String delimiter;
27882787
output String outString;
27892788
algorithm
2790-
outString:=
2791-
match (inPath,inString)
2792-
local
2793-
Ident s,ns,s1,ss,str;
2794-
Path n;
2795-
case (IDENT(name = s),_) then s;
2796-
case (QUALIFIED(name = s,path = n),str)
2797-
equation
2798-
ns = pathString2(n, str);
2799-
s1 = stringAppend(s, str);
2800-
ss = stringAppend(s1, ns);
2801-
then
2802-
ss;
2803-
case(FULLYQUALIFIED(path=n),str)
2804-
equation
2805-
ss = "." +& pathString2(n,str);
2806-
then ss;
2789+
outString := match (path,delimiter)
2790+
case (FULLYQUALIFIED(path=_),_)
2791+
then "." +& stringDelimitList(pathToStringList(path),delimiter);
2792+
else
2793+
then stringDelimitList(pathToStringList(path),delimiter);
28072794
end match;
28082795
end pathString2;
28092796

@@ -3054,20 +3041,27 @@ end pathSuffixOfr;
30543041
public function pathToStringList
30553042
input Path path;
30563043
output list<String> outPaths;
3057-
algorithm outPaths := match(path)
3044+
algorithm
3045+
outPaths := listReverse(pathToStringListWork(path,{}));
3046+
end pathToStringList;
3047+
3048+
protected function pathToStringListWork
3049+
input Path path;
3050+
input list<String> acc;
3051+
output list<String> outPaths;
3052+
algorithm
3053+
outPaths := match(path,acc)
30583054
local
30593055
String n;
30603056
Path p;
30613057
list<String> strings;
30623058

3063-
case (IDENT(name = n)) then {n};
3064-
case (FULLYQUALIFIED(path = p)) then pathToStringList(p);
3065-
case (QUALIFIED(name = n,path = p))
3066-
equation
3067-
strings = pathToStringList(p);
3068-
then n::strings;
3069-
end match;
3070-
end pathToStringList;
3059+
case (IDENT(name = n),_) then n::acc;
3060+
case (FULLYQUALIFIED(path = p),_) then pathToStringListWork(p,acc);
3061+
case (QUALIFIED(name = n,path = p),_)
3062+
then pathToStringListWork(p,n::acc);
3063+
end match;
3064+
end pathToStringListWork;
30713065

30723066
public function pathReplaceFirstIdent "
30733067
Replaces the first part of a path with a replacement path:

Compiler/FrontEnd/ComponentReference.mo

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ algorithm
491491
equation
492492
ns = stringAppendList({inPreString, s, inNameSeparator});
493493
ss = crefToStr(ns,n,inNameSeparator);
494-
then
495-
ss;
494+
then ss;
496495
end match;
497496
end crefToStr;
498497

@@ -2892,10 +2891,21 @@ protected
28922891
DAE.ComponentRef first;
28932892
list<DAE.ComponentRef> rest;
28942893
algorithm
2895-
first :: rest := listReverse(inParts);
2896-
outCref := implode_tail(rest, first);
2894+
outCref := implode_reverse(listReverse(inParts));
28972895
end implode;
28982896

2897+
public function implode_reverse
2898+
"Constructs a cref from a reversed list of CREF_IDENTs."
2899+
input list<DAE.ComponentRef> inParts;
2900+
output DAE.ComponentRef outCref;
2901+
protected
2902+
DAE.ComponentRef first;
2903+
list<DAE.ComponentRef> rest;
2904+
algorithm
2905+
first :: rest := inParts;
2906+
outCref := implode_tail(rest, first);
2907+
end implode_reverse;
2908+
28992909
protected function implode_tail
29002910
input list<DAE.ComponentRef> inParts;
29012911
input DAE.ComponentRef inAccumCref;

Compiler/FrontEnd/Env.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,21 +1626,21 @@ protected
16261626
Env rest;
16271627
Absyn.Path p;
16281628
algorithm
1629-
outPath := matchcontinue(inEnv)
1629+
outPath := match (inEnv)
16301630
case (FRAME(name = SOME(id)) :: rest)
16311631
equation
16321632
outPath = getEnvName2(rest, Absyn.IDENT(id));
16331633
then
16341634
outPath;
1635-
end matchcontinue;
1635+
end match;
16361636
end getEnvName;
16371637

16381638
public function getEnvName2
16391639
input Env inEnv;
16401640
input Absyn.Path inPath;
16411641
output Absyn.Path outPath;
16421642
algorithm
1643-
outPath := matchcontinue(inEnv, inPath)
1643+
outPath := match (inEnv, inPath)
16441644
local
16451645
Ident id;
16461646
Env rest, env;
@@ -1650,7 +1650,7 @@ algorithm
16501650
then getEnvName2(rest, Absyn.QUALIFIED(id, inPath));
16511651

16521652
else inPath;
1653-
end matchcontinue;
1653+
end match;
16541654
end getEnvName2;
16551655

16561656
public function getEnvPath "This function returns all partially instantiated parents as an Absyn.Path

Compiler/FrontEnd/PrefixUtil.mo

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function prefixPath "Prefix a Path variable by adding the supplied
271271
input Prefix.Prefix inPrefix;
272272
output Absyn.Path outPath;
273273
algorithm
274-
outPath := matchcontinue (inPath,inPrefix)
274+
outPath := match (inPath,inPrefix)
275275
local
276276
Absyn.Path p,p_1;
277277
String s;
@@ -282,14 +282,12 @@ algorithm
282282
case (p,Prefix.PREFIX(Prefix.PRE(prefix = s,next = Prefix.NOCOMPPRE()),cp))
283283
equation
284284
p_1 = Absyn.QUALIFIED(s,p);
285-
then
286-
p_1;
285+
then p_1;
287286
case (p,Prefix.PREFIX(Prefix.PRE(prefix = s,next = ss),cp))
288287
equation
289288
p_1 = prefixPath(Absyn.QUALIFIED(s,p), Prefix.PREFIX(ss,cp));
290-
then
291-
p_1;
292-
end matchcontinue;
289+
then p_1;
290+
end match;
293291
end prefixPath;
294292

295293
public function prefixToPath "Convert a Prefix to a Path"
@@ -471,27 +469,42 @@ protected function prefixSubscriptsInCref "help function to prefixToCrefOpt2, de
471469
output Env.Cache outCache;
472470
output DAE.ComponentRef outCr;
473471
algorithm
474-
(outCache,outCr) := match (inCache,inEnv,inIH,pre,inCr)
475-
local
476-
DAE.Ident id;
477-
DAE.Type tp;
478-
list<DAE.Subscript> subs;
479-
Env.Cache cache;
480-
Env.Env env;
481-
DAE.ComponentRef cr;
482-
483-
484-
case(cache,env,_,_,DAE.CREF_IDENT(id,tp,subs)) equation
485-
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
486-
then (cache,ComponentReference.makeCrefIdent(id,tp,subs));
487-
case(cache,env,_,_,DAE.CREF_QUAL(id,tp,subs,cr)) equation
488-
(cache,cr) = prefixSubscriptsInCref(cache,env,inIH,pre,cr);
489-
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
490-
then (cache,ComponentReference.makeCrefQual(id,tp,subs,cr));
491-
case(cache,_,_,_,DAE.WILD()) then (cache,DAE.WILD());
492-
end match;
472+
(outCache,outCr) := prefixSubscriptsInCrefWork(inCache,inEnv,inIH,pre,inCr,{});
493473
end prefixSubscriptsInCref;
494474

475+
protected function prefixSubscriptsInCrefWork "help function to prefixToCrefOpt2, deals with prefixing expressions in subscripts"
476+
input Env.Cache inCache;
477+
input Env.Env inEnv;
478+
input InstanceHierarchy inIH;
479+
input Prefix.Prefix pre;
480+
input DAE.ComponentRef inCr;
481+
input list<DAE.ComponentRef> acc;
482+
output Env.Cache outCache;
483+
output DAE.ComponentRef outCr;
484+
algorithm
485+
(outCache,outCr) := match (inCache,inEnv,inIH,pre,inCr,acc)
486+
local
487+
DAE.Ident id;
488+
DAE.Type tp;
489+
list<DAE.Subscript> subs;
490+
Env.Cache cache;
491+
Env.Env env;
492+
DAE.ComponentRef cr,crid;
493+
case(cache,env,_,_,DAE.CREF_IDENT(id,tp,subs),_)
494+
equation
495+
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
496+
cr = ComponentReference.makeCrefIdent(id,tp,subs);
497+
then (cache,ComponentReference.implode(listReverse(cr::acc)));
498+
case(cache,env,_,_,DAE.CREF_QUAL(id,tp,subs,cr),_)
499+
equation
500+
(cache,subs) = prefixSubscripts(cache,env,inIH,pre,subs);
501+
crid = ComponentReference.makeCrefIdent(id,tp,subs);
502+
(cache,cr) = prefixSubscriptsInCrefWork(cache,env,inIH,pre,cr,crid::acc);
503+
then (cache,cr);
504+
case(cache,_,_,_,DAE.WILD(),_) then (cache,DAE.WILD());
505+
end match;
506+
end prefixSubscriptsInCrefWork;
507+
495508
protected function prefixSubscripts "help function to prefixSubscriptsInCref, adds prefix to subscripts"
496509
input Env.Cache inCache;
497510
input Env.Env inEnv;

Compiler/Global/Global.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ encapsulated package Global
3939
The Global package contains structures that are available globally."
4040

4141

42-
constant Integer recursionDepthLimit = 1000;
42+
constant Integer recursionDepthLimit = 256;
4343
constant Integer maxFunctionFileLength = 50;
4444

4545
constant Integer instHashIndex = 0;

0 commit comments

Comments
 (0)