Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.interpreter
Class BreakExpression<E>

java.lang.Object
  extended by dk.rode.thesis.interpreter.AbstractExpression<E>
      extended by dk.rode.thesis.interpreter.BreakExpression<E>
Type Parameters:
E - The type of value the evaluation of this expression produces.
All Implemented Interfaces:
Expression<E>, NonTerminalExpression<E>, TypedExpression<E>, Copyable<Expression<E>>, StrictCopyable<Expression<E>>

@Participant(value="NonTerminalExpression")
public class BreakExpression<E>
extends AbstractExpression<E>
implements NonTerminalExpression<E>, TypedExpression<E>

A break expression allows the interpretation of a given expression syntax-tree to break at the point of the break expression, and possibly continue the interpretation starting from a specified target expression. Only a TypedExpression instance can be the target.

The target expression cannot be a break expression unless explicitly cast to a TypedExpression, but a break expression can always be contained in the target expression.

Implementation notes:
A break expression is a crude way to transfer evaluation control, but quite effective for small languages though it depends on an interpreter. It utilises a break exception, which is caught and processed by the interpreter. The break exception stores the target expression to transfer control to, if any.

However, exception types cannot be generic, so the expression to transfer control to must be declared using a wild-card in the BreakException type, i.e. TypedExpression<?>. Before the expression is interpreted, the interpreter will cast it into the proper type, TypedExpression<E>. This only works because we only allow typed expressions as the targets for break exceptions. Hence, we can test on the actual type at runtime, but the compiler does not know this when we perform the cast, and we therefore have to suppress warnings explicitly.

Author:
Gunni Rode / rode.dk

Nested Class Summary
static class BreakExpression.BreakException
          A break exception is thrown when an break expression is evaluated.
 
Nested classes/interfaces inherited from interface dk.rode.thesis.interpreter.Expression
Expression.SymbolIdiom
 
Field Summary
private  TypedExpression<E> expression
          The target expression to transfer control to, if any.
 
Constructor Summary
BreakExpression()
          No-arg constructor.
BreakExpression(BreakExpression<E> expression)
          Copy constructor.
BreakExpression(TypedExpression<E> expression)
          Constructor.
 
Method Summary
 String asSymbol(Context context)
          Returns either exit or break[x], where x is the symbol representation of the target expression used, if any.
 BreakExpression<E> copy()
          Copies this expression.
 E evaluate(Context context)
          Evaluates this expression and returns the result.
 String name()
          The stand-alone symbol name for this expression.
 List<Expression<?>> operands()
          Returns the expression operands used by this expression, in order.
 String toString()
          Returns the string representation of this expression.
 Class<E> type()
          Return the type of value the evaluation of this expression produces.
 
Methods inherited from class dk.rode.thesis.interpreter.AbstractExpression
contains, equals, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.interpreter.Expression
contains
 

Field Detail

expression

private final transient TypedExpression<E> expression
The target expression to transfer control to, if any.

Can be null.

Constructor Detail

BreakExpression

public BreakExpression()
No-arg constructor.

When this expression is evaluated by an interpreter, the interpreter will exit will a null value.


BreakExpression

public BreakExpression(BreakExpression<E> expression)
Copy constructor.

Note, that if a break expression is to be the target expression, it must be cast to a TypedExpression and set using the BreakExpression(TypedExpression) constructor. This constructor is a copy constructor that simply copies expression.

Parameters:
expression - The expression to copy; cannot be null.
Throws:
NullPointerException - If expression is null.

BreakExpression

public BreakExpression(TypedExpression<E> expression)
Constructor.

When this expression is evaluated by an interpreter, the interpreter will transfer control to expression, if not null, and return the result of that evaluation.

If null, the interpreter will exit.

Parameters:
expression - The target expression to use; can be null.
Method Detail

asSymbol

public String asSymbol(Context context)
                throws ExpressionException
Returns either exit or break[x], where x is the symbol representation of the target expression used, if any.

Specified by:
asSymbol in interface Expression<E>
Parameters:
context - The context to use; never null.
Returns:
The symbolic representation; never null.
Throws:
ExpressionException - If the symbol cannot be generated.
See Also:
Expression.contains(Expression)

copy

public BreakExpression<E> copy()
Description copied from interface: Expression
Copies this expression.

Unlike Expression.asSymbol(Context), copying cannot handle cyclic expression references!

Specified by:
copy in interface Expression<E>
Specified by:
copy in interface TypedExpression<E>
Specified by:
copy in interface Copyable<Expression<E>>
Returns:
A copy of this expression; never null.

evaluate

public E evaluate(Context context)
           throws ExpressionException
Description copied from interface: Expression
Evaluates this expression and returns the result.

There is no guarantee that the evaluation of this expression will terminate!

Specified by:
evaluate in interface Expression<E>
Parameters:
context - The context to use; cannot be null.
Returns:
The result of the evaluation; never null.
Throws:
ExpressionException - If the evaluation fails.

name

public final String name()
Description copied from interface: Expression
The stand-alone symbol name for this expression.

Specified by:
name in interface Expression<E>
Returns:
exit or break.

operands

public List<Expression<?>> operands()
Description copied from interface: Expression
Returns the expression operands used by this expression, in order.

Modifying the returned list will not affect this expression.

Specified by:
operands in interface Expression<E>
Returns:
The operands; never null, but can be empty.

toString

public String toString()
Description copied from class: AbstractExpression
Returns the string representation of this expression.

Specified by:
toString in interface Expression<E>
Overrides:
toString in class AbstractExpression<E>
Returns:
The string representation; never null.

type

public final Class<E> type()
Description copied from interface: TypedExpression
Return the type of value the evaluation of this expression produces.

Specified by:
type in interface TypedExpression<E>
Returns:
The type of the target expression, or null if no expression is stored.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.