- Basic types can now be converted to the "any" type

- Changed OreDict 'contains' to respond to the 'in' operator
This commit is contained in:
Stan Hebben committed 2014-07-20 21:30:40 +02:00
1 parent 52f92dcfd4
commit 1b72abb2b4
59 files changed
+3595 -976

No files matched your search

@@ -10,9 +10,16 @@ import java.util.Iterator;
/**
*
* @author Stanneke
* @author Stan Hebben
*/
public interface IAny {
public static final int NUM_BYTE = 1;
public static final int NUM_SHORT = 2;
public static final int NUM_INT = 3;
public static final int NUM_LONG = 4;
public static final int NUM_FLOAT = 5;
public static final int NUM_DOUBLE = 6;
public IAny not();
public IAny neg();
@@ -41,7 +48,11 @@ public interface IAny {
public boolean contains(IAny value);
public IAny member(String value);
public IAny memberGet(String member);
public void memberSet(String member, IAny value);
public IAny memberCall(String member, IAny... values);
public IAny indexGet(IAny key);
@@ -67,6 +78,12 @@ public interface IAny {
public <T> T as(Class<T> cls);
public boolean is(Class<?> cls);
public boolean canCastImplicit(Class<?> cls);
public int getNumberType();
public Iterator<IAny> iteratorSingle();
public Iterator<IAny[]> iteratorMulti(int n);
@@ -1,14 +1,8 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stanhebben.zenscript.value;
/**
*
* @author Stan
*
* @author Stan Hebben
*/
public class IntRange {
private final int from;
@@ -1,177 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stanhebben.zenscript.value.builtin;
import java.util.Iterator;
import stanhebben.zenscript.ZenRuntimeException;
import stanhebben.zenscript.value.IAny;
import stanhebben.zenscript.value.IntRange;
/**
*
* @author Stanneke
*/
public class AnyByte implements IAny {
public static AnyByte valueOf(byte value) {
return new AnyByte(value);
}
private final byte value;
public AnyByte(byte value) {
this.value = value;
}
@Override
public IAny not() {
return new AnyByte((byte) ~value);
}
@Override
public IAny neg() {
return new AnyByte((byte) -value);
}
@Override
public IAny add(IAny value) {
return new AnyByte((byte) (this.value + value.asByte()));
}
@Override
public IAny sub(IAny value) {
return new AnyByte((byte) (this.value - value.asByte()));
}
@Override
public IAny cat(IAny value) {
return new AnyString(Byte.toString(this.value) + value.asString());
}
@Override
public IAny mul(IAny value) {
return new AnyByte((byte) (this.value * value.asByte()));
}
@Override
public IAny div(IAny value) {
return new AnyByte((byte) (this.value / value.asByte()));
}
@Override
public IAny mod(IAny value) {
return new AnyByte((byte) (this.value % value.asByte()));
}
@Override
public IAny and(IAny value) {
return new AnyByte((byte) (this.value & value.asByte()));
}
@Override
public IAny or(IAny value) {
return new AnyByte((byte) (this.value | value.asByte()));
}
@Override
public IAny xor(IAny value) {
return new AnyByte((byte) (this.value ^ value.asByte()));
}
@Override
public IAny range(IAny value) {
return new AnyIntRange(new IntRange(this.value, value.asInt()));
}
@Override
public int compareTo(IAny value) {
return Integer.compare(this.value, value.asByte());
}
@Override
public boolean contains(IAny value) {
throw new ZenRuntimeException("byte has no in operator");
}
@Override
public IAny member(String value) {
if (value.equals("abs")) {
return new AnyByte((byte) (Math.abs(this.value)));
} else {
throw new ZenRuntimeException("no such member in byte: " + value);
}
}
@Override
public IAny indexGet(IAny key) {
throw new ZenRuntimeException("cannot index a byte value");
}
@Override
public void indexSet(IAny key, IAny value) {
throw new ZenRuntimeException("cannot index a byte value");
}
@Override
public IAny call(IAny... values) {
throw new ZenRuntimeException("cannot call a byte value");
}
@Override
public boolean asBool() {
throw new ZenRuntimeException("cannot convert a byte to a bool");
}
@Override
public byte asByte() {
return value;
}
@Override
public short asShort() {
return value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Byte.toString(value);
}
@Override
public <T> T as(Class<T> cls) {
throw new ZenRuntimeException("cannot convert a byte to an object");
}
@Override
public Iterator<IAny> iteratorSingle() {
throw new ZenRuntimeException("cannot iterate over a byte value");
}
@Override
public Iterator<IAny[]> iteratorMulti(int n) {
throw new ZenRuntimeException("cannot iterate over a byte value");
}
}
@@ -1,177 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stanhebben.zenscript.value.builtin;
import java.util.Iterator;
import stanhebben.zenscript.ZenRuntimeException;
import stanhebben.zenscript.value.IAny;
import stanhebben.zenscript.value.IntRange;
/**
*
* @author Stanneke
*/
public class AnyInt implements IAny {
public static AnyInt valueOf(byte value) {
return new AnyInt(value);
}
private final int value;
public AnyInt(int value) {
this.value = value;
}
@Override
public IAny not() {
return new AnyInt(~value);
}
@Override
public IAny neg() {
return new AnyInt(-value);
}
@Override
public IAny add(IAny value) {
return new AnyInt(this.value + value.asInt());
}
@Override
public IAny sub(IAny value) {
return new AnyInt(this.value - value.asInt());
}
@Override
public IAny cat(IAny value) {
return new AnyString(Integer.toString(this.value) + value.asString());
}
@Override
public IAny mul(IAny value) {
return new AnyInt(this.value * value.asByte());
}
@Override
public IAny div(IAny value) {
return new AnyInt(this.value / value.asByte());
}
@Override
public IAny mod(IAny value) {
return new AnyInt(this.value % value.asByte());
}
@Override
public IAny and(IAny value) {
return new AnyInt(this.value & value.asByte());
}
@Override
public IAny or(IAny value) {
return new AnyInt(this.value | value.asByte());
}
@Override
public IAny xor(IAny value) {
return new AnyInt(this.value ^ value.asByte());
}
@Override
public IAny range(IAny value) {
return new AnyIntRange(new IntRange(this.value, value.asInt()));
}
@Override
public int compareTo(IAny value) {
return Integer.compare(this.value, value.asByte());
}
@Override
public boolean contains(IAny value) {
throw new ZenRuntimeException("int has no in operator");
}
@Override
public IAny member(String value) {
if (value.equals("abs")) {
return new AnyInt(Math.abs(this.value));
} else {
throw new ZenRuntimeException("no such member in int: " + value);
}
}
@Override
public IAny indexGet(IAny key) {
throw new ZenRuntimeException("cannot index an int value");
}
@Override
public void indexSet(IAny key, IAny value) {
throw new ZenRuntimeException("cannot index an int value");
}
@Override
public IAny call(IAny... values) {
throw new ZenRuntimeException("cannot call an int value");
}
@Override
public boolean asBool() {
throw new ZenRuntimeException("cannot convert an int to a bool");
}
@Override
public byte asByte() {
return (byte) value;
}
@Override
public short asShort() {
return (short) value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return value;
}
@Override
public String asString() {
return Integer.toString(value);
}
@Override
public <T> T as(Class<T> cls) {
throw new ZenRuntimeException("cannot convert a byte to an object");
}
@Override
public Iterator<IAny> iteratorSingle() {
throw new ZenRuntimeException("cannot iterate over a byte value");
}
@Override
public Iterator<IAny[]> iteratorMulti(int n) {
throw new ZenRuntimeException("cannot iterate over a byte value");
}
}
@@ -1,181 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stanhebben.zenscript.value.builtin;
import java.util.Iterator;
import stanhebben.zenscript.ZenRuntimeException;
import stanhebben.zenscript.value.IAny;
import stanhebben.zenscript.value.IntRange;
/**
*
* @author Stanneke
*/
public class AnyIntRange implements IAny {
private IntRange value;
public AnyIntRange(IntRange value) {
this.value = value;
}
@Override
public IAny not() {
throw new ZenRuntimeException("Cannot invert an int range");
}
@Override
public IAny neg() {
throw new ZenRuntimeException("Cannot negate an int range");
}
@Override
public IAny add(IAny value) {
throw new ZenRuntimeException("Cannot add int ranges");
}
@Override
public IAny sub(IAny value) {
throw new ZenRuntimeException("Cannot subtract int ranges");
}
@Override
public IAny cat(IAny value) {
throw new ZenRuntimeException("Cannot concatenate int ranges");
}
@Override
public IAny mul(IAny value) {
throw new ZenRuntimeException("Cannot multiply int ranges");
}
@Override
public IAny div(IAny value) {
throw new ZenRuntimeException("Cannot divide int ranges");
}
@Override
public IAny mod(IAny value) {
throw new ZenRuntimeException("Cannot perform modulo on int ranges");
}
@Override
public IAny and(IAny value) {
throw new ZenRuntimeException("Cannot perform and on int ranges");
}
@Override
public IAny or(IAny value) {
throw new ZenRuntimeException("Cannot perform or on int ranges");
}
@Override
public IAny xor(IAny value) {
throw new ZenRuntimeException("Cannot perform xor on int ranges");
}
@Override
public IAny range(IAny value) {
throw new ZenRuntimeException("Already a range!");
}
@Override
public int compareTo(IAny value) {
throw new ZenRuntimeException("Cannot compare ranges");
}
@Override
public boolean contains(IAny value) {
int ivalue = value.asInt();
return ivalue >= this.value.getFrom() && ivalue < this.value.getTo();
}
@Override
public IAny member(String value) {
if (value.equals("from")) {
return new AnyInt(this.value.getFrom());
} else if (value.equals("to")) {
return new AnyInt(this.value.getTo());
} else {
throw new ZenRuntimeException("No member " + value + " in int range");
}
}
@Override
public IAny indexGet(IAny key) {
int ivalue = key.asInt();
if (ivalue < value.getTo() - value.getFrom()) {
return new AnyInt(this.value.getFrom() + ivalue);
} else {
throw new ArrayIndexOutOfBoundsException("int range index out of bounds: " + ivalue);
}
}
@Override
public void indexSet(IAny key, IAny value) {
throw new ZenRuntimeException("Cannot modify int ranges");
}
@Override
public IAny call(IAny... values) {
throw new ZenRuntimeException("Cannot call int ranges");
}
@Override
public boolean asBool() {
throw new ZenRuntimeException("Cannot cast int range to bool");
}
@Override
public byte asByte() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public short asShort() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int asInt() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public long asLong() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public float asFloat() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public double asDouble() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String asString() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public <T> T as(Class<T> cls) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Iterator<IAny> iteratorSingle() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Iterator<IAny[]> iteratorMulti(int n) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
@@ -1,192 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stanhebben.zenscript.value.builtin;
import java.util.Iterator;
import stanhebben.zenscript.ZenRuntimeException;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stanneke
*/
public class AnyString implements IAny {
private final String value;
public AnyString(String value) {
this.value = value;
}
@Override
public IAny not() {
throw new ZenRuntimeException("cannot invert a string value");
}
@Override
public IAny neg() {
throw new ZenRuntimeException("cannot negate a string value");
}
@Override
public IAny add(IAny value) {
throw new ZenRuntimeException("cannot add to a string value (did you intend to use ~ instead?)");
}
@Override
public IAny sub(IAny value) {
throw new ZenRuntimeException("cannot subtract from a string");
}
@Override
public IAny cat(IAny value) {
return new AnyString(this.value + value.asString());
}
@Override
public IAny mul(IAny value) {
throw new ZenRuntimeException("cannot multiply a string value");
}
@Override
public IAny div(IAny value) {
throw new ZenRuntimeException("cannot divide a string value");
}
@Override
public IAny mod(IAny value) {
throw new ZenRuntimeException("cannot modulo a string value");
}
@Override
public IAny and(IAny value) {
throw new ZenRuntimeException("cannot and with a string value");
}
@Override
public IAny or(IAny value) {
throw new ZenRuntimeException("cannot or with a string value");
}
@Override
public IAny xor(IAny value) {
throw new ZenRuntimeException("cannot xor with a string value");
}
@Override
public IAny range(IAny value) {
throw new ZenRuntimeException("cannot range a string value");
}
@Override
public int compareTo(IAny value) {
return this.value.compareTo(value.asString());
}
@Override
public boolean contains(IAny value) {
return this.value.indexOf(value.asString()) >= 0;
}
@Override
public IAny member(String value) {
throw new ZenRuntimeException("strings have no members (yet)");
}
@Override
public IAny indexGet(IAny key) {
throw new ZenRuntimeException("cannot index a string (yet)");
}
@Override
public void indexSet(IAny key, IAny value) {
throw new ZenRuntimeException("cannot modify a string");
}
@Override
public IAny call(IAny... values) {
throw new ZenRuntimeException("cannot call a string");
}
@Override
public boolean asBool() {
return value != null;
}
@Override
public byte asByte() {
try {
return Byte.parseByte(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to a byte value");
}
}
@Override
public short asShort() {
try {
return Short.parseShort(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to a short value");
}
}
@Override
public int asInt() {
try {
return Integer.parseInt(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to an int value");
}
}
@Override
public long asLong() {
try {
return Long.parseLong(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to a long value");
}
}
@Override
public float asFloat() {
try {
return Float.parseFloat(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to a float value");
}
}
@Override
public double asDouble() {
try {
return Double.parseDouble(value);
} catch (NumberFormatException ex) {
throw new ZenRuntimeException("Cannot convert " + value + " to a double value");
}
}
@Override
public String asString() {
return value;
}
@Override
public <T> T as(Class<T> cls) {
throw new ZenRuntimeException("Cannot convert a string to an object");
}
@Override
public Iterator<IAny> iteratorSingle() {
throw new ZenRuntimeException("Cannot iterate over a string (yet)");
}
@Override
public Iterator<IAny[]> iteratorMulti(int n) {
throw new ZenRuntimeException("Cannot iterate over a string (yet)");
}
}