Skip to content

Commit

Permalink
- Added support for MetaModelica 2.0 "all wild" pattern REC(__)
Browse files Browse the repository at this point in the history
- Has testcase AllWild.mos


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8224 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 14, 2011
1 parent 0f85d49 commit 092bdef
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -987,6 +987,7 @@ uniontype ComponentRef "A component reference is the fully or partially qualifie
end CREF_IDENT;

record WILD end WILD;
record ALLWILD end ALLWILD;

record CREF_INVALID
"Used to indicate a cref that could not be found, which is legal as long as
Expand Down Expand Up @@ -2051,6 +2052,7 @@ algorithm
then
(CREF_IDENT(name, subs), tup);

case (ALLWILD(), _) then (inCref, inTuple);
case (WILD(), _) then (inCref, inTuple);
case (CREF_INVALID(componentRef = _), _) then (inCref, inTuple);
end match;
Expand Down Expand Up @@ -2551,6 +2553,7 @@ algorithm
s2 = printComponentRefStr(child);
s1 = "." +& s2;
then s1;
case (ALLWILD()) then "__";
case (WILD()) then "_";
case (CREF_INVALID(componentRef = child))
then printComponentRefStr(child);
Expand Down Expand Up @@ -3184,6 +3187,7 @@ algorithm
case (REAL(value = _),checkSubs) then {};
case (STRING(value = _),checkSubs) then {};
case (BOOL(value = _),checkSubs) then {};
case (CREF(componentRef = ALLWILD()),_) then {};
case (CREF(componentRef = WILD()),_) then {};
case (CREF(componentRef = CREF_INVALID(componentRef = _)), _) then {};
case (CREF(componentRef = cr),false) then {cr};
Expand Down Expand Up @@ -4970,6 +4974,7 @@ algorithm
lst=listAppend(lst_1,lst_3);
then lst;
case(id, CREF_FULLYQUALIFIED(cref)) then findIteratorInCRef(id,cref);
case (_,ALLWILD()) then {};
case (_,WILD()) then {};
case (_, CREF_INVALID(componentRef = _)) then {};
end match;
Expand Down
11 changes: 11 additions & 0 deletions Compiler/FrontEnd/Dump.mo
Expand Up @@ -3482,6 +3482,11 @@ algorithm
Print.printBuf("Absyn.WILD");
then
();
case Absyn.ALLWILD()
equation
Print.printBuf("Absyn.ALLWILD");
then
();

case Absyn.CREF_INVALID(componentRef = cr)
equation
Expand Down Expand Up @@ -3552,6 +3557,8 @@ algorithm
then
s_3;

case Absyn.ALLWILD() then "__";

case Absyn.WILD() then "_";

case Absyn.CREF_INVALID(componentRef = cr)
Expand Down Expand Up @@ -5295,6 +5302,10 @@ algorithm
printListAsCorbaString(subscripts, printSubscriptAsCorbaString, ",");
Print.printBuf(" end Absyn.CREF_IDENT;");
then ();
case Absyn.ALLWILD()
equation
Print.printBuf("record Absyn.ALLWILD end Absyn.ALLWILD;");
then ();
case Absyn.WILD()
equation
Print.printBuf("record Absyn.WILD end Absyn.WILD;");
Expand Down
20 changes: 19 additions & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -358,9 +358,11 @@ algorithm
fieldTypeList = Util.listMap(fieldVarList, Types.getVarType);
fieldNameList = Util.listMap(fieldVarList, Types.getVarName);

(funcArgs,namedArgList) = checkForAllWildCall(funcArgs,namedArgList,listLength(fieldNameList));

numPosArgs = listLength(funcArgs);
(_,fieldNamesNamed) = Util.listSplit(fieldNameList, numPosArgs);

(funcArgsNamedFixed,invalidArgs) = generatePositionalArgs(fieldNamesNamed,namedArgList,{});
funcArgs = listAppend(funcArgs,funcArgsNamedFixed);
Util.SUCCESS() = checkInvalidPatternNamedArgs(invalidArgs,Util.SUCCESS(),info);
Expand All @@ -374,6 +376,8 @@ algorithm
fieldTypeList = Util.listMap(fieldVarList, Types.getVarType);
fieldNameList = Util.listMap(fieldVarList, Types.getVarName);

(funcArgs,namedArgList) = checkForAllWildCall(funcArgs,namedArgList,listLength(fieldNameList));

numPosArgs = listLength(funcArgs);
(_,fieldNamesNamed) = Util.listSplit(fieldNameList, numPosArgs);

Expand All @@ -393,6 +397,20 @@ algorithm
end matchcontinue;
end elabPatternCall;

protected function checkForAllWildCall "Converts a call REC(__) to REC(_,_,_,_)"
input list<Absyn.Exp> args;
input list<Absyn.NamedArg> named;
input Integer numFields;
output list<Absyn.Exp> outArgs;
output list<Absyn.NamedArg> outNamed;
algorithm
(outArgs,outNamed) := match (args,named,numFields)
case ({Absyn.CREF(Absyn.ALLWILD())},{},numFields)
then (Util.listFill(Absyn.CREF(Absyn.WILD()),numFields),{});
else (args,named);
end match;
end checkForAllWildCall;

protected function validPatternType
input DAE.Type ty1;
input DAE.Type ty2;
Expand Down
20 changes: 18 additions & 2 deletions Compiler/OpenModelicaBootstrappingHeader.h
Expand Up @@ -2913,6 +2913,22 @@ extern struct record_description Absyn_ComponentRef_WILD__desc;
static const MMC_DEFSTRUCTLIT(Absyn__WILD__struct,1,6) {&Absyn_ComponentRef_WILD__desc}};
static void *Absyn__WILD = MMC_REFSTRUCTLIT(Absyn__WILD__struct);
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_ComponentRef_ALLWILD__desc_added
#define Absyn_ComponentRef_ALLWILD__desc_added
const char* Absyn_ComponentRef_ALLWILD__desc__fields[0] = {};
struct record_description Absyn_ComponentRef_ALLWILD__desc = {
"Absyn_ComponentRef_ALLWILD",
"Absyn.ComponentRef.ALLWILD",
Absyn_ComponentRef_ALLWILD__desc__fields
};
#endif
#else /* Only use the file as a header */
extern struct record_description Absyn_ComponentRef_ALLWILD__desc;
#endif
#define Absyn__ALLWILD_3dBOX0 7
static const MMC_DEFSTRUCTLIT(Absyn__ALLWILD__struct,1,7) {&Absyn_ComponentRef_ALLWILD__desc}};
static void *Absyn__ALLWILD = MMC_REFSTRUCTLIT(Absyn__ALLWILD__struct);
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_ComponentRef_CREF__INVALID__desc_added
#define Absyn_ComponentRef_CREF__INVALID__desc_added
const char* Absyn_ComponentRef_CREF__INVALID__desc__fields[1] = {"componentRef"};
Expand All @@ -2925,8 +2941,8 @@ struct record_description Absyn_ComponentRef_CREF__INVALID__desc = {
#else /* Only use the file as a header */
extern struct record_description Absyn_ComponentRef_CREF__INVALID__desc;
#endif
#define Absyn__CREF_5fINVALID_3dBOX1 7
#define Absyn__CREF_5fINVALID(componentRef) (mmc_mk_box2(7,&Absyn_ComponentRef_CREF__INVALID__desc,componentRef))
#define Absyn__CREF_5fINVALID_3dBOX1 8
#define Absyn__CREF_5fINVALID(componentRef) (mmc_mk_box2(8,&Absyn_ComponentRef_CREF__INVALID__desc,componentRef))
#ifdef ADD_METARECORD_DEFINTIONS
#ifndef Absyn_Path_QUALIFIED__desc_added
#define Absyn_Path_QUALIFIED__desc_added
Expand Down
1 change: 1 addition & 0 deletions Parser/BaseModelica_Lexer.g
Expand Up @@ -113,6 +113,7 @@ MATCHCONTINUE;
SUBTYPEOF;
UNIONTYPE;
WILD;
ALLWILD;
// ---------
// OptiMo
Expand Down
1 change: 1 addition & 0 deletions Parser/MetaModelica_Lexer.g
Expand Up @@ -54,6 +54,7 @@ LOCAL : 'local';
MATCH : 'match';
MATCHCONTINUE : 'matchcontinue';
UNIONTYPE : 'uniontype';
ALLWILD : '__';
WILD : '_';
SUBTYPEOF : 'subtypeof';
COLONCOLON : '::';
Expand Down
14 changes: 12 additions & 2 deletions Parser/Modelica.g
Expand Up @@ -194,10 +194,19 @@ class_specifier2 returns [void* ast, const char *s2] @init {
$s2 = 0;
} :
(
cmt=string_comment c=composition T_END id=identifier
(lt=LESS ids=ident_list gt=GREATER)? cmt=string_comment c=composition T_END id=identifier
{
$s2 = id;
$ast = Absyn__PARTS(c, mk_some_or_none(cmt));
if (ids != NULL) {
modelicaParserAssert(metamodelica_enabled(),"Polymorphic classes are only available in MetaModelica", class_specifier2, $start->line, $start->charPosition+1, $gt->line, $gt->charPosition+2);
c_add_source_message(2, "SYNTAX", "Warning", "TODO: Found polymorphic class definition, but it was not added to the AST (because Absyn.mo does not contain this definition yet)",
NULL, 0, $start->line, $start->charPosition+1, $gt->line, $gt->charPosition+2,
ModelicaParser_readonly, ModelicaParser_filename_C);
$ast = Absyn__PARTS(c, mk_some_or_none(cmt)); /* TODO: insert the short-hand stuff here */
} else {
$s2 = id;
$ast = Absyn__PARTS(c, mk_some_or_none(cmt));
}
}
| EQUALS attr=base_prefix path=type_specifier ( cm=class_modification )? cmt=comment
{
Expand Down Expand Up @@ -1061,6 +1070,7 @@ component_reference returns [void* ast, int isNone] :
$ast = dot ? Absyn__CREF_5fFULLYQUALIFIED(cr.ast) : cr.ast;
$isNone = cr.isNone;
}
| ALLWILD {$ast = Absyn__ALLWILD; $isNone = false;}
| WILD {$ast = Absyn__WILD; $isNone = false;}
;
Expand Down

0 comments on commit 092bdef

Please sign in to comment.