Skip to content

Commit

Permalink
Support for *break* and *return*
Browse files Browse the repository at this point in the history
Missing: (a) checking that used inside for/while and (for *return*) in function body (b) reading from flat Modelica (for/while if flat Modelica not yet supported)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2680 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
g-pavgr committed Feb 4, 2007
1 parent 96610ee commit c7c5a54
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Compiler/Absyn.mo
Expand Up @@ -538,6 +538,12 @@ uniontype Algorithm "The Algorithm type describes one algorithm statement in an
Algorithm equ;
end ALG_EQUALITY;

record ALG_RETURN
end ALG_RETURN;

record ALG_BREAK
end ALG_BREAK;

end Algorithm;

public
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Algorithm.mo
Expand Up @@ -128,6 +128,13 @@ uniontype Statement "There are four kinds of statements. Assignments (`a := b;\
Exp.Exp var "Variable";
Exp.Exp value "Value ";
end REINIT;

record RETURN
end RETURN;

record BREAK
end BREAK;

end Statement;

public
Expand Down
13 changes: 13 additions & 0 deletions Compiler/Codegen.mo
Expand Up @@ -1930,6 +1930,7 @@ algorithm
(res_var_fn,tnr_res) := generateResultVars(dae, ret_var, tnr_alg, CONTEXT(FUNCTION(),NORMAL()));
cfn_1 := cMergeFn(mem_fn, var_fn);
cfn_2 := cMergeFn(cfn_1, alg_fn);
cfn_2 := cAddStatements(cfn_2, {"", "_return:"});
cfn_3 := cMergeFn(cfn_2, res_var_fn);
cfn := cAddCleanups(cfn_3, {mem_stmt2,ret_stmt});
end generateFunctionBody;
Expand Down Expand Up @@ -2521,6 +2522,18 @@ algorithm
cfn = cMergeFns({cfn1,cfn2_1});
then
(cfn,tnr2);
case (Algorithm.RETURN(),tnr,_)
local Lib retStmt;
equation
cfn = cAddStatements(cEmptyFunction, {"goto _return;"});
then
(cfn,tnr);
case (Algorithm.BREAK(),tnr,_)
local Lib retStmt;
equation
cfn = cAddStatements(cEmptyFunction, {"break;"});
then
(cfn,tnr);
case (stmt,_,_)
local Algorithm.Statement stmt;
equation
Expand Down
12 changes: 12 additions & 0 deletions Compiler/DAE.mo
Expand Up @@ -2100,6 +2100,12 @@ algorithm
Print.printBuf(");\n");
then
();
case (Algorithm.BREAK(),i)
equation
indent(i);
Print.printBuf("break;\n");
then
();
case (_,i)
equation
indent(i);
Expand Down Expand Up @@ -2231,6 +2237,12 @@ algorithm
str = Util.stringAppendList({s1,"assert(",cond_str,", ",msg_str,");\n"});
then
str;
case (Algorithm.BREAK(),i)
equation
s1 = indentStr(i);
str = stringAppend(s1, "break;\n");
then
str;
case (_,i)
equation
s1 = indentStr(i);
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Dump.mo
Expand Up @@ -3008,6 +3008,12 @@ algorithm
str_1 = stringAppend(str, ";");
then
str_1;
case (i,Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_RETURN(),comment = optcmt)) /* ALG_RETURN */
then
"return;";
case (i,Absyn.ALGORITHMITEM(algorithm_ = Absyn.ALG_BREAK(),comment = optcmt)) /* ALG_BREAK */
then
"break;";
case (_,_)
equation
Print.printErrorBuf("#Error, unparse_algorithm_str failed\n");
Expand Down
14 changes: 14 additions & 0 deletions Compiler/Inst.mo
Expand Up @@ -7917,6 +7917,20 @@ algorithm
then
(cache,stmt);

// break
case (cache,env,Absyn.ALG_BREAK,impl)
equation
stmt = Algorithm.BREAK();
then
(cache,stmt);

// return
case (cache,env,Absyn.ALG_RETURN,impl)
equation
stmt = Algorithm.RETURN();
then
(cache,stmt);

case (cache,env,alg,impl)
equation
Debug.fprint("failtrace", "- inst_statement failed\n alg:");
Expand Down
2 changes: 2 additions & 0 deletions Compiler/absyn_builder/walker.g
Expand Up @@ -1413,6 +1413,8 @@ algorithm returns [void* ast]
| ast = for_clause_a
| ast = while_clause
| ast = when_clause_a
| BREAK { ast = Absyn__ALG_5fBREAK; }
| RETURN { ast = Absyn__ALG_5fRETURN; }
| #(FAILURE ast = fa:algorithm)
| #(EQUALITY ast = eq:algorithm)
)
Expand Down
2 changes: 2 additions & 0 deletions modelica_parser/src/modelica_lexer.g
Expand Up @@ -115,6 +115,8 @@ tokens {
WHEN = "when" ;
WHILE = "while" ;
WITHIN = "within" ;
RETURN = "return" ;
BREAK = "break" ;
/* MetaModelica keywords. I guess not all are needed here. */
AS = "as" ;
CASE = "case" ;
Expand Down
2 changes: 2 additions & 0 deletions modelica_parser/src/modelica_parser.g
Expand Up @@ -615,6 +615,8 @@ algorithm :
| for_clause_a
| while_clause
| when_clause_a
| BREAK
| RETURN
| FAILURE^ LPAR! algorithm RPAR!
| EQUALITY^ LPAR! algorithm RPAR!
)
Expand Down

0 comments on commit c7c5a54

Please sign in to comment.