www.delorie.com/gnu/docs/kawa/kawa-tour_12.html   search  
 
Buy GNU books!


Kawa: Compiling Scheme to Java

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.11 Expressions

 
class Expression {
  public abstract Object eval (Environment env);
  public abstract void compile (Compilation comp, int flags);
  ...
}

The abstract Expression class represents partially processed expressions. These are in principle independent of the source language, but in practice there are some Scheme assumptions.

The eval method evaluates the Expression in the given Environment. The interactive command interface uses eval to evaluate expressions typed by the user. However, eval only support "simple" expressions, such as literals, identifiers, and applications. Expressions that define new local bindings (such lambda expressions and let forms) do not implement eval. If the user types in such an expression, it is wrapped inside a dummy function, compiled, and immediately executed. This is to avoid dealing with lexical binding in the evaluator. (We could compile all user expressions, but that entails a certain amount of overhead. Code generation creates new classes, and JDK 1.0 does not garbage-collect unused classes.)

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 on the Java evaluation stack.

 
class QuoteExp extends Expression {
Object value;
  public QuoteExp (Object val)
  { value = val; }
  public Object eval (Environment env)
  { return value; }
  public void compile (Compilation comp, int flags)
  { comp.compileConstant (value); }
  ...
}

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, and 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 to mark code that has a syntax error);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright © 2003   by The Free Software Foundation     Updated Jun 2003