Skip to content

Commit

Permalink
- Implemented array subscripting and slicing for assignments in Inter…
Browse files Browse the repository at this point in the history
…active.

- Restructured Main.handleCommand so that it only prints a parser error on an
  actual parser error.
- Fixed List.findMap* to do what it's supposed to do.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15725 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Apr 3, 2013
1 parent 67bc192 commit b23a898
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 280 deletions.
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/CevalFunction.mo
Expand Up @@ -2112,7 +2112,7 @@ algorithm
end match;
end assignRecordComponents;

protected function assignVector
public function assignVector
"This function assigns a part of a vector by replacing the parts indicated by
the subscripts in the old value with the new value."
input Values.Value inNewValue;
Expand Down
5 changes: 3 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -4996,8 +4996,9 @@ algorithm
equation
path = Absyn.crefToPath(cr);
path_str = Absyn.pathString(path);
symbol_table_1 = Interactive.addVarToSymboltable(path_str, Values.CODE(Absyn.C_VARIABLENAME(cr)), tp,
symbol_table);
symbol_table_1 = Interactive.addVarToSymboltable(
DAE.CREF_IDENT(path_str, tp, {}),
Values.CODE(Absyn.C_VARIABLENAME(cr)), Env.emptyEnv, symbol_table);
symbol_table_2 = absynCrefListToInteractiveVarList(rest, symbol_table_1, tp);
then
symbol_table_2;
Expand Down
194 changes: 122 additions & 72 deletions Compiler/Main/Main.mo
Expand Up @@ -135,100 +135,150 @@ algorithm
end matchcontinue;
end makeDebugResult;

protected function parseCommand
"Helper function to handleCommand. First tries to parse the given command as a
list of statements, and if that fails tries to parse it as a collection of
classes. Returns either Interactive.Statements or Absyn.Program based on
which parser succeeds, or neither if a parser error occured."
input String inCommand;
output Option<Interactive.Statements> outStatements;
output Option<Absyn.Program> outProgram;
algorithm
(outStatements, outProgram) := matchcontinue(inCommand)
local
Interactive.Statements stmts;
Absyn.Program prog;
String str;

case (_)
equation
ErrorExt.setCheckpoint("parsestring");
stmts = Parser.parsestringexp(inCommand, "<interactive>");
ErrorExt.delCheckpoint("parsestring");
then
(SOME(stmts), NONE());

case (_)
equation
ErrorExt.rollBack("parsestring");
prog = Parser.parsestring(inCommand, "<interactive>");
then
(NONE(), SOME(prog));

else (NONE(), NONE());

end matchcontinue;
end parseCommand;

protected function handleCommand
"function handleCommand
This function handles the commands in form of strings send to the server
If the command is quit, the function returns false, otherwise it sends
the string to the parse function and returns true."
input String inString;
input Interactive.SymbolTable inInteractiveSymbolTable;
output Boolean outBoolean;
output String outString;
output Interactive.SymbolTable outInteractiveSymbolTable;
"This function handles the commands in form of strings send to the server.
If the command is quit, the function returns false, otherwise it sends the
string to the parse function and returns true."
input String inCommand;
input Interactive.SymbolTable inSymbolTable;
output Boolean outContinue;
output String outResult;
output Interactive.SymbolTable outSymbolTable;
protected
algorithm
(outBoolean,outString,outInteractiveSymbolTable) := matchcontinue (inString,inInteractiveSymbolTable)
(outContinue, outResult, outSymbolTable) :=
matchcontinue(inCommand, inSymbolTable)
local
String str,res_1,res,evalstr,debugstr;
Interactive.SymbolTable isymb,newisymb;
Absyn.Program p,p_1,newprog,iprog;
AbsynDep.Depends aDep;
list<Interactive.Variable> vars_1,vars;
list<Interactive.CompiledCFunction> cf;
list<Interactive.InstantiatedClass> b;
Interactive.Statements exp;
list<Interactive.LoadedFile> lf;

case (str,isymb)
Option<Interactive.Statements> stmts;
Option<Absyn.Program> prog;
Interactive.SymbolTable st;
String result;

case (_, _)
equation
true = Util.strncmp("quit()", str, 6);
true = Util.strncmp("quit()", inCommand, 6);
then
(false,"Ok\n",isymb);
(false, "Ok\n", inSymbolTable);

// Interactively evaluate an algorithm statement or expression
case (str,isymb)
else
equation
ErrorExt.setCheckpoint("parsestring");
//debug_print("Command: don't typeCheck", str);
Debug.fcall0(Flags.DUMP, Print.clearBuf);
Debug.fcall0(Flags.DUMP_GRAPHVIZ, Print.clearBuf);
Debug.fprint(Flags.DUMP,
"\nNot a class definition, trying expresion parser\n");
exp = Parser.parsestringexp(str,"<interactive>");
(evalstr,newisymb) = Interactive.evaluate(exp, isymb, false);
(stmts, prog) = parseCommand(inCommand);
(result, st) =
handleCommand2(stmts, prog, inCommand, inSymbolTable);
result = makeDebugResult(Flags.DUMP, result);
result = makeDebugResult(Flags.DUMP_GRAPHVIZ, result);
then
(true, result, st);

end matchcontinue;
end handleCommand;

protected function handleCommand2
input Option<Interactive.Statements> inStatements;
input Option<Absyn.Program> inProgram;
input String inCommand;
input Interactive.SymbolTable inSymbolTable;
output String outResult;
output Interactive.SymbolTable outSymbolTable;
algorithm
(outResult, outSymbolTable) :=
matchcontinue(inStatements, inProgram, inCommand, inSymbolTable)
local
Interactive.Statements stmts;
Absyn.Program prog, ast;
String result;
Interactive.SymbolTable st;
list<Interactive.Variable> vars;

// Interactively evaluate an algorithm statement or expression.
case (SOME(stmts), NONE(), _, _)
equation
(result, st) = Interactive.evaluate(stmts, inSymbolTable, false);
Debug.fprint(Flags.DUMP, "\n--------------- Parsed expression ---------------\n");
Debug.fcall(Flags.DUMP, Dump.dumpIstmt, exp);
res_1 = makeDebugResult(Flags.DUMP, evalstr);
res = makeDebugResult(Flags.DUMP_GRAPHVIZ, res_1);
ErrorExt.delCheckpoint("parsestring");
Debug.fcall(Flags.DUMP, Dump.dumpIstmt, stmts);
then
(true,res,newisymb);
(result, st);

// Add a class or function to the interactive symbol table.
// If it is a function, type check it.
case (str,
(isymb as Interactive.SYMBOLTABLE(
ast = iprog,depends=aDep,instClsLst = b,
lstVarVal = vars,compiledFunctions = cf,
loadedFiles = lf)))
case (NONE(), SOME(prog), _, Interactive.SYMBOLTABLE(ast = ast, lstVarVal = vars))
equation
ErrorExt.rollBack("parsestring");
// debug_print("Command: typeCheck", str);
Debug.fcall0(Flags.DUMP, Print.clearBuf);
Debug.fcall0(Flags.DUMP_GRAPHVIZ, Print.clearBuf);
Debug.fprint(Flags.DUMP, "\nTrying to parse class definition...\n");
p = Parser.parsestring(str,"<interactive>");
p_1 = Interactive.addScope(p, vars);
vars_1 = Interactive.updateScope(p, vars);
newprog = Interactive.updateProgram(p_1, iprog);
// not needed. the functions will be remove by examining
// build times and files!
prog = Interactive.addScope(prog, vars);
prog = Interactive.updateProgram(prog, ast);
Debug.fprint(Flags.DUMP, "\n--------------- Parsed program ---------------\n");
Debug.fcall(Flags.DUMP_GRAPHVIZ, DumpGraphviz.dump, newprog);
Debug.fcall(Flags.DUMP, Dump.dump, newprog);
res_1 = makeClassDefResult(p_1) "return vector of toplevel classnames";
res_1 = makeDebugResult(Flags.DUMP, res_1);
res = makeDebugResult(Flags.DUMP_GRAPHVIZ, res_1);
isymb = Interactive.SYMBOLTABLE(newprog,aDep,NONE(),b,vars_1,cf,lf);
Debug.fcall(Flags.DUMP_GRAPHVIZ, DumpGraphviz.dump, prog);
Debug.fcall(Flags.DUMP, Dump.dump, prog);
result = makeClassDefResult(prog) "Return vector of toplevel classnames.";
st = Interactive.setSymbolTableAST(inSymbolTable, prog);
then
(true,res,isymb);
(result, st);

case (_,isymb)
// A parser error occured in parseCommand, display the error message. This
// is handled here instead of in parseCommand, since parseCommand does not
// return a result string.
case (NONE(), NONE(), _, _)
equation
Print.printBuf("Error occured building AST\n");
debugstr = Print.getString();
str = stringAppend(debugstr, "Syntax Error\n");
str = stringAppend(str, Error.printMessagesStr());
Print.printBuf("Error occurred building AST\n");
result = Print.getString();
result = stringAppend(result, "Syntax Error\n");
result = stringAppend(result, Error.printMessagesStr());
then
(true,str,isymb);
(result, inSymbolTable);

case (str,isymb)
// A non-parser error occured, display the error message.
case (_, _, _, _)
equation
true = Util.isSome(inStatements) or Util.isSome(inProgram);
result = Error.printMessagesStr();
then
(result, inSymbolTable);

else
equation
true = Util.isSome(inStatements) or Util.isSome(inProgram);
_ = setStackOverflowSignal(false);
Error.addMessage(Error.STACK_OVERFLOW,{str});
Error.addMessage(Error.STACK_OVERFLOW, {inCommand});
then
(true,"",isymb);
("", inSymbolTable);

end matchcontinue;
end handleCommand;
end handleCommand2;

protected function makeClassDefResult
"creates a list of classes of the program to be returned from evaluate"
Expand All @@ -245,13 +295,13 @@ algorithm
equation
names = List.map(cls,Absyn.className);
names = List.map1(names,Absyn.joinPaths,scope);
res = "{" +& stringDelimitList(List.map(names,Absyn.pathString),",") +& "}";
res = "{" +& stringDelimitList(List.map(names,Absyn.pathString),",") +& "}\n";
then res;

case(Absyn.PROGRAM(classes=cls,within_=Absyn.TOP()))
equation
names = List.map(cls,Absyn.className);
res = "{" +& stringDelimitList(List.map(names,Absyn.pathString),",") +& "}";
res = "{" +& stringDelimitList(List.map(names,Absyn.pathString),",") +& "}\n";
then res;

end match;
Expand Down
12 changes: 9 additions & 3 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1498,7 +1498,9 @@ algorithm
("timeTotal", Values.REAL(timeTotal)) ::
("timeSimulation", Values.REAL(timeSimulation)) ::
resultValues);
newst = Interactive.addVarToSymboltable("currentSimulationResult", Values.STRING(result_file), DAE.T_STRING_DEFAULT, st);
newst = Interactive.addVarToSymboltable(
DAE.CREF_IDENT("currentSimulationResult", DAE.T_STRING_DEFAULT, {}),
Values.STRING(result_file), Env.emptyEnv, st);
//Reset PostOptModules flags
Flags.setConfigStringList(Flags.POST_OPT_MODULES, postOptModStringsOrg);
then
Expand Down Expand Up @@ -3554,7 +3556,9 @@ algorithm
("timeTotal", Values.REAL(timeTotal)) ::
("timeSimulation", Values.REAL(timeSimulation)) ::
resultValues);
newst = Interactive.addVarToSymboltable("currentSimulationResult", Values.STRING(result_file), DAE.T_STRING_DEFAULT, inSt);
newst = Interactive.addVarToSymboltable(
DAE.CREF_IDENT("currentSimulationResult", DAE.T_STRING_DEFAULT, {}),
Values.STRING(result_file), Env.emptyEnv, inSt);
then
(inCache,simValue,newst);
else
Expand Down Expand Up @@ -3599,7 +3603,9 @@ algorithm
System.readFile("output.log"),
("simulationTime", Values.REAL(timeSimulation)) ::
{});
newst = Interactive.addVarToSymboltable("currentSimulationResult", Values.STRING(result_file), DAE.T_STRING_DEFAULT, inSt);
newst = Interactive.addVarToSymboltable(
DAE.CREF_IDENT("currentSimulationResult", DAE.T_STRING_DEFAULT, {}),
Values.STRING(result_file), Env.emptyEnv, inSt);
then
(inCache,simValue,newst);
else
Expand Down

0 comments on commit b23a898

Please sign in to comment.