Commit b08b87e9 authored by Jean-Marc Ronck's avatar Jean-Marc Ronck
Browse files

Merge branch 'bram.kohlen' into 'master'

Bram.kohlen

See merge request !9
parents 42afb411 d7167db3
Pipeline #60773 failed with stages
in 1 minute and 10 seconds
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.ast.ASTNode;
import de.monticore.commonexpressions._ast.*;
import de.monticore.expressionsbasis._ast.ASTExpression;
import de.monticore.lang.math._ast.*;
import de.monticore.lang.matrix._ast.ASTMathMatrixAccessExpression;
import de.monticore.lang.matrix._ast.ASTMathMatrixValueExplicitExpression;
......@@ -29,6 +27,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
public MathPrettyPrinter() {
this.printer = new IndentPrinter();
this.printer.setIndentLength(4);
this.importVisited = false;
this.inMatrixEnvironment = false;
}
......@@ -153,10 +152,13 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printScript();
this.printSpace();
this.printer.print(node.getName());
this.printer.println();
this.printer.indent();
}
@Override
public void endVisit(ASTMathScript node) {
this.printer.unindent();
this.printEnd();
}
......@@ -234,7 +236,6 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor();
this.printSpace();
this.printer.print(node.getName());
......@@ -243,24 +244,38 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printSpace();
}
@Override
public void endVisit(ASTMathForLoopHead node) {
this.printer.println();
this.printer.indent();
}
@Override
public void endVisit(ASTMathForLoopExpression node) {
this.printer.unindent();
this.printEnd();
this.printSemicolon();
this.printer.println(2);
}
@Override
public void visit(ASTMathIfExpression node) {
this.printer.println();
this.printIf();
this.printSpace();
}
public void revisit(ASTMathIfExpression node) {
this.printer.println();
this.printer.indent();
}
@Override
public void endVisit(ASTMathIfExpression node) {
this.printer.unindent();
}
@Override
public void endVisit(ASTMathIfStatement node) {
this.printEnd();
this.printSemicolon();
this.printer.println(2);
}
......@@ -298,9 +313,27 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printSpace();
}
@Override
public void revisit(ASTMathElseIfExpression node) {
this.printer.println();
this.printer.indent();
}
@Override
public void endVisit(ASTMathElseIfExpression node) {
this.printer.unindent();
}
@Override
public void visit(ASTMathElseExpression node) {
this.printElse();
this.printer.println();
this.printer.indent();
}
@Override
public void endVisit(ASTMathElseExpression node) {
this.printer.unindent();
}
@Override
......
......@@ -494,4 +494,78 @@ public interface MathStructuredVisitor extends MathVisitor {
}
default void revisit(ASTMathArithmeticMatrixEELeftDivideExpression node) {}
@Override
default void handle(ASTMathIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
default void traversePartOne(ASTMathIfExpression node) {
if (null != node.getCondition()) {
node.getCondition().accept(this.getRealThis());
}
}
default void traversePartTwo(ASTMathIfExpression node) {
Iterator iter_bodys = node.getBodyList().iterator();
while(iter_bodys.hasNext()) {
((ASTStatement)iter_bodys.next()).accept(this.getRealThis());
}
}
default void revisit(ASTMathIfExpression node)
{
}
@Override
default void handle(ASTMathElseIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
default void traversePartOne(ASTMathElseIfExpression node) {
if (null != node.getCondition()) {
node.getCondition().accept(this.getRealThis());
}
}
default void traversePartTwo(ASTMathElseIfExpression node) {
Iterator iter_bodys = node.getBodyList().iterator();
while(iter_bodys.hasNext()) {
((ASTStatement)iter_bodys.next()).accept(this.getRealThis());
}
}
default void revisit(ASTMathElseIfExpression node)
{
}
/*@Override
default void handle(ASTMathElseExpression node) {
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
default void traverse(ASTMathElseExpression node) {
Iterator iter_bodys = node.getBodyList().iterator();
while(iter_bodys.hasNext()) {
((ASTStatement)iter_bodys.next()).accept(this.getRealThis());
}
}*/
}
package de.monticore.lang.monticar.mpp.models;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._parser.MathParser;
import de.monticore.lang.monticar.mpp.montimath.MathPrettyPrinter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import static junit.framework.TestCase.*;
public abstract class AbstractModelTest {
protected MathPrettyPrinter printer;
public AbstractModelTest()
{
this(new MathPrettyPrinter());
}
public AbstractModelTest(MathPrettyPrinter printer)
{
this.printer = printer;
}
public String printVanillaModel(String name) throws IOException
{
Path model = Paths.get("src/test/resources/models/montimath/vanilla/" + name).toAbsolutePath();
MathParser parser = new MathParser();
Optional<ASTMathCompilationUnit> astOptional = parser.parse(model.toString());
assertTrue(astOptional.isPresent());
return printer.prettyPrint(astOptional.get());
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class ArithmeticTest extends AbstractModelTest {
@Test
@Ignore
public void printTest() throws IOException
{
System.out.println(printVanillaModel("Arithmetic.m"));
}
@Test
public void testModel() throws IOException
{
String actual = printVanillaModel("Arithmetic.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script Arithmetic\n" +
" Q rational;\n" +
" rational = 1 + 2 * 3;\n" +
"end";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class NestedTest extends AbstractModelTest {
@Test
@Ignore
public void printTest() throws IOException
{
System.out.println(printVanillaModel("Nested.m"));
}
@Test
public void testModel() throws IOException
{
String actual = printVanillaModel("Nested.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script Nested\n" +
" Q sum = 1;\n" +
" for i = 1:2:9\n" +
" if i < 2\n" +
" sum -= sum * i;\n" +
" elseif i < 4\n" +
" sum += sum * i;\n" +
" else\n" +
" for j = 1:2:6\n" +
" sum += sum * (i - j);\n" +
" end\n" +
"\n" +
" end\n" +
"\n" +
" end\n" +
"\n" +
"end";
assertEquals(expected, actual);
}
}
......@@ -11,8 +11,8 @@ script Nested
else
for j = 1:2:6
sum += sum * (i - j);
end;
end;
end
end
end;
end
end
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