diff --git a/Compiler/FrontEnd/CevalFunction.mo b/Compiler/FrontEnd/CevalFunction.mo index 9a8c6b70500..80610e41ce0 100644 --- a/Compiler/FrontEnd/CevalFunction.mo +++ b/Compiler/FrontEnd/CevalFunction.mo @@ -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; diff --git a/Compiler/FrontEnd/Static.mo b/Compiler/FrontEnd/Static.mo index 3bfc44f72ee..0eff1d49a54 100644 --- a/Compiler/FrontEnd/Static.mo +++ b/Compiler/FrontEnd/Static.mo @@ -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; diff --git a/Compiler/Main/Main.mo b/Compiler/Main/Main.mo index e0a69ba6a87..1e5823811ef 100644 --- a/Compiler/Main/Main.mo +++ b/Compiler/Main/Main.mo @@ -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 outStatements; + output Option outProgram; +algorithm + (outStatements, outProgram) := matchcontinue(inCommand) + local + Interactive.Statements stmts; + Absyn.Program prog; + String str; + + case (_) + equation + ErrorExt.setCheckpoint("parsestring"); + stmts = Parser.parsestringexp(inCommand, ""); + ErrorExt.delCheckpoint("parsestring"); + then + (SOME(stmts), NONE()); + + case (_) + equation + ErrorExt.rollBack("parsestring"); + prog = Parser.parsestring(inCommand, ""); + 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 vars_1,vars; - list cf; - list b; - Interactive.Statements exp; - list lf; - - case (str,isymb) + Option stmts; + Option 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,""); - (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 inStatements; + input Option 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 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,""); - 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" @@ -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; diff --git a/Compiler/Script/CevalScript.mo b/Compiler/Script/CevalScript.mo index eb1aee8ca2f..e5cd23e2c71 100644 --- a/Compiler/Script/CevalScript.mo +++ b/Compiler/Script/CevalScript.mo @@ -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 @@ -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 @@ -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 diff --git a/Compiler/Script/Interactive.mo b/Compiler/Script/Interactive.mo index 96279eca4b1..d8467aa7076 100644 --- a/Compiler/Script/Interactive.mo +++ b/Compiler/Script/Interactive.mo @@ -60,6 +60,7 @@ public import Values; // protected imports protected import Builtin; protected import Ceval; +protected import CevalFunction; protected import CevalScript; protected import ClassInf; protected import ComponentReference; @@ -86,6 +87,7 @@ protected import Prefix; protected import Print; protected import Refactor; protected import SimCodeMain; +protected import Static; protected import StaticScript; protected import System; protected import Types; @@ -463,6 +465,9 @@ algorithm Env.Cache cache; Absyn.Info info; Absyn.FunctionArgs fargs; + list asubs; + list dsubs; + list crefs; case (Absyn.ALGORITHMITEM(info=info, algorithm_ = Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"), @@ -505,21 +510,23 @@ algorithm value = getVariableValueLst(Absyn.pathToStringList(Absyn.crefToPath(cr)), vars); str = ValuesUtil.valString(value); t = Types.typeOfValue(value); - newst = addVarToSymboltable(ident, value, t, st); + newst = addVarToSymboltable(DAE.CREF_IDENT(ident, t, {}), value, Env.emptyEnv, st); then (str,newst); case (Absyn.ALGORITHMITEM(info=info,algorithm_ = Absyn.ALG_ASSIGN(assignComponent = - Absyn.CREF(Absyn.CREF_IDENT(name = ident,subscripts = {})),value = exp)), + Absyn.CREF(Absyn.CREF_IDENT(name = ident,subscripts = asubs)),value = exp)), (st as SYMBOLTABLE(ast = p))) equation (env,st) = buildEnvFromSymboltable(st); (cache,sexp,DAE.PROP(_,_),SOME(st_1)) = StaticScript.elabExp(Env.emptyCache(),env, exp, true, SOME(st),true,Prefix.NOPRE(),info); (_,value,SOME(st_2)) = CevalScript.ceval(cache,env, sexp, true,SOME(st_1),Ceval.MSG(info)); + (_, dsubs, _) = Static.elabSubscripts(cache, env, asubs, true, Prefix.NOPRE(), info); + t = Types.typeOfValue(value) "This type can be more specific than the elaborated type; if the dimensions are unknown..."; str = ValuesUtil.valString(value); - newst = addVarToSymboltable(ident, value, t, st_2); + newst = addVarToSymboltable(DAE.CREF_IDENT(ident, t, dsubs), value, env, st_2); then (str,newst); @@ -533,9 +540,9 @@ algorithm (env,st) = buildEnvFromSymboltable(st); (cache,srexp,rprop,SOME(st_1)) = StaticScript.elabExp(Env.emptyCache(),env, rexp, true, SOME(st),true,Prefix.NOPRE(),info); DAE.T_TUPLE(tupleType = types) = Types.getPropType(rprop); - idents = List.map(crefexps, getIdentFromTupleCrefexp); + crefs = makeTupleCrefs(crefexps, types, env, cache, info); (_,Values.TUPLE(values),SOME(st_2)) = CevalScript.ceval(cache, env, srexp, true, SOME(st_1), Ceval.MSG(info)); - newst = addVarsToSymboltable(idents, values, types, st_2); + newst = addVarsToSymboltable(crefs, values, env, st_2); then ("",newst); @@ -929,26 +936,49 @@ algorithm end matchcontinue; end evaluateExprToStr; -protected function getIdentFromTupleCrefexp -"function: getIdentFromTupleCrefexp - Return the (first) identifier of a Component Reference in an expression." - input Absyn.Exp inExp; - output Absyn.Ident outIdent; +protected function makeTupleCrefs + input list inCrefs; + input list inTypes; + input Env.Env inEnv; + input Env.Cache inCache; + input Absyn.Info inInfo; + output list outCrefs; algorithm - outIdent:= - matchcontinue (inExp) + outCrefs := List.threadMap3(inCrefs, inTypes, makeTupleCref, inEnv, inCache, inInfo); +end makeTupleCrefs; + +protected function makeTupleCref + "Translates an Absyn.CREF to a DAE.CREF_IDENT." + input Absyn.Exp inCref; + input DAE.Type inType; + input Env.Env inEnv; + input Env.Cache inCache; + input Absyn.Info inInfo; + output DAE.ComponentRef outCref; +algorithm + outCref := match(inCref, inType, inEnv, inCache, inInfo) local - String id,str; - Absyn.Exp exp; - case Absyn.CREF(componentRef = Absyn.CREF_IDENT(name = id)) then id; - case exp + Absyn.Ident id; + list asubs; + list dsubs; + String str; + + case (Absyn.CREF(componentRef = Absyn.CREF_IDENT(id, asubs)), _, _, _, _) + equation + (_, dsubs, _) = Static.elabSubscripts(inCache, inEnv, asubs, true, + Prefix.NOPRE(), inInfo); + then + DAE.CREF_IDENT(id, inType, dsubs); + + else equation - str = Dump.printExpStr(exp); + str = Dump.printExpStr(inCref); Error.addMessage(Error.INVALID_TUPLE_CONTENT, {str}); then fail(); - end matchcontinue; -end getIdentFromTupleCrefexp; + + end match; +end makeTupleCref; public function getTypeOfVariable "function: getTypeOfVariables @@ -980,72 +1010,30 @@ algorithm end getTypeOfVariable; protected function addVarsToSymboltable -"function: addVarsToSymboltable - Add a list of variables to the interactive - symboltable given names, values and types." - input list inAbsynIdentLst; - input list inValuesValueLst; - input list inTypesTypeLst; + "Adds a list of variables to the interactive symboltable." + input list inCref; + input list inValues; + input Env.Env inEnv; input SymbolTable inSymbolTable; output SymbolTable outSymbolTable; algorithm - outSymbolTable:= - match (inAbsynIdentLst,inValuesValueLst,inTypesTypeLst,inSymbolTable) - local - SymbolTable st,st_1,st_2; - String id; - list idrest; - Values.Value v; - list vrest; - DAE.Type t; - list trest; - case ({},_,_,st) then st; - case ((id :: idrest),(v :: vrest),(t :: trest),st) - equation - st_1 = addVarToSymboltable(id, v, t, st); - st_2 = addVarsToSymboltable(idrest, vrest, trest, st_1); - then - st_2; - end match; + outSymbolTable := List.threadFold1(inCref, inValues, addVarToSymboltable, + inEnv, inSymbolTable); end addVarsToSymboltable; public function addVarToSymboltable -"function: addVarToSymboltable - Helper function to addVarsToSymboltable." - input Absyn.Ident inIdent; + "Adds a variable to the interactive symboltable." + input DAE.ComponentRef inCref; input Values.Value inValue; - input DAE.Type inType; + input Env.Env inEnv; input SymbolTable inSymbolTable; output SymbolTable outSymbolTable; +protected + list vars; algorithm - outSymbolTable:= - match (inIdent,inValue,inType,inSymbolTable) - local - list vars_1,vars; - String ident; - Values.Value v; - DAE.Type t; - Absyn.Program p; - Option> sp; - list id; - list cf; - list lf; - AbsynDep.Depends aDep; - - case (ident,v,t, - SYMBOLTABLE( - ast = p, - depends = aDep, - explodedAst = sp, - instClsLst = id, - lstVarVal = vars, - compiledFunctions = cf, - loadedFiles = lf)) - equation - vars_1 = addVarToVarlist(ident, v, t, vars); - then - SYMBOLTABLE(p,aDep,sp,id,vars_1,cf,lf); - end match; + SYMBOLTABLE(lstVarVal = vars) := inSymbolTable; + vars := addVarToVarList(inCref, inValue, inEnv, vars); + outSymbolTable := setSymbolTableVars(vars, inSymbolTable); end addVarToSymboltable; public function appendVarToSymboltable @@ -1059,68 +1047,24 @@ public function appendVarToSymboltable input DAE.Type inType; input SymbolTable inSymbolTable; output SymbolTable outSymbolTable; +protected + list vars; algorithm - outSymbolTable:= - match (inIdent,inValue,inType,inSymbolTable) - local - list vars_1,vars; - String ident; - Values.Value v; - DAE.Type t; - Absyn.Program p; - Option> sp; - list id; - list cf; - list lf; - AbsynDep.Depends aDep; - - case (ident,v,t, - SYMBOLTABLE( - ast = p, - depends = aDep, - explodedAst = sp, - instClsLst = id, - lstVarVal = vars, - compiledFunctions = cf, - loadedFiles = lf)) - equation - vars_1 = (IVAR(ident,v,t))::vars; - then - SYMBOLTABLE(p,aDep,sp,id,vars_1,cf,lf); - end match; + SYMBOLTABLE(lstVarVal = vars) := inSymbolTable; + vars := IVAR(inIdent, inValue, inType) :: vars; + outSymbolTable := setSymbolTableVars(vars, inSymbolTable); end appendVarToSymboltable; public function deleteVarFromSymboltable input Absyn.Ident inIdent; input SymbolTable inSymbolTable; output SymbolTable outSymbolTable; +protected + list vars; algorithm - outSymbolTable:= - match (inIdent,inSymbolTable) - local - list vars_1,vars; - String ident; - Absyn.Program p; - Option> sp; - list id; - list cf; - list lf; - AbsynDep.Depends aDep; - - case (ident, - SYMBOLTABLE( - ast = p, - depends = aDep, - explodedAst = sp, - instClsLst = id, - lstVarVal = vars, - compiledFunctions = cf, - loadedFiles = lf)) - equation - vars_1 = deleteVarFromVarlist(ident, vars); - then - SYMBOLTABLE(p,aDep,sp,id,vars_1,cf,lf); - end match; + SYMBOLTABLE(lstVarVal = vars) := inSymbolTable; + vars := deleteVarFromVarlist(inIdent, vars); + outSymbolTable := setSymbolTableVars(vars, inSymbolTable); end deleteVarFromSymboltable; protected function deleteVarFromVarlist @@ -1152,35 +1096,99 @@ algorithm end matchcontinue; end deleteVarFromVarlist; -protected function addVarToVarlist -"Assignes a value to a variable with a specific identifier." - input Absyn.Ident inIdent; +protected function addVarToVarList + "Assigns a value to a variable with a specific identifier." + input DAE.ComponentRef inCref; input Values.Value inValue; - input DAE.Type inType; - input list inVariableLst; - output list outVariableLst; + input Env.Env inEnv; + input list inVariables; + output list outVariables; +protected + Boolean found; +algorithm + (outVariables, found) := + List.findMap3(inVariables, addVarToVarList2, inCref, inValue, inEnv); + outVariables := addVarToVarList4(found, inCref, inValue, outVariables); +end addVarToVarList; + +protected function addVarToVarList2 + input Variable inOldVariable; + input DAE.ComponentRef inCref; + input Values.Value inValue; + input Env.Env inEnv; + output Variable outVariable; + output Boolean outFound; +protected + Absyn.Ident id1, id2; +algorithm + IVAR(varIdent = id1) := inOldVariable; + DAE.CREF_IDENT(ident = id2) := inCref; + outFound := stringEq(id1, id2); + outVariable := addVarToVarList3(outFound, inOldVariable, inCref, inValue, inEnv); +end addVarToVarList2; + +protected function addVarToVarList3 + input Boolean inFound; + input Variable inOldVariable; + input DAE.ComponentRef inCref; + input Values.Value inValue; + input Env.Env inEnv; + output Variable outVariable; algorithm - outVariableLst := matchcontinue (inIdent,inValue,inType,inVariableLst) + outVariable := match(inFound, inOldVariable, inCref, inValue, inEnv) local - String ident,id2; - Values.Value v,val2; - DAE.Type t,t2; - list rest,rest_1; - case (ident,v,t,(IVAR(varIdent = id2) :: rest)) + Absyn.Ident id; + Values.Value val; + DAE.Type ty; + list subs; + + // Variable is not a match, keep the old one. + case (false, _, _, _, _) then inOldVariable; + + // Assigning whole variable => return new variable. + case (true, _, DAE.CREF_IDENT(id, ty, {}), _, _) then IVAR(id, inValue, ty); + + // Assigning array slice => update the old variable's value. + case (true, IVAR(id, val, ty), DAE.CREF_IDENT(subscriptLst = subs), _, _) equation - true = stringEq(ident, id2); + (_, val, _) = CevalFunction.assignVector(inValue, val, subs, + Env.emptyCache(), inEnv, NONE()); then - (IVAR(ident,v,t) :: rest); - case (ident,v,t,(IVAR(varIdent = id2,value = val2,type_ = t2) :: rest)) + IVAR(id, val, ty); + + end match; +end addVarToVarList3; + +protected function addVarToVarList4 + input Boolean inFound; + input DAE.ComponentRef inCref; + input Values.Value inValue; + input list inVariables; + output list outVariables; +algorithm + outVariables := match(inFound, inCref, inValue, inVariables) + local + Absyn.Ident id; + DAE.Type ty; + + // Variable was already updated in addVarToVar, do nothing. + case (true, _, _, _) then inVariables; + + // Variable is new, add it to the list of variables. + case (false, DAE.CREF_IDENT(id, ty, {}), _, _) + then IVAR(id, inValue, ty) :: inVariables; + + // Assigning to an array slice is only allowed for variables that have + // already been defined, i.e. that have a size. Print an error otherwise. + case (false, DAE.CREF_IDENT(ident = id, subscriptLst = _ :: _), _, _) equation - false = stringEq(ident, id2); - rest_1 = addVarToVarlist(ident, v, t, rest); + Error.addMessage(Error.SLICE_ASSIGN_NON_ARRAY, {id}); then - (IVAR(id2,val2,t2) :: rest_1); - case (ident,v,t,{}) then {IVAR(ident,v,t)}; - end matchcontinue; -end addVarToVarlist; + fail(); + end match; +end addVarToVarList4; + public function buildEnvFromSymboltable "function: buildEnvFromSymboltable author: PA @@ -9710,54 +9718,6 @@ algorithm end matchcontinue; end addScope; -public function updateScope -"function: updateScope - This function takes a PROGRAM and updates the variable scope to according - to the value of program: - 1. BEGIN_DEFINITION ident appends ident to scope - 2.END_DEFINITION ident removes ident from scope" - input Absyn.Program inProgram; - input list inVariableLst; - output list outVariableLst; -algorithm - outVariableLst := match (inProgram,inVariableLst) - local - list vars; - - case (_,vars) then vars; - end match; -end updateScope; - -protected function removeVarFromVarlist -"function: removeVarFromVarlist - Helper function to updateScope." - input Absyn.Ident inIdent; - input list inVariableLst; - output list outVariableLst; -algorithm - outVariableLst := matchcontinue (inIdent,inVariableLst) - local - String id1,id2; - list rest,rest_1; - Variable v; - - case (_,{}) then {}; - - case (id1,(IVAR(varIdent = id2) :: rest)) - equation - true = stringEq(id1, id2); - then - rest; - - case (id1,((v as IVAR(varIdent = id2)) :: rest)) - equation - false = stringEq(id1, id2); - rest_1 = removeVarFromVarlist(id1, rest); - then - (v :: rest_1); - end matchcontinue; -end removeVarFromVarlist; - protected function getVariableValue "function: getVariableValue Return the value of an interactive variable @@ -19959,6 +19919,22 @@ algorithm end match; end getSymbolTableAST; +protected function setSymbolTableVars + input list inVars; + input SymbolTable inSymbolTable; + output SymbolTable outSymbolTable; +protected + Absyn.Program ast; + AbsynDep.Depends dep; + Option exp_ast; + list cls; + list comp_funcs; + list files; +algorithm + SYMBOLTABLE(ast, dep, exp_ast, cls, _, comp_funcs, files) := inSymbolTable; + outSymbolTable := SYMBOLTABLE(ast, dep, exp_ast, cls, inVars, comp_funcs, files); +end setSymbolTableVars; + public function getFunctionsInProgram input Absyn.Program prog; output list funcs; diff --git a/Compiler/Util/Error.mo b/Compiler/Util/Error.mo index 11a514b539c..bd43ab6423b 100644 --- a/Compiler/Util/Error.mo +++ b/Compiler/Util/Error.mo @@ -588,6 +588,9 @@ public constant Message ERRONEOUS_TYPE_ERROR = MESSAGE(247, TRANSLATION(), ERROR Util.gettext("Got type mismatch error, but matching types %s.\nThis is a ***COMPILER BUG***, please report it to https://trac.openmodelica.org/OpenModelica.")); public constant Message REINIT_MUST_BE_VAR_OR_ARRAY = MESSAGE(225, TRANSLATION(), ERROR(), Util.gettext("The first argument to reinit must be a variable of type Real or an array of such variables.")); +public constant Message SLICE_ASSIGN_NON_ARRAY = MESSAGE(226, SCRIPTING(), ERROR(), + Util.gettext("Cannot assign slice to non-initialized array %s.")); + public constant Message UNBOUND_PARAMETER_WITH_START_VALUE_WARNING = MESSAGE(499, TRANSLATION(), WARNING(), Util.gettext("Parameter %s has no value, and is fixed during initialization (fixed=true), using available start value (start=%s) as default value")); public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(), diff --git a/Compiler/Util/List.mo b/Compiler/Util/List.mo index 88196ff9a99..ffcda5eadea 100644 --- a/Compiler/Util/List.mo +++ b/Compiler/Util/List.mo @@ -8531,7 +8531,7 @@ algorithm then (accum, found); - case (true, _, _, _) then (inAccum, true); + case (true, _, _, _) then (listAppend(listReverse(inAccum), inList), true); case (_, {}, _, _) then (inAccum, false); end match; @@ -8586,7 +8586,7 @@ algorithm then (accum, found); - case (true, _, _, _, _) then (inAccum, true); + case (true, _, _, _, _) then (listAppend(listReverse(inAccum), inList), true); case (_, {}, _, _, _) then (inAccum, false); end match; @@ -8645,7 +8645,7 @@ algorithm then (accum, found); - case (true, _, _, _, _, _) then (inAccum, true); + case (true, _, _, _, _, _) then (listAppend(listReverse(inAccum), inList), true); case (_, {}, _, _, _, _) then (inAccum, false); end match; @@ -8708,7 +8708,7 @@ algorithm then (accum, found); - case (true, _, _, _, _, _, _) then (inAccum, true); + case (true, _, _, _, _, _, _) then (listAppend(listReverse(inAccum), inList), true); case (_, {}, _, _, _, _, _) then (inAccum, false); end match;