diff --git a/docco/horizontal/index.html b/docco/horizontal/index.html index f576331..b411cd4 100644 --- a/docco/horizontal/index.html +++ b/docco/horizontal/index.html @@ -1 +1 @@ -
package expression;An Addition consists of a leftHandSide and a rightHandSide expression, which are supposed to be added.
For example
+package expression;An Addition consists of a leftHandSide and a rightHandSide expression, which are supposed to be added.
For example
new Addition(new Identifier("x"), new Int(2))
represents the code
x + 2
@@ -9,10 +9,7 @@
public Addition(Expression leftHandSide, Expression rightHandSide) {
this.leftHandSide = leftHandSide;
this.rightHandSide = rightHandSide;
- } @Override
- public String toString() {
- return "(" + leftHandSide + " + " + rightHandSide + ")";
- } @Override
+ } @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/expression/Expression.java.html b/docco/horizontal/src/main/java/expression/Expression.java.html
index aa1b3b6..1e61460 100644
--- a/docco/horizontal/src/main/java/expression/Expression.java.html
+++ b/docco/horizontal/src/main/java/expression/Expression.java.html
@@ -1,4 +1,4 @@
-Expression.java Expression
Expression can be written as the following Algebraic Data Type (ADT)
+Expression.java Expression
Expression can be written as the following Algebraic Data Type (ADT)
Expression = Addition(leftHandSide: Expression, rightHandSide: Expression)
| Subtraction(leftHandSide: Expression, rightHandSide: Expression)
| Identifier(name: String)
diff --git a/docco/horizontal/src/main/java/expression/Identifier.java.html b/docco/horizontal/src/main/java/expression/Identifier.java.html
index 59aa136..3df251c 100644
--- a/docco/horizontal/src/main/java/expression/Identifier.java.html
+++ b/docco/horizontal/src/main/java/expression/Identifier.java.html
@@ -1,12 +1,9 @@
-Identifier.java Identifier
------------------Show Header Code ( lines)------------------package expression;
1An Identifier consists only of the name of the identifier. This class is only needed as a wrapper which allows us to use an identifier as an expression.
public class Identifier extends Expression {
+Identifier.java Identifier
------------------Show Header Code ( lines)------------------package expression;
1An Identifier consists only of the name of the identifier. This class is only needed as a wrapper which allows us to use an identifier as an expression.
public class Identifier extends Expression {
public final String name;
public Identifier(String name) {
this.name = name;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return name;
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/expression/Int.java.html b/docco/horizontal/src/main/java/expression/Int.java.html
index 80dcd82..decc8b1 100644
--- a/docco/horizontal/src/main/java/expression/Int.java.html
+++ b/docco/horizontal/src/main/java/expression/Int.java.html
@@ -1,12 +1,9 @@
-Int.java Int(eger)
In order to avoid confusion with Java's Integer auto-boxing class for the primitive int this wrapper is called Int instead of Integer.
------------------Show Header Code ( lines)------------------package expression;
1An Int consists only of its value. This class is only needed as a wrapper which allows us to use an integer as an expression.
public class Int extends Expression {
+Int.java Int(eger)
In order to avoid confusion with Java's Integer auto-boxing class for the primitive int this wrapper is called Int instead of Integer.
------------------Show Header Code ( lines)------------------package expression;
1An Int consists only of its value. This class is only needed as a wrapper which allows us to use an integer as an expression.
public class Int extends Expression {
public final int value;
public Int(int value) {
this.value = value;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return Integer.toString(value);
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/expression/Subtraction.java.html b/docco/horizontal/src/main/java/expression/Subtraction.java.html
index 5da1f5d..525b563 100644
--- a/docco/horizontal/src/main/java/expression/Subtraction.java.html
+++ b/docco/horizontal/src/main/java/expression/Subtraction.java.html
@@ -1,4 +1,4 @@
-Subtraction.java Subtraction
------------------Show Header Code ( lines)------------------package expression;
1A Subtraction consists of a leftHandSide and a rightHandSide expression, which are supposed to be subtracted.
For example
+Subtraction.java Subtraction
------------------Show Header Code ( lines)------------------package expression;
1A Subtraction consists of a leftHandSide and a rightHandSide expression, which are supposed to be subtracted.
For example
new Subtraction(new Identifier("x"), new Int(2))
represents the code
x - 2
@@ -9,10 +9,7 @@
public Subtraction(Expression leftHandSide, Expression rightHandSide) {
this.leftHandSide = leftHandSide;
this.rightHandSide = rightHandSide;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return "(" + leftHandSide + " - " + rightHandSide + ")";
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/interpreter/Evaluator.java.html b/docco/horizontal/src/main/java/interpreter/Evaluator.java.html
index edbb84f..e0452dd 100644
--- a/docco/horizontal/src/main/java/interpreter/Evaluator.java.html
+++ b/docco/horizontal/src/main/java/interpreter/Evaluator.java.html
@@ -1,4 +1,4 @@
-Evaluator.java Evaluator
The evaluator implements the semantics defined by the function eval: Expr * V -> Z, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. eval is inductively defined as follows:
+Evaluator.java Evaluator
The evaluator implements the semantics defined by the function eval: Expr * V -> Z, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. eval is inductively defined as follows:
eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
eval(e1 "-" e2, v) = eval(e1, v) − eval(e2, v)
eval(x, v) = v(x)
@@ -9,26 +9,25 @@ eval(z, v) = z
a variable valuation v,
and identifier x and
an integer z.
-
------------------Show Code ( lines)------------------package interpreter;
+
------------------Show Header Code ( lines)------------------package interpreter;
import expression.*;
import java.util.HashMap;
import java.util.Map;
1The Evaluator implements the evaluation function defined above with the help of the Visitor. The Evaluator takes an Expression in the constructor and provides a method eval() which evaluates the given expression and returns the result as an integer. For a given expression of type Expression it can be used as follows
-Evaluator evaluator = new Evaluator(expression)
-System.out.println(evaluator.eval());
+Evaluator evaluator = new Evaluator(expression, valuation)
+System.out.println(evaluator.getValue());
The evaluation function eval takes the variable valuation v, which is passed on recursively. As the valuation is not changed during the evaluation process, it can be stored in a global variable which is not changed.
public class Evaluator extends Visitor<Integer> {
-
- final Expression expression;
- final Map<String, Integer> valuation = new HashMap<String, Integer>();
+ private final int value;
+ private final Map<String, Integer> valuation = new HashMap<String, Integer>();
public Evaluator(Expression expression, Map<String, Integer> valuation) {
- this.expression = expression;
this.valuation.putAll(valuation);
+ value = visit(expression);
}
- public int eval() {
- return visit(expression);
+ public int getValue() {
+ return value;
}
2eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
public Integer visitAddition(Addition addition) {
return visit(addition.leftHandSide) + visit(addition.rightHandSide);
diff --git a/docco/horizontal/src/main/java/interpreter/Interpreter.java.html b/docco/horizontal/src/main/java/interpreter/Interpreter.java.html
index 28efca1..138d7ab 100644
--- a/docco/horizontal/src/main/java/interpreter/Interpreter.java.html
+++ b/docco/horizontal/src/main/java/interpreter/Interpreter.java.html
@@ -1,4 +1,4 @@
-Interpreter.java Interpreter
The interpreter consists of the Interpreter defined in this file that can run a Program and the Evaluator that can evaluate an Expression.
The interpreter implements the semantics defined by the function sem: Prog * V -> V, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. sem is inductively defined as follows:
+Interpreter.java Interpreter
The interpreter consists of the Interpreter defined in this file that can run a Program and the Evaluator that can evaluate an Expression.
The interpreter implements the semantics defined by the function sem: Prog * V -> V, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. sem is inductively defined as follows:
sem(x ":=" e, v) = v.update(x, eval(e, v))
sem(c1 ";" c2) = sem(c2, sem(c1, v))
sem("if" "(" e ")" "then" "{" c1 "}" else "{" c2 "}") =
@@ -22,8 +22,7 @@ import java.util.Map;
public class Interpreter extends Visitor {
- final Program program;
- final Map<String, Integer> valuation = new HashMap<String, Integer>();
+ private final Map<String, Integer> valuation = new HashMap<String, Integer>();
public Map<String, Integer> getValuation() {
Map<String, Integer> result = new HashMap<String, Integer>();
@@ -32,12 +31,11 @@ System.out.println(interpreter.getValuation());
}
public Interpreter(Program program) {
- this.program = program;
visit(program);
}
2sem(x ":=" e, v) = v.update(x, eval(e, v))
public void visitAssignment(Assignment assignment) {
Evaluator evaluator = new Evaluator(assignment.expression, valuation);
- valuation.put(assignment.identifier.name, evaluator.eval());
+ valuation.put(assignment.identifier.name, evaluator.getValue());
}
3sem(c1 ";" c2) = sem(c2, sem(c1, v))
public void visitComposition(Composition composition) {
visit(composition.first);
@@ -47,7 +45,7 @@ System.out.println(interpreter.getValuation());
sem(c2, v) else
public void visitConditional(Conditional conditional) {
Evaluator evaluator = new Evaluator(conditional.condition, valuation);
- if (evaluator.eval() != 0) {
+ if (evaluator.getValue() != 0) {
visit(conditional.thenCase);
} else {
visit(conditional.elseCase);
@@ -57,7 +55,7 @@ System.out.println(interpreter.getValuation());
v else
public void visitLoop(Loop loop) {
Evaluator evaluator = new Evaluator(loop.condition, valuation);
- if (evaluator.eval() != 0) {
+ if (evaluator.getValue() != 0) {
visit(new Composition(loop.program, loop));
}
}
diff --git a/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html b/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html
index c852954..64abbce 100644
--- a/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html
+++ b/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html
@@ -1,4 +1,4 @@
-InterpreterException.java InterpreterException
------------------Show Code ( lines)------------------package interpreter;
1The InterpreterException is raised if anything goes wrong during the evaluation of an Expression or running a Program.
public class InterpreterException extends RuntimeException {
+InterpreterException.java InterpreterException
------------------Show Header Code ( lines)------------------package interpreter;
1The InterpreterException is raised if anything goes wrong during the evaluation of an Expression or running a Program.
public class InterpreterException extends RuntimeException {
public InterpreterException(String error) {
super(error);
}
diff --git a/docco/horizontal/src/main/java/interpreter/Visitor.java.html b/docco/horizontal/src/main/java/interpreter/Visitor.java.html
index 79ebfb6..c22ea71 100644
--- a/docco/horizontal/src/main/java/interpreter/Visitor.java.html
+++ b/docco/horizontal/src/main/java/interpreter/Visitor.java.html
@@ -1,4 +1,4 @@
-Visitor.java Visitor
The interpreter (and the evaluator) are performing structural recursion on the inductive data structure Program and Expression, respectively. We want to define one interpreter function that behaves differently depending on the argument. In functional languages this is done with Pattern Matching and in Java this is typically implemented using the Visitor Pattern.
------------------Show Code ( lines)------------------package interpreter;
1This Visitor is implemented using [Reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)). That is kind of cheating, but simplifies the classical Visitor pattern a lot. Of course the performance is bad, but performance is not an issue in this little demonstration and actually there are a lot of other performance issues as well.
public abstract class Visitor<T> {
+Visitor.java Visitor
The interpreter (and the evaluator) are performing structural recursion on the inductive data structure Program and Expression, respectively. We want to define one interpreter function that behaves differently depending on the argument. In functional languages this is done with Pattern Matching and in Java this is typically implemented using the Visitor Pattern.
------------------Show Header Code ( lines)------------------package interpreter;
1This Visitor is implemented using [Reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)). That is kind of cheating, but simplifies the classical Visitor pattern a lot. Of course the performance is bad, but performance is not an issue in this little demonstration and actually there are a lot of other performance issues as well.
public abstract class Visitor<T> {
@SuppressWarnings("unchecked")
public T visit(Object object) {
try {
2Get the name of the class of the given object, search for a method with this name in self and call it.
return (T) this.getClass().getMethod("visit" + object.getClass().getSimpleName(), object.getClass()).invoke(this, object);
diff --git a/docco/horizontal/src/main/java/parser/Parser.java.html b/docco/horizontal/src/main/java/parser/Parser.java.html
index 9093f17..f937ce8 100644
--- a/docco/horizontal/src/main/java/parser/Parser.java.html
+++ b/docco/horizontal/src/main/java/parser/Parser.java.html
@@ -1,4 +1,4 @@
-Parser.java Parser
In order to parse simple while programs we use a Recursive descent parser. The syntax of our while programs are defined by the following grammar in Extended Backus-Naur Form (EBNF):
+Parser.java Parser
In order to parse simple while programs we use a Recursive descent parser. The syntax of our while programs are defined by the following grammar in Extended Backus-Naur Form (EBNF):
Prog = Id ":=" Expr |
Prog ";" Prog |
"if" "(" Expr ")" "then" "{" Prog "}" "else" "{" Prog "}" |
diff --git a/docco/horizontal/src/main/java/parser/SyntaxException.java.html b/docco/horizontal/src/main/java/parser/SyntaxException.java.html
index bf95ef2..3262425 100644
--- a/docco/horizontal/src/main/java/parser/SyntaxException.java.html
+++ b/docco/horizontal/src/main/java/parser/SyntaxException.java.html
@@ -1,4 +1,4 @@
-SyntaxException.java SyntaxException
------------------Show Code ( lines)------------------package parser;
1A SyntaxException is raised by the function in the Parser if an expected was not found at the current position.
public class SyntaxException extends RuntimeException {
+SyntaxException.java SyntaxException
------------------Show Header Code ( lines)------------------package parser;
1A SyntaxException is raised by the function in the Parser if an expected was not found at the current position.
public class SyntaxException extends RuntimeException {
public final String expected;
public final int position;
diff --git a/docco/horizontal/src/main/java/printer/ExpressionPrinter.java.html b/docco/horizontal/src/main/java/printer/ExpressionPrinter.java.html
new file mode 100644
index 0000000..de5c92f
--- /dev/null
+++ b/docco/horizontal/src/main/java/printer/ExpressionPrinter.java.html
@@ -0,0 +1,22 @@
+ExpressionPrinter.java ExpressionPrinter
The ExpressionPrinter is used for string serialization of a given Expression.
------------------Show Header Code ( lines)------------------package printer;
+
+import expression.*;
+import interpreter.Visitor;
public class ExpressionPrinter extends Visitor<String> {
+ public String visitAddition(Addition addition) {
+ return visit(addition.leftHandSide) + " + " + visit(addition.rightHandSide);
+ }
+
+ public String visitIdentifier(Identifier identifier) {
+ return identifier.name;
+ }
+
+ public String visitInt(Int integer) {
+ return Integer.toString(integer.value);
+ }
+
+ public String visitSubtraction(Subtraction subtraction) {
+ return visit(subtraction.leftHandSide) + " - " + visit(subtraction.rightHandSide);
+ }
2The print function takes an Expression instance as an argument and returns the string serialization of the given expression.
public String print(Expression expression) {
+ return visit(expression);
+ }
+}
\ No newline at end of file
diff --git a/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html b/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html
new file mode 100644
index 0000000..26d8dc6
--- /dev/null
+++ b/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html
@@ -0,0 +1,24 @@
+ProgramPrinter.java ProgramPrinter
The ProgramPrinter is used for string serialization of a given Program.
------------------Show Header Code ( lines)------------------package printer;
+
+import interpreter.Visitor;
+import program.*;
public class ProgramPrinter extends Visitor<String> {
+ private final ExpressionPrinter printer = new ExpressionPrinter();
+
+ public String visitAssignment(Assignment assignment) {
+ return printer.print(assignment.identifier) + " := " + printer.print(assignment.expression);
+ }
+
+ public String visitComposition(Composition composition) {
+ return visit(composition.first) + " ; " + visit(composition.second);
+ }
+
+ public String visitConditional(Conditional conditional) {
+ return "if (" + printer.print(conditional.condition) + ") then { " + visit(conditional.thenCase) + " } else { " + visit(conditional.elseCase) + " }";
+ }
+
+ public String visitLoop(Loop loop) {
+ return "while (" + printer.print(loop.condition) + ") { " + visit(loop.program) + " }";
+ }
2The print function takes a Program instance as an argument and returns the string serialization of the given program.
public String print(Program program) {
+ return visit(program);
+ }
+}
\ No newline at end of file
diff --git a/docco/horizontal/src/main/java/program/Assignment.java.html b/docco/horizontal/src/main/java/program/Assignment.java.html
index 40b3263..f23a32a 100644
--- a/docco/horizontal/src/main/java/program/Assignment.java.html
+++ b/docco/horizontal/src/main/java/program/Assignment.java.html
@@ -1,4 +1,4 @@
-Assignment.java Assignment
------------------Show Header Code ( lines)------------------package program;
+Assignment.java Assignment
------------------Show Header Code ( lines)------------------package program;
import expression.Expression;
import expression.Identifier;
1An Assignment consists of an identifier and an expression which should be evaluated and the result stored in the variable named by the identifier.
For example
@@ -12,10 +12,7 @@ import expression.Identifier;------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return identifier + " := " + expression;
- }
------------------Show generated equals method Code ( lines)------------------ @Override
+ }
------------------Show generated equals method Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/program/Composition.java.html b/docco/horizontal/src/main/java/program/Composition.java.html
index ad9f8ca..bda20c4 100644
--- a/docco/horizontal/src/main/java/program/Composition.java.html
+++ b/docco/horizontal/src/main/java/program/Composition.java.html
@@ -1,14 +1,11 @@
-Composition.java Composition
------------------Show Header Code ( lines)------------------package program;
1A Composition combines two programs (first and second) with the intended semantics of sequential composition.
public class Composition extends Program {
+Composition.java Composition
------------------Show Header Code ( lines)------------------package program;
1A Composition combines two programs (first and second) with the intended semantics of sequential composition.
public class Composition extends Program {
public final Program first;
public final Program second;
public Composition(Program first, Program second) {
this.first = first;
this.second = second;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return first + " ; " + second;
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/program/Conditional.java.html b/docco/horizontal/src/main/java/program/Conditional.java.html
index 3cc317d..a02295c 100644
--- a/docco/horizontal/src/main/java/program/Conditional.java.html
+++ b/docco/horizontal/src/main/java/program/Conditional.java.html
@@ -1,4 +1,4 @@
-Conditional.java Conditional
------------------Show Header Code ( lines)------------------package program;
+Conditional.java Conditional
------------------Show Header Code ( lines)------------------package program;
import expression.Expression;
1A Conditional consists of the condition expression and the two programs thenCase and elseCase with the intended semantics of execution the elseCase if the expression evaluates to 0 and the thenCase otherwise.
public class Conditional extends Program {
public final Expression condition;
@@ -20,8 +20,5 @@ import expression.Expression;
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return "if (" + condition + ") then { " + thenCase + " } else { " + elseCase + " }";
}
}
\ No newline at end of file
diff --git a/docco/horizontal/src/main/java/program/Loop.java.html b/docco/horizontal/src/main/java/program/Loop.java.html
index cee02eb..f716f21 100644
--- a/docco/horizontal/src/main/java/program/Loop.java.html
+++ b/docco/horizontal/src/main/java/program/Loop.java.html
@@ -1,4 +1,4 @@
-Loop.java Loop
------------------Show Header Code ( lines)------------------package program;
+Loop.java Loop
------------------Show Header Code ( lines)------------------package program;
import expression.Expression;
1A Loop consists of a condition and a program with the intended semantics of execution the program while the condition evaluates to a non-zero value.
public class Loop extends Program {
public final Expression condition;
@@ -7,10 +7,7 @@ import expression.Expression;
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return "while (" + condition + ") { " + program + " }";
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/horizontal/src/main/java/program/Program.java.html b/docco/horizontal/src/main/java/program/Program.java.html
index 5c3620d..86520f9 100644
--- a/docco/horizontal/src/main/java/program/Program.java.html
+++ b/docco/horizontal/src/main/java/program/Program.java.html
@@ -1,4 +1,4 @@
-Program.java Program
Program can be written as the following Algebraic Data Type (ADT)
+Program.java Program
Program can be written as the following Algebraic Data Type (ADT)
Program = Assignment(identifier: Identifier, expression: Expression)
| Composition(first: Program, second: Program)
| Loop(condition: Expression, program: Program)
diff --git a/docco/vertical/index.html b/docco/vertical/index.html
index f576331..b411cd4 100644
--- a/docco/vertical/index.html
+++ b/docco/vertical/index.html
@@ -1 +1 @@
-Docco Index Docco Index
\ No newline at end of file
+Docco Index Docco Index
\ No newline at end of file
diff --git a/docco/vertical/src/main/java/expression/Addition.java.html b/docco/vertical/src/main/java/expression/Addition.java.html
index a112452..9ef9faf 100644
--- a/docco/vertical/src/main/java/expression/Addition.java.html
+++ b/docco/vertical/src/main/java/expression/Addition.java.html
@@ -1,4 +1,4 @@
-Addition.java Addition
------------------Show Header Code ( lines)------------------package expression;
1An Addition consists of a leftHandSide and a rightHandSide expression, which are supposed to be added.
For example
+Addition.java Addition
------------------Show Header Code ( lines)------------------package expression;
1An Addition consists of a leftHandSide and a rightHandSide expression, which are supposed to be added.
For example
new Addition(new Identifier("x"), new Int(2))
represents the code
x + 2
@@ -9,10 +9,7 @@
public Addition(Expression leftHandSide, Expression rightHandSide) {
this.leftHandSide = leftHandSide;
this.rightHandSide = rightHandSide;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return "(" + leftHandSide + " + " + rightHandSide + ")";
- }
------------------Show Generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show Generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/vertical/src/main/java/expression/Expression.java.html b/docco/vertical/src/main/java/expression/Expression.java.html
index 5255ef7..701a60e 100644
--- a/docco/vertical/src/main/java/expression/Expression.java.html
+++ b/docco/vertical/src/main/java/expression/Expression.java.html
@@ -1,4 +1,4 @@
-Expression.java Expression
Expression can be written as the following Algebraic Data Type (ADT)
+Expression.java Expression
Expression can be written as the following Algebraic Data Type (ADT)
Expression = Addition(leftHandSide: Expression, rightHandSide: Expression)
| Subtraction(leftHandSide: Expression, rightHandSide: Expression)
| Identifier(name: String)
diff --git a/docco/vertical/src/main/java/expression/Identifier.java.html b/docco/vertical/src/main/java/expression/Identifier.java.html
index 40f6077..b5e4258 100644
--- a/docco/vertical/src/main/java/expression/Identifier.java.html
+++ b/docco/vertical/src/main/java/expression/Identifier.java.html
@@ -1,12 +1,9 @@
-Identifier.java Identifier
------------------Show Header Code ( lines)------------------package expression;
1An Identifier consists only of the name of the identifier. This class is only needed as a wrapper which allows us to use an identifier as an expression.
public class Identifier extends Expression {
+Identifier.java Identifier
------------------Show Header Code ( lines)------------------package expression;
1An Identifier consists only of the name of the identifier. This class is only needed as a wrapper which allows us to use an identifier as an expression.
public class Identifier extends Expression {
public final String name;
public Identifier(String name) {
this.name = name;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return name;
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/vertical/src/main/java/expression/Int.java.html b/docco/vertical/src/main/java/expression/Int.java.html
index 0e60993..a68bb07 100644
--- a/docco/vertical/src/main/java/expression/Int.java.html
+++ b/docco/vertical/src/main/java/expression/Int.java.html
@@ -1,12 +1,9 @@
-Int.java Int(eger)
In order to avoid confusion with Java's Integer auto-boxing class for the primitive int this wrapper is called Int instead of Integer.
------------------Show Header Code ( lines)------------------package expression;
1An Int consists only of its value. This class is only needed as a wrapper which allows us to use an integer as an expression.
public class Int extends Expression {
+Int.java Int(eger)
In order to avoid confusion with Java's Integer auto-boxing class for the primitive int this wrapper is called Int instead of Integer.
------------------Show Header Code ( lines)------------------package expression;
1An Int consists only of its value. This class is only needed as a wrapper which allows us to use an integer as an expression.
public class Int extends Expression {
public final int value;
public Int(int value) {
this.value = value;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return Integer.toString(value);
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/vertical/src/main/java/expression/Subtraction.java.html b/docco/vertical/src/main/java/expression/Subtraction.java.html
index 439e6b7..77c85a5 100644
--- a/docco/vertical/src/main/java/expression/Subtraction.java.html
+++ b/docco/vertical/src/main/java/expression/Subtraction.java.html
@@ -1,4 +1,4 @@
-Subtraction.java Subtraction
------------------Show Header Code ( lines)------------------package expression;
1A Subtraction consists of a leftHandSide and a rightHandSide expression, which are supposed to be subtracted.
For example
+Subtraction.java Subtraction
------------------Show Header Code ( lines)------------------package expression;
1A Subtraction consists of a leftHandSide and a rightHandSide expression, which are supposed to be subtracted.
For example
new Subtraction(new Identifier("x"), new Int(2))
represents the code
x - 2
@@ -9,10 +9,7 @@
public Subtraction(Expression leftHandSide, Expression rightHandSide) {
this.leftHandSide = leftHandSide;
this.rightHandSide = rightHandSide;
- }
------------------Show String serialization Code ( lines)------------------ @Override
- public String toString() {
- return "(" + leftHandSide + " - " + rightHandSide + ")";
- }
------------------Show generated equals implementation Code ( lines)------------------ @Override
+ }
------------------Show generated equals implementation Code ( lines)------------------ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
diff --git a/docco/vertical/src/main/java/interpreter/Evaluator.java.html b/docco/vertical/src/main/java/interpreter/Evaluator.java.html
index 46a4601..d8b516a 100644
--- a/docco/vertical/src/main/java/interpreter/Evaluator.java.html
+++ b/docco/vertical/src/main/java/interpreter/Evaluator.java.html
@@ -1,4 +1,4 @@
-Evaluator.java Evaluator
The evaluator implements the semantics defined by the function eval: Expr * V -> Z, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. eval is inductively defined as follows:
+Evaluator.java Evaluator
The evaluator implements the semantics defined by the function eval: Expr * V -> Z, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. eval is inductively defined as follows:
eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
eval(e1 "-" e2, v) = eval(e1, v) − eval(e2, v)
eval(x, v) = v(x)
@@ -9,26 +9,25 @@ eval(z, v) = z
a variable valuation v,
and identifier x and
an integer z.
-
------------------Show Code ( lines)------------------package interpreter;
+
------------------Show Header Code ( lines)------------------package interpreter;
import expression.*;
import java.util.HashMap;
import java.util.Map;
1The Evaluator implements the evaluation function defined above with the help of the Visitor. The Evaluator takes an Expression in the constructor and provides a method eval() which evaluates the given expression and returns the result as an integer. For a given expression of type Expression it can be used as follows
-Evaluator evaluator = new Evaluator(expression)
-System.out.println(evaluator.eval());
+Evaluator evaluator = new Evaluator(expression, valuation)
+System.out.println(evaluator.getValue());
The evaluation function eval takes the variable valuation v, which is passed on recursively. As the valuation is not changed during the evaluation process, it can be stored in a global variable which is not changed.
public class Evaluator extends Visitor<Integer> {
-
- final Expression expression;
- final Map<String, Integer> valuation = new HashMap<String, Integer>();
+ private final int value;
+ private final Map<String, Integer> valuation = new HashMap<String, Integer>();
public Evaluator(Expression expression, Map<String, Integer> valuation) {
- this.expression = expression;
this.valuation.putAll(valuation);
+ value = visit(expression);
}
- public int eval() {
- return visit(expression);
+ public int getValue() {
+ return value;
}
2eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
public Integer visitAddition(Addition addition) {
return visit(addition.leftHandSide) + visit(addition.rightHandSide);
diff --git a/docco/vertical/src/main/java/interpreter/Interpreter.java.html b/docco/vertical/src/main/java/interpreter/Interpreter.java.html
index 85fc3bf..28b6eaa 100644
--- a/docco/vertical/src/main/java/interpreter/Interpreter.java.html
+++ b/docco/vertical/src/main/java/interpreter/Interpreter.java.html
@@ -1,4 +1,4 @@
-Interpreter.java Interpreter
The interpreter consists of the Interpreter defined in this file that can run a Program and the Evaluator that can evaluate an Expression.
The interpreter implements the semantics defined by the function sem: Prog * V -> V, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. sem is inductively defined as follows:
+Interpreter.java Interpreter
The interpreter consists of the Interpreter defined in this file that can run a Program and the Evaluator that can evaluate an Expression.
The interpreter implements the semantics defined by the function sem: Prog * V -> V, where V = Id -> Z is the set of all variable valuations to the set Z set of integers. sem is inductively defined as follows:
sem(x ":=" e, v) = v.update(x, eval(e, v))
sem(c1 ";" c2) = sem(c2, sem(c1, v))
sem("if" "(" e ")" "then" "{" c1 "}" else "{" c2 "}") =
@@ -22,8 +22,7 @@ import java.util.Map;
public class Interpreter extends Visitor {
- final Program program;
- final Map<String, Integer> valuation = new HashMap<String, Integer>();
+ private final Map<String, Integer> valuation = new HashMap<String, Integer>();
public Map<String, Integer> getValuation() {
Map<String, Integer> result = new HashMap<String, Integer>();
@@ -32,12 +31,11 @@ System.out.println(interpreter.getValuation());
}
public Interpreter(Program program) {
- this.program = program;
visit(program);
}
2sem(x ":=" e, v) = v.update(x, eval(e, v))
public void visitAssignment(Assignment assignment) {
Evaluator evaluator = new Evaluator(assignment.expression, valuation);
- valuation.put(assignment.identifier.name, evaluator.eval());
+ valuation.put(assignment.identifier.name, evaluator.getValue());
}
3sem(c1 ";" c2) = sem(c2, sem(c1, v))
public void visitComposition(Composition composition) {
visit(composition.first);
@@ -47,7 +45,7 @@ System.out.println(interpreter.getValuation());
sem(c2, v) else
public void visitConditional(Conditional conditional) {
Evaluator evaluator = new Evaluator(conditional.condition, valuation);
- if (evaluator.eval() != 0) {
+ if (evaluator.getValue() != 0) {
visit(conditional.thenCase);
} else {
visit(conditional.elseCase);
@@ -57,7 +55,7 @@ System.out.println(interpreter.getValuation());
v else
public void visitLoop(Loop loop) {
Evaluator evaluator = new Evaluator(loop.condition, valuation);
- if (evaluator.eval() != 0) {
+ if (evaluator.getValue() != 0) {
visit(new Composition(loop.program, loop));
}
}
diff --git a/docco/vertical/src/main/java/interpreter/InterpreterException.java.html b/docco/vertical/src/main/java/interpreter/InterpreterException.java.html
index b602e00..4a3a9b4 100644
--- a/docco/vertical/src/main/java/interpreter/InterpreterException.java.html
+++ b/docco/vertical/src/main/java/interpreter/InterpreterException.java.html
@@ -1,4 +1,4 @@
-InterpreterException.java InterpreterException
------------------Show Code ( lines)------------------package interpreter;
1The InterpreterException is raised if anything goes wrong during the evaluation of an Expression or running a Program.
public class InterpreterException extends RuntimeException {
+InterpreterException.java InterpreterException
------------------Show Header Code ( lines)------------------package interpreter;
1The InterpreterException is raised if anything goes wrong during the evaluation of an Expression or running a Program.
public class InterpreterException extends RuntimeException {
public InterpreterException(String error) {
super(error);
}
diff --git a/docco/vertical/src/main/java/interpreter/Visitor.java.html b/docco/vertical/src/main/java/interpreter/Visitor.java.html
index 64e511a..82bffb0 100644
--- a/docco/vertical/src/main/java/interpreter/Visitor.java.html
+++ b/docco/vertical/src/main/java/interpreter/Visitor.java.html
@@ -1,4 +1,4 @@
-Visitor.java Visitor
The interpreter (and the evaluator) are performing structural recursion on the inductive data structure Program and Expression, respectively. We want to define one interpreter function that behaves differently depending on the argument. In functional languages this is done with Pattern Matching and in Java this is typically implemented using the Visitor Pattern.
------------------Show Code ( lines)------------------package interpreter;
1This Visitor is implemented using [Reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)). That is kind of cheating, but simplifies the classical Visitor pattern a lot. Of course the performance is bad, but performance is not an issue in this little demonstration and actually there are a lot of other performance issues as well.
public abstract class Visitor<T> {
+Visitor.java Visitor
The interpreter (and the evaluator) are performing structural recursion on the inductive data structure Program and Expression, respectively. We want to define one interpreter function that behaves differently depending on the argument. In functional languages this is done with Pattern Matching and in Java this is typically implemented using the Visitor Pattern.
------------------Show Header Code ( lines)------------------package interpreter;
1This Visitor is implemented using [Reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)). That is kind of cheating, but simplifies the classical Visitor pattern a lot. Of course the performance is bad, but performance is not an issue in this little demonstration and actually there are a lot of other performance issues as well.
public abstract class Visitor<T> {
@SuppressWarnings("unchecked")
public T visit(Object object) {
try {
2Get the name of the class of the given object, search for a method with this name in self and call it.
return (T) this.getClass().getMethod("visit" + object.getClass().getSimpleName(), object.getClass()).invoke(this, object);
diff --git a/docco/vertical/src/main/java/parser/Parser.java.html b/docco/vertical/src/main/java/parser/Parser.java.html
index 04354ec..653679e 100644
--- a/docco/vertical/src/main/java/parser/Parser.java.html
+++ b/docco/vertical/src/main/java/parser/Parser.java.html
@@ -1,4 +1,4 @@
-Parser.java Parser
In order to parse simple while programs we use a Recursive descent parser. The syntax of our while programs are defined by the following grammar in Extended Backus-Naur Form (EBNF):
+Parser.java Parser
In order to parse simple while programs we use a Recursive descent parser. The syntax of our while programs are defined by the following grammar in Extended Backus-Naur Form (EBNF):
Prog = Id ":=" Expr |
Prog ";" Prog |
"if" "(" Expr ")" "then" "{" Prog "}" "else" "{" Prog "}" |
diff --git a/docco/vertical/src/main/java/parser/SyntaxException.java.html b/docco/vertical/src/main/java/parser/SyntaxException.java.html
index f23db95..6871d99 100644
--- a/docco/vertical/src/main/java/parser/SyntaxException.java.html
+++ b/docco/vertical/src/main/java/parser/SyntaxException.java.html
@@ -1,4 +1,4 @@
-SyntaxException.java SyntaxException
------------------Show Code ( lines)------------------package parser;
1A SyntaxException is raised by the function in the Parser if an expected was not found at the current position.
public class SyntaxException extends RuntimeException {
+SyntaxException.java