Skip to content

Commit

Permalink
- Added some more scripting functions
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10706 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 13, 2011
1 parent 7db42bb commit 78ea4b7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 61 deletions.
24 changes: 23 additions & 1 deletion Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1288,6 +1288,14 @@ function strtok "Splits the strings at the places given by the token, for exampl
external "builtin";
end strtok;

public function stringReplace
input String str;
input String source;
input String target;
output String res;
external "builtin";
end stringReplace;

function list "Lists the contents of the given class, or all loaded classes"
input TypeName class_ := $TypeName(AllLoadedClasses);
output String contents;
Expand Down Expand Up @@ -1363,7 +1371,9 @@ end setClassComment;

function getClassNames
input TypeName class_ := $TypeName(AllLoadedClasses);
output TypeName classNames[:];
input Boolean recursive := false;
input Boolean qualified := false;
output TypeName classNames[:];
external "builtin";
end getClassNames;

Expand Down Expand Up @@ -1758,6 +1768,18 @@ function typeNameString
external "builtin";
end typeNameString;

function typeNameStrings
input TypeName cl;
output String out[:];
external "builtin";
end typeNameStrings;

function getClassComment
input TypeName cl;
output String comment;
external "builtin";
end getClassComment;

end Scripting;

annotation(Documentation(info="<html>OpenModelica internal defintions and scripting functions are defined here.</html>", __Dymola_DocumentationClass = true));
Expand Down
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -1625,6 +1625,8 @@ algorithm
(cache,DAE.RCONST(r),DAE.PROP(DAE.T_REAL_DEFAULT,DAE.C_CONST()));

case (cache,_,Absyn.STRING(value = s),impl,_,info)
equation
s = System.unescapedString(s);
then
(cache,DAE.SCONST(s),DAE.PROP(DAE.T_STRING_DEFAULT,DAE.C_CONST()));

Expand Down
44 changes: 42 additions & 2 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -741,6 +741,7 @@ algorithm
list<Interactive.CompiledCFunction> cf;
DAE.Type tp;
Absyn.Class absynClass;
Absyn.ClassDef cdef;
DAE.DAElist dae;
BackendDAE.BackendDAE daelow,optdae;
BackendDAE.Variables vars;
Expand Down Expand Up @@ -833,21 +834,50 @@ algorithm
then
(cache,Values.BOOL(b),st);

case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses")))},st as Interactive.SYMBOLTABLE(ast = p),msg)
case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses"))),Values.BOOL(false),_},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
paths = Interactive.getTopClassnames(p);
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
then
(cache,ValuesUtil.makeArray(vals),st);


case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(path))},st as Interactive.SYMBOLTABLE(ast = p),msg)
case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(path)),Values.BOOL(false),Values.BOOL(b)},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
paths = Interactive.getClassnamesInPath(path, p);
paths = Debug.bcallret3(b,List.map1r,paths,Absyn.joinPaths,path,paths);
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
then
(cache,ValuesUtil.makeArray(vals),st);

case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses"))),Values.BOOL(true),_},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
(_,paths) = Interactive.getClassNamesRecursive(NONE(),p,{});
paths = listReverse(paths);
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
then
(cache,ValuesUtil.makeArray(vals),st);


case (cache,env,"getClassNames",{Values.CODE(Absyn.C_TYPENAME(path)),Values.BOOL(true),_},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
(_,paths) = Interactive.getClassNamesRecursive(SOME(path),p,{});
paths = listReverse(paths);
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
then
(cache,ValuesUtil.makeArray(vals),st);

case (cache,env,"getClassComment",{Values.CODE(Absyn.C_TYPENAME(path))},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
Absyn.CLASS(_,_,_,_,_,cdef,_) = Interactive.getPathedClassInProgram(path, p);
str = Interactive.getClassComment(cdef);
then
(cache,Values.STRING(str),st);

case (cache,env,"getClassComment",{Values.CODE(Absyn.C_TYPENAME(path))},st as Interactive.SYMBOLTABLE(ast = p),msg)
then
(cache,Values.STRING(""),st);

case (cache,env,"getPackages",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses")))},st as Interactive.SYMBOLTABLE(ast = p),msg)
equation
paths = Interactive.getTopPackages(p);
Expand Down Expand Up @@ -1454,6 +1484,11 @@ algorithm
str = Absyn.pathString(path);
then (cache,Values.STRING(str),st);

case (cache,env,"typeNameStrings",{Values.CODE(A=Absyn.C_TYPENAME(path=path))},st,_)
equation
v = ValuesUtil.makeArray(List.map(Absyn.pathToStringList(path),ValuesUtil.makeString));
then (cache,v,st);

case (cache,env,"generateHeader",{Values.STRING(filename)},(st as Interactive.SYMBOLTABLE(ast = p)),msg)
equation
str = Tpl.tplString(Unparsing.programExternalHeader, SCodeUtil.translateAbsyn2SCode(p));
Expand Down Expand Up @@ -1637,6 +1672,11 @@ algorithm
i = listLength(vals);
then (cache,Values.ARRAY(vals,{i}),st);

case (cache,env,"stringReplace",{Values.STRING(str1),Values.STRING(str2),Values.STRING(str3)},st,msg)
equation
str = System.stringReplace(str1, str2, str3);
then (cache,Values.STRING(str),st);

/* Checks the installation of OpenModelica and tries to find common errors */
case (cache,env,"checkSettings",{},st,msg)
equation
Expand Down
85 changes: 27 additions & 58 deletions Compiler/Script/Interactive.mo
Expand Up @@ -1847,25 +1847,6 @@ algorithm
then
(resstr,st);

case (istmts, st as SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getClassNamesRecursive");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
path = Absyn.crefToPath(cr);
resstr = getClassNamesRecursive(SOME(path), p, " ");
Print.clearErrorBuf();
then
(resstr,st);

case (istmts, st as SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getClassNamesRecursive");
{} = getApiFunctionArgs(istmts);
resstr = getClassNamesRecursive(NONE(), p, " ");
Print.clearErrorBuf();
then
(resstr,st);

case (istmts, st as SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "refactorClass");
Expand Down Expand Up @@ -1933,16 +1914,6 @@ algorithm
then
("error",st);

case (istmts, st as SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getClassComment");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
path = Absyn.crefToPath(cr);
Absyn.CLASS(_,_,_,_,_,cdef,_) = getPathedClassInProgram(path, p);
resstr = getClassComment(cdef);
then
(resstr,st);

case (istmts, st as SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getClassRestriction");
Expand Down Expand Up @@ -8271,7 +8242,7 @@ algorithm
str_ecol := intString(ec);
dim_str := getClassDimensions(cdef);
res_1 := stringAppendList(
{"{\"",res,"\",",cmt,",\"",file,"\",{",strPartial,",",
{"{\"",res,"\",\"",cmt,"\",\"",file,"\",{",strPartial,",",
strFinal,",",strEncapsulated,"},{\"",str_readonly,"\",",str_sline,",",
str_scol,",",str_eline,",",str_ecol,"},",dim_str,"}"}) "composing the final returned string" ;
end getClassInformation;
Expand Down Expand Up @@ -8327,13 +8298,13 @@ algorithm
str_ecol := intString(ec);
res_1 := stringAppendList(
{"{ rec(name=\"",name,"\", partial=",strPartial,", final=",
strFinal,", encapsulated=",strEncapsulated,", restriction=",res,", comment=",
cmt,", file=\"",file,"\", readonly=\"",str_readonly,"\", startLine=",
strFinal,", encapsulated=",strEncapsulated,", restriction=",res,", comment=\"",
cmt,"\", file=\"",file,"\", readonly=\"",str_readonly,"\", startLine=",
str_sline,", startColumn=",str_scol,", endLine=",str_eline,", endColumn=",
str_ecol,") }"}) "composing the final returned string" ;
end getClassAttributes;

protected function getClassComment
public function getClassComment
"function: getClassComment
author: PA
Returns the class comment of a Absyn.ClassDef"
Expand All @@ -8343,8 +8314,7 @@ protected
String s;
algorithm
s := getClassComment2(cdef);
s := System.unescapedString(s);
res := stringAppendList({"\"",s,"\""});
res := System.unescapedString(s);
end getClassComment;

protected function getClassComment2
Expand Down Expand Up @@ -13433,10 +13403,14 @@ algorithm
list<Absyn.ElementArg> arglst;
String info, revisions;
String s;
Boolean partialInst;
case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
equation
partialInst = System.getPartialInstantiation();
System.setPartialInstantiation(true);
info = getDocumentationAnnotationInfo(arglst);
revisions = getDocumentationAnnotationRevision(arglst);
System.setPartialInstantiation(partialInst);
then ((info,revisions));
end match;
end getDocumentationAnnotationString;
Expand All @@ -13458,7 +13432,7 @@ algorithm
case (Absyn.MODIFICATION(componentRef = Absyn.CREF_IDENT(name = "info"),
modification=SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=exp))))::xs)
equation
(_,dexp,_) = Static.elabGraphicsExp(Env.emptyCache(), Env.emptyEnv, exp, false, Prefix.NOPRE(), Absyn.dummyInfo);
(_,dexp,_) = Static.elabGraphicsExp(Env.emptyCache(), Env.emptyEnv, exp, true, Prefix.NOPRE(), Absyn.dummyInfo);
(DAE.SCONST(s),_) = ExpressionSimplify.simplify(dexp);
// ss = getDocumentationAnnotationInfo(xs);
then s;
Expand Down Expand Up @@ -13486,7 +13460,7 @@ algorithm
case (Absyn.MODIFICATION(componentRef = Absyn.CREF_IDENT(name = "revisions"),
modification=SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=exp))))::xs)
equation
(_,dexp,_) = Static.elabGraphicsExp(Env.emptyCache(), Env.emptyEnv, exp, false, Prefix.NOPRE(), Absyn.dummyInfo);
(_,dexp,_) = Static.elabGraphicsExp(Env.emptyCache(), Env.emptyEnv, exp, true, Prefix.NOPRE(), Absyn.dummyInfo);
(DAE.SCONST(s),_) = ExpressionSimplify.simplify(dexp);
then s;
case (_::xs)
Expand Down Expand Up @@ -19089,16 +19063,16 @@ algorithm
end match;
end joinPaths;

protected function getClassNamesRecursive
public function getClassNamesRecursive
"function: getClassNamesRecursive
Returns a string with all the classes for a given path."
input Option<Absyn.Path> inPath;
input Absyn.Program inProgram;
input String indent;
output String outString;
input list<Absyn.Path> acc;
output Option<Absyn.Path> opath;
output list<Absyn.Path> paths;
algorithm
outString:=
matchcontinue (inPath,inProgram,indent)
(opath,paths) := matchcontinue (inPath,inProgram,acc)
local
Absyn.Class cdef;
String s1,res, parent_string, result;
Expand All @@ -19107,30 +19081,25 @@ algorithm
Absyn.Program p;
list<Absyn.Class> classes;
list<Option<Absyn.Path>> result_path_lst;
case (SOME(pp),p,indent)
case (SOME(pp),p,acc)
equation
acc = pp::acc;
cdef = getPathedClassInProgram(pp, p);
strlst = getClassnamesInClassList(pp, p, cdef);
parent_string = Absyn.pathString(pp);
result_path_lst = List.map(List.map1(strlst, joinPaths, pp),Util.makeOption);
indent = indent +& " ";
result = stringAppendList(List.map2(result_path_lst,
getClassNamesRecursive, p, indent));
res = stringAppendList({indent, parent_string,"\n",result});
then
res;
case (NONE(),p as Absyn.PROGRAM(classes=classes),indent)
(_,acc) = List.map1Fold(result_path_lst, getClassNamesRecursive, p, acc);
then (inPath,acc);
case (NONE(),p as Absyn.PROGRAM(classes=classes),acc)
equation
strlst = List.map(classes, Absyn.getClassName);
result_path_lst = List.mapMap(strlst, Absyn.makeIdentPathFromString, Util.makeOption);
then stringAppendList(List.map2(result_path_lst, getClassNamesRecursive, p, indent));
case (SOME(pp),_,indent)
(_,acc) = List.map1Fold(result_path_lst, getClassNamesRecursive, p, acc);
then (inPath,acc);
case (SOME(pp),_,_)
equation
parent_string = Absyn.pathString(pp);
indent = indent +& " ";
s1 = Error.printMessagesStr();
res = stringAppendList({indent, parent_string,"\n","PROBLEM GETTING CLASS NAMES: ", s1});
then res;
s1 = Absyn.pathString(pp);
Error.addMessage(Error.LOOKUP_ERROR, {s1,"<TOP>"});
then (inPath,{});
end matchcontinue;
end getClassNamesRecursive;

Expand Down

0 comments on commit 78ea4b7

Please sign in to comment.