From f68f891adbbdc5f6940b6921db61cf3e79cd60e9 Mon Sep 17 00:00:00 2001 From: villberg Date: Mon, 15 Sep 2014 07:58:59 +0000 Subject: [PATCH] Support break and return in functions. Fix for initialization of function local variables refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30274 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../src/fi/semantum/sysdyn/solver/Break.java | 29 +++++++++++++++++++ .../semantum/sysdyn/solver/ForStatement.java | 17 +++++++---- .../fi/semantum/sysdyn/solver/Function.java | 7 ++++- .../fi/semantum/sysdyn/solver/IStatement.java | 6 +++- .../semantum/sysdyn/solver/IfStatement.java | 6 ++-- .../sysdyn/solver/MultiStatement.java | 3 +- .../src/fi/semantum/sysdyn/solver/Parser.java | 10 ++++++- .../src/fi/semantum/sysdyn/solver/Return.java | 29 +++++++++++++++++++ .../fi/semantum/sysdyn/solver/Statement.java | 3 +- .../semantum/sysdyn/solver/StatementList.java | 8 +++-- .../src/fi/semantum/sysdyn/solver/While.java | 10 +++++-- .../sysdyn/solver/parser/ModelParser.java | 2 ++ .../sysdyn/solver/parser/ModelicaParser.jj | 4 +-- .../sysdyn/solver/parser/ModelicaParser.jjt | 4 +-- 14 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java create mode 100644 fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Return.java diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java new file mode 100644 index 00000000..588cfe06 --- /dev/null +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Break.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2013 Semantum Oy. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package fi.semantum.sysdyn.solver; + + +public class Break implements IStatement { + + public Break() { + } + + @Override + public String toString() { + return "break"; + } + + @Override + public ReturnValue evaluate(IEnvironment environment) { + return ReturnValue.BREAK; + } + +} diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForStatement.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForStatement.java index 5dedd79e..bd487b19 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForStatement.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ForStatement.java @@ -27,23 +27,28 @@ public class ForStatement implements IStatement { return "for " + indices + " loop "+ statement + " end for"; } - private void loop(IEnvironment environment, int i) { + private ReturnValue loop(IEnvironment environment, int i) { if(i == indices.size()) { - statement.evaluate(environment); - return; + ReturnValue ret = statement.evaluate(environment); + if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE; + else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN; + else return ReturnValue.CONTINUE; } else { ForIndex index = indices.get(i); Array array = (Array)index.expression.evaluate(environment); for(Object element : array.elements()) { environment.put(index.base.index, element); - loop(environment, i+1); + ReturnValue ret = loop(environment, i+1); + if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE; + else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN; } + return ReturnValue.CONTINUE; } } @Override - public void evaluate(IEnvironment environment) { - loop(environment, 0); + public ReturnValue evaluate(IEnvironment environment) { + return loop(environment, 0); } } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Function.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Function.java index 200cf365..9af1f4b3 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Function.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Function.java @@ -48,7 +48,12 @@ final public class Function implements Fn, IFrame { } for(int i=0;i values = (ArrayList)environment.getSystem().evaluateFunction(environment, fn.base.name, args); for(int i=0;i