Commit 8803420c authored by Evgeny Kusmenko's avatar Evgeny Kusmenko
Browse files

Merge branch 'richter-dev' into 'master'

Richter dev

See merge request !8
parents 209850b3 560cebbd
Pipeline #60379 passed with stage
in 2 minutes and 27 seconds
......@@ -8,7 +8,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.0.16-SNAPSHOT</version>
<version>0.0.17-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......
......@@ -2,28 +2,36 @@ package de.monticore.lang.monticar.generator.cpp.converter;
/**
* @author Sascha Schneiders
* @author Christoph Richter
*/
public class StringIndexHelper {
public static String modifyContentBetweenBracketsByAdding(String input, String modifier) {
return modifyContentBetweenBracketsByAdding(input, modifier, "(", ",", ")");
}
/**
* Only adds modifier in the first occurring bracket
*/
public static String modifyContentBetweenBracketsByAdding(String input, String modifier, String bracketStart, String bracketSep, String bracketEnd) {
String result = "";
boolean done = false;
int indexFirst = input.indexOf("(", 0);
int indexFirst = input.indexOf(bracketStart, 0);
if (indexFirst == -1)
return input;
result += input.substring(0, indexFirst);
int indexSecond = input.indexOf(",", indexFirst + 1);
int indexSecond = input.indexOf(bracketSep, indexFirst + bracketSep.length());
if (indexSecond == -1) {
indexSecond = input.indexOf(")", indexFirst + 1);
indexSecond = input.indexOf(bracketEnd, indexFirst + bracketEnd.length());
done = true;
}
while (!done) {
result += input.substring(indexFirst, indexSecond) + modifier;
indexFirst = indexSecond;
indexSecond = input.indexOf(",", indexSecond + 1);
indexSecond = input.indexOf(bracketSep, indexSecond + bracketSep.length());
if (indexSecond == -1) {
indexSecond = input.indexOf(")", indexFirst + 1);
indexSecond = input.indexOf(bracketEnd, indexFirst + bracketEnd.length());
done = true;
}
}
......@@ -37,9 +45,22 @@ public class StringIndexHelper {
if (input.endsWith(endPart)) {
int indexFirst = input.lastIndexOf(endPart);
result = input.substring(0, indexFirst);
result+=newEndPart;
result += newEndPart;
}
return result;
}
public static String modifyContentBetweenBracketsByAddingForAllBrackets(String input, String modifier, String bracketStart, String bracketSep, String bracketEnd) {
String[] parts = input.split("(?<=\\))");
StringBuilder sb = new StringBuilder();
for (String s : parts) {
sb.append(modifyContentBetweenBracketsByAdding(s, modifier, bracketStart, bracketSep, bracketEnd));
}
return sb.toString();
}
public static String modifyContentBetweenBracketsByAddingForAllBrackets(String input, String modifier) {
return modifyContentBetweenBracketsByAddingForAllBrackets(input, modifier, "(", ",", ")");
}
}
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.expression.MathNumberExpressionSymbol;
import de.monticore.lang.math._symboltable.expression.MathValueExpressionSymbol;
import de.monticore.lang.math._symboltable.expression.MathValueSymbol;
import de.monticore.lang.math._symboltable.expression.*;
import de.monticore.lang.math._symboltable.matrix.MathMatrixAccessOperatorSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.BluePrint;
import de.monticore.lang.monticar.generator.Variable;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
......@@ -19,6 +17,7 @@ import java.util.Optional;
* This class stores information of already encountered math information
*
* @author Sascha Schneiders
* @author Christoph Richter
*/
public class MathInformationRegister {
protected List<Variable> variables = new ArrayList<>();//contains in implementation Math section declared Variables
......@@ -164,6 +163,7 @@ public class MathInformationRegister {
var.addDimensionalInformation(dimension.getTextualRepresentation());
this.variables.add(var);
var.addProperties(mathValueSymbol.getType().getProperties());
this.mathValueSymbols.add(mathValueSymbol);
}
public Variable getVariable(String name) {
......@@ -200,4 +200,74 @@ public class MathInformationRegister {
}
return result;
}
/**
* Searches MathValueSymbol in list and returns first matching symbol which contains the type declaration information
*
* @param symbol Symbol for which type information should be collected
* @return returns the first type declaration as symbol
*/
public MathValueSymbol getFullTypeInformation(MathValueSymbol symbol) {
MathValueSymbol result = getMathValueSymbol(symbol.getName());
if (result == null)
result = symbol;
return result;
}
/**
* Variables are replaced by their definition.
*
* @param nonAtomarExpression Expression in which the variables will be substituted
* @param atomarValueName Defines the name of the atomar variable after which no more substitution will be made
* @return MathExpressionSymbol containing the replaces variables
*/
public MathExpressionSymbol resolveMathExpressionToAtomarExpression(MathExpressionSymbol nonAtomarExpression, String atomarValueName) {
MathExpressionSymbol result;
if (nonAtomarExpression.isArithmeticExpression()) {
// recursively substitute expressions
MathArithmeticExpressionSymbol arithmeticSubstituted = (MathArithmeticExpressionSymbol) nonAtomarExpression;
MathArithmeticExpressionSymbol arithmeticResult = new MathArithmeticExpressionSymbol();
arithmeticResult.setMathOperator(arithmeticSubstituted.getOperator());
arithmeticResult.setLeftExpression(resolveMathExpressionToAtomarExpression(arithmeticSubstituted.getLeftExpression(), atomarValueName));
arithmeticResult.setRightExpression(resolveMathExpressionToAtomarExpression(arithmeticSubstituted.getRightExpression(), atomarValueName));
result = arithmeticResult;
} else if (nonAtomarExpression instanceof MathValueSymbol) {
// substitute value expr by assigned expr
MathValueSymbol valueExpr = (MathValueSymbol) nonAtomarExpression;
result = getSubstituteByName(valueExpr.getName(), nonAtomarExpression, atomarValueName);
} else if (nonAtomarExpression instanceof MathMatrixNameExpressionSymbol) {
MathMatrixNameExpressionSymbol sourceExpr = (MathMatrixNameExpressionSymbol) nonAtomarExpression;
MathMatrixNameExpressionSymbol targetExpr;
// modifiy access name
String accessName = getSubstituteByName(sourceExpr.getNameToAccess(), nonAtomarExpression, atomarValueName).getTextualRepresentation();
if (sourceExpr.isMathMatrixAccessOperatorSymbolPresent() && !(accessName.contains("("))) {
targetExpr = new MathMatrixNameExpressionSymbol(String.format("(%s)", accessName));
// modify access operator
MathMatrixAccessOperatorSymbol sourceAccessOperator = sourceExpr.getMathMatrixAccessOperatorSymbol();
MathMatrixAccessOperatorSymbol targetAccessOperator = new MathMatrixAccessOperatorSymbol();
targetAccessOperator.setMathMatrixAccessSymbols(sourceAccessOperator.getMathMatrixAccessSymbols());
targetAccessOperator.setAccessStartSymbol(".at(");
targetExpr.setMathMatrixAccessOperatorSymbol(targetAccessOperator);
result = targetExpr;
} else {
result = nonAtomarExpression;
}
} else if (nonAtomarExpression instanceof IMathNamedExpression) {
// substitute value expr by assigned expr
result = getSubstituteByName(((IMathNamedExpression) nonAtomarExpression).getNameToAccess(), nonAtomarExpression, atomarValueName);
} else {
result = nonAtomarExpression;
}
return result;
}
private MathExpressionSymbol getSubstituteByName(String name, MathExpressionSymbol expr, String atomarValueName) {
MathExpressionSymbol result;
MathValueSymbol declaration = getMathValueSymbol(name);
if ((declaration != null) && (declaration.getValue() != null) && (!name.contentEquals(atomarValueName)))
result = declaration.getValue();
else
result = expr;
return result;
}
}
Markdown is supported
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