diff --git a/docco/horizontal/src/main/java/expression/Addition.java.html b/docco/horizontal/src/main/java/expression/Addition.java.html index 3fb77b7..419dcef 100644 --- a/docco/horizontal/src/main/java/expression/Addition.java.html +++ b/docco/horizontal/src/main/java/expression/Addition.java.html @@ -1,4 +1,4 @@ -Addition.java

Addition

------------------Show Header Code ( lines)------------------
package expression;
1

An 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;
1

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
diff --git a/docco/horizontal/src/main/java/expression/Expression.java.html b/docco/horizontal/src/main/java/expression/Expression.java.html
index 1e61460..333e678 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 3df251c..a628fe9 100644
--- a/docco/horizontal/src/main/java/expression/Identifier.java.html
+++ b/docco/horizontal/src/main/java/expression/Identifier.java.html
@@ -1,4 +1,4 @@
-Identifier.java

Identifier

------------------Show Header Code ( lines)------------------
package expression;
1

An 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;
1

An 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) {
diff --git a/docco/horizontal/src/main/java/expression/Int.java.html b/docco/horizontal/src/main/java/expression/Int.java.html
index decc8b1..7f48af2 100644
--- a/docco/horizontal/src/main/java/expression/Int.java.html
+++ b/docco/horizontal/src/main/java/expression/Int.java.html
@@ -1,4 +1,4 @@
-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;
1

An 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;
1

An 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) {
diff --git a/docco/horizontal/src/main/java/expression/Subtraction.java.html b/docco/horizontal/src/main/java/expression/Subtraction.java.html
index 525b563..13325f8 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;
1

A 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;
1

A 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
diff --git a/docco/horizontal/src/main/java/interpreter/Evaluator.java.html b/docco/horizontal/src/main/java/interpreter/Evaluator.java.html
index e0452dd..edb5113 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)
diff --git a/docco/horizontal/src/main/java/interpreter/Interpreter.java.html b/docco/horizontal/src/main/java/interpreter/Interpreter.java.html
index 138d7ab..96294ea 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 "}") =
diff --git a/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html b/docco/horizontal/src/main/java/interpreter/InterpreterException.java.html
index 64abbce..27e4bb2 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 Header Code ( lines)------------------
package interpreter;
1

The 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;
1

The 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 c22ea71..7370df8 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 Header Code ( lines)------------------
package interpreter;
1

This 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;
1

This 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 {
2

Get 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 f937ce8..38914f1 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 3262425..d3c29ca 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 Header Code ( lines)------------------
package parser;
1

A 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;
1

A 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
index de5c92f..0631635 100644
--- a/docco/horizontal/src/main/java/printer/ExpressionPrinter.java.html
+++ b/docco/horizontal/src/main/java/printer/ExpressionPrinter.java.html
@@ -1,4 +1,4 @@
-ExpressionPrinter.java

ExpressionPrinter

The ExpressionPrinter is used for string serialization of a given Expression.

------------------Show Header Code ( lines)------------------
package printer;
+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;
1

The ExpressionPrinter implements the string serialization with the help of the Visitor.

public class ExpressionPrinter extends Visitor<String> {
diff --git a/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html b/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html
index 26d8dc6..7dcfe53 100644
--- a/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html
+++ b/docco/horizontal/src/main/java/printer/ProgramPrinter.java.html
@@ -1,4 +1,4 @@
-ProgramPrinter.java

ProgramPrinter

The ProgramPrinter is used for string serialization of a given Program.

------------------Show Header Code ( lines)------------------
package printer;
+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.*;
1

The ProgramPrinter implements the string serialization with the help of the Visitor.

public class ProgramPrinter extends Visitor<String> {
diff --git a/docco/horizontal/src/main/java/program/Assignment.java.html b/docco/horizontal/src/main/java/program/Assignment.java.html
index f23a32a..e1c702f 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;
1

An 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

diff --git a/docco/horizontal/src/main/java/program/Composition.java.html b/docco/horizontal/src/main/java/program/Composition.java.html index bda20c4..ffe98bf 100644 --- a/docco/horizontal/src/main/java/program/Composition.java.html +++ b/docco/horizontal/src/main/java/program/Composition.java.html @@ -1,4 +1,4 @@ -Composition.java

Composition

------------------Show Header Code ( lines)------------------
package program;
1

A 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;
1

A 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;
 
diff --git a/docco/horizontal/src/main/java/program/Conditional.java.html b/docco/horizontal/src/main/java/program/Conditional.java.html
index a02295c..84efa84 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;
1

A 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;
diff --git a/docco/horizontal/src/main/java/program/Loop.java.html b/docco/horizontal/src/main/java/program/Loop.java.html
index f716f21..bf26b55 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;
1

A 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;
diff --git a/docco/horizontal/src/main/java/program/Program.java.html b/docco/horizontal/src/main/java/program/Program.java.html
index 86520f9..26a30ca 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/src/main/java/expression/Addition.java.html b/docco/vertical/src/main/java/expression/Addition.java.html
index 9ef9faf..2982a3f 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;
1

An 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;
1

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
diff --git a/docco/vertical/src/main/java/expression/Expression.java.html b/docco/vertical/src/main/java/expression/Expression.java.html
index 701a60e..794362b 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 b5e4258..fd4456a 100644
--- a/docco/vertical/src/main/java/expression/Identifier.java.html
+++ b/docco/vertical/src/main/java/expression/Identifier.java.html
@@ -1,4 +1,4 @@
-Identifier.java

Identifier

------------------Show Header Code ( lines)------------------
package expression;
1

An 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;
1

An 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) {
diff --git a/docco/vertical/src/main/java/expression/Int.java.html b/docco/vertical/src/main/java/expression/Int.java.html
index a68bb07..9bd1ab7 100644
--- a/docco/vertical/src/main/java/expression/Int.java.html
+++ b/docco/vertical/src/main/java/expression/Int.java.html
@@ -1,4 +1,4 @@
-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;
1

An 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;
1

An 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) {
diff --git a/docco/vertical/src/main/java/expression/Subtraction.java.html b/docco/vertical/src/main/java/expression/Subtraction.java.html
index 77c85a5..3e3ff62 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;
1

A 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;
1

A 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
diff --git a/docco/vertical/src/main/java/interpreter/Evaluator.java.html b/docco/vertical/src/main/java/interpreter/Evaluator.java.html
index d8b516a..8de0078 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)
diff --git a/docco/vertical/src/main/java/interpreter/Interpreter.java.html b/docco/vertical/src/main/java/interpreter/Interpreter.java.html
index 28b6eaa..03d09ae 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 "}") =
diff --git a/docco/vertical/src/main/java/interpreter/InterpreterException.java.html b/docco/vertical/src/main/java/interpreter/InterpreterException.java.html
index 4a3a9b4..3ce7ed4 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 Header Code ( lines)------------------
package interpreter;
1

The 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;
1

The 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 82bffb0..6e2522b 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 Header Code ( lines)------------------
package interpreter;
1

This 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;
1

This 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 {
2

Get 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 653679e..29deea6 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 6871d99..5867d96 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 Header Code ( lines)------------------
package parser;
1

A 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;
1

A 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/vertical/src/main/java/printer/ExpressionPrinter.java.html b/docco/vertical/src/main/java/printer/ExpressionPrinter.java.html
index a2cf457..d024355 100644
--- a/docco/vertical/src/main/java/printer/ExpressionPrinter.java.html
+++ b/docco/vertical/src/main/java/printer/ExpressionPrinter.java.html
@@ -1,4 +1,4 @@
-ExpressionPrinter.java

ExpressionPrinter

The ExpressionPrinter is used for string serialization of a given Expression.

------------------Show Header Code ( lines)------------------
package printer;
+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;
1

The ExpressionPrinter implements the string serialization with the help of the Visitor.

public class ExpressionPrinter extends Visitor<String> {
diff --git a/docco/vertical/src/main/java/printer/ProgramPrinter.java.html b/docco/vertical/src/main/java/printer/ProgramPrinter.java.html
index dec6c05..36f1761 100644
--- a/docco/vertical/src/main/java/printer/ProgramPrinter.java.html
+++ b/docco/vertical/src/main/java/printer/ProgramPrinter.java.html
@@ -1,4 +1,4 @@
-ProgramPrinter.java

ProgramPrinter

The ProgramPrinter is used for string serialization of a given Program.

------------------Show Header Code ( lines)------------------
package printer;
+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.*;
1

The ProgramPrinter implements the string serialization with the help of the Visitor.

public class ProgramPrinter extends Visitor<String> {
diff --git a/docco/vertical/src/main/java/program/Assignment.java.html b/docco/vertical/src/main/java/program/Assignment.java.html
index 892c65e..d94dfdc 100644
--- a/docco/vertical/src/main/java/program/Assignment.java.html
+++ b/docco/vertical/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;
1

An 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

diff --git a/docco/vertical/src/main/java/program/Composition.java.html b/docco/vertical/src/main/java/program/Composition.java.html index b756822..5627116 100644 --- a/docco/vertical/src/main/java/program/Composition.java.html +++ b/docco/vertical/src/main/java/program/Composition.java.html @@ -1,4 +1,4 @@ -Composition.java

Composition

------------------Show Header Code ( lines)------------------
package program;
1

A 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;
1

A 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;
 
diff --git a/docco/vertical/src/main/java/program/Conditional.java.html b/docco/vertical/src/main/java/program/Conditional.java.html
index 07075a8..4af7889 100644
--- a/docco/vertical/src/main/java/program/Conditional.java.html
+++ b/docco/vertical/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;
1

A 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;
diff --git a/docco/vertical/src/main/java/program/Loop.java.html b/docco/vertical/src/main/java/program/Loop.java.html
index 4f6cdd1..975d00a 100644
--- a/docco/vertical/src/main/java/program/Loop.java.html
+++ b/docco/vertical/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;
1

A 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;
diff --git a/docco/vertical/src/main/java/program/Program.java.html b/docco/vertical/src/main/java/program/Program.java.html
index 7f95257..f560dac 100644
--- a/docco/vertical/src/main/java/program/Program.java.html
+++ b/docco/vertical/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)