Skip to content

Commit 4aaf6b1

Browse files
committed
- changes by Alexey Lebedev [alexey dot lebedev at equa dot se] to support implicit range in for loops
- more tests for loops git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/OpenModelica1.5.0@4272 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 7ae9668 commit 4aaf6b1

File tree

3 files changed

+260
-22
lines changed

3 files changed

+260
-22
lines changed

Compiler/Absyn.mo

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ algorithm crefs := matchcontinue(subs)
17891789
list<ComponentRef> crefs1;
17901790
Exp exp;
17911791
case({}) then {};
1792-
case(NOSUB::subs) then getCrefsFromSubs(subs);
1792+
case(NOSUB::subs) then getCrefsFromSubs(subs);
17931793
case(SUBSCRIPT(exp)::subs)
17941794
equation
17951795
crefs1 = getCrefsFromSubs(subs);
@@ -2729,6 +2729,7 @@ algorithm
27292729
list<ForIterator> forIterators;
27302730
FunctionArgs funcArgs;
27312731
AlgorithmItem algItem;
2732+
Boolean bool;
27322733

27332734
case (id,ALG_ASSIGN(e_1,e_2))
27342735
equation
@@ -2744,7 +2745,7 @@ algorithm
27442745
lst_4=findIteratorInAlgorithmItemLst(id,algLst_2);
27452746
lst=Util.listFlatten({lst_1,lst_2,lst_3,lst_4});
27462747
then lst;
2747-
case (id, ALG_FOR(forIterators,algLst_1))
2748+
/* case (id, ALG_FOR(forIterators,algLst_1))
27482749
equation
27492750
true=iteratorPresentAmongIterators(id,forIterators);
27502751
lst=findIteratorInForIteratorsBounds(id,forIterators);
@@ -2755,6 +2756,13 @@ algorithm
27552756
lst_1=findIteratorInAlgorithmItemLst(id,algLst_1);
27562757
lst_2=findIteratorInForIteratorsBounds(id,forIterators);
27572758
lst=listAppend(lst_1,lst_2);
2759+
then lst; */
2760+
case (id, ALG_FOR(forIterators,algLst_1))
2761+
equation
2762+
lst_1=findIteratorInAlgorithmItemLst(id,algLst_1);
2763+
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,forIterators);
2764+
lst_1=Util.if_(bool, {}, lst_1);
2765+
lst=listAppend(lst_1,lst_2);
27582766
then lst;
27592767
case (id, ALG_WHILE(e_1,algLst_1))
27602768
equation
@@ -2862,7 +2870,7 @@ algorithm
28622870
end matchcontinue;
28632871
end findIteratorInElseIfExpBranch;
28642872

2865-
protected function findIteratorInFunctionArgs
2873+
public function findIteratorInFunctionArgs
28662874
input String inString;
28672875
input FunctionArgs inFunctionArgs;
28682876
output list<tuple<ComponentRef, Integer>> outLst;
@@ -2875,13 +2883,14 @@ algorithm
28752883
list<NamedArg> namedArgs;
28762884
Exp exp;
28772885
list<ForIterator> forIterators;
2886+
Boolean bool;
28782887
case (id,FUNCTIONARGS(expLst,namedArgs))
28792888
equation
28802889
lst_1=findIteratorInExpLst(id,expLst);
28812890
lst_2=findIteratorInNamedArgs(id,namedArgs);
28822891
lst=listAppend(lst_1,lst_2);
28832892
then lst;
2884-
case (id, FOR_ITER_FARG(exp,forIterators))
2893+
/* case (id, FOR_ITER_FARG(exp,forIterators))
28852894
equation
28862895
true=iteratorPresentAmongIterators(id,forIterators);
28872896
lst=findIteratorInForIteratorsBounds(id,forIterators);
@@ -2892,11 +2901,18 @@ algorithm
28922901
lst_1=findIteratorInExp(id,exp);
28932902
lst_2=findIteratorInForIteratorsBounds(id,forIterators);
28942903
lst=listAppend(lst_1,lst_2);
2904+
then lst; */
2905+
case (id, FOR_ITER_FARG(exp,forIterators))
2906+
equation
2907+
lst_1=findIteratorInExp(id,exp);
2908+
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,forIterators);
2909+
lst_1=Util.if_(bool, {}, lst_1);
2910+
lst=listAppend(lst_1,lst_2);
28952911
then lst;
28962912
end matchcontinue;
28972913
end findIteratorInFunctionArgs;
28982914

2899-
protected function iteratorPresentAmongIterators
2915+
/*protected function iteratorPresentAmongIterators
29002916
input String inString;
29012917
input list<ForIterator> inForIterators;
29022918
output Boolean outBool;
@@ -2917,9 +2933,9 @@ algorithm
29172933
bool=iteratorPresentAmongIterators(id,rest);
29182934
then bool;
29192935
end matchcontinue;
2920-
end iteratorPresentAmongIterators;
2936+
end iteratorPresentAmongIterators; */
29212937

2922-
protected function findIteratorInExpLst//This function is not tail-recursive, and I don't know how to fix it -- alleb
2938+
public function findIteratorInExpLst//This function is not tail-recursive, and I don't know how to fix it -- alleb
29232939
input String inString;
29242940
input list<Exp> inExpLst;
29252941
output list<tuple<ComponentRef, Integer>> outLst;
@@ -2982,7 +2998,7 @@ algorithm
29822998
end matchcontinue;
29832999
end findIteratorInNamedArgs;
29843000

2985-
protected function findIteratorInForIteratorsBounds
3001+
/*protected function findIteratorInForIteratorsBounds
29863002
input String inString;
29873003
input list<ForIterator> inForIterators;
29883004
output list<tuple<ComponentRef, Integer>> outLst;
@@ -3005,9 +3021,43 @@ algorithm
30053021
lst=listAppend(lst_1,lst_2);
30063022
then lst;
30073023
end matchcontinue;
3008-
end findIteratorInForIteratorsBounds;
3024+
end findIteratorInForIteratorsBounds; */
3025+
3026+
protected function findIteratorInForIteratorsBounds2 "
3027+
This is a fixed version of the function; it stops looking for the iterator when it finds another iterator
3028+
with the same name. It also returns information about whether it has found such an iterator"
3029+
input String inString;
3030+
input list<ForIterator> inForIterators;
3031+
output Boolean outBool;
3032+
output list<tuple<ComponentRef, Integer>> outLst;
3033+
algorithm
3034+
(outBool,outLst):=matchcontinue(inString,inForIterators)
3035+
local
3036+
list<tuple<ComponentRef, Integer>> lst,lst_1,lst_2;
3037+
Boolean bool;
3038+
String id, id_1;
3039+
list<ForIterator> rest;
3040+
Exp exp;
3041+
case (_,{}) then (false,{});
3042+
case (id,(id_1,_)::_)
3043+
equation
3044+
equality(id=id_1);
3045+
then
3046+
(true,{});
3047+
case (id,(_,NONE)::rest)
3048+
equation
3049+
(bool,lst)=findIteratorInForIteratorsBounds2(id,rest);
3050+
then (bool,lst);
3051+
case (id,(_,SOME(exp))::rest)
3052+
equation
3053+
lst_1=findIteratorInExp(id,exp);
3054+
(bool,lst_2)=findIteratorInForIteratorsBounds2(id,rest);
3055+
lst=listAppend(lst_1,lst_2);
3056+
then (bool,lst);
3057+
end matchcontinue;
3058+
end findIteratorInForIteratorsBounds2;
30093059

3010-
protected function findIteratorInExp
3060+
public function findIteratorInExp
30113061
input String inString;
30123062
input Exp inExp;
30133063
output list<tuple<ComponentRef, Integer>> outLst;
@@ -3111,7 +3161,7 @@ algorithm
31113161
end matchcontinue;
31123162
end findIteratorInExpOpt;
31133163

3114-
protected function findIteratorInCRef "
3164+
public function findIteratorInCRef "
31153165
The most important among \"findIteratorIn...\" functions -- they all use this one in the end
31163166
"
31173167
input String inString;
@@ -3208,8 +3258,8 @@ algorithm
32083258
lst=qualifyCRefIntLst(name,subLst,rest);
32093259
then (CREF_QUAL(name,subLst,cref),i)::lst;
32103260
end matchcontinue;
3211-
end qualifyCRefIntLst;
3212-
3261+
end qualifyCRefIntLst;
3262+
32133263
public function setBuildTimeInInfo
32143264
input Real buildTime;
32153265
input Info inInfo;

Compiler/Inst.mo

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ public function instantiateImplicit
275275
Implicit instantiation of a program can be used for e.g. code generation
276276
of functions, since a function must be implicitly instantiated in order to
277277
generate code from it."
278-
input Env.Cache inCache;
279-
input InstanceHierarchy inIH;
278+
input Env.Cache inCache;
279+
input InstanceHierarchy inIH;
280280
input SCode.Program inProgram;
281281
output Env.Cache outCache;
282282
output InstanceHierarchy outIH;
@@ -371,8 +371,8 @@ public function instantiatePartialClass
371371
This is a function for instantiating partial 'top' classes.
372372
It does so by converting the partial class into a non partial class.
373373
Currently used by: MathCore.modelEquations, CevalScript.checkModel"
374-
input Env.Cache inCache;
375-
input InstanceHierarchy inIH;
374+
input Env.Cache inCache;
375+
input InstanceHierarchy inIH;
376376
input SCode.Program inProgram;
377377
input SCode.Path inPath;
378378
output Env.Cache outCache;
@@ -9540,6 +9540,8 @@ algorithm
95409540
Absyn.ForIterators rangeIdList;
95419541
ConnectionGraph.ConnectionGraph graph;
95429542
InstanceHierarchy ih;
9543+
list<tuple<Absyn.ComponentRef, Integer>> lst;
9544+
tuple<Absyn.ComponentRef, Integer> tpl;
95439545

95449546
/* connect statements */
95459547
case (cache,env,ih,mods,pre,csets,ci_state,SCode.EQ_CONNECT(crefLeft = c1,crefRight = c2),initial_,impl,graph)
@@ -9740,6 +9742,30 @@ algorithm
97409742
then
97419743
(cache,env,ih,dae,csets_1,ci_state_1,graph);
97429744

9745+
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.END(),eEquationLst = el),initial_,impl,graph) // Implicit range
9746+
equation
9747+
lst=SCode.findIteratorInEEquationLst(i,el);
9748+
equality(lst={});
9749+
Error.addMessage(Error.IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY,{i});
9750+
then
9751+
fail();
9752+
9753+
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.END(),eEquationLst = el),initial_,impl,graph)
9754+
equation
9755+
lst=SCode.findIteratorInEEquationLst(i,el);
9756+
failure(equality(lst={}));
9757+
tpl=Util.listFirst(lst);
9758+
e=rangeExpression(tpl);
9759+
(cache,e_1,Types.PROP((Types.T_ARRAY(Types.DIM(_),id_t),_),_),_) = Static.elabExp(cache,env, e, impl, NONE,true) "//Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1\") &" ;
9760+
env_1 = addForLoopScope(env, i, id_t) "//Debug.fprintln (\"insti\", \"for expression elaborated\") &" ;
9761+
(cache,Types.ATTR(false,false,SCode.RW(),SCode.VAR(),_,_),(Types.T_INTEGER(_),_),Types.UNBOUND(),_,_)
9762+
= Lookup.lookupVar(cache,env_1, Exp.CREF_IDENT(i,Exp.OTHER(),{})) " //Debug.fprintln (\"insti\", \"loop-variable added to scope\") &" ;
9763+
(cache,v,_) = Ceval.ceval(cache,env, e_1, impl, NONE, NONE, Ceval.MSG()) " //Debug.fprintln (\"insti\", \"loop variable looked up\") & FIXME: Check bounds" ;
9764+
(cache,dae,csets_1,graph) = unroll(cache,env_1, mod, pre, csets, ci_state, i, v, el, initial_, impl,graph) " //Debug.fprintln (\"insti\", \"for expression evaluated\") &" ;
9765+
ci_state_1 = instEquationCommonCiTrans(ci_state, initial_) " //Debug.fprintln (\"insti\", \"for expression unrolled\") & & //Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1 succeeded\")" ;
9766+
then
9767+
(cache,env,ih,dae,csets_1,ci_state_1,graph);
9768+
97439769
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = e,eEquationLst = el),initial_,impl,graph)
97449770
equation
97459771
(cache,e_1,Types.PROP((Types.T_ARRAY(Types.DIM(_),id_t),_),_),_) = Static.elabExp(cache,env, e, impl, NONE,true) "//Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1\") &" ;
@@ -11062,7 +11088,7 @@ algorithm
1106211088
stmt = Algorithm.makeFor(i, e_2, prop, sl_1);
1106311089
then
1106411090
(cache,stmt);
11065-
case (cache,env,pre,{(i,NONE)},sl,initial_,impl)
11091+
/* case (cache,env,pre,{(i,NONE)},sl,initial_,impl)
1106611092
equation
1106711093
lst=Absyn.findIteratorInAlgorithmItemLst(i,sl);
1106811094
// len=listLength(lst);
@@ -11071,7 +11097,7 @@ algorithm
1107111097
equality(lst={});
1107211098
Error.addMessage(Error.IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY,{i});
1107311099
then
11074-
fail();
11100+
fail();*/
1107511101
case (cache,env,pre,(i,NONE)::restIterators,sl,initial_,impl)
1107611102
equation
1107711103
lst=Absyn.findIteratorInAlgorithmItemLst(i,sl);
@@ -11911,7 +11937,7 @@ protected function connectArrayComponents "
1191111937
Help functino to connectComponents
1191211938
Traverses arrays of complex connectors and calls connectComponents for each index
1191311939
"
11914-
input Env.Cache inCache;
11940+
input Env.Cache inCache;
1191511941
input Env inEnv;
1191611942
input InstanceHierarchy inIH;
1191711943
input Connect.Sets inSets;
@@ -14996,7 +15022,7 @@ algorithm
1499615022

1499715023
/*
1499815024
* adrpo 2009-05-15: also T_COMPLEX that is NOT record but TYPE should be allowed
14999-
as is used in Modelica.Mechanics.MultiBody (Orientation type)
15025+
* as is used in Modelica.Mechanics.MultiBody (Orientation type)
1500015026
*/
1500115027
case(lhs,rhs,tp,initial_) equation
1500215028
// adrpo: TODO! check if T_COMPLEX(ClassInf.TYPE)!

0 commit comments

Comments
 (0)