Commit 79727c15 authored by Ferdinand Alexander Mehlan's avatar Ferdinand Alexander Mehlan
Browse files

Fixed printing CommonMatrixTypes in ports

parent 05b6a7a4
package de.monticore.lang.monticar.generator.helper;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.monticar.common2._ast.ASTCommonDimension;
import de.monticore.lang.monticar.common2._ast.ASTCommonDimensionElement;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.common2._visitor.Common2Visitor;
import de.monticore.lang.monticar.helper.IndentPrinter;
import de.monticore.lang.monticar.types2._ast.ASTElementType;
import de.monticore.lang.numberunit._ast.ASTUnitNumber;
/**
* Created by kt on 07.05.2018.
*/
public class CommonMatrixPrettyPrinter implements Common2Visitor {
protected CommonMatrixPrettyPrinter realThis;
protected IndentPrinter ip;
public CommonMatrixPrettyPrinter(IndentPrinter ip) {
this.ip = ip;
this.realThis = this;
}
public static void prettyprint(ASTCommonMatrixType node, IndentPrinter ip) {
CommonMatrixPrettyPrinter printer = new CommonMatrixPrettyPrinter(ip);
node.accept(printer);
}
@Override
public void visit(ASTElementType node) {
ip.print(node.getTElementType().get());
}
@Override
public void traverse(ASTCommonDimension node) {
ip.print("^{");
int i = 0;
int s = node.getCommonDimensionElements().size();
for (ASTCommonDimensionElement e : node.getCommonDimensionElements()) {
e.accept(realThis);
if (i != s - 1) {
ip.print(", ");
}
i++;
}
ip.print("}");
}
@Override
public void traverse(ASTCommonDimensionElement node) {
if(node.nameIsPresent()) {
ip.print(node.getName().get());
}
if(node.unitNumberIsPresent()) {
node.getUnitNumber().get().accept(realThis);
}
}
@Override
public void traverse(ASTUnitNumber node) {
if(node.tUnitNumberIsPresent()) {
ip.print(node.getTUnitNumber().get());
}
}
}
package de.monticore.lang.monticar.generator.helper;
import de.monticore.expressions.prettyprint.JavaClassExpressionsPrettyPrinter;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.helper.IndentPrinter;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference;
import de.monticore.symboltable.types.references.ActualTypeArgument;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Ferdinand Mehlan on 07.05.2018.
*/
public class PortSymbolsPrinter {
public static void printPorts(Collection<PortSymbol> ports, IndentPrinter ip) {
if (!ports.isEmpty()) {
ip.println("ports");
ip.indent();
int i = 0;
int s = ports.size();
for (PortSymbol p : ports) {
printPort(p, ip);
if (i == s - 1) {
ip.println(";");
} else {
ip.println(",");
}
i++;
}
ip.unindent();
}
}
private static void printPort(PortSymbol port, IndentPrinter ip) {
if (port.isIncoming()) {
ip.print("in ");
} else {
ip.print("out ");
}
String type = port.getTypeReference().getName();
if(type.equals("CommonMatrixType")) {
ASTCommonMatrixType astType = (ASTCommonMatrixType)((MCASTTypeSymbolReference)port.getTypeReference()).getAstType();
CommonMatrixPrettyPrinter.prettyprint(astType, ip);
} else {
ip.print(type);
ip.print(printTypeParameters(port.getTypeReference().getActualTypeArguments()));
}
ip.print(" ");
ip.print(port.getName());
}
private static String printTypeParameters(List<ActualTypeArgument> arg) {
if (arg == null || arg.isEmpty())
return "";
return "<" + arg.stream().map(a -> printWildCardPrefix(a) + printTypeParameters(a) + printArrayDimensions(a)).
collect(Collectors.joining(",")) + ">";
}
private static String printTypeParameters(ActualTypeArgument arg) {
String ret = arg.getType().getReferencedSymbol().getFullName();
if (arg.getType().getActualTypeArguments() != null && !arg.getType().getActualTypeArguments().isEmpty()) {
ret += "<" + arg.getType().getActualTypeArguments().stream().
map(a -> printWildCardPrefix(a) + printTypeParameters(a) + printArrayDimensions(a)).collect(Collectors.joining(",")) + ">";
}
return ret;
}
private static String printWildCardPrefix(ActualTypeArgument a) {
if (a.isLowerBound()) {
return "? super ";
} else if (a.isUpperBound()) {
return "? extends ";
}
return "";
}
private static String printArrayDimensions(ActualTypeArgument a) {
if (a.getType() instanceof MCTypeReference) {
int dim = ((MCTypeReference) a.getType()).getDimension();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dim; i++) {
sb.append("[]");
}
return sb.toString();
}
return "";
}
}
......@@ -3,14 +3,17 @@ package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._visitor.EmbeddedMontiArcMathVisitor;
import de.monticore.lang.embeddedmontiarc.helper.SymbolPrinter;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath.helper.SymbolPrinter;
import de.monticore.lang.math.math._ast.*;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathAssignmentExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.generator.helper.PortSymbolsPrinter;
import de.monticore.lang.monticar.generator.order.ImplementExecutionOrder;
import de.monticore.lang.monticar.helper.IndentPrinter;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import java.util.Collection;
......@@ -50,7 +53,7 @@ public class MathFlatten implements EmbeddedMontiArcMathVisitor {
private void handleComponent() {
printer.print("component " + topSymbol.getName() + "{\n").indent();
SymbolPrinter.printPorts(topSymbol.getPorts(), printer);
PortSymbolsPrinter.printPorts(topSymbol.getPorts(), printer);
printer.println();
handleConnectors();
handleBehaviours();
......
package de.monticore.lang.monticar.generator;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.optimization.MathFlatten;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
......
component MatrixModifier{
ports
in Q(-oo:oo)^{1000, 2} mat1,
in Q(-oo:oo)^{2, 1000} mat2,
in Q(-oo:oo)^{1000, 2} mat3,
in Q(-oo:oo)^{2, 10000} mat4,
in Q(-oo:oo)^{10000, 10000} mat5,
out Q(-oo:oo)^{1000, 10000} matOut;
component matrixModifier{
ports
in Q(-oo:oo)^{1000, 2} mat1,
in Q(-oo:oo)^{2, 1000} mat2,
in Q(-oo:oo)^{1000, 2} mat3,
in Q(-oo:oo)^{2, 10000} mat4,
in Q(-oo:oo)^{10000, 10000} mat5,
out Q(-oo:oo)^{1000, 10000} matOut;
implementation Math{
Q^{1000, 1000} h1 = mat1 * mat2;
Q^{1000, 10000} h2 = mat3 * mat4;
Q^{1000, 10000} h3 = h1 * h2;
matOut = h3 * mat5;
}
}
implementation Math{
Q^{1000, 1000} h1 = mat1 * mat2;
Q^{1000, 10000} h2 = mat3 * mat4;
Q^{1000, 10000} h3 = h1 * h2;
matOut = h3 * mat5;
}
}
\ No newline at end of file
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