Jatha Software

org.jatha.compile
Class LispPrimitive

java.lang.Object
  extended by org.jatha.dynatype.StandardLispValue
      extended by org.jatha.compile.LispPrimitive
All Implemented Interfaces:
Comparable, LispValue
Direct Known Subclasses:
AbsoluteValuePrimitive, AddOnePrimitive, AddPrimitive, AppendPrimitive, ApplyPrimitive, AproposPrimitive, ArcCosinePrimitive, ArcSinePrimitive, ArcTangent2Primitive, ArcTangentPrimitive, AssocPrimitive, AtomPrimitive, BackquotePrimitive, BlockPrimitive, BoundpPrimitive, ButlastPrimitive, CarPrimitive, CdrPrimitive, CeilingPrimitive, CharacterpPrimitive, ClrhashPrimitive, ConcatenatePrimitive, ConspPrimitive, ConsPrimitive, ConstantpPrimitive, CopyListPrimitive, CosecantPrimitive, CosinePrimitive, CotangentPrimitive, DefconstantPrimitive, DefparameterPrimitive, DefvarPrimitive, DegreesToRadiansPrimitive, DividePrimitive, DocumentationPrimitive, EighthPrimitive, EltPrimitive, EqlPrimitive, EqPrimitive, EqualNumericPrimitive, EvalPrimitive, ExitPrimitive, ExportPrimitive, FactorialPrimitive, FboundpPrimitive, FifthPrimitive, FindPackagePrimitive, FirstPrimitive, FloatpPrimitive, FloorPrimitive, FourthPrimitive, FreePrimitive, FuncallPrimitive, FunctionPrimitive, GcFullPrimitive, GcPrimitive, GethashPrimitive, GoPrimitive, GreaterThanOrEqualPrimitive, GreaterThanPrimitive, GrindefPrimitive, HashtableCountPrimitive, HashtablepPrimitive, HashtableRehashSizePrimitive, HashtableRehashThresholdPrimitive, HashtableSizePrimitive, HashtableTestPrimitive, ImportPrimitive, IntegerpPrimitive, InternPrimitive, KeywordpPrimitive, LastPrimitive, LengthPrimitive, LessThanOrEqualPrimitive, LessThanPrimitive, ListAllPackagesPrimitive, ListpPrimitive, ListPrimitive, ListStarPrimitive, LoadFromJarPrimitive, LoadPrimitive, Macroexpand1Primitive, MacroexpandPrimitive, MakeHashTablePrimitive, MakepackagePrimitive, MaxPrimitive, MemberPrimitive, MinPrimitive, MultiplyPrimitive, NconcPrimitive, NinthPrimitive, NotPrimitive, NreversePrimitive, NStringCapitalizePrimitive, NStringDowncasePrimitive, NStringUpcasePrimitive, NullPrimitive, NumberpPrimitive, PackageNamePrimitive, PackageNicknamesPrimitive, PackageUseListPrimitive, PopPrimitive, PositionPrimitive, Prin1Primitive, PrincPrimitive, PrintPrimitive, PushPrimitive, QuotePrimitive, RadiansToDegreesPrimitive, RassocPrimitive, ReadFromStringPrimitive, ReciprocalPrimitive, RemhashPrimitive, RemovePrimitive, RestPrimitive, ReturnFromPrimitive, ReversePrimitive, RplacaPrimitive, RplacdPrimitive, SecantPrimitive, SECDop, SecondPrimitive, SetfDocumentationPrimitive, SetfGethashPrimitive, SetfSymbolFunctionPrimitive, SetfSymbolPlistPrimitive, SetfSymbolValuePrimitive, SetPrimitive, SetqPrimitive, SeventhPrimitive, ShadowingImportPrimitive, ShadowPrimitive, SinePrimitive, SixthPrimitive, SquareRootPrimitive, StringCapitalizePrimitive, StringDowncasePrimitive, StringEndsWithPrimitive, StringEqPrimitive, StringEqualPrimitive, StringGreaterpPrimitive, StringGreaterThanOrEqualPrimitive, StringGreaterThanPrimitive, StringLeftTrimPrimitive, StringLesspPrimitive, StringLessThanOrEqualPrimitive, StringLessThanPrimitive, StringNeqPrimitive, StringNotGreaterpPrimitive, StringNotLesspPrimitive, StringpPrimitive, StringPrimitive, StringRightTrimPrimitive, StringStartsWithPrimitive, StringTrimPrimitive, StringUpcasePrimitive, SubstPrimitive, SubtractOnePrimitive, SubtractPrimitive, SymbolFunctionPrimitive, SymbolNamePrimitive, SymbolPackagePrimitive, SymbolPlistPrimitive, SymbolpPrimitive, SymbolValuePrimitive, TagbodyPrimitive, TangentPrimitive, TenthPrimitive, ThirdPrimitive, TimePrimitive, TracePrimitive, TypeOfPrimitive, UsePackagePrimitive, ZeropPrimitive

public abstract class LispPrimitive
extends StandardLispValue

The LispPrimitive class makes the transition from LISP code to Java code. There is a LispPrimitive for each builtin LISP function. 1) Create the new LISP primitive as an instance of this class. It must have several methods as described below. 2) Register the new primitive with the compiler. Each primitive must implement one method: public void Execute(SECDMachine machine)

See Also:
LispCompiler

Field Summary
protected  String functionName
          the functionName is part of the string that gets printed when the instruction appears in a printed list.
protected  LispValue functionNameSymbol
           
 boolean inlineP
          Set inlineP to true if the function effectively evaluates itself simply by compiling its argument list.
protected  long maxNumberOfArgs
           
protected  long minNumberOfArgs
           
 
Fields inherited from class org.jatha.dynatype.StandardLispValue
f_lisp
 
Constructor Summary
LispPrimitive(Jatha lisp, String fnName)
           
LispPrimitive(Jatha lisp, String fnName, long minArgs)
           
LispPrimitive(Jatha lisp, String fnName, long minArgs, long maxArgs)
          The constructor for the LispPrimitive class.
 
Method Summary
 boolean basic_functionp()
          Returns Java true if the object is a function.
(package private)  LispValue BuiltinFunctionCode(LispValue fn)
           
 LispValue CompileArgs(LispCompiler compiler, SECDMachine machine, LispValue args, LispValue valueList, LispValue code)
          The CompileArgs method turns the arguments of the function call into SECD abstract machine code.
 LispValue CompileArgs(LispCompiler compiler, SECDMachine machine, LispValue function, LispValue args, LispValue valueList, LispValue code)
           
abstract  void Execute(SECDMachine machine)
          Execute performs the operation using the abstract machine registers.
 LispValue grindef(LispValue code, int indentAmount)
           
 void indent(int amount)
           
 void internal_prin1(PrintStream os)
           
 void internal_princ(PrintStream os)
           
 void internal_print(PrintStream os)
           
 String LispFunctionNameString()
           
 LispValue LispFunctionNameSymbol()
           
 String parameterCountString()
          This method returns a Java string denoting the length of the expected argument list in some readable form.
 void printCode(LispValue code)
          printCode prints a list of compiled code in a nice manner.
 void printCode(LispValue code, int indentAmount)
           
 String toString()
          The output of this function is printed when the instruction needs to be printed.
(package private)  boolean validArgumentLength(LispValue numberOfArguments)
          This method returns true if the list of arguments satisfies the length restrictions posed by the function, and false otherwise.
 boolean validArgumentList(LispValue args)
          This method returns true if the list of arguments satisfies the length and format restrictions posed by the function, and false otherwise.
 
Methods inherited from class org.jatha.dynatype.StandardLispValue
abs, acos, add, adjustSpecialCount, append, apply, apropos_print, asin, assoc, atan, atan2, atom, basic_atom, basic_bignump, basic_consp, basic_constantp, basic_floatp, basic_foreignp, basic_integerp, basic_keywordp, basic_length, basic_listp, basic_macrop, basic_null, basic_numberp, basic_stringp, basic_symbolp, bignump, boundp, butlast, car, cdr, ceiling, characterp, clrhash, compareTo, concatenate, consp, constantp, contains, copy_list, copy, cos, cot, csc, degreesToRadians, divide, documentation, eighth, elt, elt, eq, eql, equal, equalNumeric, factorial, fboundp, fifth, first, floatp, floor, fourth, funcall, functionp, get_specialCount, gethash, gethash, getLisp, greaterThan, greaterThanOrEqual, hash_table_count, hash_table_rehash_size, hash_table_rehash_threshold, hash_table_size, hash_table_test, hashtablep, integerp, internal_getName, internal_prin1_as_cdr, internal_princ_as_cdr, internal_print_as_cdr, iterator, keywordp, last, length, lessThan, lessThanOrEqual, lisp_null, list, listp, max, member, min, multiply, nconc, negate, neql, ninth, nreverse, nstringCapitalize, nstringDowncase, nstringUpcase, numberp, pop, position, prin1, princ, print, push, radiansToDegrees, rassoc, readFromString, reciprocal, remhash, remove, rest, reverse, rplaca, rplacd, sec, second, set_special, setf_documentation, setf_gethash, setf_symbol_function, setf_symbol_plist, setf_symbol_value, setLisp, setPackage, setq, seventh, showStackTrace, sin, sixth, specialP, sqrt, string, stringCapitalize, stringDowncase, stringEndsWith, stringEq, stringEqual, stringGreaterP, stringGreaterThan, stringGreaterThanOrEqual, stringLeftTrim, stringLeftTrim, stringLessP, stringLessThan, stringLessThanOrEqual, stringNeq, stringNotGreaterP, stringNotLessP, stringp, stringRightTrim, stringRightTrim, stringStartsWith, stringTrim, stringTrim, stringUpcase, subst, substring, substring, subtract, symbol_function, symbol_name, symbol_package, symbol_plist, symbol_value, symbolp, tan, tenth, third, toCollection, toJava, toJava, toString, toStringAsCdr_internal, toStringAsCdr, toStringShort, toStringShort, toStringSimple, type_of, typep, uses, zerop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

minNumberOfArgs

protected long minNumberOfArgs

maxNumberOfArgs

protected long maxNumberOfArgs

inlineP

public boolean inlineP
Set inlineP to true if the function effectively evaluates itself simply by compiling its argument list. This is true for functions like LIST, LIST*, and QUOTE. This inhibits putting the function call on the stack, thus saving a millisecond of time.

See Also:
LispPrimitive

functionName

protected String functionName
the functionName is part of the string that gets printed when the instruction appears in a printed list.


functionNameSymbol

protected LispValue functionNameSymbol
Constructor Detail

LispPrimitive

public LispPrimitive(Jatha lisp,
                     String fnName,
                     long minArgs,
                     long maxArgs)
The constructor for the LispPrimitive class.

Parameters:
fnName - The LISP function name being implemented.
minArgs - The minimum number of Arguments to this function.
maxArgs - The maximum number of Arguments to this function.
See Also:
LispCompiler

LispPrimitive

public LispPrimitive(Jatha lisp,
                     String fnName,
                     long minArgs)

LispPrimitive

public LispPrimitive(Jatha lisp,
                     String fnName)
Method Detail

toString

public String toString()
The output of this function is printed when the instruction needs to be printed.

Specified by:
toString in interface LispValue
Overrides:
toString in class StandardLispValue
Returns:
String The value in a string.

basic_functionp

public boolean basic_functionp()
Description copied from interface: LispValue
Returns Java true if the object is a function.

Specified by:
basic_functionp in interface LispValue
Overrides:
basic_functionp in class StandardLispValue

printCode

public void printCode(LispValue code)
printCode prints a list of compiled code in a nice manner. Calls the 'grindef' function on each primitive. Grindef is an historical LISP function not found in Common LISP. Example:
   printCode(compiled-function, 2);
 

Parameters:
code - the code to be printed, with indent 2.
See Also:
LispPrimitive

printCode

public void printCode(LispValue code,
                      int indentAmount)

grindef

public LispValue grindef(LispValue code,
                         int indentAmount)

indent

public void indent(int amount)

LispFunctionNameString

public String LispFunctionNameString()

LispFunctionNameSymbol

public LispValue LispFunctionNameSymbol()

internal_princ

public void internal_princ(PrintStream os)
Specified by:
internal_princ in interface LispValue
Overrides:
internal_princ in class StandardLispValue

internal_prin1

public void internal_prin1(PrintStream os)
Specified by:
internal_prin1 in interface LispValue
Overrides:
internal_prin1 in class StandardLispValue

internal_print

public void internal_print(PrintStream os)
Specified by:
internal_print in interface LispValue
Overrides:
internal_print in class StandardLispValue

validArgumentLength

boolean validArgumentLength(LispValue numberOfArguments)
This method returns true if the list of arguments satisfies the length restrictions posed by the function, and false otherwise.

Parameters:
numberOfArguments - usually the result of args.length()
Returns:
boolean
See Also:
LispPrimitive

validArgumentList

public boolean validArgumentList(LispValue args)
This method returns true if the list of arguments satisfies the length and format restrictions posed by the function, and false otherwise. It calls validArgumentLength, so the programmer doesn't need to call it.

This method is called by the compiler.

Parameters:
args - the list of arguments.
Returns:
boolean
See Also:
LispPrimitive, LispCompiler

parameterCountString

public String parameterCountString()
This method returns a Java string denoting the length of the expected argument list in some readable form.

This method is called by the compiler when an argument count exception is generated.

Returns:
a Java string denoting the length of the expected argument list.
See Also:
LispPrimitive, LispCompiler

Execute

public abstract void Execute(SECDMachine machine)
                      throws CompilerException
Execute performs the operation using the abstract machine registers. Arguments are found on the S register stack, in reverse order. UNLIMITED argument lists are collected into a list which is the top element on the stack. The implementation should pop an appropriate number of arguments from the stack, perform a computation, then push a result back on the S stack. The instruction should then be popped from the C (code) register. A LispValueFactory objects is available in the static variable LispValueFactory. Example implementations:
   FIRST
   class FirstPrimitive extends LispPrimitive
   {
     public First()
     {
       super("FIRST", 1);   // 1 argument
     }

     public void Execute(SECDMachine machine)
     {
       LispValue arg = machine.S.pop();

       machine.S.push(my_first(arg));
       machine.C.pop();
     }
   }
 
A multi-argument function must pop the arguments in reverse order.
     public void Execute(SECDMachine machine)
     {
       LispValue arg2 = machine.S.pop();
       LispValue arg1 = machine.S.pop();

       machine.S.push(my_new_function(arg1, arg2));
       machine.C.pop();
     }
   }
 
To register the new primitive, call:
    Jatha.COMPILER.Register(new FirstPrimitive());
 

Parameters:
machine - The abstract machine instance.
Throws:
CompilerException
See Also:
LispCompiler

BuiltinFunctionCode

LispValue BuiltinFunctionCode(LispValue fn)

CompileArgs

public LispValue CompileArgs(LispCompiler compiler,
                             SECDMachine machine,
                             LispValue args,
                             LispValue valueList,
                             LispValue code)
                      throws CompilerException
The CompileArgs method turns the arguments of the function call into SECD abstract machine code. Most functions won't need to override the default code generation, but ones that do funny things with argument lists will need to.

Parameters:
compiler -
args -
valueList -
code -
Returns:
LispValue The code generated and cons'ed onto the front of the incoming code.
Throws:
CompilerException
See Also:
LispCompiler

CompileArgs

public LispValue CompileArgs(LispCompiler compiler,
                             SECDMachine machine,
                             LispValue function,
                             LispValue args,
                             LispValue valueList,
                             LispValue code)
                      throws CompilerException
Throws:
CompilerException

Jatha Software