Skip to content

Commit

Permalink
- first implementation of further enumeration support:
Browse files Browse the repository at this point in the history
  + comparison of enumerations
  + array declaration using enumerations
  + array indexing using enumerations
  + for equations using enumerations
- changes to Values.ENUM
- all flattening tests go

- more improvements and handling in the back-end will come later

git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/MathCoreOSMC@4018 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed May 7, 2009
1 parent 7b06369 commit 7b9a75c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 19 deletions.
14 changes: 11 additions & 3 deletions Compiler/Ceval.mo
Expand Up @@ -3492,7 +3492,7 @@ algorithm
end matchcontinue;
end cevalRelation;

protected function cevalRange "function: cevalRange
public function cevalRange "function: cevalRange
This function evaluates a range expression.
It only handles integers."
input Integer inInteger1;
Expand Down Expand Up @@ -3689,9 +3689,9 @@ algorithm
/* Search in env for binding, special rule for enumerations, the cr does not have a value since it -is- a value. */
case (cache,env,c,impl,msg)
equation
(cache,attr,ty as (Types.T_ENUM(),_),binding,_,_) = Lookup.lookupVar(cache,env, c);
(cache,attr,ty as (Types.T_ENUM(),_),binding,_,_) = Lookup.lookupVar(cache, env, c);
then
(cache,Values.ENUM(c));
(cache,Values.ENUM(c, 0));

/* Search in env for binding. */
case (cache,env,c,impl,msg)
Expand Down Expand Up @@ -3978,6 +3978,14 @@ algorithm
true = indx <= dim;
then
(cache,Exp.INDEX(e1_1));
/* indexing using enum! */
case (cache,env,Exp.INDEX(exp = e1),dim,impl,msg)
equation
(cache,v1 as Values.ENUM(_,indx),_) = ceval(cache,env, e1, impl, NONE, SOME(dim), msg);
e1_1 = Static.valueExp(v1);
true = indx <= dim;
then
(cache,Exp.INDEX(e1_1));
case (cache,env,Exp.SLICE(exp = e1),dim,impl,msg)
equation
(cache,v1,_) = ceval(cache,env, e1, impl, NONE, SOME(dim), msg);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Cevalfunc.mo
Expand Up @@ -1110,7 +1110,7 @@ algorithm oval := matchcontinue(inType)
case((Types.T_REAL(_),_)) then Values.REAL(0.0);
case((Types.T_STRING(_),_)) then Values.STRING("");
case((Types.T_BOOL(_),_)) then Values.BOOL(false);
case((Types.T_ENUM,_)) then Values.ENUM(Exp.CREF_IDENT("",Exp.ENUM(),{}));
case((Types.T_ENUM,_)) then Values.ENUM(Exp.CREF_IDENT("",Exp.ENUM(),{}),0);
case((Types.T_COMPLEX(ClassInf.RECORD(str), typesVar,_,_),_))
local
list<Types.Var> typesVar;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/Convert.mo
Expand Up @@ -1540,7 +1540,7 @@ algorithm
case (Exp.REALVAL(r)) then Values.REAL(r);
case (Exp.STRINGVAL(s)) then Values.STRING(s);
case (Exp.BOOLVAL(b)) then Values.BOOL(b);
case (Exp.ENUMVAL(cr)) then Values.ENUM(cr);
case (Exp.ENUMVAL(cr)) then Values.ENUM(cr,0);
case (Exp.CODEVAL(c)) then Values.CODE(c);
case (Exp.LISTVAL(vLst))
equation
Expand Down Expand Up @@ -1993,7 +1993,7 @@ algorithm
case (Values.REAL(r)) then Exp.REALVAL(r);
case (Values.STRING(s)) then Exp.STRINGVAL(s);
case (Values.BOOL(b)) then Exp.BOOLVAL(b);
case (Values.ENUM(cr)) then Exp.ENUMVAL(cr);
case (Values.ENUM(cr,_)) then Exp.ENUMVAL(cr);
case (Values.LIST(vLst))
equation
vLst2 = fromValueLstToValueTypesLst(vLst,{});
Expand Down
63 changes: 62 additions & 1 deletion Compiler/Inst.mo
Expand Up @@ -3842,7 +3842,21 @@ algorithm
env_2 = addClassdefsToEnv2(env_1, xs, impl,redeclareMod);
then
env_2;
//otherwise, extend frame with in class.

// adrpo: see if is an enumeration! then extend frame with in class.
case (env,( (sel1 as SCode.CLASSDEF(name = s, classDef = SCode.CLASS(classDef=SCode.ENUMERATION(identLst)))) :: xs),impl,redeclareMod)
local
String s;
list<SCode.Ident> identLst;
SCode.Class enumclass;
equation
enumclass = instEnumeration(s, identLst);
env_1 = Env.extendFrameC(env, enumclass);
env_2 = addClassdefsToEnv2(env_1, xs, impl,redeclareMod);
then
env_2;

// otherwise, extend frame with in class.
case (env,( (sel1 as SCode.CLASSDEF(classDef = cl)) :: xs),impl,redeclareMod)
equation
env_1 = Env.extendFrameC(env, cl);
Expand Down Expand Up @@ -6562,6 +6576,19 @@ algorithm
then
(cache,NONE :: l);

/* adrpo: See if our array dimension comes from an enumeration! */
case (cache,env,cref,(Absyn.SUBSCRIPT(subScript = Absyn.CREF(cr)) :: ds),impl,st,doVect)
local Absyn.ComponentRef cr; Absyn.Path typePath; list<SCode.Element> elementLst;
equation
typePath = Absyn.crefToPath(cr);
/* make sure is an enumeration! */
(_, SCode.CLASS(_, _, _, SCode.R_ENUMERATION(), SCode.PARTS(elementLst, {}, {}, {}, {}, NONE())), _) =
Lookup.lookupClass(cache, env, typePath, false);
i = listLength(elementLst);
(cache,l) = elabArraydimDecl(cache,env, cref, ds, impl, st,doVect);
then
(cache,SOME(DIMINT(i)) :: l);

/* Constant dimension creates DIMINT */
case (cache,env,cref,(Absyn.SUBSCRIPT(subScript = d) :: ds),impl,st,doVect)
equation
Expand Down Expand Up @@ -6777,6 +6804,14 @@ algorithm
l = elabArraydimType2(t, ad);
then
(SOME(DIMINT(i)) :: l);
/*
case ((Types.T_ARRAY(arrayDim = Types.DIM(integerOption = NONE),arrayType = t),_),{})
then
(NONE :: {});
case ((Types.T_ARRAY(arrayDim = Types.DIM(integerOption = SOME(i)),arrayType = t),_),{})
then
(SOME(DIMINT(i)) :: {});
*/
case (_,{}) then {};
case (t,(_ :: ad)) /* PR, for debugging */
equation
Expand Down Expand Up @@ -7704,6 +7739,7 @@ algorithm
outClass := SCode.CLASS(n,false,false,SCode.R_ENUMERATION(),SCode.PARTS(comp,{},{},{},{},NONE));
end instEnumeration;


protected function makeEnumComponents
"function: makeEnumComponents
author: PA
Expand Down Expand Up @@ -8481,11 +8517,36 @@ algorithm
then
(cache,{DAE.WHEN_EQUATION(e_2,dae1,NONE)},env_1,csets,ci_state_1,graph);


/* seems unnecessary to handle when equations that are initial `for\' loops
The loop expression is evaluated to a constant array of
integers, and then the loop is unrolled.
FIXME: Why lookup after add_for_loop_scope ?
*/

// adrpo: handle the case where range is a enumeration!
case (cache,env,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = Absyn.CREF(cr),eEquationLst = el),initial_,impl,graph)
local
Absyn.ComponentRef cr;
Absyn.Path typePath;
Integer len;
list<SCode.Element> elementLst;
list<Values.Value> vals;
equation
typePath = Absyn.crefToPath(cr);
/* make sure is an enumeration! */
(_, SCode.CLASS(_, _, _, SCode.R_ENUMERATION(), SCode.PARTS(elementLst, {}, {}, {}, {}, NONE())), _) =
Lookup.lookupClass(cache, env, typePath, false);
len = listLength(elementLst);
env_1 = addForLoopScope(env, i, (Types.T_INTEGER({}),NONE())) "//Debug.fprintln (\"insti\", \"for expression elaborated\") &" ;
(cache,Types.ATTR(false,false,SCode.RW(),SCode.VAR(),_,_),(Types.T_INTEGER(_),_),Types.UNBOUND(),_,_)
= Lookup.lookupVar(cache,env_1, Exp.CREF_IDENT(i,Exp.OTHER(),{})) " //Debug.fprintln (\"insti\", \"loop-variable added to scope\") &" ;
vals = Ceval.cevalRange(1,1,len);
(cache,dae,csets_1,graph) = unroll(cache,env_1, mod, pre, csets, ci_state, i, Values.ARRAY(vals), el, initial_, impl,graph) " //Debug.fprintln (\"insti\", \"for expression evaluated\") &" ;
ci_state_1 = instEquationCommonCiTrans(ci_state, initial_) " //Debug.fprintln (\"insti\", \"for expression unrolled\") & & //Debug.fprintln (\"insttr\", \"inst_equation_common_eqfor_1 succeeded\")" ;
then
(cache,dae,env,csets_1,ci_state_1,graph);

case (cache,env,mod,pre,csets,ci_state,SCode.EQ_FOR(index = i,range = e,eEquationLst = el),initial_,impl,graph)
equation
(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\") &" ;
Expand Down
21 changes: 14 additions & 7 deletions Compiler/Static.mo
Expand Up @@ -9738,6 +9738,7 @@ algorithm
tuple<Types.TType, Option<Absyn.Path>> t;
Absyn.Exp e;
case ((Types.T_INTEGER(varLstInt = _),_),_,sub) then Exp.INDEX(sub);
case ((Types.T_ENUM(),_),_,sub) then Exp.INDEX(sub);
case ((Types.T_ARRAY(arrayType = (Types.T_INTEGER(varLstInt = _),_)),_),_,sub) then Exp.SLICE(sub);
case (t,e,_)
equation
Expand Down Expand Up @@ -10104,7 +10105,7 @@ algorithm
varlst = Util.listThreadMap(namelst,tpl,Exp.makeVar);
name = Absyn.pathString(path);
then Exp.CALL(path,expl,false,false,Exp.COMPLEX("",varlst,ClassInf.RECORD(name)));
case(Values.ENUM(cr)) then Exp.CREF(cr,Exp.ENUM());
case(Values.ENUM(cr,x)) then Exp.CREF(cr,Exp.ENUM());
case v
equation
print("value_exp failed for "+&Values.valString(v)+&"\n");
Expand Down Expand Up @@ -11077,7 +11078,8 @@ algorithm
(Exp.LESS(Exp.REAL()),
{(Types.T_REAL({}),NONE),(Types.T_REAL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.LESS(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE))} "\'<\' operator" ;
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.LESS(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'<\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"less", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand All @@ -11090,7 +11092,8 @@ algorithm
(Exp.LESSEQ(Exp.REAL()),
{(Types.T_REAL({}),NONE),(Types.T_REAL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.LESSEQ(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE))} "\'<=\' operator" ;
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.LESSEQ(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'<=\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"lessEqual", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand All @@ -11103,7 +11106,8 @@ algorithm
(Exp.GREATER(Exp.REAL()),
{(Types.T_REAL({}),NONE),(Types.T_REAL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.GREATER(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE))} "\'>\' operator" ;
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.GREATER(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'>\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"greater", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand All @@ -11116,7 +11120,8 @@ algorithm
(Exp.GREATEREQ(Exp.REAL()),
{(Types.T_REAL({}),NONE),(Types.T_REAL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.GREATEREQ(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE))} "\'>=\' operator" ;
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.GREATEREQ(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'>=\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"greaterEqual", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand All @@ -11131,7 +11136,8 @@ algorithm
(Exp.EQUAL(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.EQUAL(Exp.BOOL()),
{(Types.T_BOOL({}),NONE),(Types.T_BOOL({}),NONE)},(Types.T_BOOL({}),NONE))} "\'==\' operator" ;
{(Types.T_BOOL({}),NONE),(Types.T_BOOL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.EQUAL(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'==\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"equal", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand All @@ -11146,7 +11152,8 @@ algorithm
(Exp.NEQUAL(Exp.STRING()),
{(Types.T_STRING({}),NONE),(Types.T_STRING({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.NEQUAL(Exp.BOOL()),
{(Types.T_BOOL({}),NONE),(Types.T_BOOL({}),NONE)},(Types.T_BOOL({}),NONE))} "\'!=\' operator" ;
{(Types.T_BOOL({}),NONE),(Types.T_BOOL({}),NONE)},(Types.T_BOOL({}),NONE)),
(Exp.NEQUAL(Exp.ENUM()),{t1,t2},(Types.T_BOOL({}),NONE))} "\'!=\' operator" ;
/*(cache,userops) = getKoeningOperatorTypes(cache,"notEqual", env, t1, t2);*/
types = Util.listFlatten({scalars/*,userops*/});
then
Expand Down
5 changes: 2 additions & 3 deletions Compiler/Values.mo
Expand Up @@ -66,6 +66,7 @@ uniontype Value

record ENUM
Exp.ComponentRef value;
Integer index;
end ENUM;

record ARRAY
Expand Down Expand Up @@ -1771,7 +1772,7 @@ algorithm
s_2 = stringAppend(s_1, "}");
then
s_2;
case(ENUM(cr)) equation
case(ENUM(cr,_)) equation
s = Exp.printComponentRefStr(cr);
then s;
case _
Expand All @@ -1782,8 +1783,6 @@ algorithm
end matchcontinue;
end valString;



protected function valRecordString
"function: valRecordString
This function returns a textual representation of a record,
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -4257,7 +4257,7 @@ static int value_to_type_desc(void *value, type_description *desc)
data = RML_CDR(data);
}
}; break;
case Values__ENUM_3dBOX1:
case Values__ENUM_3dBOX2:
case Values__LIST_3dBOX1:
case Values__CODE_3dBOX1:
/* unsupported */
Expand Down Expand Up @@ -4412,7 +4412,7 @@ static int get_array_type_and_dims(type_description *desc, void *arrdata)
return 1;
case Values__ARRAY_3dBOX1:
return (1 + get_array_type_and_dims(desc, RML_STRUCTDATA(item)[0]));
case Values__ENUM_3dBOX1:
case Values__ENUM_3dBOX2:
case Values__LIST_3dBOX1:
case Values__TUPLE_3dBOX1:
case Values__RECORD_3dBOX3:
Expand Down

0 comments on commit 7b9a75c

Please sign in to comment.