--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+\r
+public class Break implements IStatement {\r
+ \r
+ public Break() {\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ return "break";\r
+ }\r
+ \r
+ @Override\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
+ return ReturnValue.BREAK;\r
+ }\r
+ \r
+}\r
return "for " + indices + " loop "+ statement + " end for";\r
}\r
\r
- private void loop(IEnvironment environment, int i) {\r
+ private ReturnValue loop(IEnvironment environment, int i) {\r
if(i == indices.size()) {\r
- statement.evaluate(environment);\r
- return;\r
+ ReturnValue ret = statement.evaluate(environment);\r
+ if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+ else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+ else return ReturnValue.CONTINUE;\r
} else {\r
ForIndex index = indices.get(i);\r
Array array = (Array)index.expression.evaluate(environment);\r
for(Object element : array.elements()) {\r
environment.put(index.base.index, element);\r
- loop(environment, i+1);\r
+ ReturnValue ret = loop(environment, i+1);\r
+ if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+ else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
}\r
+ return ReturnValue.CONTINUE;\r
}\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
- loop(environment, 0);\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
+ return loop(environment, 0);\r
}\r
\r
}\r
}\r
for(int i=0;i<internals.size();i++) {\r
VariableDeclaration decl = internals.get(i);\r
- decl.variable.assignPlain(environment, null);\r
+ if(decl.modification.args.length == 1) {\r
+ Object o = decl.modification.args[0].modification.evaluate(environment);\r
+ decl.variable.assignPlain(environment, o);\r
+ } else {\r
+ decl.variable.assignPlain(environment, null);\r
+ }\r
}\r
}\r
\r
\r
public interface IStatement {\r
\r
- public void evaluate(IEnvironment environment);\r
+ public enum ReturnValue {\r
+ CONTINUE,BREAK,RETURN;\r
+ }\r
+ \r
+ public ReturnValue evaluate(IEnvironment environment);\r
\r
}\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
if((Boolean)exp.evaluate(environment)) {\r
- t.evaluate(environment);\r
+ return t.evaluate(environment);\r
} else {\r
- e.evaluate(environment);\r
+ return e.evaluate(environment);\r
}\r
}\r
\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
@SuppressWarnings("unchecked")\r
ArrayList<Object> values = (ArrayList<Object>)environment.getSystem().evaluateFunction(environment, fn.base.name, args);\r
for(int i=0;i<targets.size();i++) {\r
targets.get(i).assignPlain(environment, values.get(i));\r
}\r
+ return ReturnValue.CONTINUE;\r
}\r
\r
}\r
// | when_statement() )\r
// comment() \r
\r
- if(n.jjtGetNumChildren() == 1) {\r
+ if(n.jjtGetNumChildren() == 0) {\r
+ if("break".equals(n.op)) {\r
+ return new Break();\r
+ } else if ("return".equals(n.op)) {\r
+ return new Return();\r
+ } else {\r
+ throw new IllegalStateException();\r
+ }\r
+ } else if(n.jjtGetNumChildren() == 1) {\r
return (IStatement)walk((SimpleNode)n.jjtGetChild(0), indent+2, frame);\r
} else if (n.jjtGetNumChildren() == 2) {\r
SimpleNode n0 = (SimpleNode)n.jjtGetChild(1);\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Semantum Oy.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.semantum.sysdyn.solver;\r
+\r
+\r
+public class Return implements IStatement {\r
+ \r
+ public Return() {\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ return "return";\r
+ }\r
+ \r
+ @Override\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
+ return ReturnValue.RETURN;\r
+ }\r
+ \r
+}\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
Object value = expression.evaluate(environment);\r
target.assign(environment, subscripts, value);\r
+ return ReturnValue.CONTINUE;\r
}\r
\r
}\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
- for(IStatement stm : statements) stm.evaluate(environment);\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
+ for(IStatement stm : statements) {\r
+ ReturnValue ret = stm.evaluate(environment);\r
+ if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+ }\r
+ return ReturnValue.CONTINUE;\r
}\r
\r
}\r
}\r
\r
@Override\r
- public void evaluate(IEnvironment environment) {\r
- while ((Boolean)expression.evaluate(environment))\r
- statement.evaluate(environment);\r
+ public ReturnValue evaluate(IEnvironment environment) {\r
+ while ((Boolean)expression.evaluate(environment)) {\r
+ ReturnValue ret = statement.evaluate(environment);\r
+ if(ReturnValue.BREAK.equals(ret)) return ReturnValue.CONTINUE;\r
+ else if(ReturnValue.RETURN.equals(ret)) return ReturnValue.RETURN;\r
+ }\r
+ return ReturnValue.CONTINUE;\r
}\r
\r
}\r
break;\r
case 29:\r
jj_consume_token(29);\r
+ jjtn000.op = "break";\r
break;\r
case 18:\r
jj_consume_token(18);\r
+ jjtn000.op = "return";\r
break;\r
case 31:\r
if_statement();\r
// comment\r
( component_reference() ( ":=" expression() | function_call_args() )\r
| "(" output_expression_list() ")" ":=" component_reference() function_call_args()\r
- | "break"\r
- | "return"\r
+ | "break" { jjtn000.op = "break";}\r
+ | "return" { jjtn000.op = "return";}\r
| if_statement()\r
| for_statement()\r
| while_statement()\r
// comment\r
( component_reference() ( ":=" expression() | function_call_args() )\r
| "(" output_expression_list() ")" ":=" component_reference() function_call_args()\r
- | "break"\r
- | "return"\r
+ | "break" { jjtThis.op = "break";}\r
+ | "return" { jjtThis.op = "return";}\r
| if_statement()\r
| for_statement()\r
| while_statement()\r