codesimian
Class CS

java.lang.Object
  extended by codesimian.CS
All Implemented Interfaces:
CodeSimian
Direct Known Subclasses:
DefaultCS, IndependentCS

public abstract class CS
extends java.lang.Object
implements CodeSimian

CS is the root of all other classes! Its the top of the CodeSimian heirarchy. Everything you need to know to add new Java code To CodeSimian is in this file.

Theoretically some parts of CodeSimian code (created at runtime) will later be converted to Strings of Java code for a subclass of CS.java. The existing subclasses of CS (in many combinations) will generate new unique and useful subclasses of CS, and those subclasses will build even smarter subclasses.

Since it will be Turing-Complete, these subclasses will be flexible enough to contain a STRONG-ARTIFICIAL-INTELLIGENCE without stepping outside-the-box of CodeSimian. CodeSimian is designed to be used to create a Strong-AI that is BACKWARDS-COMPATIBLE with CodeSimian because it is an ancestor (subclass of a subclass...) of CS.java. Of course, such a software would be so advanced that it could only be created by automatically generated evolved code many generations above Java...

GNU General Public License (GPL) ************
LEGAL: By using CodeSimian, you agree to the GPL license,
version 2 or higher (your choice), in file license.txt.
CodeSimian is FREE for most people ***
but BUSINESSES must pay $$$ to USE A DIFFERENT LICENSE.
CodeSimian: Copyright 2006, Benjamin F Rayfield.
Websites: www.codesimian.com codesimian.sourceforge.net



All functions in CS are public and some are also abstract. There are no variables, only functions.

Each CS is a list of other CSs, and each subclass of CS uses the contents of the list differently.

Each CS has 1 main action: EXECUTE this CS. All of these functions can EXECUTE a CS: V() L(Class) L(int,Class,int) Z() B() C() S() I() J() F() and D(). Each does approximately the same thing, but with a different return-type. All types of primitive and array can be returned, and many object types. Different CSs can return different object types, many types per CS.

CS objects should use other CS objects only as type CS instead of their runtime-type, to avoid tangling CodeSimian with hard-code. Example: CS mult = new Multiply(); instead of: Multiply mult = new Multiply(); In codesimian code, 2 times 3 times 4 is written as: *(2 3 4) Any type-specific functions you need to call can be called dynamicly with a JavaMethod2 CS.

Functions whose names start with "set" return boolean. Most of them are allowed to ALWAYS RETURN FALSE, which would mean that feature is not available in the CS it is called from, but may be available in other CSs in the same group.

A TYPE is any primitive, Object, primitive[] array, Object[] array, or a CS containing any of those.
In addition to that, CodeSimian uses a more advanced idea of TYPE:
A TYPE is a CS whose minP() is at least 1, that measures the TYPE of its 1 param (the CS returned by theType.P(0)), and returns a more positive number if its more like that type, or 0 or less if it is not that type. Its like an anonymous fitness-function in a genetic-algorithm.

Some functions (that usually do the same thing) come in groups of 10-12, each for a different TYPE. Example: the EXECUTE functions above. Example: the PREV functions. Example: the setX functions where X is any EXECUTE function.


WHY SO MANY FUNCTIONS?
These 100+ functions seem confusing, but they are ordered on 3 dimensions+++
+ There are 4 main actions: SET, GET, INSERT, and DELETE.
+ There are 3 main targets: 1 CS in params, multiple adjacent CSs in params, the value of this CS.
+ TYPES. Multiply 3 x 4 x all the TYPES, and you get a 3D block of functions, where the location tells what the function does.
There is a hole in the cube at INSERT or DELETE x VALUE OF THIS CS.

Some functions like heap() and setHeap(CS) are not included in that cube. They're simply the GET and SET for variables that a CS should have, but are not copied for each TYPE. Theres only 1 get and 1 set. But these functions (like heap() and parsePriority()) are complicating CS.java, so they should eventually be changed to only be accessed by L("heap") setL("heap",someObject) etc. Only important functions, like myFuel() and name(), will remain hard-coded into CS.java.

Each CS is a list of CSs containing a quantity of CSs between minP() and maxP() and equal to countP(). All functions whose names contain a capital P (except "Proxy" "Parse" and "Parent") are about PARAMS (CSs inside this CS). Examples: P(int), PD(), setP(int,CS). Params are the contents of the list: Each CS is a list of other CSs.

Functions whose name contains a capital L allow Java Object types to be converted to and from CSs. L functions with 1 or more int parameters allow OPTIMIZATIONS for specific Object types.
Example: if some CS's params are represented as chars in a String (instead of a CS[] array) then that CS should override L(int i,Class c,int j) to return new S(String.substring(i,i+j)) if c == String.class; else return super.L(i,c,j); //S is a type of CS that is stored as a String in memory

WHY IS CS A CLASS INSTEAD OF AN INTERFACE?
I would have liked to make CS be an interface instead of an abstract class, for more flexibility in extending it, but I found that as a class it is approximately 30% faster than as an interface. Thats probably because as a class its functions are in known positions in the table of functions, but as an interface its functions could be in any position since the CLASS determines position, and functions must be looked up more DYNAMICLY which is slower. Not much has been lost: Since this class has approximately 100 functions, its unlikely anybody would use it as an interface except by automatic generation of Java code (see Reflect.java).

8/06 I'm working on the solution to the class/interface problem. CS implements interface CodeSimian, which contains only the most important functions. All other functions can be reflected in terms of these few functions. I've found some functions that must be included, but I've not decided all of which functions will be in the final CodeSimian.java, which is the most important file in CodeSimian software.

Many functions use this CS as a list of CSs. The default implementations are lazy and dont always throw IndexOutOfBoundsException's when they should.

5/06 I'm thinking about removing HEAP from class CS. Heap used negative indexs. It will still use negative indexs, but only hard-coded indexs like these...

public static final int NULL = -1;      
public static final int EXECPROXY = -2;
public static final int PREV = -3;
public static final int FUEL = -4; 
public static final int MYFUEL = -5;
public static final int NAME = -6;
public static final int NEWINSTANCE = -7;
public static final int PARENT = -8;
public static final int DESCRIPTION = -9;
public static final int PARSEPRIORITY = -10;
public static final int JAVACODE = -11;
public static final int HEAP = -12; 


Field Summary
static int DESCRIPTION
           
static int END
          the lowest negative index reserved for CS reflection.
static int EXECPROXY
          someCS.P(CS.EXECPROXY) should return someCS.getExec(),
and optionally someCS.setP(CS.EXECPROXY,x) (or other SET functions) should call someCS.setExec(x), but if the SET functions dont do that, they should return false in that case.
static int FUEL
           
static int HEAP
           
static int JAVACODE
           
static int MYFUEL
           
static int NAME
           
static int NEWINSTANCE
           
static int NULL
          NULL is returned, for example, by indexP(CS) when the CS cant be found.
static int PARENT
           
static int PARSEPRIORITY
           
static int PREV
           
static int TESTER
           
 
Constructor Summary
CS()
           
 
Method Summary
 CS addP(CS param)
          Adds a new param at the end of the list.
 CS addP(CS add0, CS add1)
          same as addP(add0) && addP(add1).
 CS addP(CS add0, CS add1, CS add2)
           
 CS addP(CS add0, CS add1, CS add2, CS add3)
           
 CS addP(CS add0, CS add1, CS add2, CS add3, CS add4)
           
 byte B()
           
 byte BForProxy()
           
 char C()
           
 char CForProxy()
           
 java.lang.Object clone()
          returns newInstance(), but if thats null then throws a CloneNotSupportedException This function overrides java.lang.Object.clone();
 double cost()
          cost() should be changed to return a float, and should be renamed to costToExecute()

cost of EXECUTING this CS, not including any CSs it executes recursively.
abstract  int countP()
          Quantity of params.
 double D()
          Execute this CS and cast to double.
abstract  boolean decrementMyFuel()
          Try to use 1 fuel credit.
abstract  boolean deleteP(int index)
          Deletes a Param at specified index, and slides the higher params down 1 index.
 boolean deleteP(int startIndex, int quantity)
          deletes a range of Params
abstract  java.lang.String description()
          a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for.
abstract  double DForProxy()
          D() and DForProxy() are the 2 most important functions in CS.
 float F()
           
 float FForProxy()
           
abstract  CS fuel()
          Fuel limits EXECUTION of CSs, to make sure none execute too much more than the others.
abstract  CS getExec()
          Every CS must have an EXECPROXY (also called PROXY), even if its a trivial wrapper.
 java.lang.Object getObject()
          Deprecated.  
abstract  CS heap()
          HEAP is a place to put CSs that are not directly related to the CS that contains them.
 int I()
           
 int IForProxy()
           
 int indexP(CS myParam)
          Opposite of P(int).
Returns the index of the specified param, or -1 if its not a param here.
 int indexPName(java.lang.String paramName)
          returns the index of the Param with specified Name, or -1 if no Param has that Name.
 boolean insertB(int paramIndex, byte value)
           
 boolean insertC(int paramIndex, char value)
           
abstract  boolean insertD(int paramIndex, double value)
          inserts a double as a param at a specific index.
 boolean insertF(int paramIndex, float value)
           
 boolean insertI(int paramIndex, int value)
           
 boolean insertJ(int paramIndex, long value)
           
abstract  boolean insertL(int startIndex, java.lang.Object insertMe)
          Inserts an Object into MULTIPLE param indexs.
abstract  boolean insertL(int startIndex, java.lang.Object insertMe, int indexQuantity)
          Inserts an Object into a specific subset of param indexs.
abstract  boolean insertL1(int singleIndex, java.lang.Object value)
          inserts an Object into 1 param index.
abstract  boolean insertP(int index, CS insertMe)
          same as setP(int,CS) but inserts instead of overwriting.
 boolean insertS(int paramIndex, short value)
           
 boolean insertZ(int paramIndex, boolean value)
           
 byte isIllusion(int index)
          Describes if the CS in an index is an illusion or not.
 long J()
          Execute this CS and cast to long.
Most subclasses should override JForProxy() instead of J(), or neither.
WARNING: by default, like the other execute functions, J() calls D() and casts to J's type.
long is the only primitive type that double has problems with.
double maps to long correctly for all values between at least -(2^51) and 2^51 - 1.
Past that, accuracy is less than integer precision.
 java.lang.String javaCode(CS listOfCodeAlreadyTraversed)
          using this CS as the root, returns this CS and its children recursively as a String of JAVA CODE that often (but not always) does the same thing as executing this root CS.
 java.lang.String javaCode(CS listOfCodeAlreadyTraversed, java.lang.Class returnType)
          similar to javaCode(CS) except Class returnType specifies what type the java code should have.
 long JForProxy()
           
abstract  java.lang.String keyword()
          Deprecated.  
 java.lang.Object L(java.lang.Class castToThisType)
          Deprecated. Use L(Object) instead.
 java.lang.Object L(int startIndex, java.lang.Class castToThisType, int indexQuantity)
          same as L(Class) except only uses a subset of param indexs.
Like many other L functions, allows optimizations of converting CSs to specific Object types.
Example: new S("abcdefg").L(2,String.class,3) returns "cde".
 java.lang.Object L(int startIndex, java.lang.Object castToThisType, int indexQuantity)
           
 java.lang.Object L(java.lang.Object key)
          This function will replace L(String) and L(Class)
and can do the same things as P(int) if "key" is a Number (uses Number.intValue()).
 java.lang.Object L(java.lang.String varName)
          Deprecated. replace me with L(Object) and setL(Object,Object)
abstract  java.lang.Object LForProxy(java.lang.Class castToThisType)
          The "?ForProxy" functions should only be called by a PROXY CS.
abstract  java.lang.Object LForProxy(int startIndex, java.lang.Class castToThisType, int indexQuantity)
           
 double maxD()
          maximum value D() can ever return (or any of the other primitive EXECUTE functions).
 int maxP()
          Maximum quantity of Params
 double minD()
          minimum value D() can ever return (or any of the other primitive EXECUTE functions).
abstract  int minP()
          Minimum quantity of Params.
abstract  int myFuel()
          Returns how many times this CS may EXECUTE before it needs to trade some CS fuel for int fuel.
abstract  java.lang.String name()
          returns the name of this CS.
abstract  CS newInstance()
          The primary way to instantiate subclasses of CS.
 byte overwrites(int index)
          Describes when (if ever) this CS overwrites one of its own childs.
abstract  CS P(int index)
          Returns a CS from this list with specific index.
 CS parent()
          9/06 parent() is not yet finished.
 int parsePriority()
          Returns the parsing priority of this CS type, in some language syntax, probably CodeSimian's.
 byte PB(int paramIndex)
           
 char PC(int paramIndex)
           
 double PD(int paramIndex)
          Returns a param as a double.
 float PF(int paramIndex)
           
 int PI(int paramIndex)
           
 long PJ(int paramIndex)
           
 java.lang.Object PL(int paramIndex, java.lang.Class castParamToThisType)
          PL PD PF PJ PI PS PC PB and PZ are only for convenience and optimization.
 byte prevB()
           
 char prevC()
           
abstract  double prevD()
          Returns the value of the last EXECUTION of this CS
 float prevF()
           
 int prevI()
           
 long prevJ()
           
abstract  java.lang.Object prevL()
          returns the previous execute value as an Object.
 short prevS()
           
 boolean prevZ()
          Returns true if the PREVIOUS EXECUTION of this CS was a positive number (or true).
 CS proxyOf(java.lang.reflect.Method anyMethodInThisClass)
          a proxy for a specific Java Method.
 short PS(int paramIndex)
           
abstract  CS PType(int indexP)
          There is a TYPE for each Param.
A TYPE is a CS whose minP() is at least 1.
 boolean PZ(int paramIndex)
           
 java.lang.Object reflect(java.lang.Object[] nameAndParameters)
          Calls any function by reflection, including reflective functions on higher levels, reflection of reflection of reflection...
 java.lang.Object reflect(java.lang.String name, java.lang.Object[] parameters)
          same as reflect(Object[]) except the array is 1 smaller, and all indexs are 1 lower, and the first element which was taken out is String name,

This function is useful because it can use the same array as java.lang.reflect.Method.invoke(Object[]); Name should equal java.lang.reflect.Method.getName() of some Java function (Method).
 short S()
           
 boolean setB(byte setToThisValue)
           
 boolean setB(int paramIndex, byte value)
           
 boolean setC(char setToThisValue)
           
 boolean setC(int paramIndex, char value)
           
 boolean setCost(float cost)
          Rename this to setCostToExecute()
abstract  boolean setD(double setToThisValue)
           
abstract  boolean setD(int paramIndex, double value)
          sets a param to a double value.
 boolean setDescription(java.lang.String newDescription)
           
 boolean setExec(CS newExecProxy)
           
 boolean setF(float setToThisValue)
           
 boolean setF(int paramIndex, float value)
           
abstract  boolean setFuel(CS newFuel)
           
 boolean setHeap(CS newHeap)
           
 boolean setI(int setToThisValue)
           
 boolean setI(int paramIndex, int value)
           
 boolean setJ(int paramIndex, long value)
           
 boolean setJ(long setToThisValue)
           
abstract  boolean setL(int startIndex, java.lang.Object setToThisValue)
          Overwrites a range of params with some interpretation of the Object.
Like setL(int,Object,int) but the Object determines the quantity of indexs.
abstract  boolean setL(int startIndex, java.lang.Object setToThisValue, int indexQuantity)
          like setL(Object) but sets exactly the indexs between startIndex and startIndex+indexQuantity-1 inclusive
abstract  boolean setL(java.lang.Object setToThisValue)
          setL setD setF setJ setI setS setC setB setZ are functions that SET THE VALUE OF THIS CS to some object, primitive, or array.
 boolean setL(java.lang.Object key, java.lang.Object value)
          Sets the value of something.
 boolean setL(java.lang.String varName, java.lang.Object value)
          Deprecated. replace me with L(Object) and setL(Object,Object)
 boolean setL1(int singleIndex, java.lang.Object value)
          overwrites 1 param with an Object.
abstract  boolean setMyFuel(int newValue)
           
 boolean setName(java.lang.String newName)
          sets the name of this CS
 boolean setObject(java.lang.Object setTo)
          Deprecated.  
abstract  boolean setP(int index, CS setTo)
          Every CS is a list of other CSs, between size minP() and maxP() inclusive.
 boolean setParent(CS parent)
           
 boolean setParsePriority(int parsePriority)
           
abstract  void setPrevExec(double d)
          Deprecated.  
 boolean setProxyOf(java.lang.reflect.Method anyMethodInThisClass, CS proxy)
          sets the proxy of a Method.
 boolean setPType(int indexP, CS newTypeOfThatParam)
          Since only a small fraction of CSs use types other than the default type, setPType(int,CS) returns false by default.
 boolean setS(int paramIndex, short value)
           
 boolean setS(short setToThisValue)
           
 boolean setTester(CS tester)
          default: false
 boolean setZ(boolean setToThisValue)
           
 boolean setZ(int paramIndex, boolean value)
           
 short SForProxy()
           
 CS tester()
          Returns a CS that can test if this CS works correctly, or null if dont know how to test.
 void V()
           
 void VForProxy()
           
 void voidReflect(java.lang.Object[] returnAndNameAndParameters)
          Same as reflect(Object[]) except the array is 1 bigger, and returnAndNameAndParameters[0] is replaced by the return-value of the reflected function being called, and all indexs are 1 higher.
 boolean Z()
           
 boolean ZForProxy()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL

public static final int NULL
NULL is returned, for example, by indexP(CS) when the CS cant be found.

See Also:
Constant Field Values

EXECPROXY

public static final int EXECPROXY
someCS.P(CS.EXECPROXY) should return someCS.getExec(),
and optionally someCS.setP(CS.EXECPROXY,x) (or other SET functions) should call someCS.setExec(x), but if the SET functions dont do that, they should return false in that case.

There are others that work similarly: PREV, FUEL, NEWINSTANCE, PARENT, HEAP, NAME, DESCRIPTION, AND PARSEPRIORITY

See Also:
Constant Field Values

PREV

public static final int PREV
See Also:
Constant Field Values

FUEL

public static final int FUEL
See Also:
Constant Field Values

MYFUEL

public static final int MYFUEL
See Also:
Constant Field Values

NAME

public static final int NAME
See Also:
Constant Field Values

NEWINSTANCE

public static final int NEWINSTANCE
See Also:
Constant Field Values

PARENT

public static final int PARENT
See Also:
Constant Field Values

HEAP

public static final int HEAP
See Also:
Constant Field Values

DESCRIPTION

public static final int DESCRIPTION
See Also:
Constant Field Values

PARSEPRIORITY

public static final int PARSEPRIORITY
See Also:
Constant Field Values

JAVACODE

public static final int JAVACODE
See Also:
Constant Field Values

TESTER

public static final int TESTER
See Also:
Constant Field Values

END

public static final int END
the lowest negative index reserved for CS reflection.

If you call P(x) or setP(x,someCS) and CS.END <= x < 0 then it calls a get or set function. Example: setI(CS.MYFUEL,20) and setP(CS.MYFUEL,new N(20)) both call setMyFuel(20).

You may use any indexs lower than this for the HEAP, which is meant to contain "meta instance variables" for a CS which can be used automatically in CodeSimian code or by calling L(String) or setL(String,Object).

Some CSs will allow you to call setP(END-1,someCS) then P(END-1) == someCS.
The default way this is implemented is by using HEAP's params with positive indexs as negative indexs here. HEAP is any CS that can be a list of CSs, but thats not set-in-stone.

See Also:
Constant Field Values
Constructor Detail

CS

public CS()
Method Detail

P

public abstract CS P(int index)
Returns a CS from this list with specific index. Every CS is a list of other CSs, each with an index between 0 and countP()-1 inclusive. The quantity of CSs in the list is countP(), which should be between minP() and maxP() inclusive. Errors could occur if quantity is outside that range when this CS is EXECUTED.

There can also be CSs at negative indexs. Maps some negative indexs to the HEAP:
If x <= -2 then CS.P(x) returns CS.heap().P(-x). Index -1 represents NULL.

Parameters:
index - range 0 to countP()-1 inclusive
See Also:
heap()

setP

public abstract boolean setP(int index,
                             CS setTo)
Every CS is a list of other CSs, between size minP() and maxP() inclusive. setP overwrites one of those CSs or adds a new one at the end, depending on the index. If index is between 0 and countP()-1, overwrites. If it equals countP(), adds at end.


insertP

public abstract boolean insertP(int index,
                                CS insertMe)
same as setP(int,CS) but inserts instead of overwriting. All higher params slide up 1 index.


deleteP

public abstract boolean deleteP(int index)
Deletes a Param at specified index, and slides the higher params down 1 index.


deleteP

public boolean deleteP(int startIndex,
                       int quantity)
deletes a range of Params

Specified by:
deleteP in interface CodeSimian

minP

public abstract int minP()
Minimum quantity of Params. countP() may be below minP() only a short time during compiling, and you must not EXECUTE this CS during that time or you risk an error.


countP

public abstract int countP()
Quantity of params. Should be between minP() and maxP() inclusive when you EXECUTE this CS. Can be below minP() for a short time during compiling, but you must not EXECUTE this CS during that time.


maxP

public int maxP()
Maximum quantity of Params


indexP

public int indexP(CS myParam)
Opposite of P(int).
Returns the index of the specified param, or -1 if its not a param here.

This is needed for efficiency of finding params in CSs whose countP() is very large. For most CSs, countP() < 10, and a linear search should be fast enough, but for CSs that are used like huge arrays, indexP should use a CS-to-int hashtable.

This default implementation does not search the HEAP.


indexPName

public int indexPName(java.lang.String paramName)
returns the index of the Param with specified Name, or -1 if no Param has that Name.

This default implementation does not search the HEAP.

See Also:
name(), setName(String), indexP(CS)

PType

public abstract CS PType(int indexP)
There is a TYPE for each Param.
A TYPE is a CS whose minP() is at least 1. When a TYPE executes, it MEASURES THE TYPE OF its first param CS. It returns a positive number if it is that type, else 0 or negative. Some TYPEs use only 2 numbers, while others can return any number on a continuous scale.

Types are optional. To not use types, return the default type: Static.defaultType(). The default type returns true for any CS. Most CSs do not need types therefore they use the default type.


setPType

public boolean setPType(int indexP,
                        CS newTypeOfThatParam)
Since only a small fraction of CSs use types other than the default type, setPType(int,CS) returns false by default. The same is true of many other functions of CS, which may choose not to cooperate.


addP

public CS addP(CS param)
Adds a new param at the end of the list. Same as setP(countP(),param).

P means param. getP setP insertP and deleteP are the primary way to access params. addP must ALWAYS use those to access params. You should not set it up so any of those 4 use addP.

Returns 'this' CS so code like this works:
int ten = new Multiply().addP(new N(2),new N(5)).I();

Returns:
'this' CS if added param, or null if did not add.

addP

public CS addP(CS add0,
               CS add1)
same as addP(add0) && addP(add1).


addP

public CS addP(CS add0,
               CS add1,
               CS add2)
See Also:
addP(CS)

addP

public CS addP(CS add0,
               CS add1,
               CS add2,
               CS add3)
See Also:
addP(CS)

addP

public CS addP(CS add0,
               CS add1,
               CS add2,
               CS add3,
               CS add4)
See Also:
addP(CS)

PL

public java.lang.Object PL(int paramIndex,
                           java.lang.Class castParamToThisType)
                    throws CSCastException
PL PD PF PJ PI PS PC PB and PZ are only for convenience and optimization. Each gets P(int) then calls L(Class) D() F() etc on it, the second letter of that function's name.
PL(5,String.class) should return P(5).L(String.class) or something that gets the same result faster.

Throws:
CSCastException

PD

public double PD(int paramIndex)
Returns a param as a double. Same as P(paramIndex).D()

Why not just use P(paramIndex).D() instead? Its not just for convenience. Some CSs store their params as doubles or other primitives, and if you predict correctly how params are stored, PD(paramIndex) is much faster than P(paramIndex).D(), but BEWARE: if you predict incorrectly its a little slower. It calculates the same double either way.

For all paramIndex: P(paramIndex).D() == PD(paramIndex)
except when the function call causes params to change. Example: param is an instance of the Count class.


PF

public float PF(int paramIndex)
See Also:
PD(int)

PJ

public long PJ(int paramIndex)
See Also:
PD(int)

PI

public int PI(int paramIndex)
See Also:
PD(int)

PS

public short PS(int paramIndex)
See Also:
PD(int)

PC

public char PC(int paramIndex)
See Also:
PD(int)

PB

public byte PB(int paramIndex)
See Also:
PD(int)

PZ

public boolean PZ(int paramIndex)
See Also:
PD(int)

L

public java.lang.Object L(java.lang.Class castToThisType)
                   throws CSCastException
Deprecated. Use L(Object) instead.

Optionally execute this CS, and definitely try to CAST it to the specified Java type. If fail, throw a ClassCastException.

Should I change this from throwing a CSCastException to return null instead?

It is preferred not to return CSs from this function. If you have a CS to return, the standard is to put it in param0... setP(0,returnValue), where the function caller should get the output from... P(0).

The functions: V L Z B C S I J F and D, usually EXECUTE this CS and CAST its value to the specified type.
L does not have to execute this CS, but all the others do. The other difference is L can throw a CSCastException (extends ClassCastException), which is a RuntimeException so you do not have to use try/catch if you trust it.

No pattern of L()'s behavior is guaranteed.
The L's of some some CSs are more predictable than others, often written about in javadoc.
If it casts correctly, it could fail later. If it fails many times, and you try again, it could still succeed.

Example: new int[7][6][5].getClass().getName() returns "[[[I". These 9 function names (not 10 because excludes Void) are also used by a very important part of Java (but not as function names): java.lang.Class.name() .

EXAMPLES:
JButton b = (JButton) new ButtonCS().L(Component.class); //JButton inherits from Component
int fiveFiftyFive = new N(555).I();
double charValues[] = (double[]) new S("arraySize11").L(double[].class);

Throws:
CSCastException
See Also:
Z(), B(), C(), S(), I(), J(), F(), D()

LForProxy

public abstract java.lang.Object LForProxy(java.lang.Class castToThisType)
                                    throws CSCastException
The "?ForProxy" functions should only be called by a PROXY CS. They skip the proxy. Example: B() calls the PROXY on this CS, then that PROXY calls thisCS.BForProxy(). Generally, subclasses of CS should reimplement only the ?ForProxy functions.

Throws:
CSCastException
See Also:
execProxy(), setExecProxy(CS)

L

public java.lang.Object L(int startIndex,
                          java.lang.Object castToThisType,
                          int indexQuantity)
Specified by:
L in interface CodeSimian

L

public java.lang.Object L(int startIndex,
                          java.lang.Class castToThisType,
                          int indexQuantity)
                   throws CSCastException
same as L(Class) except only uses a subset of param indexs.
Like many other L functions, allows optimizations of converting CSs to specific Object types.
Example: new S("abcdefg").L(2,String.class,3) returns "cde".

Throws:
CSCastException

LForProxy

public abstract java.lang.Object LForProxy(int startIndex,
                                           java.lang.Class castToThisType,
                                           int indexQuantity)
                                    throws CSCastException
Throws:
CSCastException
See Also:
L(int,Class,int)

L

public java.lang.Object L(java.lang.String varName)
Deprecated. replace me with L(Object) and setL(Object,Object)

Reflects some of CS's functions as variables, and returns the value of a variable. Also returns variables that actually are variables (not reflected).

Returns a variable with specified name in the namespace of this CS. Returns null if no variable exists with that name.

Example: someCS.L("description") returns someCS.description(); Example: someCS.L("cost") returns someCS.cost();

STANDARD VARIABLE NAMES include name, cost, proxy, fuel, myFuel, description, newInstance, parent, parsePriority, heap, tester, prevD, etc. Returns the CS found by indexPName(varName) if varName is not a standard name.

FIX THE DESIGN OF CS.JAVA: Eventually description() and cost() should become protected or private (instead of public) and be completely replaced by L(String) and setL(String,Object), because class CS should have as few functions as possible. Its ok for CS to have lots of functions that interact with params, like PD(int), P(int), setD(int,double) etc, but some functions are best abstracted to a variable inside a CS instead of a required part of every CS. Its a little slower, but more efficient. For efficiency, functions like execProxy() and myFuel() etc will stay as functions, but can also be used by L("proxy") or setL("proxy",someProxyCS);

See Also:
Static.defaultVarNames(), Static.defaultVarTypes()

L

public java.lang.Object L(java.lang.Object key)
This function will replace L(String) and L(Class)
and can do the same things as P(int) if "key" is a Number (uses Number.intValue()).

The 3 most common key types are: Class, Integer, and Number. Some instances of all 3 work by default.

Specified by:
L in interface CodeSimian

setL

public boolean setL(java.lang.Object key,
                    java.lang.Object value)
Sets the value of something. Which thing is described by the KEY.

Key can be any Number between 0 and countP()-1, some Strings, or some Classes. Not all Strings and Classes work. Key may be any other Object, but CSs probably will not understand it unless you program them to. (today is 9/06) When more REFLECTION code is finished, this problem may be fixed, so any Object would work for key and value.

Specified by:
setL in interface CodeSimian
See Also:
L(Object)

V

public void V()
See Also:
Execute this CS. Do not return anything. V = Void. Some CSs have side-effects, so void is not useless.

VForProxy

public void VForProxy()
See Also:
DForProxy()

Z

public boolean Z()
See Also:
execute this CS and cast to boolean. By default, all positive numbers are true, 0 & neg false. Its called Z because B is used by byte, and java.lang.java.lang.Class.name() uses Z for boolean. All the functions that execute a CS have names equal to one of java.lang.java.lang.Class.name();\

ZForProxy

public boolean ZForProxy()
See Also:
DForProxy()

B

public byte B()
See Also:
Execute this CS and cast to byte

BForProxy

public byte BForProxy()
See Also:
DForProxy()

C

public char C()
See Also:
Execute this CS and cast to char

CForProxy

public char CForProxy()
See Also:
DForProxy()

S

public short S()
See Also:
Execute this CS and cast to short

SForProxy

public short SForProxy()
See Also:
DForProxy()

I

public int I()
See Also:
Execute this CS and cast to int

IForProxy

public int IForProxy()
See Also:
DForProxy()

J

public long J()
Execute this CS and cast to long.
Most subclasses should override JForProxy() instead of J(), or neither.
WARNING: by default, like the other execute functions, J() calls D() and casts to J's type.
long is the only primitive type that double has problems with.
double maps to long correctly for all values between at least -(2^51) and 2^51 - 1.
Past that, accuracy is less than integer precision.

See Also:
L(Class), java.lang.Double.doubleToLongBits(double)

JForProxy

public long JForProxy()
See Also:
DForProxy()

F

public float F()
See Also:
Execute this CS and cast to float

FForProxy

public float FForProxy()
See Also:
DForProxy()

D

public double D()
Execute this CS and cast to double. All subclasses should override DForProxy() instead of D().

See Also:
L(java.lang.Class)

DForProxy

public abstract double DForProxy()
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()


minD

public double minD()
minimum value D() can ever return (or any of the other primitive EXECUTE functions). Default is -Double.MAX_VALUE, but functions like Math.sin(double) range from -1 to 1, so return -1.


maxD

public double maxD()
maximum value D() can ever return (or any of the other primitive EXECUTE functions). Default is Double.MAX_VALUE, but functions like Math.sin(double) range from -1 to 1, so return 1.


prevL

public abstract java.lang.Object prevL()
returns the previous execute value as an Object.

The function prevL() does not fit well into this interface. If it had a Class parameter, it would take too much instance space and time. If it has no Class parameter, it might not match the Object you're comparing to.

The functions L(Class) Z B C S I J D and F should set the values that these PREV functions return.

Other PREV functions return every other type. There are 9 PREV functions. All 9 are about the same one variable, which can be ANY type. Any one of these functions reads or writes the value of the other 8.


prevZ

public boolean prevZ()
Returns true if the PREVIOUS EXECUTION of this CS was a positive number (or true).

See Also:
prevD()

prevB

public byte prevB()
See Also:
prevD()

prevC

public char prevC()
See Also:
prevD()

prevS

public short prevS()
See Also:
prevD()

prevI

public int prevI()
See Also:
prevD()

prevJ

public long prevJ()
See Also:
prevD()

prevF

public float prevF()
See Also:
prevD()

prevD

public abstract double prevD()
Returns the value of the last EXECUTION of this CS


setL

public abstract boolean setL(java.lang.Object setToThisValue)
setL setD setF setJ setI setS setC setB setZ are functions that SET THE VALUE OF THIS CS to some object, primitive, or array.


setD

public abstract boolean setD(double setToThisValue)

setF

public boolean setF(float setToThisValue)
See Also:
setD(double)

setJ

public boolean setJ(long setToThisValue)
See Also:
setD(double)

setI

public boolean setI(int setToThisValue)
See Also:
setD(double)

setS

public boolean setS(short setToThisValue)
See Also:
setD(double)

setC

public boolean setC(char setToThisValue)
See Also:
setD(double)

setB

public boolean setB(byte setToThisValue)
See Also:
setD(double)

setZ

public boolean setZ(boolean setToThisValue)
See Also:
setD(double)

setL1

public boolean setL1(int singleIndex,
                     java.lang.Object value)
              throws CSCastException
overwrites 1 param with an Object. Like setL(int,Object) but only uses 1 index. The easiest way to do this is put it in a CS and setP(singleIndex,that CS).

Specified by:
setL1 in interface CodeSimian
Throws:
CSCastException

setL

public abstract boolean setL(int startIndex,
                             java.lang.Object setToThisValue)
Overwrites a range of params with some interpretation of the Object.
Like setL(int,Object,int) but the Object determines the quantity of indexs.

Specified by:
setL in interface CodeSimian
See Also:
setL(int,Object,int)

setL

public boolean setL(java.lang.String varName,
                    java.lang.Object value)
Deprecated. replace me with L(Object) and setL(Object,Object)

Reflects some of CS's functions as variables, and sets those variables. Also sets variables that acually are variables (not reflected).

Sets (overwrites) the variable with specified name. STANDARD VARIABLE NAMES include name, cost, proxy, fuel, myFuel, description, newInstance, parent, parsePriority, heap, prevD, etc. Replaces the CS found by indexPName(varName) if varName is not a standard name.

FIX THE DESIGN OF CS.JAVA: Eventually description() and cost() should become protected or private (instead of public) and be completely replaced by L(String) and setL(String,Object), because class CS should have as few functions as possible. Its ok for CS to have lots of functions that interact with params, like PD(int), P(int), setD(int,double) etc, but some functions are best abstracted to a variable inside a CS instead of a required part of every CS. Its a little slower, but more efficient. For efficiency, functions like execProxy() and myFuel() etc will stay as functions, but can also be used by L("proxy") or setL("proxy",someProxyCS);

See Also:
Static.defaultVarNames(), Static.defaultVarTypes()

setL

public abstract boolean setL(int startIndex,
                             java.lang.Object setToThisValue,
                             int indexQuantity)
like setL(Object) but sets exactly the indexs between startIndex and startIndex+indexQuantity-1 inclusive

Specified by:
setL in interface CodeSimian

setZ

public boolean setZ(int paramIndex,
                    boolean value)
See Also:
setD(int,double)

setB

public boolean setB(int paramIndex,
                    byte value)
See Also:
setD(int,double)

setC

public boolean setC(int paramIndex,
                    char value)
See Also:
setD(int,double)

setS

public boolean setS(int paramIndex,
                    short value)
See Also:
setD(int,double)

setI

public boolean setI(int paramIndex,
                    int value)
See Also:
setD(int,double)

setJ

public boolean setJ(int paramIndex,
                    long value)
See Also:
setD(int,double)

setF

public boolean setF(int paramIndex,
                    float value)
See Also:
setD(int,double)

setD

public abstract boolean setD(int paramIndex,
                             double value)
sets a param to a double value. Same as P(paramIndex).setD(value)


insertL1

public abstract boolean insertL1(int singleIndex,
                                 java.lang.Object value)
inserts an Object into 1 param index. The easiest way to do this is put it in a CS and insert that CS with setP(int,CS)

Specified by:
insertL1 in interface CodeSimian

insertL

public abstract boolean insertL(int startIndex,
                                java.lang.Object insertMe)
Inserts an Object into MULTIPLE param indexs. Part of the object goes into each index.

Same as insertL(int,Object,int) but the Object determines the quantity of indexs.

Specified by:
insertL in interface CodeSimian
See Also:
insertL(int,Object,int)

insertL

public abstract boolean insertL(int startIndex,
                                java.lang.Object insertMe,
                                int indexQuantity)
Inserts an Object into a specific subset of param indexs. Part of the object goes into each index.

same as setL(int,Object,int) but INSERTS instead of REPLACES. The values in those indexs are pushed up to higher indexs.


insertZ

public boolean insertZ(int paramIndex,
                       boolean value)
See Also:
insertD(int,double)

insertB

public boolean insertB(int paramIndex,
                       byte value)
See Also:
insertD(int,double)

insertC

public boolean insertC(int paramIndex,
                       char value)
See Also:
insertD(int,double)

insertS

public boolean insertS(int paramIndex,
                       short value)
See Also:
insertD(int,double)

insertI

public boolean insertI(int paramIndex,
                       int value)
See Also:
insertD(int,double)

insertJ

public boolean insertJ(int paramIndex,
                       long value)
See Also:
insertD(int,double)

insertF

public boolean insertF(int paramIndex,
                       float value)
See Also:
insertD(int,double)

insertD

public abstract boolean insertD(int paramIndex,
                                double value)
inserts a double as a param at a specific index. Same as insertP(paramIndex,new N(value))


newInstance

public abstract CS newInstance()
The primary way to instantiate subclasses of CS. CodeSimian is partially a PROTOTYPE LANGUAGE.

Copies the INSTANCE CS and pointers to its childs.
anyCS.P(2) == anyCS.newInstance().P(2)


anyCS.getClass() and anyCS.newInstance().getClass() do not have to share any ancestor class except CS.class. Classes that use artificial-intelligence may choose to return a different class type to improve themselves, but that class type should try to BEHAVE SIMILARLY and must have the SAME childs when newInstance() returns it.

If a subclass is uses variables in a strange way, it may need to override newInstance() to copy them.

Returns a new CS instance of this "type". This has the same name and purpose as java.lang.Class.newInstance().

Its vague. Subclasses of CS that have trees or networks of objects that the CS depends on might need to copy the whole tree etc, or might use the same tree, or only copy the root etc. Example: the Function class might need to override newInstance() differently.


parent

public CS parent()
9/06 parent() is not yet finished. CSs do not yet use parent(). They use keyword() instead of parent().name().

Returns the CS whose newInstance() created this CS, or returns a CS which this CS should think of as its parent.

No cycles may exist in parent().parent().parent()... paths.
One way to enforce this is to only do y.setParent(x) when x.newInstance() created y.

Recursive namespaces could be implemented by searching parent().parent()... until find the requested name. Could also search childs' parents recursively. Namespaces might search in many directions.

CS.keyword() will be replaced by CS.parent().name()

Returns:
the parent CS, or null if this CS was not created by an other CS. Returns null by default. You do not have to use the parent system.

setParent

public boolean setParent(CS parent)
See Also:
parent()

name

public abstract java.lang.String name()
returns the name of this CS.

After keyword() is refactored out, parent().name() will replace keyword().


setName

public boolean setName(java.lang.String newName)
sets the name of this CS


cost

public double cost()
cost() should be changed to return a float, and should be renamed to costToExecute()

cost of EXECUTING this CS, not including any CSs it executes recursively. The INTERNAL cost.

One unit of myFuel() costs cost() units of fuel(). Beware of CSs that counterfeit fuel.

Returns 1 by default.


setCost

public boolean setCost(float cost)
Rename this to setCostToExecute()

See Also:
cost()

description

public abstract java.lang.String description()
a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for. Example use: in automatically generated webpages for CodeSimian. Example: "returns sum of all params" for Add.


setDescription

public boolean setDescription(java.lang.String newDescription)
See Also:
description()

parsePriority

public int parsePriority()
Returns the parsing priority of this CS type, in some language syntax, probably CodeSimian's. Default parsePriority is 0. Default CodeSimian parsePriorities use +100 or -100 increments, to allow many priorities to be inserted in the middle.

This will later be useful for infix syntax. Instead of: +(/(1 2) *(3 4 5)) you type: 1/2+3*4*5

Returns 0 by default.


setParsePriority

public boolean setParsePriority(int parsePriority)
See Also:
parsePriority()

getExec

public abstract CS getExec()
Every CS must have an EXECPROXY (also called PROXY), even if its a trivial wrapper. PROXY is a middle step between all EXECUTIONS.

A PROXY should implement all 11 EXECUTE functions: L(Class) L(int,Class,int) Z() B() C() S() I() J() F() D() V(). A normal CS should usually not implement any of those functions. Normal CSs should instead implement DForProxy() and LForProxy(Class) etc...

A proxy's X() function should call XForProxy(), where X is one of the 11 functions above