unsw.cse.framescript
Class Subr

java.lang.Object
  extended by unsw.cse.framescript.Term
      extended by unsw.cse.framescript.Subr
Direct Known Subclasses:
Expr

public abstract class Subr
extends Term

Abstract class that provides the common code for all subroutines and FrameScript written functions.

Author:
mmcgill

Field Summary
protected  Atom functor
          The name of the function.
 
Constructor Summary
Subr(java.lang.String str)
          Creates a subroutine for any any number of arguments.
Subr(java.lang.String str, int nArgs)
          Creates a subroutine for a specific number of arguments.
 
Method Summary
abstract  Term apply(Instance currentObject, Term[] args, StackFrame frame)
          This is the method that needs to be implemented for each subroutine.
protected  Alternatives check_alternatives(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check alternatives arguments for built-in functions.
protected  Atom check_atom(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check atom arguments for built-in functions.
protected  Atom check_boolean(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check boolean arguments for built-in functions.
protected  Compound check_compound(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check compound arguments for built-in functions.
protected  Domain check_domain(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check domain arguments for built-in functions.
protected  ExprList check_exprlist(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check expression list arguments for built-in functions.
protected  Frame check_frame(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check frame arguments for built-in functions.
protected  Generic check_generic(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check generic frame arguments for built-in functions.
protected  Instance check_instance(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check instance frame arguments for built-in functions.
protected  FSInteger check_integer(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check integer arguments for built-in functions.
protected  FSList check_list(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check list arguments for built-in functions.
protected  FSNumber check_number(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check numerical arguments for built-in functions.
protected  Pattern check_pattern(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check pattern arguments for built-in functions.
protected  RDR check_rdr(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check RDR arguments for built-in functions.
protected  Script check_script(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check script arguments for built-in functions.
protected  Sequence check_sequence(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check sequence arguments for built-in functions.
protected  FSString check_string(Instance currentObject, Term[] args, int n, StackFrame frame)
          Convenience routine to check string arguments for built-in functions.
 boolean equals(Term t)
          Compares two Terms for equality.
 Term eval(Instance currentObject, StackFrame stackframe)
          Evaluates a Term.
protected  Domain getCurrentDomain(StackFrame frame)
          Method for subroutines to get the current domain.
 Atom getSlotName(Term arg, StackFrame frame)
          Helper function for getting the hard coded name of a slot from an argument.
 FSException hardCodeSlotNames(Term found, StackFrame frame)
          Throws an error with standard message if a function expects a slot name to be hard coded in FrameScript but it isn't.
 FSException noEnclosingInstance(StackFrame frame)
          Throws an error with standard message if a function expects an enclosing instance frame
static FSException notDefined(Atom functor, StackFrame frame)
          Method for throwing errors with a standard error message if a function has not been defined.
 FSException notForNArgs(int nArgs, StackFrame frame)
          Method for throwing an error with standard error message if a subroutine is not defined to handle a given number of arguments.
 FSException notImplemented(StackFrame frame)
          Throws an error if a subroutine is defined but not actually implemented.
 java.lang.String toString()
          Turns the Term into a string.
 
Methods inherited from class unsw.cse.framescript.Term
assign, asString, fullMatch, indents, mapVars, match
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

functor

protected Atom functor
The name of the function.

Constructor Detail

Subr

public Subr(java.lang.String str)
Creates a subroutine for any any number of arguments.

Parameters:
str - the name of the function

Subr

public Subr(java.lang.String str,
            int nArgs)
     throws FSException
Creates a subroutine for a specific number of arguments.

Parameters:
str - the name of the function
nArgs - the number of arguments for the function
Throws:
FSException - if the function is already defined for any number of arguments
Method Detail

apply

public abstract Term apply(Instance currentObject,
                           Term[] args,
                           StackFrame frame)
                    throws FSException
This is the method that needs to be implemented for each subroutine.

Parameters:
currentObject - the enclosing instance frame
args - the arguments to the subroutine
frame - the StackFrame with the variable values
Returns:
the result of running the subroutine
Throws:
FSException - if there are problems running the subroutine

notDefined

public static FSException notDefined(Atom functor,
                                     StackFrame frame)
                              throws FSException
Method for throwing errors with a standard error message if a function has not been defined.

Parameters:
functor - the name of the function
frame - the current StackFrame
Returns:
nothing, always throws
Throws:
FSException - a standardised error

notForNArgs

public FSException notForNArgs(int nArgs,
                               StackFrame frame)
                        throws FSException
Method for throwing an error with standard error message if a subroutine is not defined to handle a given number of arguments.

Parameters:
nArgs - the number of arguments not expected
frame - the current StackFrame
Returns:
nothing, always throws
Throws:
FSException - a standardised error

notImplemented

public FSException notImplemented(StackFrame frame)
                           throws FSException
Throws an error if a subroutine is defined but not actually implemented.

Parameters:
frame - the current StackFrame
Returns:
nothing, always throws
Throws:
FSException - a standardised error

getSlotName

public Atom getSlotName(Term arg,
                        StackFrame frame)
                 throws FSException
Helper function for getting the hard coded name of a slot from an argument.

Parameters:
arg - the argument from which to get the slot name
frame - the current state of the stack
Returns:
the name of the slot
Throws:
FSException - if the argument is not a hard coded name

hardCodeSlotNames

public FSException hardCodeSlotNames(Term found,
                                     StackFrame frame)
                              throws FSException
Throws an error with standard message if a function expects a slot name to be hard coded in FrameScript but it isn't.

Parameters:
found - the found term
frame - the current StackFrame
Returns:
nothing, always throws
Throws:
FSException - a standardised error

noEnclosingInstance

public FSException noEnclosingInstance(StackFrame frame)
                                throws FSException
Throws an error with standard message if a function expects an enclosing instance frame

Parameters:
frame - the current StackFrame
Returns:
nothing, always throws
Throws:
FSException - a standardised error

eval

public Term eval(Instance currentObject,
                 StackFrame stackframe)
          throws FSException
Description copied from class: Term
Evaluates a Term. For many Terms the result is to return itself.

Specified by:
eval in class Term
Parameters:
currentObject - the enclosing instance frame for the code being evaluated
stackframe - StackFrame that holds the values for any variables
Returns:
the result of the evaluation
Throws:
FSException - if any errors occur in performing the evaluation

equals

public boolean equals(Term t)
Description copied from class: Term
Compares two Terms for equality.

Specified by:
equals in class Term
Parameters:
t - Term to be compared to
Returns:
true if the Terms are equal, false otherwise

toString

public java.lang.String toString()
Description copied from class: Term
Turns the Term into a string.

Specified by:
toString in class Term

check_alternatives

protected Alternatives check_alternatives(Instance currentObject,
                                          Term[] args,
                                          int n,
                                          StackFrame frame)
                                   throws FSException
Convenience routine to check alternatives arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the alternatives argument
Throws:
FRSerror - if the alternatives isn't an atom
FSException

check_atom

protected Atom check_atom(Instance currentObject,
                          Term[] args,
                          int n,
                          StackFrame frame)
                   throws FSException
Convenience routine to check atom arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the atom argument
Throws:
FRSerror - if the argument isn't an atom
FSException

check_boolean

protected Atom check_boolean(Instance currentObject,
                             Term[] args,
                             int n,
                             StackFrame frame)
                      throws FSException
Convenience routine to check boolean arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the boolean argument
Throws:
FRSerror - if the argument isn't a boolean
FSException

check_compound

protected Compound check_compound(Instance currentObject,
                                  Term[] args,
                                  int n,
                                  StackFrame frame)
                           throws FSException
Convenience routine to check compound arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the compound argument
Throws:
FRSerror - if the argument isn't a compound
FSException

check_domain

protected Domain check_domain(Instance currentObject,
                              Term[] args,
                              int n,
                              StackFrame frame)
                       throws FSException
Convenience routine to check domain arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the domain argument
Throws:
FRSerror - if the argument isn't a domain
FSException

check_exprlist

protected ExprList check_exprlist(Instance currentObject,
                                  Term[] args,
                                  int n,
                                  StackFrame frame)
                           throws FSException
Convenience routine to check expression list arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the expression list argument
Throws:
FRSerror - if the argument isn't a FrameScript expression list
FSException

check_frame

protected Frame check_frame(Instance currentObject,
                            Term[] args,
                            int n,
                            StackFrame frame)
                     throws FSException
Convenience routine to check frame arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the frame argument
Throws:
FRSerror - if the argument isn't a frame
FSException

check_generic

protected Generic check_generic(Instance currentObject,
                                Term[] args,
                                int n,
                                StackFrame frame)
                         throws FSException
Convenience routine to check generic frame arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the generic frame argument
Throws:
FRSerror - if the argument isn't a generic frame
FSException

check_instance

protected Instance check_instance(Instance currentObject,
                                  Term[] args,
                                  int n,
                                  StackFrame frame)
                           throws FSException
Convenience routine to check instance frame arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the instance frame argument
Throws:
FRSerror - if the argument isn't an instance frame
FSException

check_integer

protected FSInteger check_integer(Instance currentObject,
                                  Term[] args,
                                  int n,
                                  StackFrame frame)
                           throws FSException
Convenience routine to check integer arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the integer argument
Throws:
FRSerror - if the argument isn't an integer
FSException

check_list

protected FSList check_list(Instance currentObject,
                            Term[] args,
                            int n,
                            StackFrame frame)
                     throws FSException
Convenience routine to check list arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the list argument
Throws:
FRSerror - if the argument isn't a list
FSException

check_number

protected FSNumber check_number(Instance currentObject,
                                Term[] args,
                                int n,
                                StackFrame frame)
                         throws FSException
Convenience routine to check numerical arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the numerical argument
Throws:
FRSerror - if the argument isn't a number
FSException

check_pattern

protected Pattern check_pattern(Instance currentObject,
                                Term[] args,
                                int n,
                                StackFrame frame)
                         throws FSException
Convenience routine to check pattern arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the pattern argument
Throws:
FRSerror - if the argument isn't a pattern
FSException

check_rdr

protected RDR check_rdr(Instance currentObject,
                        Term[] args,
                        int n,
                        StackFrame frame)
                 throws FSException
Convenience routine to check RDR arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the RDR argument
Throws:
FRSerror - if the argument isn't an RDR
FSException

check_script

protected Script check_script(Instance currentObject,
                              Term[] args,
                              int n,
                              StackFrame frame)
                       throws FSException
Convenience routine to check script arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the script argument
Throws:
FRSerror - if the argument isn't a script
FSException

check_sequence

protected Sequence check_sequence(Instance currentObject,
                                  Term[] args,
                                  int n,
                                  StackFrame frame)
                           throws FSException
Convenience routine to check sequence arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the sequence argument
Throws:
FRSerror - if the argument isn't a sequence
FSException

check_string

protected FSString check_string(Instance currentObject,
                                Term[] args,
                                int n,
                                StackFrame frame)
                         throws FSException
Convenience routine to check string arguments for built-in functions.

Parameters:
currentObject - the current enclosing instance frame
arg - the arguments to the function
n - the index of the argument being checked
frame - a StackFrame containing contextual information
Returns:
the string argument
Throws:
FRSerror - if the argument isn't a string
FSException

getCurrentDomain

protected Domain getCurrentDomain(StackFrame frame)
                           throws FSException
Method for subroutines to get the current domain.

Parameters:
frame - a StackFrame containing contextual information
Returns:
the current domain
Throws:
FSException - if the input pattern is corrupted or there is no domain