| @@ -1,15 +1,24 @@ | |||||
| /*!! Printer */ | |||||
| /*! | |||||
| ExpressionPrinter | |||||
| ================= | |||||
| The `ExpressionPrinter` is used for string serialization of a given `Expression`. | |||||
| */ | |||||
| /*!- Header */ | |||||
| package printer; | package printer; | ||||
| import expression.*; | import expression.*; | ||||
| import interpreter.Visitor; | import interpreter.Visitor; | ||||
| /*! | |||||
| The `ExpressionPrinter` implements the string serialization with the help of the | |||||
| [Visitor](${basePath}/src/main/java/interpreter/Visitor.java.html). | |||||
| */ | |||||
| public class ExpressionPrinter extends Visitor<String> { | public class ExpressionPrinter extends Visitor<String> { | ||||
| private final String value; | |||||
| public ExpressionPrinter(Expression expression) { | |||||
| value = visit(expression); | |||||
| } | |||||
| /*!- Visit functions */ | |||||
| public String visitAddition(Addition addition) { | public String visitAddition(Addition addition) { | ||||
| return visit(addition.leftHandSide) + " + " + visit(addition.rightHandSide); | return visit(addition.leftHandSide) + " + " + visit(addition.rightHandSide); | ||||
| } | } | ||||
| @@ -26,7 +35,11 @@ public class ExpressionPrinter extends Visitor<String> { | |||||
| return visit(subtraction.leftHandSide) + " - " + visit(subtraction.rightHandSide); | return visit(subtraction.leftHandSide) + " - " + visit(subtraction.rightHandSide); | ||||
| } | } | ||||
| public String getValue() { | |||||
| return value; | |||||
| /*! | |||||
| The `getValue` function takes an `Expression` instance as an argument and returns | |||||
| the string serialisation of the given expression. | |||||
| */ | |||||
| public String getValue(Expression expression) { | |||||
| return visit(expression); | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,19 +1,27 @@ | |||||
| /*!! Printer */ | |||||
| /*! | |||||
| ProgramPrinter | |||||
| ============== | |||||
| The `ProgramPrinter` is used for string serialization of a given `Program`. | |||||
| */ | |||||
| /*!- Header */ | |||||
| package printer; | package printer; | ||||
| import interpreter.Visitor; | import interpreter.Visitor; | ||||
| import program.*; | import program.*; | ||||
| /*! | |||||
| The `ProgramPrinter` implements the string serialization with the help of the | |||||
| [Visitor](${basePath}/src/main/java/interpreter/Visitor.java.html). | |||||
| */ | |||||
| public class ProgramPrinter extends Visitor<String> { | public class ProgramPrinter extends Visitor<String> { | ||||
| private final String value; | |||||
| public ProgramPrinter(Program program) { | |||||
| value = visit(program); | |||||
| } | |||||
| /*!- Visit functions */ | |||||
| public String visitAssignment(Assignment assignment) { | public String visitAssignment(Assignment assignment) { | ||||
| ExpressionPrinter identifierPrinter = new ExpressionPrinter(assignment.identifier); | |||||
| ExpressionPrinter expressionPrinter = new ExpressionPrinter(assignment.expression); | |||||
| return identifierPrinter.getValue() + " := " + expressionPrinter.getValue(); | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| return printer.getValue(assignment.identifier) + " := " + printer.getValue(assignment.expression); | |||||
| } | } | ||||
| public String visitComposition(Composition composition) { | public String visitComposition(Composition composition) { | ||||
| @@ -21,16 +29,20 @@ public class ProgramPrinter extends Visitor<String> { | |||||
| } | } | ||||
| public String visitConditional(Conditional conditional) { | public String visitConditional(Conditional conditional) { | ||||
| ExpressionPrinter conditionPrinter = new ExpressionPrinter(conditional.condition); | |||||
| return "if (" + conditionPrinter.getValue() + ") then { " + visit(conditional.thenCase) + " } else { " + visit(conditional.elseCase) + " }"; | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| return "if (" + printer.getValue(conditional.condition) + ") then { " + visit(conditional.thenCase) + " } else { " + visit(conditional.elseCase) + " }"; | |||||
| } | } | ||||
| public String visitLoop(Loop loop) { | public String visitLoop(Loop loop) { | ||||
| ExpressionPrinter conditionPrinter = new ExpressionPrinter(loop.condition); | |||||
| return "while (" + conditionPrinter.getValue() + ") { " + visit(loop.program) + " }"; | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| return "while (" + printer.getValue(loop.condition) + ") { " + visit(loop.program) + " }"; | |||||
| } | } | ||||
| public String getValue() { | |||||
| return value; | |||||
| /*! | |||||
| The `getValue` function takes a `Program` instance as an argument and returns | |||||
| the string serialisation of the given program. | |||||
| */ | |||||
| public String getValue(Program program) { | |||||
| return visit(program); | |||||
| } | } | ||||
| } | } | ||||
| @@ -5,9 +5,6 @@ import org.junit.Test; | |||||
| import static org.junit.Assert.*; | import static org.junit.Assert.*; | ||||
| /** | |||||
| * Created by nils on 15.01.2017. | |||||
| */ | |||||
| public class ExpressionPrinterTest { | public class ExpressionPrinterTest { | ||||
| final String identifierCode = "a"; | final String identifierCode = "a"; | ||||
| final Identifier identifier = new Identifier(identifierCode); | final Identifier identifier = new Identifier(identifierCode); | ||||
| @@ -24,26 +21,26 @@ public class ExpressionPrinterTest { | |||||
| @Test | @Test | ||||
| public void testVisitAddition() { | public void testVisitAddition() { | ||||
| ExpressionPrinter printer = new ExpressionPrinter(addition); | |||||
| assertEquals(additionCode, printer.getValue()); | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| assertEquals(additionCode, printer.getValue(addition)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitIdentifier() { | public void testVisitIdentifier() { | ||||
| ExpressionPrinter printer = new ExpressionPrinter(identifier); | |||||
| assertEquals(identifierCode, printer.getValue()); | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| assertEquals(identifierCode, printer.getValue(identifier)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitInt() { | public void testVisitInt() { | ||||
| ExpressionPrinter printer = new ExpressionPrinter(integer); | |||||
| assertEquals(integerCode, printer.getValue()); | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| assertEquals(integerCode, printer.getValue(integer)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitSubtraction() { | public void testVisitSubtraction() { | ||||
| ExpressionPrinter printer = new ExpressionPrinter(subtraction); | |||||
| assertEquals(subtractionCode, printer.getValue()); | |||||
| ExpressionPrinter printer = new ExpressionPrinter(); | |||||
| assertEquals(subtractionCode, printer.getValue(subtraction)); | |||||
| } | } | ||||
| } | } | ||||
| @@ -27,32 +27,32 @@ public class ProgramPrinterTest { | |||||
| @Test | @Test | ||||
| public void testVisitAssignment() { | public void testVisitAssignment() { | ||||
| ProgramPrinter printer = new ProgramPrinter(assignment); | |||||
| assertEquals(assignmentCode, printer.getValue()); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| assertEquals(assignmentCode, printer.getValue(assignment)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitComposition() { | public void testVisitComposition() { | ||||
| ProgramPrinter printer = new ProgramPrinter(composition); | |||||
| assertEquals(compositionCode, printer.getValue()); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| assertEquals(compositionCode, printer.getValue(composition)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitConditional() { | public void testVisitConditional() { | ||||
| ProgramPrinter printer = new ProgramPrinter(conditional); | |||||
| assertEquals(conditionalCode, printer.getValue()); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| assertEquals(conditionalCode, printer.getValue(conditional)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitLoop() { | public void testVisitLoop() { | ||||
| ProgramPrinter printer = new ProgramPrinter(loop); | |||||
| assertEquals(loopCode, printer.getValue()); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| assertEquals(loopCode, printer.getValue(loop)); | |||||
| } | } | ||||
| @Test | @Test | ||||
| public void testVisitProgram() { | public void testVisitProgram() { | ||||
| ProgramPrinter printer = new ProgramPrinter(program); | |||||
| assertEquals(programCode, printer.getValue()); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| assertEquals(programCode, printer.getValue(program)); | |||||
| } | } | ||||
| } | } | ||||