gnu.expr
Class Expression

java.lang.Object
  extended by gnu.mapping.PropertySet
      extended by gnu.mapping.Procedure
          extended by gnu.mapping.Procedure0
              extended by gnu.expr.Expression
All Implemented Interfaces:
Named, Printable, SourceLocator, org.xml.sax.Locator
Direct Known Subclasses:
AccessExp, ApplyExp, BeginExp, BlockExp, ErrorExp, ExitExp, IfExp, LangExp, QuoteExp, ScopeExp, SynchronizedExp, TryExp

public abstract class Expression
extends Procedure0
implements Printable, SourceLocator

Abstract class for syntactic forms that evaluate to a value. Scheme S-expressions get re-written to these before evaluation.


Field Summary
protected  int flags
           
protected static int NEXT_AVAIL_FLAG
           
static Expression[] noExpressions
           
 
Constructor Summary
Expression()
           
 
Method Summary
 void apply(CallContext ctx)
          Evaluate the expression.
 java.lang.Object apply0()
           
 void compile(Compilation comp, Declaration lhs)
          Compile an expression with checking suitable for a known Declaration.
abstract  void compile(Compilation comp, Target target)
           
 void compile(Compilation comp, Type type)
           
static void compileButFirst(Expression exp, Compilation comp)
          Compile all but the first sub-"statement".
 void compileNotePosition(Compilation comp, Target target, Expression position)
          Compile, but take note of line number.
 void compileWithPosition(Compilation comp, Target target)
          Same as compile, but emit line number beforehard.
 void compileWithPosition(Compilation comp, Target target, Expression position)
          Same as 2-argument compileWithPosition, but use some other Expression's line number.
 java.lang.Object eval(CallContext ctx)
           
 java.lang.Object eval(Environment env)
           
 int getColumnNumber()
          Return current column number.
 java.lang.String getFileName()
          Normally same as getSystemId.
 boolean getFlag(int flag)
           
 int getFlags()
           
 int getLineNumber()
          Get the line number of (the start of) this Expression.
 java.lang.String getPublicId()
           
 java.lang.String getSystemId()
           
 Type getType()
          Return the Type used to represent the values of this Expression.
 Expression inline(ApplyExp exp, InlineCalls walker, Declaration decl)
          Apply inlining transformations on a given ApplyExp.
 boolean isStableSourceLocation()
          True if position is unlikely to change.
static Expression makeWhile(java.lang.Object cond, java.lang.Object body, Compilation parser)
          Helper method to create a `while' statement.
 int match0(CallContext ctx)
          Pass zero arguments.
protected abstract  boolean mustCompile()
           
 void print(Consumer out)
           
abstract  void print(OutPort ps)
           
 void printLineColumn(OutPort out)
          Print line and column number if specified.
 void setFile(java.lang.String filename)
           
 void setFlag(boolean setting, int flag)
           
 void setFlag(int flag)
           
 void setLine(Compilation comp)
          Set line number from current position in Compilation.
 Expression setLine(Expression old)
           
 void setLine(int lineno)
           
 void setLine(int lineno, int colno)
           
 void setLocation(SourceLocator location)
          Copies the current location.
 boolean side_effects()
          True if evaluating may have side-effects.
 java.lang.String toString()
           
 java.lang.Object valueIfConstant()
          Return value if it is constant, or null if non-constant or unknown.
protected  Expression walk(ExpWalker walker)
           
protected  void walkChildren(ExpWalker walker)
           
 
Methods inherited from class gnu.mapping.Procedure0
apply1, apply2, apply3, apply4, applyN, numArgs
 
Methods inherited from class gnu.mapping.Procedure
apply, check0, check1, check2, check3, check4, checkArgCount, checkN, getSetter, getSourceLocation, match1, match2, match3, match4, matchN, maxArgs, minArgs, set0, set1, setN, setSetter, setSourceLocation
 
Methods inherited from class gnu.mapping.PropertySet
getName, getProperty, getSymbol, removeProperty, setName, setProperty, setProperty, setSymbol
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

noExpressions

public static final Expression[] noExpressions

flags

protected int flags

NEXT_AVAIL_FLAG

protected static final int NEXT_AVAIL_FLAG
See Also:
Constant Field Values
Constructor Detail

Expression

public Expression()
Method Detail

eval

public final java.lang.Object eval(CallContext ctx)
                            throws java.lang.Throwable
Throws:
java.lang.Throwable

eval

public final java.lang.Object eval(Environment env)
                            throws java.lang.Throwable
Throws:
java.lang.Throwable

mustCompile

protected abstract boolean mustCompile()

match0

public final int match0(CallContext ctx)
Description copied from class: Procedure
Pass zero arguments.

Overrides:
match0 in class Procedure
Returns:
non-negative if the match succeeded, else negative.

apply0

public final java.lang.Object apply0()
                              throws java.lang.Throwable
Specified by:
apply0 in class Procedure0
Throws:
java.lang.Throwable

apply

public void apply(CallContext ctx)
           throws java.lang.Throwable
Evaluate the expression. This is named apply rather than eval so it is compatible with the full-tail-call calling convention, and we can stash an Expression in CallContext's proc field. FIXME - are we making use of this?

Overrides:
apply in class Procedure
Throws:
java.lang.Throwable

print

public final void print(Consumer out)
Specified by:
print in interface Printable

print

public abstract void print(OutPort ps)

printLineColumn

public void printLineColumn(OutPort out)
Print line and column number if specified. This is a helper routineintended for use by print(OutPort).


compile

public abstract void compile(Compilation comp,
                             Target target)

compileWithPosition

public final void compileWithPosition(Compilation comp,
                                      Target target)
Same as compile, but emit line number beforehard.


compileWithPosition

public final void compileWithPosition(Compilation comp,
                                      Target target,
                                      Expression position)
Same as 2-argument compileWithPosition, but use some other Expression's line number.


compileNotePosition

public final void compileNotePosition(Compilation comp,
                                      Target target,
                                      Expression position)
Compile, but take note of line number.


compile

public final void compile(Compilation comp,
                          Type type)

compile

public final void compile(Compilation comp,
                          Declaration lhs)
Compile an expression with checking suitable for a known Declaration. Leaves the result on the stack (i.e. does not assign to the lhs). It does coerce the value to a suitable type for the lhs, and throw a hopefully-informative WrongType exception on failure.


compileButFirst

public static void compileButFirst(Expression exp,
                                   Compilation comp)
Compile all but the first sub-"statement". A kludge used for constructor methods, since if the first "statement" is a super-constructor we need to inject initializer expressions.


walk

protected Expression walk(ExpWalker walker)

walkChildren

protected void walkChildren(ExpWalker walker)

inline

public Expression inline(ApplyExp exp,
                         InlineCalls walker,
                         Declaration decl)
Apply inlining transformations on a given ApplyExp. Assumes the ApplyExp's function is the this expression, or can be optimized to this expression.

Parameters:
exp - an application whose function expression can be simplified to this expression.
walker - the context for the current inlining pass
decl - if non-null, a Declaration bound to this expression
Returns:
an Expression equivalent to te passed-in exp.

makeWhile

public static Expression makeWhile(java.lang.Object cond,
                                   java.lang.Object body,
                                   Compilation parser)
Helper method to create a `while' statement.


setLocation

public final void setLocation(SourceLocator location)
Copies the current location.


setLine

public final Expression setLine(Expression old)

setFile

public final void setFile(java.lang.String filename)

setLine

public final void setLine(int lineno,
                          int colno)

setLine

public final void setLine(int lineno)

getFileName

public final java.lang.String getFileName()
Description copied from interface: SourceLocator
Normally same as getSystemId.

Specified by:
getFileName in interface SourceLocator

setLine

public void setLine(Compilation comp)
Set line number from current position in Compilation.


getPublicId

public java.lang.String getPublicId()
Specified by:
getPublicId in interface SourceLocator
Specified by:
getPublicId in interface org.xml.sax.Locator

getSystemId

public java.lang.String getSystemId()
Specified by:
getSystemId in interface SourceLocator
Specified by:
getSystemId in interface org.xml.sax.Locator

getLineNumber

public final int getLineNumber()
Get the line number of (the start of) this Expression. The "first" line is line 1; unknown is -1.

Specified by:
getLineNumber in interface SourceLocator
Specified by:
getLineNumber in interface org.xml.sax.Locator

getColumnNumber

public final int getColumnNumber()
Description copied from interface: SourceLocator
Return current column number. The "first" column is column 1; unknown is -1.

Specified by:
getColumnNumber in interface SourceLocator
Specified by:
getColumnNumber in interface org.xml.sax.Locator

isStableSourceLocation

public boolean isStableSourceLocation()
Description copied from interface: SourceLocator
True if position is unlikely to change. True for an expression but not an input file.

Specified by:
isStableSourceLocation in interface SourceLocator

getType

public Type getType()
Return the Type used to represent the values of this Expression.


valueIfConstant

public java.lang.Object valueIfConstant()
Return value if it is constant, or null if non-constant or unknown.


setFlag

public void setFlag(boolean setting,
                    int flag)

setFlag

public void setFlag(int flag)

getFlags

public int getFlags()

getFlag

public boolean getFlag(int flag)

side_effects

public boolean side_effects()
True if evaluating may have side-effects.


toString

public java.lang.String toString()
Overrides:
toString in class Procedure