From 7080d3856bb9041f0fec5e8cb405d70001766c4e Mon Sep 17 00:00:00 2001 From: Kostadin Cvejoski Date: Tue, 22 Sep 2015 11:18:11 +0200 Subject: [PATCH] ORA-664 implementatio for the math power block --- .../codegen/ev3/Ast2Ev3JavaScriptVisitor.java | 7 ++- .../codegen/ev3/Ast2Ev3JavaVisitor.java | 56 +++++++++---------- .../codegen/ev3/Ast2Ev3PythonVisitor.java | 2 +- .../ev3/AstDefaultVisitorCombining.java | 2 +- .../codegen/ev3/AstToTextlyVisitor.java | 2 +- .../syntax/functions/MathPowerFunct.java | 2 +- .../syntax/hardwarecheck/CheckVisitor.java | 4 +- .../iais/roberta/typecheck/InfoCollector.java | 2 +- .../roberta/typecheck/TypecheckVisitor.java | 2 +- .../visitor/AstDefaultVisitorInspecting.java | 2 +- .../fhg/iais/roberta/visitor/AstVisitor.java | 10 ++-- .../ast/syntax/expr/MathRoundTest.java | 2 +- .../ast/syntax/expr/MathSingleTest.java | 6 +- .../roberta/ast/syntax/expr/MathTrigTest.java | 4 +- .../simulation/robertaLogic/programEval.js | 2 +- 15 files changed, 53 insertions(+), 52 deletions(-) diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaScriptVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaScriptVisitor.java index dedb8acda..2eb853b64 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaScriptVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaScriptVisitor.java @@ -173,7 +173,12 @@ public Void visitBinary(Binary binary) { } @Override - public Void visitFunc(MathPowerFunct func) { + public Void visitMathPowerFunct(MathPowerFunct mathPowerFunct) { + this.sb.append("createBinaryExpr(" + mathPowerFunct.getFunctName() + ", "); + mathPowerFunct.getParam().get(0).visit(this); + this.sb.append(", "); + mathPowerFunct.getParam().get(1).visit(this); + this.sb.append(")"); return null; } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaVisitor.java index d766085c6..8d7cd70c0 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3JavaVisitor.java @@ -441,20 +441,6 @@ public Void visitExprList(ExprList exprList) { return null; } - @Override - public Void visitFunc(MathPowerFunct funct) { - // switch ( funct.getFunctName() ) { - // case PRINT: - // this.sb.append("System.out.println("); - // funct.getParam().get(0).visit(this); - // this.sb.append(")"); - // break; - // default: - // break; - // } - return null; - } - @Override public Void visitActionStmt(ActionStmt actionStmt) { actionStmt.getAction().visit(this); @@ -1184,59 +1170,69 @@ public Void visitMathRandomIntFunct(MathRandomIntFunct mathRandomIntFunct) public Void visitMathSingleFunct(MathSingleFunct mathSingleFunct) { switch ( mathSingleFunct.getFunctName() ) { case ROOT: - this.sb.append("Math.sqrt("); + this.sb.append("((float) Math.sqrt("); break; case ABS: - this.sb.append("Math.abs("); + this.sb.append("((float) Math.abs("); break; case LN: - this.sb.append("Math.log("); + this.sb.append("((float) Math.log("); break; case LOG10: - this.sb.append("Math.log10("); + this.sb.append("((float) Math.log10("); break; case EXP: - this.sb.append("Math.exp("); + this.sb.append("((float) Math.exp("); break; case POW10: - this.sb.append("Math.pow(10, "); + this.sb.append("((float) Math.pow(10, "); break; case SIN: - this.sb.append("Math.sin("); + this.sb.append("((float) Math.sin("); break; case COS: - this.sb.append("Math.cos("); + this.sb.append("((float) Math.cos("); break; case TAN: - this.sb.append("Math.tan("); + this.sb.append("((float) Math.tan("); break; case ASIN: - this.sb.append("Math.asin("); + this.sb.append("((float) Math.asin("); break; case ATAN: - this.sb.append("Math.atan("); + this.sb.append("((float) Math.atan("); break; case ACOS: - this.sb.append("Math.acos("); + this.sb.append("((float) Math.acos("); break; case ROUND: - this.sb.append("Math.round("); + this.sb.append("((float) Math.round("); break; case ROUNDUP: - this.sb.append("Math.ceil("); + this.sb.append("((float) Math.ceil("); break; case ROUNDDOWN: - this.sb.append("Math.floor("); + this.sb.append("((float) Math.floor("); break; default: break; } mathSingleFunct.getParam().get(0).visit(this); - this.sb.append(")"); + this.sb.append("))"); return null; } + @Override + public Void visitMathPowerFunct(MathPowerFunct mathPowerFunct) { + this.sb.append("((float) Math.pow("); + mathPowerFunct.getParam().get(0).visit(this); + this.sb.append(", "); + mathPowerFunct.getParam().get(1).visit(this); + this.sb.append("))"); + return null; + } + @Override public Void visitTextJoinFunct(TextJoinFunct textJoinFunct) { boolean isFirst = true; diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3PythonVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3PythonVisitor.java index 70f785fc7..e61f59653 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3PythonVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/Ast2Ev3PythonVisitor.java @@ -401,7 +401,7 @@ public Void visitExprList(ExprList exprList) { } @Override - public Void visitFunc(MathPowerFunct funct) { + public Void visitMathPowerFunct(MathPowerFunct mathPowerFunct) { // switch ( funct.getFunctName() ) { // case PRINT: // this.sb.append("System.out.println("); diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstDefaultVisitorCombining.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstDefaultVisitorCombining.java index 9097314f3..f3cc0e0d8 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstDefaultVisitorCombining.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstDefaultVisitorCombining.java @@ -149,7 +149,7 @@ public V visitBinary(Binary binary) { } @Override - public V visitFunc(MathPowerFunct func) { + public V visitMathPowerFunct(MathPowerFunct func) { return defaultResult(); } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstToTextlyVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstToTextlyVisitor.java index dbd903e8e..326837b85 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstToTextlyVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/codegen/ev3/AstToTextlyVisitor.java @@ -278,7 +278,7 @@ public Void visitExprList(ExprList exprList) { } @Override - public Void visitFunc(MathPowerFunct funct) { + public Void visitMathPowerFunct(MathPowerFunct funct) { // switch ( funct.getFunctName() ) { // case PRINT: // this.sb.append("System.out.println("); diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/functions/MathPowerFunct.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/functions/MathPowerFunct.java index ce1ecd4eb..6d0d3fdc8 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/functions/MathPowerFunct.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/functions/MathPowerFunct.java @@ -77,7 +77,7 @@ public Assoc getAssoc() { @Override protected V accept(AstVisitor visitor) { - return visitor.visitFunc(this); + return visitor.visitMathPowerFunct(this); } @Override diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/hardwarecheck/CheckVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/hardwarecheck/CheckVisitor.java index 9b510ac83..ccf377be8 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/hardwarecheck/CheckVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/syntax/hardwarecheck/CheckVisitor.java @@ -130,8 +130,8 @@ public Void visitBinary(Binary binary) { } @Override - public Void visitFunc(MathPowerFunct func) { - for ( Expr expr : func.getParam() ) { + public Void visitMathPowerFunct(MathPowerFunct mathPowerFunct) { + for ( Expr expr : mathPowerFunct.getParam() ) { expr.visit(this); } return null; diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/InfoCollector.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/InfoCollector.java index d00bdadc3..12382220c 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/InfoCollector.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/InfoCollector.java @@ -169,7 +169,7 @@ public T visitBinary(Binary binary) { } @Override - public T visitFunc(MathPowerFunct func) { + public T visitMathPowerFunct(MathPowerFunct func) { extractInfos(func); return null; } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/TypecheckVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/TypecheckVisitor.java index 8b91581a9..978d85e3b 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/TypecheckVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/typecheck/TypecheckVisitor.java @@ -235,7 +235,7 @@ public BlocklyType visitBinary(Binary binary) { } @Override - public BlocklyType visitFunc(MathPowerFunct func) { + public BlocklyType visitMathPowerFunct(MathPowerFunct func) { List paramTypes = typecheckList(func.getParam()); Sig signature = TypeTransformations.getFunctionSignature(func.getFunctName().name()); BlocklyType resultType = signature.typeCheck(func, paramTypes); diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstDefaultVisitorInspecting.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstDefaultVisitorInspecting.java index d5b95465e..393a4dcb4 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstDefaultVisitorInspecting.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstDefaultVisitorInspecting.java @@ -118,7 +118,7 @@ public Void visitBinary(Binary binary) { } @Override - public Void visitFunc(MathPowerFunct func) { + public Void visitMathPowerFunct(MathPowerFunct func) { return null; } diff --git a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstVisitor.java b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstVisitor.java index 99cb5a96f..82296f5d7 100644 --- a/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstVisitor.java +++ b/OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/visitor/AstVisitor.java @@ -163,7 +163,7 @@ public interface AstVisitor { * * @param funct to be visited */ - public V visitFunc(MathPowerFunct func); + public V visitMathPowerFunct(MathPowerFunct mathPowerFunct); /** * visit a {@link ActionExpr}. @@ -627,28 +627,28 @@ public interface AstVisitor { * @param methodStmt to be visited */ public V visitMethodCall(MethodCall methodCall); - + /** * visit a {@link BluetoothRecieveAction}. * * @param bluetoothReceiveActionbluetoothReceiveAction to be visited */ public V visitBluetoothReceiveAction(BluetoothReceiveAction bluetoothReceiveAction); - + /** * visit a {@link BluetoothConnectAction}. * * @param bluetoothConnectAction to be visited */ public V visitBluetoothConnectAction(BluetoothConnectAction bluetoothConnectAction); - + /** * visit a {@link BluetoothSendAction}. * * @param bluetoothSendAction to be visited */ public V visitBluetoothSendAction(BluetoothSendAction bluetoothSendAction); - + /** * visit a {@link BluetoothWaitForConnectionAction}. * diff --git a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathRoundTest.java b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathRoundTest.java index 750a03698..1e94c648c 100644 --- a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathRoundTest.java +++ b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathRoundTest.java @@ -7,7 +7,7 @@ public class MathRoundTest { @Test public void Test() throws Exception { - String a = "Math.round(0)Math.ceil(0)Math.floor(0)"; + String a = "((float)Math.round(0))((float)Math.ceil(0))((float)Math.floor(0))"; Helper.assertCodeIsOk(a, "/syntax/math/math_round.xml"); } diff --git a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathSingleTest.java b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathSingleTest.java index 2f36ebd75..bb1909250 100644 --- a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathSingleTest.java +++ b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathSingleTest.java @@ -7,21 +7,21 @@ public class MathSingleTest { @Test public void Test() throws Exception { - String a = "Math.sqrt(0)Math.abs(0)-0Math.log(0)Math.log10(0)Math.exp(0)Math.pow(10,0)"; + String a = "((float)Math.sqrt(0))((float)Math.abs(0))-0((float)Math.log(0))((float)Math.log10(0))((float)Math.exp(0))((float)Math.pow(10,0))"; Helper.assertCodeIsOk(a, "/syntax/math/math_single.xml"); } @Test public void Test1() throws Exception { - String a = "hal.setVolume(Math.sqrt(0));"; + String a = "hal.setVolume(((float)Math.sqrt(0)));"; Helper.assertCodeIsOk(a, "/syntax/math/math_single1.xml"); } @Test public void Test2() throws Exception { - String a = "item=Math.sqrt(0);"; + String a = "item=((float)Math.sqrt(0));"; Helper.assertCodeIsOk(a, "/syntax/math/math_single2.xml"); } diff --git a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathTrigTest.java b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathTrigTest.java index 3e95435ba..ff7170816 100644 --- a/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathTrigTest.java +++ b/OpenRobertaRobot/src/test/java/de/fhg/iais/roberta/ast/syntax/expr/MathTrigTest.java @@ -7,14 +7,14 @@ public class MathTrigTest { @Test public void Test() throws Exception { - String a = "Math.sin(0)Math.cos(0)Math.tan(0)Math.asin(0)Math.acos(0)Math.atan(0)"; + String a = "((float)Math.sin(0))((float)Math.cos(0))((float)Math.tan(0))((float)Math.asin(0))((float)Math.acos(0))((float)Math.atan(0))"; Helper.assertCodeIsOk(a, "/syntax/math/math_trig.xml"); } @Test public void Test1() throws Exception { - String a = "if(0==Math.sin(0)){hal.regulatedDrive(ActorPort.A,ActorPort.B,false,DriveDirection.FOREWARD,Math.acos(0));}"; + String a = "if(0==((float)Math.sin(0))){hal.regulatedDrive(ActorPort.A,ActorPort.B,false,DriveDirection.FOREWARD,((float)Math.acos(0)));}"; Helper.assertCodeIsOk(a, "/syntax/math/math_trig1.xml"); } diff --git a/OpenRobertaServer/staticResources/simulation/robertaLogic/programEval.js b/OpenRobertaServer/staticResources/simulation/robertaLogic/programEval.js index c296fe776..032df48a7 100644 --- a/OpenRobertaServer/staticResources/simulation/robertaLogic/programEval.js +++ b/OpenRobertaServer/staticResources/simulation/robertaLogic/programEval.js @@ -247,7 +247,7 @@ function evalBinary(op, left, right) { val = valLeft / valRight; break; case POWER: - val = valLeft ^ valRight; + val = Math.pow(valLeft, valRight); break; case LT: val = valLeft < valRight;