Commit 9df9cca3 authored by Christoph Richter's avatar Christoph Richter
Browse files

ExecuteMethodGeneratorHandler: Fixed wrong 0 based matrix access for...

ExecuteMethodGeneratorHandler: Fixed wrong 0 based matrix access for MathValueSymbol (gitlab fixes #5)
parent e84c3a3a
Pipeline #61625 failed with stage
in 18 seconds
package de.monticore.lang.monticar.generator.cpp.converter;
import de.monticore.lang.math._symboltable.MathAssignmentOperator;
import de.monticore.lang.math._symboltable.MathForLoopHeadSymbol;
import de.monticore.lang.math._symboltable.expression.*;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
......@@ -61,8 +62,8 @@ public class ExecuteMethodGeneratorHandler {
return generateExecuteCode((MathNameExpressionSymbol) mathValueExpressionSymbol, includeStrings);
} else if (mathValueExpressionSymbol.isNumberExpression()) {
return generateExecuteCode((MathNumberExpressionSymbol) mathValueExpressionSymbol, includeStrings);
}else if(mathValueExpressionSymbol.isBooleanExpression()) {
return generateExecuteCode((MathBooleanExpressionSymbol)mathValueExpressionSymbol,includeStrings);
} else if (mathValueExpressionSymbol.isBooleanExpression()) {
return generateExecuteCode((MathBooleanExpressionSymbol) mathValueExpressionSymbol, includeStrings);
} else if (mathValueExpressionSymbol.isAssignmentDeclarationExpression()) {
return generateExecuteCodeDeclaration((MathValueSymbol) mathValueExpressionSymbol, includeStrings);
} else {
......@@ -83,13 +84,18 @@ public class ExecuteMethodGeneratorHandler {
ComponentConverter.currentBluePrint.addVariable(var);
} else {
String type = generateExecuteCode(mathValueSymbol.getType(), includeStrings);
result += type + " " + mathValueSymbol.getName();
if (mathValueSymbol.getValue() != null) {
result += " = " + ExecuteMethodGenerator.generateExecuteCode(mathValueSymbol.getValue(), includeStrings);
} else if (mathValueSymbol.getValue() == null)
MathAssignmentExpressionSymbol assignment = new MathAssignmentExpressionSymbol();
assignment.setNameOfMathValue(mathValueSymbol.getName());
assignment.setExpressionSymbol(mathValueSymbol.getValue());
assignment.setAssignmentOperator(new MathAssignmentOperator("="));
result += type + " " + ExecuteMethodGenerator.generateExecuteCode(assignment, includeStrings);
} else if (mathValueSymbol.getValue() == null) {
result += type + " " + mathValueSymbol.getName();
result += addInitializationString(mathValueSymbol, type, includeStrings);
result += ";\n";
}
}
ComponentConverter.currentBluePrint.getMathInformationRegister().addVariable(mathValueSymbol);
//result += mathValueSymbol.getTextualRepresentation();
return result;
......@@ -102,15 +108,15 @@ public class ExecuteMethodGeneratorHandler {
List<MathExpressionSymbol> dims = mathValueSymbol.getType().getDimensions();
if (dims.size() == 1) {
if (typeString.equals(TypeConverter.getColvecAccessString(type))) {
result = "=" + TypeConverter.getDimensionString(TypeConverter.getColvecAccessString(type),dims,includeStrings);
result = "=" + TypeConverter.getDimensionString(TypeConverter.getColvecAccessString(type), dims, includeStrings);
}
} else if (dims.size() == 2) {
if (typeString.equals(TypeConverter.getMatAccessString(type))) {
result = "=" + TypeConverter.getDimensionString(TypeConverter.getMatAccessString(type),dims,includeStrings);
result = "=" + TypeConverter.getDimensionString(TypeConverter.getMatAccessString(type), dims, includeStrings);
}
} else if (dims.size() == 3) {
if (typeString.equals(TypeConverter.getCubeAccessString(type))) {
result = "=" + TypeConverter.getDimensionString(TypeConverter.getCubeAccessString(type),dims,includeStrings);
result = "=" + TypeConverter.getDimensionString(TypeConverter.getCubeAccessString(type), dims, includeStrings);
}
}
return result;
......@@ -144,7 +150,6 @@ public class ExecuteMethodGeneratorHandler {
}
private static String handleRationalType(MathValueType mathValueType) {
if (mathValueType.getDimensions().size() == 0) {
return "double";
......@@ -216,7 +221,7 @@ public class ExecuteMethodGeneratorHandler {
Log.info(mathAssignmentExpressionSymbol.getTextualRepresentation(), "mathAssignmentExpressionSymbol:");
String result;
if (mathAssignmentExpressionSymbol.getMathMatrixAccessOperatorSymbol() != null) {
Log.info(mathAssignmentExpressionSymbol.getMathMatrixAccessOperatorSymbol().getTextualRepresentation(),"accessOperatorSymbol:");
Log.info(mathAssignmentExpressionSymbol.getMathMatrixAccessOperatorSymbol().getTextualRepresentation(), "accessOperatorSymbol:");
if (MathFunctionFixer.fixForLoopAccess(mathAssignmentExpressionSymbol.getNameOfMathValue(), ComponentConverter.currentBluePrint)) {
result = mathAssignmentExpressionSymbol.getNameOfMathValue();
......@@ -255,26 +260,26 @@ public class ExecuteMethodGeneratorHandler {
String name = mathAssignmentExpressionSymbol.getNameOfMathValue();
String op = mathAssignmentExpressionSymbol.getAssignmentOperator().getOperator();
MathExpressionSymbol assignmentSymbol = mathAssignmentExpressionSymbol.getExpressionSymbol().getRealMathExpressionSymbol();
String assignment=mathAssignmentExpressionSymbol.getExpressionSymbol().getTextualRepresentation();
Log.info(assignment,"assignment0:");
String assignment = mathAssignmentExpressionSymbol.getExpressionSymbol().getTextualRepresentation();
Log.info(assignment, "assignment0:");
if (assignmentSymbol instanceof MathMatrixNameExpressionSymbol) {
MathMatrixNameExpressionSymbol matrixAssignmentSymbol = (MathMatrixNameExpressionSymbol) assignmentSymbol;
if (useZeroBasedIndexing(matrixAssignmentSymbol)) {
String matrixName = matrixAssignmentSymbol.getNameToAccess();
String matrixAccess = ExecuteMethodGenerator.getCorrectAccessString(matrixAssignmentSymbol.getNameToAccess(), matrixAssignmentSymbol.getMathMatrixAccessOperatorSymbol(), includeStrings);
assignment = String.format("%s%s", matrixName, matrixAccess);
Log.info(assignment,"assignment1:");
Log.info(assignment, "assignment1:");
} else {
assignment = ExecuteMethodGenerator.generateExecuteCode(assignmentSymbol, includeStrings);
Log.info(assignment,"assignment2:");
Log.info(assignment, "assignment2:");
}
} else {
assignment = ExecuteMethodGenerator.generateExecuteCode(assignmentSymbol, includeStrings);
Log.info(assignment,"assignment3:");
Log.info(assignment, "assignment3:");
}
String result = String.format("%s %s %s;\n", name, op, assignment);
String result = String.format("%s %s %s;\n", name, op, assignment.trim());
Log.info(name + " " + op + " " + assignment, "additionalInfo:");
Log.info("result3: " + result, "MathAssignmentExpressionSymbol");
return result;
......@@ -282,11 +287,16 @@ public class ExecuteMethodGeneratorHandler {
private static boolean useZeroBasedIndexing(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol) {
boolean isZeroBased = false;
// test if array
String name = mathMatrixNameExpressionSymbol.getNameToAccess();
Variable variable = ComponentConverter.currentBluePrint.getVariable(name).orElse(null);
if (!(variable != null && variable.isArray())) {
if (MathConverter.curBackend.usesZeroBasedIndexing()) {
if (!isFunctionCall(mathMatrixNameExpressionSymbol)) {
isZeroBased = true;
}
}
}
return isZeroBased;
}
......
......@@ -26,7 +26,7 @@ world=imat(tilesMaxX,tilesMaxY);
void execute()
{
int value = 1;
value = world(pacmanPossibleNextPositionX-1, pacmanPossibleNextPositionY-1) ;
value = world(pacmanPossibleNextPositionX-1, pacmanPossibleNextPositionY-1);
if(((value == 1))){
canMove = 0;
pacmanChosenNextPositionX = pacmanPositionX;
......
......@@ -18,7 +18,7 @@ world=imat(tilesMaxX,tilesMaxY);
}
void execute()
{
int value = world(posX, posY);
int value = world(posX-1, posY-1);
}
};
......
......@@ -8,6 +8,8 @@ using namespace arma;
class test_math_armadilloIndexTest{
public:
mat in1;
int inRow;
int inCol;
mat out1;
mat CONSTANTCONSTANTVECTOR0;
rowvec CONSTANTCONSTANTVECTOR1;
......@@ -41,14 +43,15 @@ b(1-1) = A(1-1);
b(1-1) = A(2-1, 2-1);
b(2-1) = out1(4-1, 4-1);
double x = 0;
x = A(1-1, 2-1) ;
x = b(2-1) ;
x = in1(1-1, 1-1) ;
x = A(1-1, 2-1);
x = b(2-1);
x = in1(1-1, 1-1);
out1(1-1, 1-1) = in1(1-1, 1-1);
mat test = (zeros<mat>(4, 4));
out1 = test;
out1(1-1) = test(1-1);
out1(2-1, 2-1) = A(2-1, 2-1);
double y = A(inRow-1, inCol-1);
}
};
......
......@@ -3,6 +3,8 @@ package test.math;
component ArmadilloIndexTest{
ports in Q^{2, 2} in1,
in Z inRow,
in Z inCol,
out Q^{4, 4} out1;
implementation Math{
......@@ -44,5 +46,9 @@ component ArmadilloIndexTest{
out1 = test;
out1(1) = test(1);
out1(2, 2) = A(2, 2);
// test scalar port matrix access
Q y = A(inRow, inCol);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment