Jatha Software

org.jatha.compile
Class LispCompiler

java.lang.Object
  extended by org.jatha.compile.LispCompiler

public class LispCompiler
extends Object

LispCompiler has a compile() method that will compile a LISP expression and return the SECD code for that expression. Example LISP read/eval/print loop:

    expr   = parser.read(stream);
    code   = compiler.compile(expr);
    result = machine.eval(code);
    result.print();
 

Macro compilation contributed by Jean-Pierre Gaillardon, April 2005

See Also:
SECDMachine, SECDop

Field Summary
(package private)  LispValue AMP_REST
           
(package private)  LispValue AND
           
(package private) static boolean DEBUG
           
(package private)  LispValue DEFMACRO
           
(package private)  LispValue DEFUN
           
(package private)  LispValue DUMMY_FUNCTION
           
(package private)  LispValue DUMMY_MACRO
           
private  Jatha f_lisp
           
(package private)  LispValue IF
           
(package private)  LispValue LAMBDA
           
private  Stack legalBlocks
           
private  Stack legalTags
           
(package private)  LispValue LET
           
(package private)  LispValue LETREC
           
(package private)  LispValue MACRO
           
(package private)  LispValue OR
           
(package private)  LispValue PRIMITIVE
           
(package private)  LispValue PROGN
           
(package private)  LispValue QUOTE
           
private  Map registeredDos
           
(package private)  LispValue SETQ
           
(package private)  boolean WarnAboutSpecialsP
           
 
Constructor Summary
LispCompiler(Jatha lisp)
           
 
Method Summary
 LispValue compile(LispValue expr, LispValue valueList, LispValue code)
          compile takes a LISP expression, a list of global variables, and optionally an already-generated list of code.
 LispValue compile(SECDMachine machine, LispValue expr, LispValue varValues)
          compile takes a LISP expression, a list of global variables, and optionally an already-generated list of code.
(package private)  LispValue compileAnd(SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileAndAux(LispValue dummyVar, LispValue args)
           
(package private)  LispValue compileApp(SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileAppConstant(SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
           
 LispValue compileArgsLeftToRight(LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileAtom(SECDMachine machine, LispValue expr, LispValue valueList, LispValue code)
           
(package private)  LispValue compileBuiltin(SECDMachine machine, LispValue fn, LispValue args, LispValue valueList, LispValue code)
           
 LispValue compileConstantArgsLeftToRight(SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileDefmacro(SECDMachine machine, LispValue name, LispValue argsAndBody, LispValue valueList, LispValue code)
           
(package private)  LispValue compileDefun(SECDMachine machine, LispValue name, LispValue argsAndBody, LispValue valueList, LispValue code)
           
(package private)  LispValue compileIf(SECDMachine machine, LispValue test, LispValue thenExpr, LispValue elseExpr, LispValue valueList, LispValue code)
           
(package private)  LispValue compileLambda(SECDMachine machine, LispValue body, LispValue valueList, LispValue code)
           
 LispValue compileLet(SECDMachine machine, LispValue vars, LispValue values, LispValue valueList, LispValue body, LispValue code)
           
(package private)  LispValue compileList(SECDMachine machine, LispValue expr, LispValue valueList, LispValue code)
           
(package private)  LispValue compileOptimizedIf(SECDMachine machine, LispValue test, LispValue thenExpr, LispValue elseExpr, LispValue valueList, LispValue code)
           
(package private)  LispValue compileOr(SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileOrAux(LispValue dummyVar, LispValue args)
           
(package private)  LispValue compileProgn(LispValue body, LispValue valueList, LispValue code)
           
(package private)  LispValue compileSpecialBind(SECDMachine machine, LispValue vars, LispValue values, LispValue valueList, LispValue code)
           
(package private)  LispValue compileSpecialForm(SECDMachine machine, LispValue function, LispValue args, LispValue valueList, LispValue code)
           
(package private)  LispValue compileSpecialUnbind(SECDMachine machine, LispValue vars, LispValue code)
           
(package private)  LispValue compileUserDefinedFunction(SECDMachine machine, LispValue fn, LispValue args, LispValue valueList, LispValue code)
          FN is an instance of StandardLispFunction
 Stack getLegalBlocks()
           
 Stack getLegalTags()
           
 Map getRegisteredDos()
           
 LispValue indexAndAttribute(LispValue e, LispValue l)
          Looks up the symbol in a list of lists.
 int indexInList(LispValue e, LispValue l, LispValue[] attribute)
          Looks up the symbol in a list
 void init()
          Initializes the compiler by registering the LISP primitive functions so that the compiler can recognize them.
private  void initializeConstants()
           
static boolean isBuiltinFunction(LispValue code)
          Send in either code or a symbol with a function value.
 boolean isLegalTag(LispValue tag)
           
 boolean isMacroCode(LispValue code)
           
 LispValue quoteList(LispValue l)
          Returns the input list with quotes added before every top-level expression.
 void Register(LispPrimitive primitive)
          Use this function to register any new LISP primitives that you create from Java code.
 void Register(LispPrimitive primitive, LispPackage pkg)
          Use this function to register any new LISP primitives that you create from Java code.
 void Register(LispPrimitive primitive, String pkgName)
          Use this function to register any new LISP primitives that you create from Java code.
 boolean specialFormP(LispValue fn)
           
 LispValue valuesFromLetBindings(LispValue varValueList)
           
 LispValue varsFromLetBindings(LispValue varValueList)
           
 void WarnAboutSpecials(boolean value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

static boolean DEBUG

AND

LispValue AND

DEFMACRO

LispValue DEFMACRO

DEFUN

LispValue DEFUN

IF

LispValue IF

LAMBDA

LispValue LAMBDA

LET

LispValue LET

LETREC

LispValue LETREC

OR

LispValue OR

PRIMITIVE

LispValue PRIMITIVE

PROGN

LispValue PROGN

QUOTE

LispValue QUOTE

SETQ

LispValue SETQ

AMP_REST

LispValue AMP_REST

MACRO

LispValue MACRO

DUMMY_FUNCTION

LispValue DUMMY_FUNCTION

DUMMY_MACRO

LispValue DUMMY_MACRO

WarnAboutSpecialsP

boolean WarnAboutSpecialsP

f_lisp

private Jatha f_lisp

legalBlocks

private final Stack legalBlocks

legalTags

private final Stack legalTags

registeredDos

private final Map registeredDos
Constructor Detail

LispCompiler

public LispCompiler(Jatha lisp)
Method Detail

initializeConstants

private void initializeConstants()

init

public void init()
Initializes the compiler by registering the LISP primitive functions so that the compiler can recognize them.

See Also:
LispCompiler

Register

public void Register(LispPrimitive primitive)
Use this function to register any new LISP primitives that you create from Java code. The compiler will then recognize them and compile them appropriately. Example:
   compiler.Register(new RevAppendPrimitive());
 

Parameters:
primitive -
See Also:
LispPrimitive

Register

public void Register(LispPrimitive primitive,
                     LispPackage pkg)
Use this function to register any new LISP primitives that you create from Java code. The compiler will then recognize them and compile them appropriately. This version of the constructor accepts a package in which to intern the symbol. Example:
   compiler.Register(new RevAppendPrimitive());
 

Parameters:
primitive -
See Also:
LispPrimitive

Register

public void Register(LispPrimitive primitive,
                     String pkgName)
Use this function to register any new LISP primitives that you create from Java code. The compiler will then recognize them and compile them appropriately. This version of the constructor accepts a package in which to intern the symbol. Example:
   compiler.Register(new RevAppendPrimitive());
 

Parameters:
primitive -
See Also:
LispPrimitive

getLegalBlocks

public Stack getLegalBlocks()

getLegalTags

public Stack getLegalTags()

getRegisteredDos

public Map getRegisteredDos()

isLegalTag

public boolean isLegalTag(LispValue tag)

WarnAboutSpecials

public void WarnAboutSpecials(boolean value)

indexInList

public int indexInList(LispValue e,
                       LispValue l,
                       LispValue[] attribute)
Looks up the symbol in a list

Parameters:
e - a Symbol
l - a list
attribute - The attribute of the found symbol is assigned to attribute[0]. It can be NIL or &rest
Returns:
the index in list of found symbol (it start from 1) or 0 if symbol has not been found in list

indexAndAttribute

public LispValue indexAndAttribute(LispValue e,
                                   LispValue l)
Looks up the symbol in a list of lists. Returns a dotted pair. - first element is the attribute of the found symbol it can be "&rest" or NIL for no attribute (or if symbol has not been found) - second element is the index of the list in which it is found and the index in that list. Both indexes start from 1. The &rest keyword eventually present in the list is not taken into account for index count. index is NIL if not found. Examples:
     indexAndAttribute(b, ((a b c) (d e f))     = (NIL. (1 . 2))
     indexAndAttribute(f, ((a b c) (d e f))     = (NIL.(2 . 3))
     indexAndAttribute(z, ((a b c) (d e f))     = (NIL. NIL)
     indexAndAttribute(l, ((a &rest l) (d e f)) = (&rest .(1 . 2))
 

Parameters:
e - a Symbol
l - a list of lists
Returns:
either (NIL.NIL), if not found, or a dotted pair; first is the attribute for symbol, second is a Cons of 2 LispIntegers\ (a . b) indicating list number (a) and index into that list (b)

compileArgsLeftToRight

public LispValue compileArgsLeftToRight(LispValue args,
                                        LispValue valueList,
                                        LispValue code)
                                 throws CompilerException
Throws:
CompilerException

compileConstantArgsLeftToRight

public LispValue compileConstantArgsLeftToRight(SECDMachine machine,
                                                LispValue args,
                                                LispValue valueList,
                                                LispValue code)

quoteList

public LispValue quoteList(LispValue l)
Returns the input list with quotes added before every top-level expression.


compile

public LispValue compile(SECDMachine machine,
                         LispValue expr,
                         LispValue varValues)
                  throws CompilerException
compile takes a LISP expression, a list of global variables, and optionally an already-generated list of code. It returns compiled code in a list.

Parameters:
expr - expression to compile
varValues - global or local variable list.
Returns:
LispValue - generated code
Throws:
CompilerException
See Also:
LispCompiler

compile

public LispValue compile(LispValue expr,
                         LispValue valueList,
                         LispValue code)
                  throws CompilerException
compile takes a LISP expression, a list of global variables, and optionally an already-generated list of code. It returns compiled code in a list.

Parameters:
expr - expression to compile
valueList - global variable list.
code - [optional]
Returns:
LispValue - generated code
Throws:
CompilerException
See Also:
LispCompiler

compileAtom

LispValue compileAtom(SECDMachine machine,
                      LispValue expr,
                      LispValue valueList,
                      LispValue code)
                throws CompilerException
Throws:
CompilerException

compileList

LispValue compileList(SECDMachine machine,
                      LispValue expr,
                      LispValue valueList,
                      LispValue code)
                throws CompilerException
Throws:
CompilerException

compileSpecialForm

LispValue compileSpecialForm(SECDMachine machine,
                             LispValue function,
                             LispValue args,
                             LispValue valueList,
                             LispValue code)
                       throws CompilerException
Throws:
CompilerException

compileLet

public LispValue compileLet(SECDMachine machine,
                            LispValue vars,
                            LispValue values,
                            LispValue valueList,
                            LispValue body,
                            LispValue code)
                     throws CompilerException
Throws:
CompilerException

compileSpecialBind

LispValue compileSpecialBind(SECDMachine machine,
                             LispValue vars,
                             LispValue values,
                             LispValue valueList,
                             LispValue code)
                       throws CompilerException
Throws:
CompilerException

compileSpecialUnbind

LispValue compileSpecialUnbind(SECDMachine machine,
                               LispValue vars,
                               LispValue code)

varsFromLetBindings

public LispValue varsFromLetBindings(LispValue varValueList)

valuesFromLetBindings

public LispValue valuesFromLetBindings(LispValue varValueList)

specialFormP

public boolean specialFormP(LispValue fn)

compileApp

LispValue compileApp(SECDMachine machine,
                     LispValue args,
                     LispValue valueList,
                     LispValue code)
               throws CompilerException
Throws:
CompilerException

compileAppConstant

LispValue compileAppConstant(SECDMachine machine,
                             LispValue args,
                             LispValue valueList,
                             LispValue code)
                       throws CompilerException
Throws:
CompilerException

compileBuiltin

LispValue compileBuiltin(SECDMachine machine,
                         LispValue fn,
                         LispValue args,
                         LispValue valueList,
                         LispValue code)
                   throws CompilerException
Throws:
CompilerException

compileUserDefinedFunction

LispValue compileUserDefinedFunction(SECDMachine machine,
                                     LispValue fn,
                                     LispValue args,
                                     LispValue valueList,
                                     LispValue code)
                               throws CompilerException
FN is an instance of StandardLispFunction

Parameters:
machine -
fn - an instance of StandardLispFunction
args -
valueList -
code -
Throws:
CompilerException

compileAnd

LispValue compileAnd(SECDMachine machine,
                     LispValue args,
                     LispValue valueList,
                     LispValue code)
               throws CompilerException
Throws:
CompilerException

compileAndAux

LispValue compileAndAux(LispValue dummyVar,
                        LispValue args)

compileOr

LispValue compileOr(SECDMachine machine,
                    LispValue args,
                    LispValue valueList,
                    LispValue code)
              throws CompilerException
Throws:
CompilerException

compileOrAux

LispValue compileOrAux(LispValue dummyVar,
                       LispValue args)

compileDefun

LispValue compileDefun(SECDMachine machine,
                       LispValue name,
                       LispValue argsAndBody,
                       LispValue valueList,
                       LispValue code)
                 throws CompilerException
Throws:
CompilerException

compileDefmacro

LispValue compileDefmacro(SECDMachine machine,
                          LispValue name,
                          LispValue argsAndBody,
                          LispValue valueList,
                          LispValue code)
                    throws CompilerException
Throws:
CompilerException

compileIf

LispValue compileIf(SECDMachine machine,
                    LispValue test,
                    LispValue thenExpr,
                    LispValue elseExpr,
                    LispValue valueList,
                    LispValue code)
              throws CompilerException
Throws:
CompilerException

compileOptimizedIf

LispValue compileOptimizedIf(SECDMachine machine,
                             LispValue test,
                             LispValue thenExpr,
                             LispValue elseExpr,
                             LispValue valueList,
                             LispValue code)
                       throws CompilerException
Throws:
CompilerException

compileProgn

LispValue compileProgn(LispValue body,
                       LispValue valueList,
                       LispValue code)
                 throws CompilerException
Throws:
CompilerException

compileLambda

LispValue compileLambda(SECDMachine machine,
                        LispValue body,
                        LispValue valueList,
                        LispValue code)
                  throws CompilerException
Throws:
CompilerException

isBuiltinFunction

public static boolean isBuiltinFunction(LispValue code)
Send in either code or a symbol with a function value. Returns true only if the first element of the code list is :PRIMITIVE.

Parameters:
code - a LISP list.
Returns:
true if the code indicates a built-in function

isMacroCode

public boolean isMacroCode(LispValue code)
Parameters:
code - a Lisp list
Returns:
true if code is code for a macro (the first element is :MACRO)

Jatha Software