codesimian
Class Function

java.lang.Object
  extended by codesimian.CS
      extended by codesimian.DefaultCS
          extended by codesimian.Function
All Implemented Interfaces:
CodeSimian, java.io.Serializable

public class Function
extends DefaultCS

This class allows trees of CSs to be used a little more like hard-coded functions. You dont have to copy the tree to have 2 instances of that tree with a few CSs in them different. Instead, make 2 Function's and let them both wrap the tree, and change only the CSs in the tree whose parameters are the parameters of this Function.

OBSOLETE TEXT: Its first parameter is the root of the CS that this is a wrapper-class for. You can set that param, but if you try to get it, you will instead get an Err (subclass of Num) with the value of getEncapsulated().prevD(). That is to protect the tree from being modified. getEncapsulated() is not a typo. Its how to get the real param0. You cant get it from P(0).

Its ok to overwrite the tree with setP(countP(),newTree), because this Function will change to use that new tree, but if P(countP()) were allowed to return the real param0, it would not be possible to hide the tree from CSs who use this Function.

For evolution of CodeSimian code, most Functions should have at least 2 parameters, so they'll have at least 1 parameter thats actually used as a parameter.

See Also:
Serialized Form

Nested Class Summary
static class Function.ParamProxy
          The Function class encapsulates a tree/graph of CSs.
 
Field Summary
protected  CS encapsulated
           
protected  CS[] functionContents
          includes all contained CSs except the parameters, and CSs that will be replaced.
protected  Function.ParamProxy[] paramChanges
          paramChanges.length == countP()
protected  boolean rebuildInNextExec
          Returns true if the next time DForProxy() runs, it should call encapsulate(getEncapsulated()) and set this boolean to false.
 
Fields inherited from class codesimian.CS
DESCRIPTION, END, EXECPROXY, FUEL, HEAP, JAVACODE, MYFUEL, NAME, NEWINSTANCE, NULL, PARENT, PARSEPRIORITY, PREV, TESTER
 
Constructor Summary
Function()
           
 
Method Summary
 boolean deleteP(int index)
          deleting not yet allowed
 double DForProxy()
          D() and DForProxy() are the 2 most important functions in CS.
 void encapsulate(CS encapsulateMe)
          Sets the CS that this Function is a wrapper for.
 CS getEncapsulated()
          Returuns the root of the tree (or graph) of CSs that this Function is a wrapper-class for.
 boolean insertP(int index, CS insertMe)
          inserting not yet allowed
 java.lang.String keyword()
          For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
 int maxP()
          Maximum quantity of Params
 CS P(int index)
          Works normally except if index==countP(), 1 higher than the max valid index, returns getEncapsulated()
 void prepareFunctionContentsForNextExecution()
           
 boolean setP(int index, CS setTo)
          When countP() increases, rebuildInNextExec becomes true, and the next DForProxy() calls encapsulate(the last param) therefore decreasing countP() by 1.
 
Methods inherited from class codesimian.DefaultCS
B, C, countP, decrementMyFuel, description, F, fuel, getExec, getObject, heap, I, indexP, indexPName, insertB, insertC, insertD, insertF, insertI, insertJ, insertL, insertL, insertL1, insertS, insertZ, J, javaCode, LForProxy, LForProxy, minP, myFuel, name, newInstance, objectToCS, objectToCSArray, objectToCSArray, prevD, prevL, PType, S, setB, setC, setCountP, setD, setD, setExec, setF, setFuel, setI, setJ, setL, setL, setL, setL1, setMyFuel, setName, setObject, setPrevExec, setS, setZ, start, toString, V, Z
 
Methods inherited from class codesimian.CS
addP, addP, addP, addP, addP, BForProxy, CForProxy, clone, cost, D, deleteP, FForProxy, IForProxy, isIllusion, javaCode, JForProxy, L, L, L, L, L, maxD, minD, overwrites, parent, parsePriority, PB, PC, PD, PF, PI, PJ, PL, prevB, prevC, prevF, prevI, prevJ, prevS, prevZ, proxyOf, PS, PZ, reflect, reflect, setB, setC, setCost, setDescription, setF, setHeap, setI, setJ, setL, setL, setParent, setParsePriority, setProxyOf, setPType, setS, setTester, setZ, SForProxy, tester, VForProxy, voidReflect, ZForProxy
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

paramChanges

protected Function.ParamProxy[] paramChanges
paramChanges.length == countP()


functionContents

protected CS[] functionContents
includes all contained CSs except the parameters, and CSs that will be replaced. But if a CS is in here, it could still later become a parameter of this Function.


encapsulated

protected CS encapsulated

rebuildInNextExec

protected boolean rebuildInNextExec
Returns true if the next time DForProxy() runs, it should call encapsulate(getEncapsulated()) and set this boolean to false. Starts false. Set to true after setP(0, any CS).

Constructor Detail

Function

public Function()
Method Detail

DForProxy

public double DForProxy()
Description copied from class: CS
D() and DForProxy() are the 2 most important functions in CS. They execute this CS. All other execute functions, by default, use DForProxy instead of reinventing-the-wheel for their own type.

For example, J() calls JForProxy() which calls DForProxy(). D() calls DForProxy() directly.

Execute this CS and cast to double. D() is the main EXECUTE function. D() uses DForProxy(). DForProxy() is the main action of every CS, the most important function. By default, all other primitive EXECUTE functions defer to D.
Functions that EXECUTE this CS: L(Class) L(int,Class,int) Z() B() C() S() I() J() F() D() V()

Specified by:
DForProxy in class DefaultCS

prepareFunctionContentsForNextExecution

public void prepareFunctionContentsForNextExecution()

maxP

public int maxP()
Description copied from class: CS
Maximum quantity of Params

Overrides:
maxP in class CS

keyword

public java.lang.String keyword()
Description copied from class: DefaultCS
For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
Default: Returns class name, minus package name (and its dots), and change the first letter to lowercase.

For example, CS.MaxParams does not override keyword(), which returns "maxP".

Overrides:
keyword in class DefaultCS
See Also:
CS.parent(), CS.newInstance(), CS.name()

setP

public boolean setP(int index,
                    CS setTo)
When countP() increases, rebuildInNextExec becomes true, and the next DForProxy() calls encapsulate(the last param) therefore decreasing countP() by 1. If paramChanges[] is big enough, uses paramChanges[index] to overwrite some CS with setTo in 0 or more places. The parameter setTo is copied to the places in the encapsulated function that parameter occurs in the function body.
Example: func#add(5#a 6#b +(a b))
If you setP(add 1 30) then b (parameter number 1) is replaced and the code: add returns 38 because +(5#a 33) equals 38. returns

Overrides:
setP in class DefaultCS

P

public CS P(int index)
Works normally except if index==countP(), 1 higher than the max valid index, returns getEncapsulated()

Overrides:
P in class DefaultCS
Parameters:
index - range 0 to countP()-1 inclusive
See Also:
CS.heap()

insertP

public boolean insertP(int index,
                       CS insertMe)
inserting not yet allowed

Overrides:
insertP in class DefaultCS

deleteP

public boolean deleteP(int index)
deleting not yet allowed

Overrides:
deleteP in class DefaultCS

encapsulate

public void encapsulate(CS encapsulateMe)
Sets the CS that this Function is a wrapper for. Remembers this Function's current params so they can be replaced multiple places by setP(int,CS).

SIDE-EFFECTS:
Sets paramChanges[] to an array of changes that are done by setP(int,CS).
Sets functionContents[] to the set of CSs that can be found by traversing params from encapsulateMe but not traversing past any of the params of this Function, excluding encapsulateMe.
Removes the last param if it equals encapsulateMe.


getEncapsulated

public CS getEncapsulated()
Returuns the root of the tree (or graph) of CSs that this Function is a wrapper-class for.