codesimian
Class IndependentCS

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

public class IndependentCS
extends CS

IndependentCS is used to avoid DEPENDENCY CYCLES when creating the first few CSs.
If you add a new function to CS but not override in IndependentCS, you might create a dependency cycle. All functions in IndependentCS return 0, false, or this. Override any new functions in CS to return that.

IndependentCS's functions never return any other CS, only returns itself. Examples: P(87), heap(), getExec()... all return this CS.

All EXECUTE functions do NOTHING. IndependentCS is a LEAF.


Field Summary
 
Fields inherited from class codesimian.CS
DESCRIPTION, END, EXECPROXY, FUEL, HEAP, JAVACODE, MYFUEL, NAME, NEWINSTANCE, NULL, PARENT, PARSEPRIORITY, PREV, TESTER
 
Constructor Summary
IndependentCS()
           
 
Method Summary
 byte B()
           
 byte BForProxy()
           
 char C()
           
 char CForProxy()
           
 int countP()
          Quantity of params.
 double D()
          Execute this CS and cast to double.
 boolean decrementMyFuel()
          returns true, but that might cause infinite loops...
 boolean deleteP(int index)
          Deletes a Param at specified index, and slides the higher params down 1 index.
 java.lang.String description()
          a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for.
 double DForProxy()
          D() and DForProxy() are the 2 most important functions in CS.
 float F()
           
 float FForProxy()
           
 CS fuel()
          Fuel limits EXECUTION of CSs, to make sure none execute too much more than the others.
 CS getExec()
          this is static to save memory.
 CS heap()
          HEAP is a place to put CSs that are not directly related to the CS that contains them.
 int I()
           
 int IForProxy()
           
 boolean insertD(int index, double d)
          inserts a double as a param at a specific index.
 boolean insertL(int index, java.lang.Object value)
          Inserts an Object into MULTIPLE param indexs.
 boolean insertL(int index, java.lang.Object value, int indexQuantity)
          Inserts an Object into a specific subset of param indexs.
 boolean insertL1(int index, java.lang.Object value)
          inserts an Object into 1 param index.
 boolean insertP(int index, CS insertMe)
          same as setP(int,CS) but inserts instead of overwriting.
 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.
 long JForProxy()
           
 java.lang.String keyword()
          Returns the keyword of this CS or (after more code is finished) returns parent().name();

Keyword() needs to be removed.
 java.lang.Object L(java.lang.Class castToThisType)
          Optionally execute this CS, and definitely try to CAST it to the specified Java type.
 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 LForProxy(java.lang.Class castToThisType)
          The "?ForProxy" functions should only be called by a PROXY CS.
 java.lang.Object LForProxy(int startIndex, java.lang.Class castToThisType, int indexQuantity)
           
 int maxP()
          Maximum quantity of Params
 int minP()
          Minimum quantity of Params.
 int myFuel()
          Returns how many times this CS may EXECUTE before it needs to trade some CS fuel for int fuel.
 java.lang.String name()
          cant change this name because that would require a String variable and IndependentCS must use very little memory
 CS newInstance()
          The primary way to instantiate subclasses of CS.
 CS P(int index)
          Returns a CS from this list with specific index.
 CS parent()
          9/06 parent() is not yet finished.
 double prevD()
          Returns the value of the last EXECUTION of this CS
 java.lang.Object prevL()
          returns the previous execute value as an Object.
 CS PType(int index)
          There is a TYPE for each Param.
A TYPE is a CS whose minP() is at least 1.
 short S()
           
 boolean setD(double setToThisValue)
           
 boolean setD(int index, double d)
          sets a param to a double value.
 boolean setFuel(CS newFuel)
           
 boolean setL(int index, java.lang.Object value)
          Overwrites a range of params with some interpretation of the Object.
Like setL(int,Object,int) but the Object determines the quantity of indexs.
 boolean setL(int index, java.lang.Object value, int indexQuantity)
          like setL(Object) but sets exactly the indexs between startIndex and startIndex+indexQuantity-1 inclusive
 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 setMyFuel(int i)
           
 boolean setP(int index, CS setTo)
          Every CS is a list of other CSs, between size minP() and maxP() inclusive.
 void setPrevExec(double d)
          do nothing
 short SForProxy()
           
protected  void start()
          error: without this function, theres still a lot of these error messages when CodeSimian starts, caused by CS.start(): error: Static.optionsStack is null.
 java.lang.String toString()
          independentCS.countP() should always be 0.
 void V()
           
 void VForProxy()
           
 boolean Z()
           
 boolean ZForProxy()
           
 
Methods inherited from class codesimian.CS
addP, addP, addP, addP, addP, clone, cost, deleteP, getObject, indexP, indexPName, insertB, insertC, insertF, insertI, insertJ, insertS, insertZ, isIllusion, javaCode, javaCode, L, L, L, maxD, minD, overwrites, parsePriority, PB, PC, PD, PF, PI, PJ, PL, prevB, prevC, prevF, prevI, prevJ, prevS, prevZ, proxyOf, PS, PZ, reflect, reflect, setB, setB, setC, setC, setCost, setDescription, setExec, setF, setF, setHeap, setI, setI, setJ, setJ, setL, setL, setL1, setName, setObject, setParent, setParsePriority, setProxyOf, setPType, setS, setS, setTester, setZ, setZ, tester, voidReflect
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndependentCS

public IndependentCS()
Method Detail

start

protected void start()
error: without this function, theres still a lot of these error messages when CodeSimian starts, caused by CS.start(): error: Static.optionsStack is null. This CS = 0() error: Static.optionsStack is null. This CS = varNam error: Static.optionsStack is null. This CS = varNam error: Static.optionsStack is null. This CS = varNam error: Static.optionsStack is null. This CS = 0() error: Static.optionsStack is null. This CS = err() error: Static.optionsStack is null. This CS = 0()


setL

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

Specified by:
setL in class CS

setD

public boolean setD(double setToThisValue)
Specified by:
setD in class CS

setP

public boolean setP(int index,
                    CS setTo)
Description copied from class: CS
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.

Specified by:
setP in class CS

PType

public CS PType(int index)
Description copied from class: CS
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.

Specified by:
PType in class CS

P

public CS P(int index)
Description copied from class: CS
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.

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

insertP

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

Specified by:
insertP in class CS

deleteP

public boolean deleteP(int index)
Description copied from class: CS
Deletes a Param at specified index, and slides the higher params down 1 index.

Specified by:
deleteP in class CS

description

public java.lang.String description()
Description copied from class: CS
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.

Specified by:
description in class CS

keyword

public java.lang.String keyword()
Description copied from class: CS
Returns the keyword of this CS or (after more code is finished) returns parent().name();

Keyword() needs to be removed. Keyword() can only be removed from CS.java when the primary way to instantiate the default CS types is by using newInstance() on one of the CSs in a list of default CSs.

When you instantiate someCS with someCS.parent().newInstance(), someCS.keyword() can be replaced by someCS.parent().name().

keyword() appears in the code hundreds of times. Be careful when refactoring it out.

There is intentionally no setKeyword(String) in this interface. Dont add it.

Specified by:
keyword in class CS
See Also:
CS.parent(), CS.newInstance(), CS.name()

fuel

public CS fuel()
Description copied from class: CS
Fuel limits EXECUTION of CSs, to make sure none execute too much more than the others. Fuel is ENFORCED by PROXYs, but each subclass of CS must be designed to defer all EXECUTE functions (V Z B C S I J F D L(Class) L(int,Class,int)) to the PROXY for that to work. In a proxy, the relationship between X and XForProxy (for all X) is different [I'll write more about that later]. Depending on which proxy you choose, it might decrease fuel and not execute "?ForProxy()" if fuel runs out.

There are 2 kinds of fuel. The CS fuel can be shared, and can distribute fuel in complex ways. The int fuel is individual to each CS.

This is the easiest (and slowest) way to use 1 fuel:
boolean usedFuel = fuel().setD( fuel().D() - cost() )

Fuel CSs must refuse to become negative. This code: fuel().setD(-3) must always return false.

fuel() should be changed to return CS instead of N (a subclass of CS)

WARNING: If you allow evolved code to modify fuel, it could steal or counterfeit fuel. Example CodeSimian code:
method#CodeSimian.CS.setFuel(0 someCounterfeitingCS 10000000000000)

When a CS's myFuel is decreased to 0, the only way it should ever EXECUTE again is if FUEL is TRADED for myFuel. One myFuel (type int) costs cost() (type double) amount of FUEL (type double, wrapped in a CS). That often allows the efficient use of the FUEL CS object, because you could trade many myFuels for more FUEL in the same amount of code as for trading one at a time. When a CS's myFuel runs out, you might set it up so it automatically buys 10 more, but only if some other code returns true. Certain groups of code could be allowed more execution credits depending on their expected usefulness, predicted by other intelligent code.

Can divide FUEL into heirarchies without extra code. Every CS has a FUEL object, and fuel objects are CSs, so every fuel object has a fuel object... When a fuel object runs out of fuel, it might try to get more from its fuel object... but that fuel object might refuse. A heirarchy of fuel control could be built.

Specified by:
fuel in class CS

getExec

public CS getExec()
this is static to save memory. An IndependentCS usually doesnt use fuel.

Specified by:
getExec in class CS

maxP

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

Overrides:
maxP in class CS

countP

public int countP()
Description copied from class: CS
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.

Specified by:
countP in class CS

minP

public int minP()
Description copied from class: CS
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.

Specified by:
minP in class CS

newInstance

public CS newInstance()
Description copied from class: CS
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.

Specified by:
newInstance in class CS

heap

public CS heap()
Description copied from class: CS
HEAP is a place to put CSs that are not directly related to the CS that contains them. Example: Params are directly relevant, but wrappers for the Java functions of a CS (like heap()) are not.

If you think of the positive (Param) indexs as STACK memory, this is HEAP memory and is mapped to negative param indexs.
Index 0 is grouped with the positives. Index -1 is reserved for NULL.
Heap indexs start at -2, which maps to index 2 in the HEAP. The heap's first 2 indexs are not used.


If x <= -2 then CS.P(x) returns CS.heap().P(-x)

Every CS must have a HEAP, which is used like heap memory (compared to stack memory). Multiple CSs can share the same HEAP or each could have its own. Only nonnegative P indexs are used for the heap, but they're usually mapped to negative indexs in the CS(s) that use this HEAP.

Usually HEAPs contain reflected java functions for the class of the CS that contains this HEAP. Some HEAPs might contain other variables.

Specified by:
heap in class CS

prevL

public java.lang.Object prevL()
Description copied from class: CS
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.

Specified by:
prevL in class CS

prevD

public double prevD()
Description copied from class: CS
Returns the value of the last EXECUTION of this CS

Specified by:
prevD in class CS

parent

public CS parent()
Description copied from class: CS
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()

Overrides:
parent in class CS
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.

setPrevExec

public void setPrevExec(double d)
do nothing

Specified by:
setPrevExec in class CS
See Also:
CS.prevD()

decrementMyFuel

public boolean decrementMyFuel()
returns true, but that might cause infinite loops...

Specified by:
decrementMyFuel in class CS

setMyFuel

public boolean setMyFuel(int i)
Specified by:
setMyFuel in class CS
See Also:
CS.myFuel()

myFuel

public int myFuel()
Description copied from class: CS
Returns how many times this CS may EXECUTE before it needs to trade some CS fuel for int fuel. When it runs out, it can trade to get more fuel, or it can stop executing and return its previous value instead. Example, the previous double value is prevD().

myFuel() is for this specific CS. fuel() can be shared between CSs and has more complex behaviors. Should always be 0 or positive. If 0, this CS should not EXECUTE, but it is the PROXY's job to enforce that.

Specified by:
myFuel in class CS

setFuel

public boolean setFuel(CS newFuel)
Specified by:
setFuel in class CS
See Also:
CS.fuel()

name

public java.lang.String name()
cant change this name because that would require a String variable and IndependentCS must use very little memory

Specified by:
name in class CS

insertD

public boolean insertD(int index,
                       double d)
Description copied from class: CS
inserts a double as a param at a specific index. Same as insertP(paramIndex,new N(value))

Specified by:
insertD in class CS

insertL

public boolean insertL(int index,
                       java.lang.Object value)
Description copied from class: CS
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
Specified by:
insertL in class CS
See Also:
CS.insertL(int,Object,int)

insertL

public boolean insertL(int index,
                       java.lang.Object value,
                       int indexQuantity)
Description copied from class: CS
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.

Specified by:
insertL in class CS

insertL1

public boolean insertL1(int index,
                        java.lang.Object value)
Description copied from class: CS
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
Specified by:
insertL1 in class CS

setD

public boolean setD(int index,
                    double d)
Description copied from class: CS
sets a param to a double value. Same as P(paramIndex).setD(value)

Specified by:
setD in class CS

setL

public boolean setL(int index,
                    java.lang.Object value,
                    int indexQuantity)
Description copied from class: CS
like setL(Object) but sets exactly the indexs between startIndex and startIndex+indexQuantity-1 inclusive

Specified by:
setL in interface CodeSimian
Specified by:
setL in class CS

setL

public boolean setL(int index,
                    java.lang.Object value)
Description copied from class: CS
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
Specified by:
setL in class CS
See Also:
CS.setL(int,Object,int)

V

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

Z

public boolean Z()
Overrides:
Z in class CS
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();\

B

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

C

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

S

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

I

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

J

public long J()
Description copied from class: CS
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.

Overrides:
J in class CS
See Also:
CS.L(Class), java.lang.Double.doubleToLongBits(double)

F

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

D

public double D()
Description copied from class: CS
Execute this CS and cast to double. All subclasses should override DForProxy() instead of D().

Overrides:
D in class CS
See Also:
CS.L(java.lang.Class)

L

public java.lang.Object L(java.lang.Class castToThisType)
Description copied from class: CS
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);

Overrides:
L in class CS
See Also:
CS.Z(), CS.B(), CS.C(), CS.S(), CS.I(), CS.J(), CS.F(), CS.D()

L

public java.lang.Object L(int startIndex,
                          java.lang.Class castToThisType,
                          int indexQuantity)
Description copied from class: CS
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".

Overrides:
L in class CS

VForProxy

public void VForProxy()
Overrides:
VForProxy in class CS
See Also:
CS.DForProxy()

ZForProxy

public boolean ZForProxy()
Overrides:
ZForProxy in class CS
See Also:
CS.DForProxy()

BForProxy

public byte BForProxy()
Overrides:
BForProxy in class CS
See Also:
CS.DForProxy()

CForProxy

public char CForProxy()
Overrides:
CForProxy in class CS
See Also:
CS.DForProxy()

SForProxy

public short SForProxy()
Overrides:
SForProxy in class CS
See Also:
CS.DForProxy()

IForProxy

public int IForProxy()
Overrides:
IForProxy in class CS
See Also:
CS.DForProxy()

JForProxy

public long JForProxy()
Overrides:
JForProxy in class CS
See Also:
CS.DForProxy()

FForProxy

public float FForProxy()
Overrides:
FForProxy in class CS
See Also:
CS.DForProxy()

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 CS

LForProxy

public java.lang.Object LForProxy(java.lang.Class castToThisType)
Description copied from class: CS
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.

Specified by:
LForProxy in class CS
See Also:
execProxy(), setExecProxy(CS)

LForProxy

public java.lang.Object LForProxy(int startIndex,
                                  java.lang.Class castToThisType,
                                  int indexQuantity)
Specified by:
LForProxy in class CS
See Also:
CS.L(int,Class,int)

toString

public java.lang.String toString()
independentCS.countP() should always be 0. If its more, the params still wont show here

Overrides:
toString in class java.lang.Object