net.scheinerman.phoenix.interpreter.functions
Class Function

java.lang.Object
  extended by net.scheinerman.phoenix.interpreter.Interpreter
      extended by net.scheinerman.phoenix.interpreter.functions.Function
Direct Known Subclasses:
BuiltInFunction, NativeFunction

public class Function
extends Interpreter

A Function is an Interpreter that takes can be run multiple times with different arguments. A function also has a name, a return value, and a specified order of arguments. The arguments must be provided in order otherwise an exception is thrown. A Function can take many forms. Functions are declared by placing the keyword function, followed by the return type (void, num, or str), followed by the left-hand arguments, then the name followed by the right-hand arguments then a colon (":"). Functions can also be started with the global modifier (which globalizes them) if desired. The left and right hand arguments are both optional, so each of the following function declarations are valid:

function num (num x) factorial:
function num digits (num x):
function num (num n) nCr (num r):
function void (str s) printStr (num start, num end):
function void tic:

These functions can then be called thus:

(8) factorial
digits (34256)
(6) nCr (3)
("Hello there!") printStr (0,5)
tic

Note: Any whitespace that is not right after a modifier (e.g. num or function) is ignored.

Since:
1.5
Version:
1.0
Author:
Jonah Scheinerman
See Also:
Interpreter}, {@link NativeFunction}

Field Summary
protected  java.lang.String[] af_names
          An array containing the names of the right-hand arguments to the function.
protected  Variable[] af_types
          An array containing variables of the same types as the right-hand arguments to the function.
protected  java.util.TreeMap<java.lang.String,java.lang.String> argDescriptions
           
protected  java.lang.String[] b4_names
          An array containing the names of the left-hand arguments to the function.
protected  Variable[] b4_types
          An array containing variables of the same types as the left-hand arguments to the function.
protected  java.lang.String description
           
protected  java.lang.String modifiers
           
protected  java.lang.String name
          The name of the function.
protected  java.lang.String returnDescription
           
 
Fields inherited from class net.scheinerman.phoenix.interpreter.Interpreter
_break, _continue, _return, breakValue, code, fat, file, keywordList, line, line_diff, printValues, retType, retTypeSet, retValue, run_code, stop, vat
 
Constructor Summary
Function()
          The default constructor for a Function.
Warning: This constructor bypasses the standard initialization procedure and should not be used.
Function(java.lang.String name, java.lang.String file, java.lang.String code, int line, Variable[] b4_types, java.lang.String[] b4_names, Variable[] af_types, java.lang.String[] af_names, Variable retType, boolean setupDocs)
          Constructs a Function with the given variables.
 
Method Summary
protected  void addArgs(Variable[] b4_args, Variable[] af_args)
          Adds the passed variables to the local VAT and FAT of the function.
protected  boolean check(Variable[] b4_args, Variable[] af_args)
          Checks to see whether the variables that were passed to this function are actually of the type that were specified in the function definition.
protected  void dealWithReturn(Variable passed)
          Deals with a return statement by setting the retValue field, checking whether it's the right type and stopping the function.
 java.lang.String[] getAfterNames()
           
 Variable[] getAfterTypes()
          Returns the default variables that were defined as right-hand parameters of the function.
 java.lang.String[] getBeforeNames()
           
 Variable[] getBeforeTypes()
          Returns the default variables that were defined as left-hand parameters of the function.
 java.lang.String getDefinition()
           
 java.lang.String getName()
          Returns the name of this function.
 void printHelp()
           
 void run(Variable[] b4_args, Variable[] af_args)
          Runs this function with the given left-hand and right-hand arguments.
 void setModifiers(java.lang.String modifiers)
           
 void setName(java.lang.String name)
           
 void setVATandFAT(java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat, java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)
           
 java.lang.String toString()
           
 
Methods inherited from class net.scheinerman.phoenix.interpreter.Interpreter
dealWithBreak, dealWithContinue, dealWithError, delete, doRun, getBlock, getFunction, getFunctionArguments, getFunctionFromDefinition, getReturnValue, getVariable, getWhitespace, initNewTables, isValidIdentifier, makeDefaultVariable, makeFunction, makeVariable, makeVariable, parsePhrase, putFunction, removeComments, removeWhitespace, retValueMatches, run
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name
The name of the function.


b4_types

protected Variable[] b4_types
An array containing variables of the same types as the left-hand arguments to the function.


b4_names

protected java.lang.String[] b4_names
An array containing the names of the left-hand arguments to the function.


af_types

protected Variable[] af_types
An array containing variables of the same types as the right-hand arguments to the function.


af_names

protected java.lang.String[] af_names
An array containing the names of the right-hand arguments to the function.


description

protected java.lang.String description

argDescriptions

protected java.util.TreeMap<java.lang.String,java.lang.String> argDescriptions

returnDescription

protected java.lang.String returnDescription

modifiers

protected java.lang.String modifiers
Constructor Detail

Function

public Function()
The default constructor for a Function.
Warning: This constructor bypasses the standard initialization procedure and should not be used.


Function

public Function(java.lang.String name,
                java.lang.String file,
                java.lang.String code,
                int line,
                Variable[] b4_types,
                java.lang.String[] b4_names,
                Variable[] af_types,
                java.lang.String[] af_names,
                Variable retType,
                boolean setupDocs)
Constructs a Function with the given variables.

Parameters:
name - - the function identifier.
code - - the code that the function will execute.
vat - - the global VAT variable
fat - - the global FAT variable
b4_types - - an array of variables whose types are equivalent to the types of variables of the left-hand arguments.
b4_names - - the names of the left-hand arguments.
af_types - - an array of variables whose types are equivalent to the types of variables of the right-hand arguments.
af_names - - the names of the right-hand arguments.
ret_type - - a variable of the same type as the return type of this function.
Method Detail

setVATandFAT

public void setVATandFAT(java.util.LinkedList<java.util.HashMap<java.lang.String,Variable>> vat,
                         java.util.LinkedList<java.util.HashMap<java.lang.String,Function>> fat)

getName

public java.lang.String getName()
Returns the name of this function.

Returns:
The name of this function.

run

public void run(Variable[] b4_args,
                Variable[] af_args)
Runs this function with the given left-hand and right-hand arguments. If these arguments do not match the given variable types passed to the constructor, a SyntaxException will be thrown.

Parameters:
b4_args - - The left-hand arguments.
af_args - - The right-hand arguments.
Throws:
SyntaxException - if the variables are of the wrong type.

dealWithReturn

protected void dealWithReturn(Variable passed)
Deals with a return statement by setting the retValue field, checking whether it's the right type and stopping the function.

Overrides:
dealWithReturn in class Interpreter
Parameters:
passed - - The variable passed via the return statement.
Throws:
SyntaxException - if the returned value is of the wrong type.

check

protected boolean check(Variable[] b4_args,
                        Variable[] af_args)
Checks to see whether the variables that were passed to this function are actually of the type that were specified in the function definition. If this is the case, the value true is returned, otherwise, false is returned.

Parameters:
b4_args - - The left-hand arguments passed to the function.
af_args - - The right-hand arguments passed to the function.
Returns:
true or false based on whether or not the passed variables are of the correct type.

addArgs

protected void addArgs(Variable[] b4_args,
                       Variable[] af_args)
Adds the passed variables to the local VAT and FAT of the function.

Parameters:
b4_args - - the passed left-hand arguments
af_args - - the passed right-hand arguments

getBeforeTypes

public Variable[] getBeforeTypes()
Returns the default variables that were defined as left-hand parameters of the function. The default variables allow the user of this method to determine the types of the left-hand variables.

Returns:
An array of default variables which represent the left-hand argument types.

getAfterTypes

public Variable[] getAfterTypes()
Returns the default variables that were defined as right-hand parameters of the function. The default variables allow the user of this method to determine the types of the right-hand variables.

Returns:
An array of default variables which represent the right-hand argument types.

getBeforeNames

public java.lang.String[] getBeforeNames()

getAfterNames

public java.lang.String[] getAfterNames()

setName

public void setName(java.lang.String name)

toString

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

setModifiers

public void setModifiers(java.lang.String modifiers)

getDefinition

public java.lang.String getDefinition()

printHelp

public void printHelp()