Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
monticore
EmbeddedMontiArc
generators
EMAM2Cpp
Commits
5ade1c2c
Commit
5ade1c2c
authored
Jul 13, 2018
by
Evgeny Kusmenko
Browse files
Merge branch 'richter-dev' into 'master'
Richter dev Closes #5 and
#15
See merge request
!11
parents
2b4a42a7
6d2406fa
Pipeline
#62534
passed with stage
in 2 minutes and 56 seconds
Changes
21
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
5ade1c2c
...
...
@@ -8,7 +8,7 @@
<groupId>
de.monticore.lang.monticar
</groupId>
<artifactId>
embedded-montiarc-math-generator
</artifactId>
<version>
0.0.1
8
-SNAPSHOT
</version>
<version>
0.0.1
9
-SNAPSHOT
</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
...
...
src/main/java/de/monticore/lang/monticar/generator/MathBackend.java
View file @
5ade1c2c
...
...
@@ -2,6 +2,7 @@ package de.monticore.lang.monticar.generator;
import
de.monticore.lang.math._symboltable.expression.MathArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixVectorExpressionSymbol
;
/**
* @author Sascha Schneiders
...
...
@@ -37,6 +38,7 @@ public interface MathBackend {
String
getDivisionEEString
(
MathMatrixArithmeticExpressionSymbol
mathExpressionSymbol
,
String
valueListString
);
String
getMultiplicationEEString
(
MathMatrixArithmeticExpressionSymbol
mathExpressionSymbol
,
String
valueListString
);
/**
* Does the backend use 0-based or 1-based indexing for matrix element access?
*
...
...
@@ -51,4 +53,6 @@ public interface MathBackend {
String
getWholeNumberMatrixTypeName
();
String
getWholeNumberCubeTypeName
();
String
getMathMatrixColonVectorString
(
MathMatrixVectorExpressionSymbol
mathMatrixArithmeticExpressionSymbol
);
}
src/main/java/de/monticore/lang/monticar/generator/MathCommand.java
View file @
5ade1c2c
...
...
@@ -6,6 +6,8 @@ import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol
import
java.util.HashSet
;
import
static
de
.
monticore
.
lang
.
monticar
.
generator
.
cpp
.
MathCommandRegisterCPP
.
removeBrackets
;
/**
* @author Sascha Schneiders.
*/
...
...
@@ -36,7 +38,9 @@ public abstract class MathCommand {
convert
(
mathExpressionSymbol
,
bluePrint
);
if
(
mathExpressionSymbol
instanceof
MathMatrixNameExpressionSymbol
)
{
MathMatrixNameExpressionSymbol
mathMatrixNameExpressionSymbol
=
(
MathMatrixNameExpressionSymbol
)
mathExpressionSymbol
;
targetLanguageCommandNames
.
add
(
mathMatrixNameExpressionSymbol
.
getTextualRepresentation
());
String
s
=
mathMatrixNameExpressionSymbol
.
getTextualRepresentation
();
s
=
removeBrackets
(
s
);
targetLanguageCommandNames
.
add
(
s
);
}
}
...
...
@@ -53,10 +57,9 @@ public abstract class MathCommand {
public
boolean
isTargetLanguageCommand
(
String
command
)
{
if
(!
command
.
isEmpty
())
for
(
String
s
:
getTargetLanguageCommandNames
())
if
(
s
.
cont
ain
s
(
command
))
if
(
s
.
cont
entEqual
s
(
command
))
return
true
;
return
false
;
}
}
src/main/java/de/monticore/lang/monticar/generator/Method.java
View file @
5ade1c2c
...
...
@@ -39,6 +39,22 @@ public class Method {
parameters
.
add
(
v
);
}
public
boolean
addParameterUnique
(
Variable
v
)
{
boolean
added
=
!
containsParameter
(
v
);
if
(
added
)
{
addParameter
(
v
);
}
return
added
;
}
private
boolean
containsParameter
(
Variable
v
)
{
boolean
found
=
false
;
for
(
Variable
param
:
getParameters
())
{
found
|=
param
.
getName
().
contentEquals
(
v
.
getName
());
}
return
found
;
}
public
List
<
Variable
>
getParameters
()
{
return
parameters
;
}
...
...
@@ -62,4 +78,14 @@ public class Method {
public
void
setInstructions
(
List
<
Instruction
>
instructions
)
{
this
.
instructions
=
instructions
;
}
public
String
getTargetLanguageMethodCall
()
{
String
args
=
""
;
int
size
=
getParameters
().
size
();
for
(
int
i
=
0
;
i
<
size
-
1
;
i
++)
{
args
+=
String
.
format
(
"%s, "
,
parameters
.
get
(
i
).
getNameTargetLanguageFormat
());
}
args
+=
parameters
.
get
(
size
-
1
).
getNameTargetLanguageFormat
();
return
String
.
format
(
"%s(%s)"
,
name
,
args
);
}
}
src/main/java/de/monticore/lang/monticar/generator/cpp/ArmadilloBackend.java
View file @
5ade1c2c
...
...
@@ -2,9 +2,9 @@ package de.monticore.lang.monticar.generator.cpp;
import
de.monticore.lang.math._symboltable.expression.MathArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixVectorExpressionSymbol
;
import
de.monticore.lang.monticar.generator.MathBackend
;
import
de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator
;
import
de.monticore.lang.monticar.generator.cpp.converter.MathConverter
;
import
de.se_rwth.commons.logging.Log
;
import
java.util.ArrayList
;
...
...
@@ -104,6 +104,12 @@ public class ArmadilloBackend implements MathBackend {
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
.
getRightExpression
(),
new
ArrayList
<>());
}
@Override
public
String
getMultiplicationEEString
(
MathMatrixArithmeticExpressionSymbol
mathExpressionSymbol
,
String
valueListString
)
{
return
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
.
getLeftExpression
(),
new
ArrayList
<>())
+
" % "
+
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
.
getRightExpression
(),
new
ArrayList
<>());
}
@Override
public
boolean
usesZeroBasedIndexing
()
{
return
true
;
...
...
@@ -128,4 +134,14 @@ public class ArmadilloBackend implements MathBackend {
public
String
getWholeNumberCubeTypeName
()
{
return
"icube"
;
}
@Override
public
String
getMathMatrixColonVectorString
(
MathMatrixVectorExpressionSymbol
symbol
)
{
String
start
=
ExecuteMethodGenerator
.
generateExecuteCode
(
symbol
.
getStart
(),
new
ArrayList
<>());
String
delta
=
"1"
;
if
(
symbol
.
getStep
().
isPresent
())
delta
=
ExecuteMethodGenerator
.
generateExecuteCode
(
symbol
.
getStep
().
get
(),
new
ArrayList
<>());
String
end
=
ExecuteMethodGenerator
.
generateExecuteCode
(
symbol
.
getEnd
(),
new
ArrayList
<>());
return
String
.
format
(
"regspace<rowvec>(%s, %s, %s)"
,
start
,
delta
,
end
);
}
}
src/main/java/de/monticore/lang/monticar/generator/cpp/LinalgBackend.java
View file @
5ade1c2c
...
...
@@ -2,6 +2,7 @@ package de.monticore.lang.monticar.generator.cpp;
import
de.monticore.lang.math._symboltable.expression.MathArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixVectorExpressionSymbol
;
import
de.monticore.lang.monticar.generator.MathBackend
;
import
de.se_rwth.commons.logging.Log
;
...
...
@@ -89,6 +90,12 @@ public class LinalgBackend implements MathBackend {
return
null
;
}
@Override
public
String
getMultiplicationEEString
(
MathMatrixArithmeticExpressionSymbol
mathExpressionSymbol
,
String
valueListString
)
{
Log
.
debug
(
"Not supported yet"
,
"Not Implemented"
);
return
null
;
}
@Override
public
boolean
usesZeroBasedIndexing
()
{
// TODO: check this! Do not know this backend...
...
...
@@ -118,4 +125,10 @@ public class LinalgBackend implements MathBackend {
Log
.
error
(
"Not supported yet"
);
return
null
;
}
@Override
public
String
getMathMatrixColonVectorString
(
MathMatrixVectorExpressionSymbol
mathMatrixArithmeticExpressionSymbol
)
{
Log
.
error
(
"Not supported yet"
);
return
null
;
}
}
src/main/java/de/monticore/lang/monticar/generator/cpp/MathCommandRegisterCPP.java
View file @
5ade1c2c
...
...
@@ -97,6 +97,14 @@ public class MathCommandRegisterCPP extends MathCommandRegister {
return
fullString
;
}
public
static
String
removeBrackets
(
String
fullString
)
{
String
result
=
removeTrailingStrings
(
fullString
,
"("
);
int
idx
=
result
.
indexOf
(
"("
);
if
(
idx
>
0
)
result
=
result
.
substring
(
0
,
idx
);
return
result
;
}
public
static
String
calculateName
(
String
fullName
)
{
int
index
=
fullName
.
indexOf
(
"("
);
String
name
=
""
;
...
...
src/main/java/de/monticore/lang/monticar/generator/cpp/OctaveBackend.java
View file @
5ade1c2c
...
...
@@ -3,6 +3,7 @@ package de.monticore.lang.monticar.generator.cpp;
import
de.monticore.lang.math._symboltable.expression.MathArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.expression.MathExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixVectorExpressionSymbol
;
import
de.monticore.lang.monticar.generator.MathBackend
;
import
de.se_rwth.commons.logging.Log
;
...
...
@@ -92,6 +93,12 @@ public class OctaveBackend implements MathBackend {
return
OctaveHelper
.
getCallOctaveFunctionFirstResult
(
mathExpressionSymbol
.
getLeftExpression
(),
"ldivide"
,
valueListString
,
false
);
}
@Override
public
String
getMultiplicationEEString
(
MathMatrixArithmeticExpressionSymbol
mathExpressionSymbol
,
String
valueListString
)
{
Log
.
warn
(
"Backend deprecated"
);
return
OctaveHelper
.
getCallOctaveFunctionFirstResult
(
mathExpressionSymbol
.
getLeftExpression
(),
".*"
,
valueListString
,
false
);
}
@Override
public
boolean
usesZeroBasedIndexing
()
{
return
false
;
...
...
@@ -120,4 +127,10 @@ public class OctaveBackend implements MathBackend {
Log
.
warn
(
"Octave does not support whole number matrices. Using real matrix instead!"
);
return
getCubeTypeName
();
}
@Override
public
String
getMathMatrixColonVectorString
(
MathMatrixVectorExpressionSymbol
mathMatrixArithmeticExpressionSymbol
)
{
Log
.
warn
(
"Not supported."
,
mathMatrixArithmeticExpressionSymbol
.
getSourcePosition
());
return
null
;
}
}
src/main/java/de/monticore/lang/monticar/generator/cpp/commands/MathSumCommand.java
View file @
5ade1c2c
...
...
@@ -3,23 +3,34 @@ package de.monticore.lang.monticar.generator.cpp.commands;
import
de.monticore.lang.math._symboltable.expression.MathExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixAccessSymbol
;
import
de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol
;
import
de.monticore.lang.monticar.generator.BluePrint
;
import
de.monticore.lang.monticar.generator.MathCommand
;
import
de.monticore.lang.monticar.generator.*
;
import
de.monticore.lang.monticar.generator.cpp.BluePrintCPP
;
import
de.monticore.lang.monticar.generator.cpp.MathFunctionFixer
;
import
de.monticore.lang.monticar.generator.cpp.OctaveHelper
;
import
de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter
;
import
de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator
;
import
de.monticore.lang.monticar.generator.cpp.MathFunctionFixer
;
import
de.monticore.lang.monticar.generator.cpp.converter.MathConverter
;
import
de.monticore.lang.monticar.generator.cpp.converter.StringIndexHelper
;
import
de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression
;
import
de.se_rwth.commons.logging.Log
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
/**
* @author Sascha Schneiders
* @author Christoph Richter
* Overloaded syntax to more math convinient way:
* sum(function, sum_variable, start_value, end_value)
*/
public
class
MathSumCommand
extends
MathCommand
{
private
static
final
String
SUM_SYNTAX_EXTENDED
=
"sum( EXPRESSION , SUM_VARIABLE , START_VALUE , END_VALUE )"
;
private
static
final
String
CALC_SUM_METHOD_NAME
=
"calcSum"
;
private
static
int
sumCommandCounter
=
0
;
public
MathSumCommand
()
{
setMathCommandName
(
"sum"
);
//setTargetCommand("LALALA");
...
...
@@ -44,33 +55,208 @@ public class MathSumCommand extends MathCommand {
String
valueListString
=
""
;
for
(
MathMatrixAccessSymbol
accessSymbol
:
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
())
MathFunctionFixer
.
fixMathFunctions
(
accessSymbol
,
(
BluePrintCPP
)
bluePrint
);
valueListString
+=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
,
new
ArrayList
<
String
>());
valueListString
+=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
,
new
ArrayList
<>());
//OctaveHelper.getCallOctaveFunction(mathExpressionSymbol, "sum","Double", valueListString));
List
<
MathMatrixAccessSymbol
>
newMatrixAccessSymbols
=
new
ArrayList
<>();
MathStringExpression
stringExpression
=
new
MathStringExpression
(
OctaveHelper
.
getCallBuiltInFunction
(
mathExpressionSymbol
,
"Fsum"
,
"Double"
,
valueListString
,
"FirstResult"
,
false
,
1
),
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
MathStringExpression
stringExpression
=
new
MathStringExpression
(
OctaveHelper
.
getCallBuiltInFunction
(
mathExpressionSymbol
,
"Fsum"
,
"Double"
,
valueListString
,
"FirstResult"
,
false
,
1
),
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
newMatrixAccessSymbols
.
add
(
new
MathMatrixAccessSymbol
(
stringExpression
));
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setMathMatrixAccessSymbols
(
newMatrixAccessSymbols
);
((
BluePrintCPP
)
bluePrint
).
addAdditionalIncludeString
(
"octave/builtin-defun-decls"
);
// error if using extended syntax here
if
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
size
()
==
4
)
{
Log
.
error
(
String
.
format
(
"Syntax: \"%s\" is not supported when using deprecated backend Octave"
,
SUM_SYNTAX_EXTENDED
));
}
}
public
void
convertUsingArmadilloBackend
(
MathExpressionSymbol
mathExpressionSymbol
,
BluePrint
bluePrint
)
{
MathMatrixNameExpressionSymbol
mathMatrixNameExpressionSymbol
=
(
MathMatrixNameExpressionSymbol
)
mathExpressionSymbol
;
mathMatrixNameExpressionSymbol
.
setNameToAccess
(
""
);
String
valueListString
=
""
;
BluePrintCPP
bluePrintCPP
=
(
BluePrintCPP
)
bluePrint
;
for
(
MathMatrixAccessSymbol
accessSymbol
:
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
())
MathFunctionFixer
.
fixMathFunctions
(
accessSymbol
,
(
BluePrintCPP
)
bluePrint
);
valueListString
+=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
,
new
ArrayList
<
String
>());
//OctaveHelper.getCallOctaveFunction(mathExpressionSymbol, "sum","Double", valueListString));
MathFunctionFixer
.
fixMathFunctions
(
accessSymbol
,
bluePrintCPP
);
if
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
size
()
==
1
)
{
convertAccuSumImplementationArmadillo
(
mathMatrixNameExpressionSymbol
,
bluePrintCPP
);
}
else
if
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
size
()
==
2
)
{
convertSumImplementationArmadillo
(
mathMatrixNameExpressionSymbol
,
bluePrintCPP
);
}
else
if
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
size
()
==
4
)
{
MathMatrixAccessSymbol
func
=
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
get
(
0
);
MathMatrixAccessSymbol
sumVar
=
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
get
(
1
);
MathMatrixAccessSymbol
sumStart
=
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
get
(
2
);
MathMatrixAccessSymbol
sumEnd
=
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
get
(
3
);
convertExtendedSumImplementationArmadillo
(
mathMatrixNameExpressionSymbol
,
func
,
sumVar
,
sumStart
,
sumEnd
,
bluePrintCPP
);
}
else
{
Log
.
error
(
String
.
format
(
"No implementation found for sum operation: \"sum(%s)\". Possible syntax is \"sum( X )\", \"sum(X,dim)\" or \"%s\""
,
mathExpressionSymbol
.
getTextualRepresentation
(),
SUM_SYNTAX_EXTENDED
));
}
}
private
void
convertSumImplementationArmadillo
(
MathMatrixNameExpressionSymbol
mathMatrixNameExpressionSymbol
,
BluePrintCPP
bluePrintCPP
)
{
String
valueListString
=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathMatrixNameExpressionSymbol
,
new
ArrayList
<>());
// correct index for armadillo
valueListString
=
valueListString
.
substring
(
0
,
valueListString
.
length
()
-
1
)
+
"-1)"
;
MathStringExpression
stringExpression
=
new
MathStringExpression
(
"sum"
+
valueListString
,
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
List
<
MathMatrixAccessSymbol
>
newMatrixAccessSymbols
=
new
ArrayList
<>();
MathStringExpression
stringExpression
=
new
MathStringExpression
(
"accu"
+
valueListString
,
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
newMatrixAccessSymbols
.
add
(
new
MathMatrixAccessSymbol
(
stringExpression
));
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setMathMatrixAccessSymbols
(
newMatrixAccessSymbols
);
}
/**
* Implements the sum command using Armadillos accu command
*
* @param mathMatrixNameExpressionSymbol MathMatrixNameExpressionSymbol passed to convert
* @param bluePrint BluePrint of current code generation
* @see <a href="http://arma.sourceforge.net/docs.html#accu">Armadillo Documentation</a>
*/
private
void
convertAccuSumImplementationArmadillo
(
MathMatrixNameExpressionSymbol
mathMatrixNameExpressionSymbol
,
BluePrintCPP
bluePrint
)
{
String
valueListString
=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathMatrixNameExpressionSymbol
,
new
ArrayList
<>());
//OctaveHelper.getCallOctaveFunction(mathExpressionSymbol, "sum","Double", valueListString));
MathStringExpression
stringExpression
=
new
MathStringExpression
(
"accu"
+
valueListString
,
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
List
<
MathMatrixAccessSymbol
>
newMatrixAccessSymbols
=
new
ArrayList
<>();
newMatrixAccessSymbols
.
add
(
new
MathMatrixAccessSymbol
(
stringExpression
));
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setMathMatrixAccessSymbols
(
newMatrixAccessSymbols
);
((
BluePrintCPP
)
bluePrint
).
addAdditionalIncludeString
(
"HelperA"
);
bluePrint
.
addAdditionalIncludeString
(
"HelperA"
);
// question: why? (CR)
}
/**
* Implements a sum function with syntax "sum( EXPRESSION , SUM_VARIABLE , START_VALUE , END_VALUE )"
* This syntax makes sum expressions easier to model.
*
* @param mathMatrixNameExpressionSymbol symbol to convert
* @param func expression from which the sum is calculates
* @param sumVar name of the sum variable
* @param sumStart start value of the sum variable
* @param sumEnd end value of the sum variable
*/
private
void
convertExtendedSumImplementationArmadillo
(
MathMatrixNameExpressionSymbol
mathMatrixNameExpressionSymbol
,
MathMatrixAccessSymbol
func
,
MathMatrixAccessSymbol
sumVar
,
MathMatrixAccessSymbol
sumStart
,
MathMatrixAccessSymbol
sumEnd
,
BluePrintCPP
bluePrint
)
{
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setAccessStartSymbol
(
""
);
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setAccessEndSymbol
(
""
);
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
clear
();
// create method
Method
calcSumMethod
=
getSumCalculationMethod
(
func
,
sumVar
,
sumStart
,
sumEnd
,
bluePrint
);
// create code string
String
code
=
calcSumMethod
.
getTargetLanguageMethodCall
();
MathStringExpression
codeExpr
=
new
MathStringExpression
(
code
,
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
().
add
(
new
MathMatrixAccessSymbol
(
codeExpr
));
// add method to bluePrint
bluePrint
.
addMethod
(
calcSumMethod
);
}
private
Method
getSumCalculationMethod
(
MathMatrixAccessSymbol
func
,
MathMatrixAccessSymbol
sumVar
,
MathMatrixAccessSymbol
sumStart
,
MathMatrixAccessSymbol
sumEnd
,
BluePrintCPP
bluePrint
)
{
// create new method
Method
method
=
getNewEmptySumCalculationMethod
();
// generate function code
String
f
=
ExecuteMethodGenerator
.
generateExecuteCode
(
func
,
new
ArrayList
<>());
String
varString
=
ExecuteMethodGenerator
.
generateExecuteCode
(
sumVar
,
new
ArrayList
<>());
String
start
=
ExecuteMethodGenerator
.
generateExecuteCode
(
sumStart
,
new
ArrayList
<>());
String
end
=
ExecuteMethodGenerator
.
generateExecuteCode
(
sumEnd
,
new
ArrayList
<>());
// add loop var
Variable
loopVar
=
generateLoopVariable
(
varString
,
bluePrint
);
// parameters
setParameters
(
method
,
bluePrint
);
// add instructions
method
.
addInstruction
(
accumulatorInitialization
());
method
.
addInstruction
(
forLoopHeader
(
varString
,
start
,
end
));
method
.
addInstruction
(
forLoopBody
(
f
));
method
.
addInstruction
(
returnAccumulator
());
// add loopvar to children
addLoopVarParamToMethod
(
method
,
loopVar
,
bluePrint
);
// delete loop var
bluePrint
.
getMathInformationRegister
().
getVariables
().
remove
(
loopVar
);
return
method
;
}
private
Method
getNewEmptySumCalculationMethod
()
{
sumCommandCounter
++;
Method
method
=
new
Method
();
method
.
setName
(
CALC_SUM_METHOD_NAME
+
sumCommandCounter
);
method
.
setReturnTypeName
(
"double"
);
return
method
;
}
private
void
setParameters
(
Method
method
,
BluePrint
bluePrint
)
{
List
<
Variable
>
vars
=
bluePrint
.
getMathInformationRegister
().
getVariables
();
for
(
int
i
=
0
;
i
<
vars
.
size
()
-
2
;
i
++)
{
// the last variable is the one we are assigning now
method
.
addParameterUnique
(
vars
.
get
(
i
));
}
}
private
Variable
generateLoopVariable
(
String
name
,
BluePrint
bluePrint
)
{
Variable
loopVar
=
new
Variable
(
name
,
Variable
.
FORLOOPINFO
);
loopVar
.
setVariableType
(
new
VariableType
(
"Integer"
,
"int"
,
""
));
bluePrint
.
getMathInformationRegister
().
addVariable
(
loopVar
);
return
loopVar
;
}
private
Instruction
accumulatorInitialization
()
{
return
new
Instruction
()
{
@Override
public
String
getTargetLanguageInstruction
()
{
return
" double res = 0; \n"
;
}
@Override
public
boolean
isConnectInstruction
()
{
return
false
;
}
};
}
private
Instruction
forLoopHeader
(
String
sumVar
,
String
sumStart
,
String
sumEnd
)
{
return
new
Instruction
()
{
@Override
public
String
getTargetLanguageInstruction
()
{
return
String
.
format
(
" for (int %s = %s; %s <= %s; %s++)\n"
,
sumVar
,
sumStart
,
sumVar
,
sumEnd
,
sumVar
);
}
@Override
public
boolean
isConnectInstruction
()
{
return
false
;
}
};
}
private
Instruction
forLoopBody
(
String
func
)
{
return
new
Instruction
()
{
@Override
public
String
getTargetLanguageInstruction
()
{
return
String
.
format
(
" res += %s;\n"
,
func
);
}
@Override
public
boolean
isConnectInstruction
()
{
return
false
;
}
};
}
private
Instruction
returnAccumulator
()
{
return
new
Instruction
()
{
@Override
public
String
getTargetLanguageInstruction
()
{
return
" return res;\n"
;
}
@Override
public
boolean
isConnectInstruction
()
{
return
false
;
}
};
}
private
void
addLoopVarParamToMethod
(
Method
method
,
Variable
loopVar
,
BluePrintCPP
bluePrint
)
{
String
func
=
method
.
getInstructions
().
get
(
2
).
getTargetLanguageInstruction
();
if
(
func
.
contains
(
CALC_SUM_METHOD_NAME
))
{
String
[]
split1
=
func
.
split
(
CALC_SUM_METHOD_NAME
);
String
[]
split2
=
split1
[
1
].
split
(
"[)]"
);
func
=
CALC_SUM_METHOD_NAME
+
split2
[
0
]
+
", "
+
loopVar
.
getNameTargetLanguageFormat
()
+
")"
;
// and change the method signiture of the calc sum function
String
mName
=
CALC_SUM_METHOD_NAME
+
split1
[
1
].
substring
(
0
,
split1
[
1
].
indexOf
(
"("
));
Optional
<
Method
>
affectedMethod
=
bluePrint
.
getMethod
(
mName
);
if
(
affectedMethod
.
isPresent
())
{
affectedMethod
.
get
().
addParameterUnique
(
loopVar
);
addLoopVarParamToMethod
(
affectedMethod
.
get
(),
loopVar
,
bluePrint
);
}
method
.
getInstructions
().
set
(
2
,
forLoopBody
(
func
));
}
}
}
src/main/java/de/monticore/lang/monticar/generator/cpp/converter/ExecuteMethodGeneratorHandler.java
View file @
5ade1c2c
...
...
@@ -228,14 +228,7 @@ public class ExecuteMethodGeneratorHandler {
result
+=
ExecuteMethodGenerator
.
getCorrectAccessString
(
mathAssignmentExpressionSymbol
.
getNameOfMathValue
(),
mathAssignmentExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
(),
includeStrings
);
result
+=
mathAssignmentExpressionSymbol
.
getAssignmentOperator
().
getOperator
()
+
" "
;
String
input
=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathAssignmentExpressionSymbol
.
getExpressionSymbol
(),
includeStrings
)
+
";\n"
;
if
(
MathCommandRegisterCPP
.
containsCommandExpression
(
mathAssignmentExpressionSymbol
.
getExpressionSymbol
(),
input
))
{
result
+=
input
;
}
else
{
if
(!
StringValueListExtractorUtil
.
containsPortName
(
input
))
result
+=
StringIndexHelper
.
modifyContentBetweenBracketsByAdding
(
input
,
"-1"
);
else
result
+=
input
;
}
result
+=
input
;
Log
.
info
(
"result1: "
+
result
,
"MathAssignmentExpressionSymbol"
);
}
else
{
/*if (mathAssignmentExpressionSymbol.getNameOfMathValue().equals("eigenVectors")) {
...
...
@@ -277,7 +270,6 @@ public class ExecuteMethodGeneratorHandler {
}
else
{
assignment
=
ExecuteMethodGenerator
.
generateExecuteCode
(
assignmentSymbol
,
includeStrings
);
Log
.
info
(
assignment
,
"assignment3:"
);
}
String
result
=
String
.
format
(
"%s %s %s;\n"
,
name
,
op
,
assignment
.
trim
());
Log
.
info
(
name
+
" "
+
op
+
" "
+
assignment
,
"additionalInfo:"
);
...
...
src/main/java/de/monticore/lang/monticar/generator/cpp/converter/ExecuteMethodGeneratorMatrixExpressionHandler.java
View file @
5ade1c2c
...
...
@@ -3,8 +3,10 @@ package de.monticore.lang.monticar.generator.cpp.converter;
import
de.monticore.lang.math._symboltable.expression.IArithmeticExpression
;
import
de.monticore.lang.math._symboltable.expression.MathExpressionSymbol
;
import
de.monticore.lang.math._symboltable.matrix.*
;
import
de.monticore.lang.monticar.generator.cpp.MathCommandRegisterCPP
;
import
de.monticore.lang.monticar.generator.cpp.MathFunctionFixer
;
import
de.monticore.lang.monticar.generator.cpp.OctaveHelper
;
import
de.monticore.lang.monticar.generator.cpp.StringValueListExtractorUtil
;
import
de.se_rwth.commons.logging.Log
;
import
java.util.ArrayList
;
...
...
@@ -24,6 +26,8 @@ public class ExecuteMethodGeneratorMatrixExpressionHandler {
result
=
generateExecuteCodeMatrixEEPowerOf
(
mathMatrixArithmeticExpressionSymbol
,
includeStrings
);
}
else
if
(
mathMatrixArithmeticExpressionSymbol
.
getMathOperator
().
equals
(
"./"
))
{
result
=
generateExecuteCodeMatrixEEDivide
(
mathMatrixArithmeticExpressionSymbol
,
includeStrings
);
}
else
if
(
mathMatrixArithmeticExpressionSymbol
.
getMathOperator
().
equals
(
".*"
))
{
result
=
generateExecuteCodeMatrixEEMult
(
mathMatrixArithmeticExpressionSymbol
,
includeStrings
);
/*} else if (mathArithmeticExpressionSymbol.getMathOperator().equals("./")) {
Log.error("reace");
result += "\"ldivide\"";
...
...
@@ -44,6 +48,11 @@ public class ExecuteMethodGeneratorMatrixExpressionHandler {
return
result
;
}
private
static
String
generateExecuteCodeMatrixEEMult
(
MathMatrixArithmeticExpressionSymbol
mathMatrixArithmeticExpressionSymbol
,
List
<
String
>
includeStrings
)
{
String
valueListString
=
calculateValueListString
(
mathMatrixArithmeticExpressionSymbol
);
return
MathConverter
.
curBackend
.
getMultiplicationEEString
(
mathMatrixArithmeticExpressionSymbol
,
valueListString
);
}
public
static
String
calculateValueListString
(
IArithmeticExpression
mathExpressionSymbol
)
{
List
<
MathExpressionSymbol
>
list
=
new
ArrayList
<
MathExpressionSymbol
>();
list
.
add
(
mathExpressionSymbol
.
getLeftExpression
());
...
...
@@ -224,10 +233,12 @@ public class ExecuteMethodGeneratorMatrixExpressionHandler {
result
=
generateExecuteCode
((
MathMatrixNameExpressionSymbol
)
mathMatrixExpressionSymbol
,
includeStrings
);
}
else
if
(
mathMatrixExpressionSymbol
.
isValueExpression
())
{
result
=
generateExecuteCode
((
MathMatrixArithmeticValueSymbol
)
mathMatrixExpressionSymbol
,
includeStrings
);
}
else
if
(
mathMatrixExpressionSymbol
.
isMatrixVectorExpression
())
{
result
=
generateExecuteCode
((
MathMatrixVectorExpressionSymbol
)
mathMatrixExpressionSymbol
,
includeStrings
);
}
else
{
Log
.
info
(
mathMatrixExpressionSymbol
.
getTextualRepresentation
(),
"Symbol:"
);
Log
.
info
(
mathMatrixExpressionSymbol
.
getClass
().
getName
(),
"Symbol Name:"
);
Log
.
debug
(
"0xMAMAEXSY
"
,
"
Case not handled"
);
Log
.
error
(
"0xMAMAEXSY
:
Case not handled"
);
}
return
result
;
}
...
...
@@ -242,8 +253,26 @@ public class ExecuteMethodGeneratorMatrixExpressionHandler {
result
+=
mathMatrixNameExpressionSymbol
.
getNameToAccess
();
if
(
mathMatrixNameExpressionSymbol
.
isMathMatrixAccessOperatorSymbolPresent
())
{
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setMathMatrixNameExpressionSymbol
(
mathMatrixNameExpressionSymbol
);
result
+=
generateExecuteCode
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
(),
includeStrings
);
String
input
=
generateExecuteCode
(
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
(),
includeStrings
);
// fix indexing
if
(
useZeroBasedIndexingForMatrixAccess
(
mathMatrixNameExpressionSymbol
,
input
))
result
+=
StringIndexHelper
.
modifyContentBetweenBracketsByAdding
(
input
,
"-1"
);