org.teatrove.tea.parsetree
Class Expression

java.lang.Object
  extended by org.teatrove.tea.parsetree.Node
      extended by org.teatrove.tea.parsetree.Expression
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
ArrayLookup, BinaryExpression, CallExpression, Literal, Lookup, NegateExpression, NewArrayExpression, NoOpExpression, NotExpression, ParenExpression, SpreadExpression, TernaryExpression, TypeExpression, VariableRef

public class Expression
extends Node

An Expression is a piece of code that, when executed, produces a value. All expressions have a type which represents the type of value is produces.

Author:
Brian S O'Neill
See Also:
Serialized Form

Nested Class Summary
static class Expression.Conversion
           
 
Constructor Summary
Expression(SourceInfo info)
           
 
Method Summary
 Object accept(NodeVisitor visitor)
          Every subclass of Node must override this method with the following: return visitor.visit(this).
 Object clone()
          Returns a clone of this Node and all its children.
 void convertTo(Type toType)
          Applies a type conversion to this expression which is chained to all previous conversions.
 void convertTo(Type toType, boolean preferCast)
          Applies a type conversion to this expression which is chained to all previous conversions.
 void forceConversion(Type toType, boolean preferCast)
           
 LinkedList<Expression.Conversion> getConversionChain()
          Returns a list of Conversion objects representing the all the conversions that have been applied to this Expression.
 Type getInitialType()
          Returns the type of this expression before any conversions were applied, or null if the type isn't set.
 Type getType()
          The type of an expression is not necessarily set by a parser.
 Object getValue()
          Most expressions can't generate a value at compile-time, so this method simply returns null.
 boolean isExceptionPossible()
          Returns true if an exception can be thrown while executing this Expression.
 boolean isValueKnown()
          Returns true if the value generated by this expression is known at compile-time.
 void setInitialType(Type type)
          Sets the intial type in the conversion chain, but does not clear the conversions.
 void setType(Type type)
          Sets the type of this expression, clearing the conversion chain.
 
Methods inherited from class org.teatrove.tea.parsetree.Node
getSourceInfo, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Expression

public Expression(SourceInfo info)
Method Detail

accept

public Object accept(NodeVisitor visitor)
Description copied from class: Node
Every subclass of Node must override this method with the following: return visitor.visit(this).

Specified by:
accept in class Node
Parameters:
visitor - A visitor of this Node
Returns:
Node The Node returned by the visitor
See Also:
NodeVisitor

clone

public Object clone()
Description copied from class: Node
Returns a clone of this Node and all its children. Immutable child objects are not necessarily cloned

Overrides:
clone in class Node

isExceptionPossible

public boolean isExceptionPossible()
Returns true if an exception can be thrown while executing this Expression. By default, returns true only if a type conversion could cause an exception.


getType

public Type getType()
The type of an expression is not necessarily set by a parser. It is typically set by a type checker. An expression's type may represent its natural type or its coerced type. A code generator is responsible for ensuring that the type it generates correctly matches the type of the expression.

Returns:
null if type is unknown

getInitialType

public Type getInitialType()
Returns the type of this expression before any conversions were applied, or null if the type isn't set.


convertTo

public final void convertTo(Type toType)
Applies a type conversion to this expression which is chained to all previous conversions.

Parameters:
toType - the type to convert to.

convertTo

public void convertTo(Type toType,
                      boolean preferCast)
Applies a type conversion to this expression which is chained to all previous conversions.

Parameters:
toType - the type to convert to.
preferCast - a hint that the conversion should be performed by a type cast operation, by default is true.
Throws:
IllegalArgumentException - when the conversion is illegal.

forceConversion

public void forceConversion(Type toType,
                            boolean preferCast)

getConversionChain

public LinkedList<Expression.Conversion> getConversionChain()
Returns a list of Conversion objects representing the all the conversions that have been applied to this Expression. Unless the type isn't set, the chain contains at least one element. The conversion chain may be reduced or expanded, so its length doesn't necessarily represent the exact sequence of calls to convertTo(org.teatrove.tea.compiler.Type).


setType

public void setType(Type type)
Sets the type of this expression, clearing the conversion chain.


setInitialType

public void setInitialType(Type type)
Sets the intial type in the conversion chain, but does not clear the conversions.


isValueKnown

public boolean isValueKnown()
Returns true if the value generated by this expression is known at compile-time. For most expressions, false is returned. Literals always return true.

See Also:
Literal

getValue

public Object getValue()
Most expressions can't generate a value at compile-time, so this method simply returns null. Call isValueKnown to check if the expression's value is known at compile-time.



Copyright © 1997-2012 TeaTrove.org. All Rights Reserved.