Skip to content

Commit

Permalink
Fix stack overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Feb 22, 2016
1 parent 7153a59 commit 08f36de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 59 deletions.
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -6721,6 +6721,16 @@ algorithm
end match;
end isElementItem;

public function isAlgorithmItem
input AlgorithmItem inAlg;
output Boolean outIsClass;
algorithm
outIsClass := match inAlg
case ALGORITHMITEM() then true;
else false;
end match;
end isAlgorithmItem;

public function isElementItemClassNamed
input String inName;
input ElementItem inElement;
Expand Down
105 changes: 46 additions & 59 deletions Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -795,37 +795,24 @@ end translateClassdefInitialalgorithms;

public function translateClassdefAlgorithmitems
input list<Absyn.AlgorithmItem> inStatements;
output list<SCode.Statement> outStatements = {};
protected
SCode.Comment cmt;
SourceInfo info;
SCode.Statement s;
algorithm
for stmt in inStatements loop
_ := match stmt
case Absyn.ALGORITHMITEM(info = info)
algorithm
(cmt, info) := translateCommentWithLineInfoChanges(stmt.comment, info);
s := translateClassdefAlgorithmItem(stmt.algorithm_, cmt, info);
outStatements := s :: outStatements;
then
();

else ();
end match;
end for;

outStatements := listReverse(outStatements);
output list<SCode.Statement> outStatements;
algorithm
outStatements := list(translateClassdefAlgorithmItem(stmt) for stmt guard Absyn.isAlgorithmItem(stmt) in inStatements);
end translateClassdefAlgorithmitems;

protected function translateClassdefAlgorithmItem
"Translates an Absyn algorithm (statement) into SCode statement."
input Absyn.Algorithm inAlgorithm;
input SCode.Comment inComment;
input SourceInfo inInfo;
input Absyn.AlgorithmItem inAlgorithm;
output SCode.Statement outStatement;
protected
Option<Absyn.Comment> absynComment;
SCode.Comment comment;
SourceInfo info;
Absyn.Algorithm alg;
algorithm
outStatement := match inAlgorithm
Absyn.ALGORITHMITEM(algorithm_=alg, comment=absynComment, info=info) := inAlgorithm;
(comment, info) := translateCommentWithLineInfoChanges(absynComment, info);
outStatement := match alg
local
list<SCode.Statement> body, else_body;
list<tuple<Absyn.Exp, list<SCode.Statement>>> branches;
Expand All @@ -836,101 +823,101 @@ algorithm
Absyn.ComponentRef cr;

case Absyn.ALG_ASSIGN()
then SCode.ALG_ASSIGN(inAlgorithm.assignComponent, inAlgorithm.value,
inComment, inInfo);
then SCode.ALG_ASSIGN(alg.assignComponent, alg.value,
comment, info);

case Absyn.ALG_IF()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.trueBranch);
else_body := translateClassdefAlgorithmitems(inAlgorithm.elseBranch);
branches := translateAlgBranches(inAlgorithm.elseIfAlgorithmBranch);
body := translateClassdefAlgorithmitems(alg.trueBranch);
else_body := translateClassdefAlgorithmitems(alg.elseBranch);
branches := translateAlgBranches(alg.elseIfAlgorithmBranch);
then
SCode.ALG_IF(inAlgorithm.ifExp, body, branches, else_body, inComment, inInfo);
SCode.ALG_IF(alg.ifExp, body, branches, else_body, comment, info);

case Absyn.ALG_FOR()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.forBody);
body := translateClassdefAlgorithmitems(alg.forBody);

// Convert for-loops with multiple iterators into nested for-loops.
for i in listReverse(inAlgorithm.iterators) loop
(iter_name, iter_range) := translateIterator(i, inInfo);
body := {SCode.ALG_FOR(iter_name, iter_range, body, inComment, inInfo)};
for i in listReverse(alg.iterators) loop
(iter_name, iter_range) := translateIterator(i, info);
body := {SCode.ALG_FOR(iter_name, iter_range, body, comment, info)};
end for;
then
listHead(body);

case Absyn.ALG_PARFOR()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.parforBody);
body := translateClassdefAlgorithmitems(alg.parforBody);

// Convert for-loops with multiple iterators into nested for-loops.
for i in listReverse(inAlgorithm.iterators) loop
(iter_name, iter_range) := translateIterator(i, inInfo);
body := {SCode.ALG_PARFOR(iter_name, iter_range, body, inComment, inInfo)};
for i in listReverse(alg.iterators) loop
(iter_name, iter_range) := translateIterator(i, info);
body := {SCode.ALG_PARFOR(iter_name, iter_range, body, comment, info)};
end for;
then
listHead(body);

case Absyn.ALG_WHILE()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.whileBody);
body := translateClassdefAlgorithmitems(alg.whileBody);
then
SCode.ALG_WHILE(inAlgorithm.boolExpr, body, inComment, inInfo);
SCode.ALG_WHILE(alg.boolExpr, body, comment, info);

case Absyn.ALG_WHEN_A()
algorithm
branches := translateAlgBranches((inAlgorithm.boolExpr, inAlgorithm.whenBody)
:: inAlgorithm.elseWhenAlgorithmBranch);
branches := translateAlgBranches((alg.boolExpr, alg.whenBody)
:: alg.elseWhenAlgorithmBranch);
then
SCode.ALG_WHEN_A(branches, inComment, inInfo);
SCode.ALG_WHEN_A(branches, comment, info);

// assert(condition, message)
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2}, argNames = {}))
then SCode.ALG_ASSERT(e1, e2, ASSERTION_LEVEL_ERROR, inComment, inInfo);
then SCode.ALG_ASSERT(e1, e2, ASSERTION_LEVEL_ERROR, comment, info);

// assert(condition, message, level)
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2, e3}, argNames = {}))
then SCode.ALG_ASSERT(e1, e2, e3, inComment, inInfo);
then SCode.ALG_ASSERT(e1, e2, e3, comment, info);

// assert(condition, message, level = arg)
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2},
argNames = {Absyn.NAMEDARG("level", e3)}))
then SCode.ALG_ASSERT(e1, e2, e3, inComment, inInfo);
then SCode.ALG_ASSERT(e1, e2, e3, comment, info);

case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "terminate"),
functionArgs = Absyn.FUNCTIONARGS(args = {e1}, argNames = {}))
then SCode.ALG_TERMINATE(e1, inComment, inInfo);
then SCode.ALG_TERMINATE(e1, comment, info);

case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "reinit"),
functionArgs = Absyn.FUNCTIONARGS(args = {Absyn.CREF(componentRef = cr), e2},
argNames = {}))
then SCode.ALG_REINIT(cr, e2, inComment, inInfo);
then SCode.ALG_REINIT(cr, e2, comment, info);

case Absyn.ALG_NORETCALL()
algorithm
e1 := Absyn.CALL(inAlgorithm.functionCall, inAlgorithm.functionArgs);
e1 := Absyn.CALL(alg.functionCall, alg.functionArgs);
then
SCode.ALG_NORETCALL(e1, inComment, inInfo);
SCode.ALG_NORETCALL(e1, comment, info);

case Absyn.ALG_FAILURE()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.equ);
body := translateClassdefAlgorithmitems(alg.equ);
then
SCode.ALG_FAILURE(body, inComment, inInfo);
SCode.ALG_FAILURE(body, comment, info);

case Absyn.ALG_TRY()
algorithm
body := translateClassdefAlgorithmitems(inAlgorithm.body);
else_body := translateClassdefAlgorithmitems(inAlgorithm.elseBody);
body := translateClassdefAlgorithmitems(alg.body);
else_body := translateClassdefAlgorithmitems(alg.elseBody);
then
SCode.ALG_TRY(body, else_body, inComment, inInfo);
SCode.ALG_TRY(body, else_body, comment, info);

case Absyn.ALG_RETURN() then SCode.ALG_RETURN(inComment, inInfo);
case Absyn.ALG_BREAK() then SCode.ALG_BREAK(inComment, inInfo);
case Absyn.ALG_CONTINUE() then SCode.ALG_CONTINUE(inComment, inInfo);
case Absyn.ALG_RETURN() then SCode.ALG_RETURN(comment, info);
case Absyn.ALG_BREAK() then SCode.ALG_BREAK(comment, info);
case Absyn.ALG_CONTINUE() then SCode.ALG_CONTINUE(comment, info);

end match;
end translateClassdefAlgorithmItem;
Expand Down

0 comments on commit 08f36de

Please sign in to comment.