Skip to content

Commit

Permalink
- Load dependent libraries when using the command-line (`omc a.mo Mod…
Browse files Browse the repository at this point in the history
…elica` now loads ModelicaServices)

- Allow calling the compiler without running any files (`omc +i=Modelica.Blocks.Examples.Filter`)
- Print a parser error message if we fail to load a library from command-line (also if this is the "main" file)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22881 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 22, 2014
1 parent 213280c commit eca612a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
80 changes: 33 additions & 47 deletions Compiler/Main/Main.mo
Expand Up @@ -320,6 +320,17 @@ algorithm
end matchcontinue;
end isModelicaFile;

protected function isEmptyOrFirstIsModelicaFile
input list<String> libs;
algorithm
_ := match libs
local
String f;
case {} then ();
case f::_ equation isModelicaFile(f); then ();
end match;
end isEmptyOrFirstIsModelicaFile;

protected function isFlatModelicaFile
"Succeeds if filename ends with .mof"
input String filename;
Expand Down Expand Up @@ -430,12 +441,12 @@ algorithm
end matchcontinue;
end parsePathFromString;

protected function loadLibs
input list<String> inLibs;
protected function loadLib
input String inLib;
input GlobalScript.SymbolTable inSymTab;
output GlobalScript.SymbolTable outSymTab;
algorithm
outSymTab := matchcontinue(inLibs, inSymTab)
outSymTab := matchcontinue(inLib, inSymTab)
local
String lib, mp, f;
list<String> rest;
Expand All @@ -447,40 +458,39 @@ algorithm
GlobalScript.SymbolTable st, newst;
Absyn.Path path;

// no libs or end, return!
case ({}, st) then st;

// A .mo-file.
case (f :: rest, GlobalScript.SYMBOLTABLE(p, _, ic, iv, cf, lf))
case (f, GlobalScript.SYMBOLTABLE(p, _, ic, iv, cf, lf))
equation
isModelicaFile(f);
pnew = Parser.parse(f,"UTF-8");
pnew = Interactive.updateProgram(pnew, p);
newst = GlobalScript.SYMBOLTABLE(pnew, NONE(), ic, iv, cf, lf);
newst = loadLibs(rest, newst);
then
newst;

// some libs present
case (lib::rest, GlobalScript.SYMBOLTABLE(p,_,ic,iv,cf,lf))
case (lib, GlobalScript.SYMBOLTABLE(p,_,ic,iv,cf,lf))
equation
failure(isModelicaFile(lib));
path = parsePathFromString(lib);
mp = Settings.getModelicaPath(Config.getRunningTestsuite());
pnew = ClassLoader.loadClass(path, {"default"}, mp, NONE());
pnew = Interactive.updateProgram(pnew, p);
(pnew, true) = CevalScript.loadModel({(path, {"default"})}, mp, p, true, true, true);
newst = GlobalScript.SYMBOLTABLE(pnew,NONE(),ic,iv,cf,lf);
newst = loadLibs(rest, newst); // load the remaining
then
newst;
// problem with the libs, ignore!
case (lib::rest, st)
case (f, st)
equation
Print.printErrorBuf("Failed to load library: " +& lib +& " ... ignoring!\n");
newst = loadLibs(rest, st); // load the remaining
then
newst;
failure(isModelicaFile(f));
Print.printErrorBuf("Failed to load library: " +& f +& "!\n");
then fail();
case (f, st)
equation
isModelicaFile(f);
Print.printErrorBuf("Failed to parse file: " +& f +& "!\n");
then fail();
end matchcontinue;
end loadLibs;
end loadLib;

protected function translateFile
"This function invokes the translator on a source file. The argument should be
Expand All @@ -505,31 +515,23 @@ algorithm

// A .mo-file, followed by an optional list of extra .mo-files and libraries.
// The last class in the first file will be instantiated.
case (f :: libs)
case (libs)
equation
//print("Class to instantiate: " +& Config.classToInstantiate() +& "\n");
isEmptyOrFirstIsModelicaFile(libs);
System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT);
System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT_CUMULATIVE);
// Check that it's a .mo-file.
isModelicaFile(f);
// Parse the first file.
(p as Absyn.PROGRAM(classes = _)) = Parser.parse(f,"UTF-8");
// Parse libraries and extra mo-files that might have been given at the command line.
GlobalScript.SYMBOLTABLE(ast = pLibs) = loadLibs(libs, GlobalScript.emptySymboltable);
GlobalScript.SYMBOLTABLE(ast = p) = List.fold(libs, loadLib, GlobalScript.emptySymboltable);
// Show any errors that occured during parsing.
showErrors(Print.getErrorString(), ErrorExt.printMessagesStr(false));

// Merge our program with the possible libs and models from extra .mo-files.
p = Interactive.updateProgram(pLibs, p);

Debug.fprint(Flags.DUMP, "\n--------------- Parsed program ---------------\n");
Debug.fcall(Flags.DUMP_GRAPHVIZ, DumpGraphviz.dump, p);
Debug.fcall(Flags.DUMP, Dump.dump, p);
s = Debug.fcallret0(Flags.DUMP, Print.getString, "");
Debug.fcall(Flags.DUMP,print,s);

p = transformFlatProgram(p,f);

SimCodeUtil.execStat("Parsed file");

// Instantiate the program.
Expand Down Expand Up @@ -567,7 +569,7 @@ algorithm
equation
isModelicaScriptFile(f);
// loading possible libraries given at the command line
st = loadLibs(libs, GlobalScript.emptySymboltable);
st = List.fold(libs, loadLib, GlobalScript.emptySymboltable);

//System.startTimer();
//print("\nParseExp");
Expand Down Expand Up @@ -612,22 +614,6 @@ algorithm
end matchcontinue;
end translateFile;

protected function transformFlatProgram
"Transforms the variables in equations to have the same format as for variables,
i.e. a.b[3].c[2] becomes CREF_IDENT(\"a.b[3].c\",[INDEX(ICONST(2))])"
input Absyn.Program p;
input String filename;
output Absyn.Program outP;
algorithm
outP := matchcontinue(p,filename)
case(_,_) equation
isFlatModelicaFile(filename);
outP = Interactive.transformFlatProgram(p);
then outP;
case (_,_) then p;
end matchcontinue;
end transformFlatProgram;

protected function instantiate
"Translates the Absyn.Program to SCode and instantiates either a given class
specified by the +i flag on the command line, or the last class in the
Expand Down Expand Up @@ -1053,7 +1039,7 @@ algorithm
interactivemodeCorba(symbolTable);
then ();

case _::_
case _
equation
false = Flags.isSet(Flags.INTERACTIVE);
false = Flags.isSet(Flags.INTERACTIVE_CORBA);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Script/CevalScript.mo
Expand Up @@ -716,7 +716,7 @@ algorithm
end matchcontinue;
end loadFile;

protected function loadModel
public function loadModel
input list<tuple<Absyn.Path,list<String>>> imodelsToLoad;
input String modelicaPath;
input Absyn.Program ip;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -1961,7 +1961,7 @@ public function printUsage
algorithm
Print.clearBuf();
Print.printBuf("OpenModelica Compiler "); Print.printBuf(Settings.getVersionNr()); Print.printBuf("\n");
Print.printBuf(System.gettext("Copyright © 2013 Open Source Modelica Consortium (OSMC)\n"));
Print.printBuf(System.gettext("Copyright © 2014 Open Source Modelica Consortium (OSMC)\n"));
Print.printBuf(System.gettext("Distributed under OMSC-PL and GPL, see www.openmodelica.org\n\n"));
//Print.printBuf("Please check the System Guide for full information about flags.\n");
Print.printBuf(System.gettext("Usage: omc [-runtimeOptions +omcOptions] (Model.mo | Script.mos) [Libraries | .mo-files] \n* Libraries: Fully qualified names of libraries to load before processing Model or Script.\n The libraries should be separated by spaces: Lib1 Lib2 ... LibN.\n* runtimeOptions: call omc -help to see runtime options\n"));
Expand Down

0 comments on commit eca612a

Please sign in to comment.