The abstract Expression
class represents partially processed expressions.
These are in principle independent of the source language,
though there are still some Scheme assumptions wired in.
class Expression
{ ...;
public abstract Object eval
(Environment e);
public abstract void compile
(Compilation comp, Target targ);
}
The eval
method evaluates the
Expression
in the given
Environment
.
The compile
method is called when we are compiling
the body of a procedure. It is responsible for generating bytecodes
that evaluate the expression, and leave the result in a result
specified by the Target
parameter.
This is usually the Java evaluation stack,
but we will go into more detail later.
class QuoteExp extends Expression
{ ...;
Object value;
public QuoteExp(Object val)
{ value = val; }
public Object eval(Environment env)
{ return value; }
public void compile
(Compilation comp, Target target)
{ comp.compileConstant (value, target); }
}
A QuoteExp
represents a literal (self-evaluating form),
or a quoted form.
class ReferenceExp extends Expression
{ ...;
Symbol symbol;
Declaration binding;
}
A ReferenceExp
is a reference to a named variable.
The symbol
is the source form identifier.
If binding
is non-null, it is the lexical
binding of the identifier.
class ApplyExp extends Expression
{ ...;
Expression func;
Expression[] args;
}
An ApplyExp
is an application of a procedure func
to an argument list args
.
class ScopeExp extends Expression
{ ...;
ScopeExp outer; // Surrounding scope.
public Declaration add_decl(Symbol name)
{ ...Create new local variable... }
}
A ScopeExp
is a abstract class that represents a lexical
scoping construct. Concrete sub-classes are LetExp
(used for a let
binding form) and LambdaExp
.
class LambdaExp extends ScopeExp
{ ...;
Symbol name; // Optional.
Expression body;
int min_args;
int max_args;
}
The Scheme primitive syntax lambda
is translated
into a LambdaExp
, which represents anonymous procedures.
Each LambdaExp
is compiled into a different bytecoded class.
Invoking eval
causes the LambdaExp
to be compiled
into a class, the class to be loaded, an instance of the
class to be created, and the result coerced to a Procedure
.
Other sub-classes of Expression
are
IfExp
(used for conditional expressions);
BeginExp
(used for compound expressions);
SetExp
(used for assignments); and
ErrorExp
(used where a syntax error was found);