org.omegahat.Environment.Language
Class Function

java.lang.Object
  |
  +--org.omegahat.Environment.Language.Function
All Implemented Interfaces:
Evaluable, java.io.Serializable
Direct Known Subclasses:
Closure, LazyFunction, TailRecursiveFunction

public class Function
extends java.lang.Object
implements Evaluable, java.io.Serializable

This class represents a "function" object in Omega that one can use as a method separate from any class. In this respect, one can think of it as being a static method. This facility extends Java (slightly, by removing the verbiage of creating a class for a single method) and brings the environment more in line with R, S, Xlisp, etc. A Function can be evaluated by supplying named arguments, etc in the form of a local database or hashtable. In this way, arguments can be missing and a parallel database/name space can provide defaults. This gives functionality similar to S and may aid simple translation from S/R to org.omegahat.Environment. A powerful difference between Omega functions and those in the other three languages is that Omega supports optional type-checking. This is also available in Dylan and other languages. In addition to the simple hashtable used to evaluate a call to the function, arguments can be specified as an un-named array of objects or as an unevaluated List. These provide flexible ways to invoke the function.

See Also:
Serialized Form

Field Summary
protected  java.util.Vector argumentNames
           
protected  Evaluable body
          The expessions that constitute the body or actions of this function.
protected  java.lang.String documentation
          Stores the comments associated with the function defined in the S4-like manner.
protected  java.util.Vector exceptions
          Collection of the classes of exceptions that this function can throw.
protected  boolean hasVariableArguments
           
static java.lang.Object MissingArgument
           
protected  java.lang.String name
           
static java.lang.String OptionalArgumentsVariableName
           
static java.lang.String OptionalArgumentVariablePrefix
           
protected  java.lang.Class returnType
          The class of the value returned by this function, either left unset for variable types or specified in the declaration.
protected  Database templateFrame
          A database containing the parameters and their default values, etc.
 
Constructor Summary
Function()
          Trivial constructor to be used when the fields are to be specified later.
Function(ExpressionInt body, List args, Name returnType, List exceptions, java.lang.String name, Evaluator evaluator)
          Specifies the different aspects of the function: the body, parameters, return type, exceptions and the name of the function, and finally the evaluator in which the function is being defined.
Function(MethodDefinition def)
          Unimplemented.
 
Method Summary
 java.lang.String addArgument(AssignExpression assignment)
           
 java.lang.String addArgument(AssignExpression assignment, Evaluator evaluator)
           
 java.lang.String addArgument(MethodParameter param)
           
 java.lang.String addArgument(MethodParameter param, Evaluator evaluator)
           
 java.lang.String addArgument(MethodParameter param, java.lang.Object value)
           
 java.lang.String addArgument(MethodParameter param, java.lang.Object value, Evaluator evaluator)
           
 java.lang.String addArgumentName(java.lang.String name)
           
 Database argList()
           
 Database argList(Database db)
           
 Database argList(List args, Evaluator evaluator)
           
 java.lang.String argumentName(int which)
           
 java.util.Vector argumentNames()
           
 java.util.Vector argumentNames(java.util.Vector v)
           
 java.lang.Object assignArgument(Database db, java.lang.Object arg, java.lang.String name, java.lang.String optionalArgument, Evaluator evaluator)
           
 java.lang.Object assignNamedArgument(Database db, AssignExpression assign, java.lang.String name, java.lang.String optionalArgument, Evaluator evaluator)
           
 Evaluable body()
           
 Evaluable body(Evaluable b)
           
 Evaluable body(StatementList b)
           
 Database callFrame(Evaluator evaluator)
           
 boolean checkReturnType(java.lang.Object value, Evaluator evaluator)
           
protected  Database createCallFrame(Evaluator evaluator)
           
 int createVariable(LocalVariable var, Database db, Evaluator evaluator)
           
 java.lang.Object eval(Database db, boolean merge, Evaluator evaluator)
          Evaluate the function with the arguments provided in the given Database which should have been created previously by matching the arguments to the parameters of this function.
 java.lang.Object eval(Database db, Evaluator evaluator)
           
 java.lang.Object eval(Evaluator evaluator)
           
 java.lang.Object eval(List args, Evaluator evaluator)
          Method for invoking the function with the arguments specified in the args list.
 java.lang.Object eval(java.lang.Object[] args, Evaluator evaluator)
          Evaluate the function using the ordered collection of arguments.
 java.lang.Object eval(java.lang.Object arg, Evaluator evaluator)
          Evaluate the function with the single argument.
 java.lang.Object evalBody(Evaluator evaluator)
           
 java.util.Vector exceptions()
           
 java.util.Vector exceptions(List args, Evaluator evaluator)
           
 java.util.Vector exceptions(java.util.Vector v)
           
protected  Database frame(Evaluator evaluator)
           
 java.lang.String functionName(Evaluator evaluator)
           
 java.lang.String getDocumentation()
          Retrieve the documentation text associated with this function.
 antlr.collections.AST getFirstChild()
           
 boolean hasVariableArguments()
           
 boolean hasVariableArguments(boolean val)
           
 boolean isSynchronized()
          Determines whether the function has been declared as synchronized indicating that it can be evaluated in only one thread at a given time and other threads invoking it should wait for the lock on the function object to be released before entering.
 boolean lazy(Evaluator evaluator)
           
 int matchArgument(Database db, java.lang.Object arg, java.util.Vector v, int currentArg, Evaluator evaluator)
           
 Database matchArguments(Database db, List l, Evaluator evaluator)
          Match the named arguments in the database to the
 Database matchArguments(List l, Evaluator evaluator)
           
 java.lang.String name()
           
 java.lang.String name(java.lang.String n)
           
 java.lang.Class[] parameterTypes()
           
 java.lang.Class returnType()
           
 java.lang.Class returnType(java.lang.Class type)
           
 java.lang.String setDocumentation(Comment com)
           
 java.lang.String setDocumentation(java.lang.String doc)
           
 java.lang.StringBuffer showArg(java.lang.String name, Database db, java.lang.StringBuffer buf)
           
 java.lang.String showArguments()
           
 java.lang.String showArguments(java.lang.StringBuffer buf)
           
 Signature signature()
           
 java.lang.Object templateArgument(Evaluable arg, java.lang.String name, Evaluator evaluator)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MissingArgument

public static final java.lang.Object MissingArgument

OptionalArgumentsVariableName

public static final java.lang.String OptionalArgumentsVariableName

OptionalArgumentVariablePrefix

public static final java.lang.String OptionalArgumentVariablePrefix

body

protected Evaluable body
The expessions that constitute the body or actions of this function.

exceptions

protected java.util.Vector exceptions
Collection of the classes of exceptions that this function can throw.

returnType

protected java.lang.Class returnType
The class of the value returned by this function, either left unset for variable types or specified in the declaration.

templateFrame

protected Database templateFrame
A database containing the parameters and their default values, etc. which is copied to create an actual call frame when a function is evaluated with particular arguments.

argumentNames

protected java.util.Vector argumentNames

name

protected java.lang.String name

hasVariableArguments

protected boolean hasVariableArguments

documentation

protected java.lang.String documentation
Stores the comments associated with the function defined in the S4-like manner. Note that the comment expressions will still be part of the function object within the body.
Constructor Detail

Function

public Function()
Trivial constructor to be used when the fields are to be specified later.

Function

public Function(MethodDefinition def)
Unimplemented.

Function

public Function(ExpressionInt body,
                List args,
                Name returnType,
                List exceptions,
                java.lang.String name,
                Evaluator evaluator)
         throws java.lang.ClassNotFoundException
Specifies the different aspects of the function: the body, parameters, return type, exceptions and the name of the function, and finally the evaluator in which the function is being defined.
Method Detail

eval

public java.lang.Object eval(List args,
                             Evaluator evaluator)
                      throws java.lang.Throwable
Method for invoking the function with the arguments specified in the args list.

eval

public java.lang.Object eval(Database db,
                             boolean merge,
                             Evaluator evaluator)
                      throws java.lang.Throwable
Evaluate the function with the arguments provided in the given Database which should have been created previously by matching the arguments to the parameters of this function.
See Also:
#matchArguments()

eval

public java.lang.Object eval(Database db,
                             Evaluator evaluator)
                      throws java.lang.Throwable

eval

public java.lang.Object eval(Evaluator evaluator)
                      throws java.lang.Throwable
Specified by:
eval in interface Evaluable

eval

public java.lang.Object eval(java.lang.Object arg,
                             Evaluator evaluator)
                      throws java.lang.Throwable
Evaluate the function with the single argument.

eval

public java.lang.Object eval(java.lang.Object[] args,
                             Evaluator evaluator)
                      throws java.lang.Throwable
Evaluate the function using the ordered collection of arguments.
Specified by:
eval in interface Evaluable

evalBody

public java.lang.Object evalBody(Evaluator evaluator)
                          throws java.lang.Throwable

matchArguments

public Database matchArguments(Database db,
                               List l,
                               Evaluator evaluator)
                        throws java.lang.Throwable
Match the named arguments in the database to the

matchArguments

public Database matchArguments(List l,
                               Evaluator evaluator)
                        throws java.lang.Throwable

matchArgument

public int matchArgument(Database db,
                         java.lang.Object arg,
                         java.util.Vector v,
                         int currentArg,
                         Evaluator evaluator)
                  throws java.lang.Throwable

callFrame

public Database callFrame(Evaluator evaluator)
                   throws java.lang.Throwable

createVariable

public int createVariable(LocalVariable var,
                          Database db,
                          Evaluator evaluator)
                   throws java.lang.Throwable

frame

protected Database frame(Evaluator evaluator)
                  throws java.lang.Throwable

argumentNames

public java.util.Vector argumentNames()

argumentNames

public java.util.Vector argumentNames(java.util.Vector v)

body

public Evaluable body()

body

public Evaluable body(StatementList b)

body

public Evaluable body(Evaluable b)

returnType

public java.lang.Class returnType()

returnType

public java.lang.Class returnType(java.lang.Class type)

name

public java.lang.String name()

name

public java.lang.String name(java.lang.String n)

functionName

public java.lang.String functionName(Evaluator evaluator)

createCallFrame

protected Database createCallFrame(Evaluator evaluator)

argList

public Database argList()

argList

public Database argList(Database db)

argList

public Database argList(List args,
                        Evaluator evaluator)
                 throws java.lang.ClassNotFoundException

parameterTypes

public java.lang.Class[] parameterTypes()

addArgument

public java.lang.String addArgument(MethodParameter param)
                             throws java.lang.ClassNotFoundException

addArgument

public java.lang.String addArgument(MethodParameter param,
                                    Evaluator evaluator)
                             throws java.lang.ClassNotFoundException

addArgument

public java.lang.String addArgument(MethodParameter param,
                                    java.lang.Object value)
                             throws java.lang.ClassNotFoundException

addArgument

public java.lang.String addArgument(MethodParameter param,
                                    java.lang.Object value,
                                    Evaluator evaluator)
                             throws java.lang.ClassNotFoundException

addArgument

public java.lang.String addArgument(AssignExpression assignment,
                                    Evaluator evaluator)
                             throws java.lang.ClassNotFoundException

addArgument

public java.lang.String addArgument(AssignExpression assignment)
                             throws java.lang.ClassNotFoundException

addArgumentName

public java.lang.String addArgumentName(java.lang.String name)

argumentName

public java.lang.String argumentName(int which)

exceptions

public java.util.Vector exceptions(List args,
                                   Evaluator evaluator)
                            throws java.lang.ClassNotFoundException

exceptions

public java.util.Vector exceptions()

exceptions

public java.util.Vector exceptions(java.util.Vector v)

signature

public Signature signature()

checkReturnType

public boolean checkReturnType(java.lang.Object value,
                               Evaluator evaluator)

assignNamedArgument

public java.lang.Object assignNamedArgument(Database db,
                                            AssignExpression assign,
                                            java.lang.String name,
                                            java.lang.String optionalArgument,
                                            Evaluator evaluator)
                                     throws java.lang.Throwable

assignArgument

public java.lang.Object assignArgument(Database db,
                                       java.lang.Object arg,
                                       java.lang.String name,
                                       java.lang.String optionalArgument,
                                       Evaluator evaluator)
                                throws java.lang.Throwable

templateArgument

public java.lang.Object templateArgument(Evaluable arg,
                                         java.lang.String name,
                                         Evaluator evaluator)
                                  throws java.lang.Throwable

hasVariableArguments

public boolean hasVariableArguments()

hasVariableArguments

public boolean hasVariableArguments(boolean val)

lazy

public boolean lazy(Evaluator evaluator)

toString

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

showArguments

public java.lang.String showArguments()

showArguments

public java.lang.String showArguments(java.lang.StringBuffer buf)

showArg

public java.lang.StringBuffer showArg(java.lang.String name,
                                      Database db,
                                      java.lang.StringBuffer buf)

getDocumentation

public java.lang.String getDocumentation()
Retrieve the documentation text associated with this function.

setDocumentation

public java.lang.String setDocumentation(Comment com)

setDocumentation

public java.lang.String setDocumentation(java.lang.String doc)

getFirstChild

public antlr.collections.AST getFirstChild()

isSynchronized

public boolean isSynchronized()
Determines whether the function has been declared as synchronized indicating that it can be evaluated in only one thread at a given time and other threads invoking it should wait for the lock on the function object to be released before entering.