mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-17 10:55:04 +00:00
- Basic types can now be converted to the "any" type
- Changed OreDict 'contains' to respond to the 'in' operator
This commit is contained in:
1 parent
52f92dcfd4
commit
1b72abb2b4
59 files changed
+3595
-976
No files matched your search
@@ -7,6 +7,8 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.annotations.ZenCaster;
|
||||
import stanhebben.zenscript.annotations.ZenGetter;
|
||||
@@ -23,6 +25,7 @@ import stanhebben.zenscript.type.expand.ZenExpandCaster;
|
||||
import stanhebben.zenscript.type.expand.ZenExpandMember;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.type.natives.ZenNativeOperator;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -179,6 +182,7 @@ public class TypeExpansion {
|
||||
return caster;
|
||||
}
|
||||
}
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
if (caster.getTarget().canCastImplicit(type, environment)) {
|
||||
return caster;
|
||||
@@ -305,6 +309,71 @@ public class TypeExpansion {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void compileAnyCast(ZenType type, MethodOutput output, IEnvironmentGlobal environment, int localValue, int localClass) {
|
||||
if (type == null)
|
||||
throw new IllegalArgumentException("type cannot be null");
|
||||
Type asmType = type.toASMType();
|
||||
if (asmType == null) {
|
||||
throw new RuntimeException("type has no asm type");
|
||||
}
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.constant(caster.getTarget().toASMType());
|
||||
output.ifACmpNe(skip);
|
||||
output.load(asmType, localValue);
|
||||
caster.compile(output);
|
||||
output.returnType(caster.getTarget().toASMType());
|
||||
output.label(skip);
|
||||
}
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
String casterAny = caster.getTarget().getAnyClassName(environment);
|
||||
if (casterAny == null)
|
||||
// TODO: make sure this isn't necessary
|
||||
continue;
|
||||
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.invokeStatic(casterAny, "rtCanCastImplicit", "(Ljava/lang/Class;)Z");
|
||||
output.ifEQ(skip);
|
||||
output.load(type.toASMType(), localValue);
|
||||
caster.compile(output);
|
||||
output.invokeStatic(casterAny, "rtAs", "(Ljava/lang/Class;)Z");
|
||||
output.returnType(caster.getTarget().toASMType());
|
||||
output.label(skip);
|
||||
}
|
||||
}
|
||||
|
||||
public void compileAnyCanCastImplicit(ZenType type, MethodOutput output, IEnvironmentGlobal environment, int localClass) {
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.constant(caster.getTarget().toASMType());
|
||||
output.ifACmpNe(skip);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
output.label(skip);
|
||||
}
|
||||
|
||||
for (ZenExpandCaster caster : casters) {
|
||||
String casterAny = caster.getTarget().getAnyClassName(environment);
|
||||
if (casterAny == null) {
|
||||
// TODO: make sure no type ever does this
|
||||
continue;
|
||||
}
|
||||
|
||||
Label skip = new Label();
|
||||
output.loadObject(localClass);
|
||||
output.invokeStatic(casterAny, "rtCanCastImplicit", "(Ljava/lang/Class;)Z");
|
||||
output.ifEQ(skip);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
output.label(skip);
|
||||
}
|
||||
}
|
||||
|
||||
// #######################
|
||||
// ### Private methods ###
|
||||
// #######################
|
||||
|
||||
@@ -23,18 +23,14 @@ import stanhebben.zenscript.compiler.EnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.Import;
|
||||
import stanhebben.zenscript.definitions.ParsedFunction;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.statements.StatementReturn;
|
||||
import stanhebben.zenscript.symbols.IZenSymbol;
|
||||
import stanhebben.zenscript.symbols.SymbolArgument;
|
||||
import stanhebben.zenscript.symbols.SymbolZenStaticMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.StringUtil;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
|
||||
/**
|
||||
@@ -187,7 +183,10 @@ public class ZenModule {
|
||||
outputDir.mkdir();
|
||||
|
||||
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
|
||||
File outputFile = new File(outputDir, entry.getKey() + ".class");
|
||||
File outputFile = new File(outputDir, entry.getKey().replace('.', '/') + ".class");
|
||||
if (!outputFile.getParentFile().exists()) {
|
||||
outputFile.getParentFile().mkdirs();
|
||||
}
|
||||
FileOutputStream output = new FileOutputStream(outputFile);
|
||||
output.write(entry.getValue());
|
||||
output.close();
|
||||
@@ -246,7 +245,7 @@ public class ZenModule {
|
||||
outputDir.mkdir();
|
||||
|
||||
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
|
||||
File outputFile = new File(outputDir, entry.getKey() + ".class");
|
||||
File outputFile = new File(outputDir, entry.getKey().replace('.', '/') + ".class");
|
||||
FileOutputStream output = new FileOutputStream(outputFile);
|
||||
output.write(entry.getValue());
|
||||
output.close();
|
||||
|
||||
@@ -11,7 +11,6 @@ import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeArrayBasic;
|
||||
@@ -34,6 +33,7 @@ import stanhebben.zenscript.type.ZenTypeShort;
|
||||
import stanhebben.zenscript.type.ZenTypeShortObject;
|
||||
import stanhebben.zenscript.type.ZenTypeString;
|
||||
import stanhebben.zenscript.type.ZenTypeVoid;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -45,7 +45,7 @@ public class TypeRegistry implements ITypeRegistry {
|
||||
public TypeRegistry() {
|
||||
types = new HashMap<Class, ZenType>();
|
||||
|
||||
types.put(boolean.class, ZenTypeBool.INSTANCE);
|
||||
types.put(boolean.class, ZenType.BOOL);
|
||||
types.put(byte.class, ZenTypeByte.INSTANCE);
|
||||
types.put(short.class, ZenTypeShort.INSTANCE);
|
||||
types.put(int.class, ZenTypeInt.INSTANCE);
|
||||
@@ -62,6 +62,8 @@ public class TypeRegistry implements ITypeRegistry {
|
||||
types.put(Float.class, ZenTypeFloatObject.INSTANCE);
|
||||
types.put(Double.class, ZenTypeDoubleObject.INSTANCE);
|
||||
|
||||
types.put(IAny.class, ZenTypeAny.INSTANCE);
|
||||
|
||||
types.put(String.class, ZenTypeString.INSTANCE);
|
||||
types.put(List.class, new ZenTypeArrayBasic(ZenTypeAny.INSTANCE));
|
||||
}
|
||||
|
||||
@@ -33,18 +33,15 @@ public abstract class Expression implements IPartialExpression {
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (getType() == type) {
|
||||
return this;
|
||||
} else if (getType().canCastImplicit(type, environment)) {
|
||||
return new ExpressionAs(position, this, type);
|
||||
} else {
|
||||
environment.error(position, "Cannot cast " + getType() + " to " + type);
|
||||
return new ExpressionInvalid(position, type);
|
||||
return getType().cast(position, environment, this, type);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void compile(boolean result, IEnvironmentMethod environment);
|
||||
|
||||
public void compileIf(Label onElse, IEnvironmentMethod environment) {
|
||||
if (getType() == ZenTypeBool.INSTANCE) {
|
||||
if (getType() == ZenType.BOOL) {
|
||||
compile(true, environment);
|
||||
environment.getOutput().ifEQ(onElse);
|
||||
} else if (getType().isPointer()) {
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ public class ExpressionArithmeticBinary extends Expression {
|
||||
ZenType type = a.getType();
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
if (type == ZenTypeBool.INSTANCE) {
|
||||
if (type == ZenType.BOOL) {
|
||||
switch (operator) {
|
||||
case AND:
|
||||
output.iAnd();
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
return ZenType.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,7 +39,7 @@ public class ExpressionArithmeticCompare extends Expression {
|
||||
|
||||
if (result) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (a.getType() == ZenTypeBool.INSTANCE) {
|
||||
if (a.getType() == ZenType.BOOL) {
|
||||
if (type == CompareType.EQ) {
|
||||
Label onThen = new Label();
|
||||
Label onEnd = new Label();
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ public class ExpressionArithmeticUnary extends Expression {
|
||||
MethodOutput output = environment.getOutput();
|
||||
if (result) {
|
||||
ZenType type = base.getType();
|
||||
if (type == ZenTypeBool.INSTANCE) {
|
||||
if (type == ZenType.BOOL) {
|
||||
if (operator == OperatorType.NOT) {
|
||||
output.iNot();
|
||||
return;
|
||||
|
||||
@@ -18,10 +18,7 @@ public class ExpressionAs extends Expression {
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (!this.type.canCastExplicit(type, environment)) {
|
||||
environment.error(position, "cannot cast " + this.type + " to " + type);
|
||||
}
|
||||
return new ExpressionAs(getPosition(), this, type);
|
||||
return this.type.cast(position, environment, this, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,12 +7,9 @@
|
||||
package stanhebben.zenscript.expression;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import stanhebben.zenscript.compiler.EnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.compiler.EnvironmentMethod;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeBool;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -30,25 +27,22 @@ public class ExpressionBool extends Expression {
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
|
||||
if (type == ZenTypeBool.INSTANCE) {
|
||||
if (type == ZenType.BOOL) {
|
||||
return this;
|
||||
} else if (type.canCastExplicit(type, environment)) {
|
||||
return new ExpressionAs(position, this, type);
|
||||
} else {
|
||||
environment.error(position, "Cannot cast a bool constant to " + type.toString());
|
||||
return new ExpressionInvalid(position, type);
|
||||
return ZenType.BOOL.cast(position, environment, this, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
|
||||
environment.error(position, "Bool constants do not have members");
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
return new ExpressionInvalid(position, ZenType.BOOL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
return ZenType.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ public class ExpressionCompareGeneric extends Expression {
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
return ZenType.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ExpressionFloat extends Expression {
|
||||
} else if (type == ZenTypeDouble.INSTANCE) {
|
||||
environment.getOutput().constant(value);
|
||||
} else {
|
||||
// TODO: internal error
|
||||
throw new RuntimeException("Internal compiler error: source type is not a floating point type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class ExpressionInt extends Expression {
|
||||
} else if (type == ZenTypeLong.INSTANCE) {
|
||||
output.constant(value);
|
||||
} else {
|
||||
// TODO: internal error
|
||||
throw new RuntimeException("Internal compiler error: int constant type is not an int");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class ExpressionMapContains extends Expression {
|
||||
|
||||
@Override
|
||||
public ZenType getType() {
|
||||
return ZenTypeBool.INSTANCE;
|
||||
return ZenType.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,7 +20,7 @@ public class ParseException extends RuntimeException {
|
||||
private final String message;
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super(token == null ? "Error at end of file - " + error : "Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
this.file = token.getPosition().getFile();
|
||||
this.line = token.getPosition().getLine();
|
||||
@@ -44,11 +44,11 @@ public class ParseException extends RuntimeException {
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return token == null ? -1 : token.getPosition().getLine();
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return token == null ? -1 : token.getPosition().getLineOffset();
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
|
||||
@@ -5,13 +5,17 @@
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
|
||||
/**
|
||||
* Thrown when an exception occurs while tokening a character stream.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class TokenException extends RuntimeException {
|
||||
public TokenException(int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + line + ":" + lineOffset + " - " + value);
|
||||
public TokenException(ZenParsedFile file, int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + file + ":" + line + " - " + value);
|
||||
|
||||
if (line < 0) throw new IllegalArgumentException("Line cannot be negative");
|
||||
}
|
||||
}
|
||||
@@ -220,15 +220,18 @@ public class TokenStream implements Iterator<Token> {
|
||||
lineOffset = reader.lineOffset;
|
||||
nextChar = reader.read();
|
||||
}
|
||||
|
||||
if (line < 0) throw new IllegalStateException("Line cannot be negative");
|
||||
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0) throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
if (state == 0) throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
if (nextChar < 0 && value.length() == 0) {
|
||||
return; // happens on comments at the end of files
|
||||
}
|
||||
System.out.println("Value: " + value.length() + " characters: " + value);
|
||||
throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
//System.out.println("Value: " + value.length() + " characters: " + value);
|
||||
throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
@@ -28,7 +28,7 @@ public class StatementIf extends Statement {
|
||||
|
||||
Expression cCondition = condition.compile(environment).eval(environment);
|
||||
ZenType expressionType = cCondition.getType();
|
||||
if (expressionType.canCastImplicit(ZenTypeBool.INSTANCE, environment)) {
|
||||
if (expressionType.canCastImplicit(ZenType.BOOL, environment)) {
|
||||
Label labelEnd = new Label();
|
||||
Label labelElse = onElse == null ? labelEnd : new Label();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
public abstract class ZenType {
|
||||
public static final ZenTypeAny ANY = ZenTypeAny.INSTANCE;
|
||||
public static final ZenTypeBool BOOL = ZenTypeBool.INSTANCE;
|
||||
public static final ZenTypeBool BOOL = new ZenTypeBool();
|
||||
public static final ZenTypeByte BYTE = ZenTypeByte.INSTANCE;
|
||||
public static final ZenTypeShort SHORT = ZenTypeShort.INSTANCE;
|
||||
public static final ZenTypeInt INT = ZenTypeInt.INSTANCE;
|
||||
@@ -185,6 +185,8 @@ public abstract class ZenType {
|
||||
|
||||
public abstract void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type);
|
||||
|
||||
public abstract String getAnyClassName(IEnvironmentGlobal environment);
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public abstract Expression defaultValue(ZenPosition position);
|
||||
@@ -282,7 +284,7 @@ public abstract class ZenType {
|
||||
if (expansion != null) {
|
||||
ZenExpandCaster caster = expansion.getCaster(toType, environment);
|
||||
if (caster != null) {
|
||||
caster.compile(environment);
|
||||
caster.compile(environment.getOutput());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
/*
|
||||
* 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.type;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -17,9 +11,11 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionCompareGeneric;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionJavaCallVirtual;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.*;
|
||||
import stanhebben.zenscript.util.MethodCompiler;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
@@ -79,6 +75,12 @@ public class ZenTypeAny extends ZenType {
|
||||
output.invokeInterface(IAny.class, "asDouble", double.class);
|
||||
}
|
||||
});
|
||||
CASTERS.put("string", new MethodCompiler() {
|
||||
@Override
|
||||
public void compile(MethodOutput output) {
|
||||
output.invokeInterface(IAny.class, "asString", String.class);
|
||||
}
|
||||
});
|
||||
CASTERS.put(Boolean.class.getName(), new MethodCompiler() {
|
||||
@Override
|
||||
public void compile(MethodOutput output) {
|
||||
@@ -152,7 +154,7 @@ public class ZenTypeAny extends ZenType {
|
||||
|
||||
@Override
|
||||
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
|
||||
return null; // TODO: how to handle iteration on any-values
|
||||
return null; // TODO: handle iteration on any-values
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,165 +206,62 @@ public class ZenTypeAny extends ZenType {
|
||||
|
||||
@Override
|
||||
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
|
||||
//try {
|
||||
switch (operator) {
|
||||
/*case NEG:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("neg"),
|
||||
ZenTypeAny.INSTANCE,
|
||||
value);
|
||||
case NOT:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("not"),
|
||||
ZenTypeAny.INSTANCE,
|
||||
value);*/
|
||||
default:
|
||||
return new ExpressionInvalid(position, ZenTypeAny.INSTANCE);
|
||||
}
|
||||
/*} catch (NoSuchMethodException ex) {
|
||||
throw new RuntimeException("could not find method", ex);
|
||||
}*/
|
||||
switch (operator) {
|
||||
case NEG:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_NEG, value);
|
||||
case NOT:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_NOT, value);
|
||||
default:
|
||||
return new ExpressionInvalid(position, ZenTypeAny.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
|
||||
//try {
|
||||
switch (operator) {
|
||||
/*case ADD:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("add", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case CAT:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("cat", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case SUB:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("sub", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case MUL:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("mul", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case DIV:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("div", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case MOD:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("mod", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case AND:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("and", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case OR:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("or", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case XOR:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("xor", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case CONTAINS:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("contains", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case INDEXGET:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("indexGet", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case RANGE:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("range", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));
|
||||
case COMPARE:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("compareTo", IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
left,
|
||||
right.cast(position, environment, ANY));*/
|
||||
default:
|
||||
return new ExpressionInvalid(position, ANY);
|
||||
}
|
||||
/*} catch (NoSuchMethodException ex) {
|
||||
throw new RuntimeException("method not found", ex);
|
||||
}*/
|
||||
switch (operator) {
|
||||
case ADD:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_ADD, left, right.cast(position, environment, ANY));
|
||||
case CAT:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_CAT, left, right.cast(position, environment, ANY));
|
||||
case SUB:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_SUB, left, right.cast(position, environment, ANY));
|
||||
case MUL:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_MUL, left, right.cast(position, environment, ANY));
|
||||
case DIV:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_DIV, left, right.cast(position, environment, ANY));
|
||||
case MOD:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_MOD, left, right.cast(position, environment, ANY));
|
||||
case AND:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_AND, left, right.cast(position, environment, ANY));
|
||||
case OR:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_OR, left, right.cast(position, environment, ANY));
|
||||
case XOR:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_XOR, left, right.cast(position, environment, ANY));
|
||||
case CONTAINS:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_CONTAINS, left, right.cast(position, environment, ANY));
|
||||
case INDEXGET:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_INDEXGET, left, right.cast(position, environment, ANY));
|
||||
case RANGE:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_RANGE, left, right.cast(position, environment, ANY));
|
||||
case COMPARE:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_COMPARETO, left, right.cast(position, environment, ANY));
|
||||
case MEMBERGETTER:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_MEMBERGET, left, right.cast(position, environment, STRING));
|
||||
default:
|
||||
return new ExpressionInvalid(position, ANY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression trinary(ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
|
||||
//try {
|
||||
switch (operator) {
|
||||
/*case INDEXSET:
|
||||
return new ExpressionJavaCallVirtual(
|
||||
position,
|
||||
IAny.class,
|
||||
IAny.class.getMethod("indexSet", IAny.class, IAny.class),
|
||||
ZenTypeAny.INSTANCE,
|
||||
first,
|
||||
second.cast(position, environment, ANY),
|
||||
third.cast(position, environment, ANY));*/
|
||||
default:
|
||||
return new ExpressionInvalid(position, ANY);
|
||||
}
|
||||
/*} catch (NoSuchMethodException ex) {
|
||||
throw new RuntimeException("method not found", ex);
|
||||
}*/
|
||||
switch (operator) {
|
||||
case INDEXSET:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_INDEXSET, first, second.cast(position, environment, ANY), third.cast(position, environment, ANY));
|
||||
case MEMBERSETTER:
|
||||
return new ExpressionJavaCallVirtual(position, METHOD_MEMBERSET, first, second.cast(position, environment, STRING), third.cast(position, environment, ANY));
|
||||
default:
|
||||
return new ExpressionInvalid(position, ANY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -399,6 +298,11 @@ public class ZenTypeAny extends ZenType {
|
||||
public String getName() {
|
||||
return "any";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
throw new UnsupportedOperationException("Cannot get any class name from the any type. That's like trying to stuff a freezer into a freezer.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -57,6 +57,11 @@ public class ZenTypeArrayBasic extends ZenTypeArray {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type toASMType() {
|
||||
|
||||
@@ -59,6 +59,11 @@ public class ZenTypeArrayList extends ZenTypeArray {
|
||||
public Class toJavaClass() {
|
||||
return List.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type toASMType() {
|
||||
|
||||
@@ -51,6 +51,11 @@ public class ZenTypeAssociative extends ZenType {
|
||||
public ZenType getKeyType() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,13 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionBool;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.BYTE;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBOOL;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeBool extends ZenType {
|
||||
public static final ZenTypeBool INSTANCE = new ZenTypeBool();
|
||||
//private static final JavaMethod BOOL_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "toString", boolean.class);
|
||||
private static final String ANY_NAME = "any/AnyBool";
|
||||
private static final String ANY_NAME_2 = "any.AnyBool";
|
||||
private static final String ANY_NAME_DESC = "Lany/AnyBool;";
|
||||
|
||||
private ZenTypeBool() {}
|
||||
public ZenTypeBool() {}
|
||||
|
||||
@Override
|
||||
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
|
||||
@@ -28,17 +47,17 @@ public class ZenTypeBool extends ZenType {
|
||||
|
||||
@Override
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type == INSTANCE || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
|
||||
return type == BOOL || type == ANY || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type == INSTANCE || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
|
||||
return type == BOOL || type == ANY || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
|
||||
if (type == BOOL || type == ZenTypeBoolObject.INSTANCE || type == STRING) {
|
||||
if (type == BOOL || type == ZenTypeBoolObject.INSTANCE || type == STRING || type == ANY) {
|
||||
return new ExpressionAs(position, value, type);
|
||||
} else if (canCastExpansion(environment, type)) {
|
||||
return castExpansion(position, environment, value, type);
|
||||
@@ -69,23 +88,23 @@ public class ZenTypeBool extends ZenType {
|
||||
|
||||
@Override
|
||||
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
|
||||
if (right.getType().canCastImplicit(ZenTypeBool.INSTANCE, environment)) {
|
||||
if (right.getType().canCastImplicit(BOOL, environment)) {
|
||||
switch (operator) {
|
||||
case AND:
|
||||
case OR:
|
||||
case XOR:
|
||||
if (right.getType() != ZenTypeBool.INSTANCE) {
|
||||
right = right.cast(position, environment, ZenTypeBool.INSTANCE);
|
||||
if (right.getType() != BOOL) {
|
||||
right = right.cast(position, environment, BOOL);
|
||||
}
|
||||
|
||||
return new ExpressionArithmeticBinary(position, operator, left, right);
|
||||
default:
|
||||
environment.error(position, "unsupported bool operator: " + operator);
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
return new ExpressionInvalid(position, BOOL);
|
||||
}
|
||||
} else {
|
||||
environment.error(right.getPosition(), "not a valid bool value");
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
return new ExpressionInvalid(position, BOOL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +112,7 @@ public class ZenTypeBool extends ZenType {
|
||||
public Expression trinary(
|
||||
ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
|
||||
environment.error(position, "operation not supported on a bool value");
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
return new ExpressionInvalid(position, BOOL);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,7 +122,7 @@ public class ZenTypeBool extends ZenType {
|
||||
return new ExpressionArithmeticCompare(position, type, left, right);
|
||||
} else {
|
||||
environment.error(position, "such comparison not supported on a bool");
|
||||
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
|
||||
return new ExpressionInvalid(position, BOOL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +168,8 @@ public class ZenTypeBool extends ZenType {
|
||||
environment.getOutput().invokeStatic(Boolean.class, "valueOf", Boolean.class, boolean.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeStatic(Boolean.TYPE, "toString", String.class, boolean.class);
|
||||
} else if (type == ANY) {
|
||||
environment.getOutput().invokeStatic(getAnyClassName(environment), "valueOf", "(Z)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "Cannot compile bool to " + type);
|
||||
}
|
||||
@@ -158,9 +179,345 @@ public class ZenTypeBool extends ZenType {
|
||||
public String getName() {
|
||||
return "bool";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionBool(environment), ANY_NAME, Type.BOOLEAN_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionBool(position, false);
|
||||
}
|
||||
|
||||
private class AnyDefinitionBool implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionBool(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "TRUE", ANY_NAME_DESC, null, null);
|
||||
output.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "FALSE", ANY_NAME_DESC, null, null);
|
||||
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "Z", null, null);
|
||||
|
||||
MethodOutput clinit = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
|
||||
clinit.start();
|
||||
clinit.newObject(ANY_NAME);
|
||||
clinit.dup();
|
||||
clinit.iConst0();
|
||||
clinit.invokeSpecial(ANY_NAME, "<init>", "(Z)V");
|
||||
clinit.putStaticField(ANY_NAME, "FALSE", ANY_NAME_DESC);
|
||||
clinit.newObject(ANY_NAME);
|
||||
clinit.dup();
|
||||
clinit.iConst1();
|
||||
clinit.invokeSpecial(ANY_NAME, "<init>", "(Z)V");
|
||||
clinit.putStaticField(ANY_NAME, "TRUE", ANY_NAME_DESC);
|
||||
clinit.returnType(Type.VOID_TYPE);
|
||||
clinit.end();
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(Z)" + signature(IAny.class), null, null);
|
||||
Label lblFalse = new Label();
|
||||
valueOf.start();
|
||||
valueOf.load(Type.BOOLEAN_TYPE, 0);
|
||||
valueOf.ifEQ(lblFalse);
|
||||
valueOf.getStaticField(ANY_NAME, "TRUE", ANY_NAME_DESC);
|
||||
valueOf.returnObject();
|
||||
valueOf.label(lblFalse);
|
||||
valueOf.getStaticField(ANY_NAME, "FALSE", ANY_NAME_DESC);
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(Z)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.BOOLEAN_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "Z");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BOOLEAN_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(BOOL, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(BOOL, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "bool", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.iNot();
|
||||
valueOf(output);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "negate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "+");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "-");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, boolean.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "%");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBOOL);
|
||||
output.iAnd();
|
||||
valueOf(output);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBOOL);
|
||||
output.iOr();
|
||||
valueOf(output);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iXor();
|
||||
valueOf(output);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "range");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBOOL);
|
||||
output.iSub();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
throwCastException(output, "bool", "byte");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
throwCastException(output, "bool", "short");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
throwCastException(output, "bool", "int");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
throwCastException(output, "bool", "long");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
throwCastException(output, "bool", "float");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
throwCastException(output, "bool", "double");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Boolean.class, "toString", String.class, boolean.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.BYTE_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.BYTE_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(BYTE, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "bool", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.BOOLEAN_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "bool", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "Z");
|
||||
}
|
||||
|
||||
private void valueOf(MethodOutput output) {
|
||||
output.invokeStatic(ANY_NAME, "valueOf", "(Z)" + signature(IAny.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -17,8 +18,10 @@ import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -31,7 +34,7 @@ public class ZenTypeBoolObject extends ZenType {
|
||||
|
||||
@Override
|
||||
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
|
||||
return BOOL.unary(position, environment, value.cast(position, environment, ZenTypeBool.INSTANCE), operator);
|
||||
return BOOL.unary(position, environment, value.cast(position, environment, ZenType.BOOL), operator);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,7 +49,7 @@ public class ZenTypeBoolObject extends ZenType {
|
||||
|
||||
@Override
|
||||
public Expression compare(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, CompareType type) {
|
||||
return BOOL.compare(position, environment, left.cast(position, environment, ZenTypeBool.INSTANCE), right, type);
|
||||
return BOOL.compare(position, environment, left.cast(position, environment, BOOL), right, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,6 +115,22 @@ public class ZenTypeBoolObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Boolean.class, "booleanValue", boolean.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Boolean.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Boolean.class, "booleanValue", boolean.class);
|
||||
output.invokeStatic(BOOL.getAnyClassName(environment), "valueOf", "(Z)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Boolean.class, "booleanValue", boolean.class);
|
||||
BOOL.compileCast(position, environment, type);
|
||||
@@ -122,6 +141,11 @@ public class ZenTypeBoolObject extends ZenType {
|
||||
public String getName() {
|
||||
return "bool";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return BOOL.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,14 +17,25 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import static stanhebben.zenscript.type.ZenTypeAny.*;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import stanhebben.zenscript.value.builtin.AnyByte;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeByte extends ZenType {
|
||||
public static final ZenTypeByte INSTANCE = new ZenTypeByte();
|
||||
|
||||
private static final String ANY_NAME = "any/AnyByte";
|
||||
private static final String ANY_NAME_2 = "any.AnyByte";
|
||||
|
||||
private ZenTypeByte() {}
|
||||
|
||||
@Override
|
||||
@@ -33,6 +48,7 @@ public class ZenTypeByte extends ZenType {
|
||||
int itype = type.getNumberType();
|
||||
return itype != 0
|
||||
|| type == STRING
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -40,12 +56,13 @@ public class ZenTypeByte extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == STRING
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
|
||||
if (type.getNumberType() > 0 || type == STRING) {
|
||||
if (type.getNumberType() > 0 || type == STRING || type == ANY) {
|
||||
return new ExpressionAs(position, value, type);
|
||||
} else if (canCastExpansion(environment, type)) {
|
||||
return castExpansion(position, environment, value, type);
|
||||
@@ -73,7 +90,7 @@ public class ZenTypeByte extends ZenType {
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
|
||||
IPartialExpression result = memberExpansion(position, environment, value.eval(environment), name);
|
||||
if (result == null) {
|
||||
environment.error(position, "bool value has no members");
|
||||
environment.error(position, "bool value has no member named " + name);
|
||||
return new ExpressionInvalid(position, ZenTypeAny.INSTANCE);
|
||||
} else {
|
||||
return result;
|
||||
@@ -129,7 +146,7 @@ public class ZenTypeByte extends ZenType {
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Byte.class, "toString", String.class, byte.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(AnyByte.class, "valueOf", AnyByte.class, byte.class);
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(B)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -167,9 +184,393 @@ public class ZenTypeByte extends ZenType {
|
||||
public String getName() {
|
||||
return "byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionByte(environment), ANY_NAME, Type.BYTE_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionInt(position, 0, ZenType.BYTE);
|
||||
}
|
||||
|
||||
private class AnyDefinitionByte implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionByte(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "B", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(B)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.BYTE_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "B");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(B)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.BYTE_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "B");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(BYTE, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(BYTE, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "byte", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNot();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iAnd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iOr();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iXor();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iSub();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2l();
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2f();
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2d();
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Byte.class, "toString", String.class, byte.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.BYTE_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.BYTE_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(BYTE, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "byte", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_BYTE);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "byte", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "B");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeByteObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Byte.class, "byteValue", byte.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Byte.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Byte.class, "byteValue", byte.class);
|
||||
output.invokeStatic(BYTE.getAnyClassName(environment), "valueOf", "(B)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Byte.class, "byteValue", byte.class);
|
||||
BYTE.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeByteObject extends ZenType {
|
||||
public String getName() {
|
||||
return "byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return BYTE.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,13 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionFloat;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.FLOAT;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASDOUBLE;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeDouble extends ZenType {
|
||||
public static final ZenTypeDouble INSTANCE = new ZenTypeDouble();
|
||||
|
||||
//private static final JavaMethod DOUBLE_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Double.class, "toString", double.class);
|
||||
private static final String ANY_NAME = "any/AnyDouble";
|
||||
private static final String ANY_NAME_2 = "any.AnyDouble";
|
||||
|
||||
private ZenTypeDouble() {}
|
||||
|
||||
@Override
|
||||
@@ -31,6 +50,7 @@ public class ZenTypeDouble extends ZenType {
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_FLOAT)
|
||||
|| type == STRING
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -38,6 +58,7 @@ public class ZenTypeDouble extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == STRING
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -132,6 +153,8 @@ public class ZenTypeDouble extends ZenType {
|
||||
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Double.class, "toString", String.class, double.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(D)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -170,6 +193,16 @@ public class ZenTypeDouble extends ZenType {
|
||||
return "double";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionDouble(environment), ANY_NAME, Type.DOUBLE_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLarge() {
|
||||
return true;
|
||||
@@ -179,4 +212,355 @@ public class ZenTypeDouble extends ZenType {
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionFloat(position, 0.0, DOUBLE);
|
||||
}
|
||||
|
||||
private class AnyDefinitionDouble implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionDouble(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "D", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(D)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.DOUBLE_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "D");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(D)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.DOUBLE_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "D");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(FLOAT, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(DOUBLE, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "double", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "not");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.dNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.dAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.dSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, double.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.dMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.dDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.dRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "and");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "or");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "xor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "range");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
// return Double.compare(x, y)
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASDOUBLE);
|
||||
output.invokeStatic(Float.class, "compare", int.class, double.class, double.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.d2i();
|
||||
output.i2b();
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.d2i();
|
||||
output.i2s();
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.d2i();
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.d2l();
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.d2f();
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Double.class, "toString", String.class, double.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.DOUBLE_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.DOUBLE_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(DOUBLE, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "double", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_DOUBLE);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "double", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "D");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeDoubleObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Double.class, "doubleValue", double.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Double.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Double.class, "doubleValue", double.class);
|
||||
output.invokeStatic(DOUBLE.getAnyClassName(environment), "valueOf", "(D)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Double.class, "doubleValue", double.class);
|
||||
DOUBLE.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeDoubleObject extends ZenType {
|
||||
public String getName() {
|
||||
return "double";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return DOUBLE.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,13 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionFloat;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.LONG;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASFLOAT;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeFloat extends ZenType {
|
||||
public static final ZenTypeFloat INSTANCE = new ZenTypeFloat();
|
||||
|
||||
//private static final JavaMethod FLOAT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Float.class, "toString", float.class);
|
||||
private static final String ANY_NAME = "any/AnyFloat";
|
||||
private static final String ANY_NAME_2 = "any.AnyFloat";
|
||||
|
||||
private ZenTypeFloat() {}
|
||||
|
||||
@Override
|
||||
@@ -29,8 +48,9 @@ public class ZenTypeFloat extends ZenType {
|
||||
|
||||
@Override
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_FLOAT)
|
||||
return (type.getNumberType() != 0)
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -38,6 +58,7 @@ public class ZenTypeFloat extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -127,6 +148,8 @@ public class ZenTypeFloat extends ZenType {
|
||||
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Float.class, "toString", String.class, float.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(F)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -169,9 +192,370 @@ public class ZenTypeFloat extends ZenType {
|
||||
public String getName() {
|
||||
return "float";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionFloat(environment), ANY_NAME, Type.FLOAT_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionFloat(position, 0.0, FLOAT);
|
||||
}
|
||||
|
||||
private class AnyDefinitionFloat implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionFloat(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "F", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(F)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.FLOAT_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "F");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(F)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.FLOAT_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "F");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(FLOAT, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(FLOAT, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "float", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "not");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.fNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.fAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.fSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, float.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.fMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.fDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.fRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "and");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "or");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "xor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "range");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
// return Float.compare(x, y)
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASFLOAT);
|
||||
output.invokeStatic(Float.class, "compare", int.class, float.class, float.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.f2i();
|
||||
output.i2b();
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.f2i();
|
||||
output.i2s();
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.f2i();
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.f2l();
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.f2d();
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Float.class, "toString", String.class, float.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.FLOAT_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.FLOAT_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(FLOAT, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "float", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_FLOAT);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "float", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "F");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeFloatObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Float.class, "floatValue", float.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Float.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Float.class, "floatValue", float.class);
|
||||
output.invokeStatic(FLOAT.getAnyClassName(environment), "valueOf", "(F)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Float.class, "floatValue", float.class);
|
||||
FLOAT.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeFloatObject extends ZenType {
|
||||
public String getName() {
|
||||
return "float";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return FLOAT.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -63,6 +63,11 @@ public class ZenTypeFunction extends ZenType {
|
||||
nameBuilder.append(returnType.getName());
|
||||
name = nameBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
throw new UnsupportedOperationException("functions cannot yet be used as any value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,13 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.BYTE;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASINT;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeInt extends ZenType {
|
||||
public static final ZenTypeInt INSTANCE = new ZenTypeInt();
|
||||
|
||||
//private static final JavaMethod INT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "toString", byte.class);
|
||||
private static final String ANY_NAME = "any/AnyInt";
|
||||
private static final String ANY_NAME_2 = "any.AnyInt";
|
||||
|
||||
private ZenTypeInt() {}
|
||||
|
||||
@Override
|
||||
@@ -31,6 +50,7 @@ public class ZenTypeInt extends ZenType {
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_INT)
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -38,6 +58,7 @@ public class ZenTypeInt extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -123,6 +144,8 @@ public class ZenTypeInt extends ZenType {
|
||||
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Integer.class, "toString", String.class, int.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(I)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -165,9 +188,395 @@ public class ZenTypeInt extends ZenType {
|
||||
public String getName() {
|
||||
return "int";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionInt(environment), ANY_NAME, Type.INT_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionInt(position, 0, INT);
|
||||
}
|
||||
|
||||
private class AnyDefinitionInt implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionInt(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "I", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(I)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.INT_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "I");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(I)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.INT_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "I");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(INT, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(INT, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "int", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNot();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iAnd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iOr();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iXor();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.iSub();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2b();
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2s();
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2l();
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2f();
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2d();
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Integer.class, "toString", String.class, int.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.INT_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.INT_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(INT, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "int", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_INT);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "int", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "I");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeIntObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Integer.class, "intValue", int.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Integer.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Integer.class, "intValue", int.class);
|
||||
output.invokeStatic(INT.getAnyClassName(environment), "valueOf", "(I)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Integer.class, "intValue", int.class);
|
||||
INT.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeIntObject extends ZenType {
|
||||
public String getName() {
|
||||
return "int";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return INT.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -41,6 +41,11 @@ public class ZenTypeIntRange extends ZenType {
|
||||
methodTo = JavaMethod.get(dummy, IntRange.class, "getTo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
throw new UnsupportedOperationException("range values cannot yet be used as any value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
|
||||
environment.error(position, "cannot apply unary operators on int ranges");
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,13 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BYTE;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASLONG;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeLong extends ZenType {
|
||||
public static final ZenTypeLong INSTANCE = new ZenTypeLong();
|
||||
|
||||
//private static final JavaMethod LONG_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Long.class, "toString", long.class);
|
||||
private static final String ANY_NAME = "any/AnyLong";
|
||||
private static final String ANY_NAME_2 = "any.AnyLong";
|
||||
|
||||
private ZenTypeLong() {}
|
||||
|
||||
@Override
|
||||
@@ -31,6 +50,7 @@ public class ZenTypeLong extends ZenType {
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_LONG)
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -38,6 +58,7 @@ public class ZenTypeLong extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -127,6 +148,8 @@ public class ZenTypeLong extends ZenType {
|
||||
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Long.class, "toString", String.class, long.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(J)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -170,6 +193,16 @@ public class ZenTypeLong extends ZenType {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionLong(environment), ANY_NAME, Type.LONG_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLarge() {
|
||||
return true;
|
||||
@@ -179,4 +212,383 @@ public class ZenTypeLong extends ZenType {
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionInt(position, 0, LONG);
|
||||
}
|
||||
|
||||
private class AnyDefinitionLong implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionLong(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "J", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(J)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.LONG_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "J");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(J)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.LONG_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "J");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(LONG, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(LONG, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "long", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.lNot();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.lNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, long.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lAnd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lOr();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.lXor();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
// return Long.compare(x, y)
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASLONG);
|
||||
output.invokeStatic(Long.class, "compare", int.class, long.class, long.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.l2i();
|
||||
output.i2b();
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.l2i();
|
||||
output.i2s();
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.l2i();
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.l2f();
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.l2d();
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Long.class, "toString", String.class, long.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.LONG_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.LONG_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(LONG, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "long", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_LONG);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "long", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "J");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeLongObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Long.class, "longValue", long.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Long.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Long.class, "longValue", long.class);
|
||||
output.invokeStatic(LONG.getAnyClassName(environment), "valueOf", "(J)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Long.class, "longValue", long.class);
|
||||
LONG.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeLongObject extends ZenType {
|
||||
public String getName() {
|
||||
return "long";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return LONG.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -270,6 +270,11 @@ public class ZenTypeNative extends ZenType {
|
||||
iteratorValueType = ZenType.parse(annotation.value(), environment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal global) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
|
||||
|
||||
@@ -126,6 +126,11 @@ public class ZenTypeNull extends ZenType {
|
||||
public String getName() {
|
||||
return "null";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
throw new UnsupportedOperationException("The null type does not have an any type");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -13,12 +17,28 @@ import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.BYTE;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSHORT;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeShort extends ZenType {
|
||||
public static final ZenTypeShort INSTANCE = new ZenTypeShort();
|
||||
|
||||
//private static final JavaMethod SHORT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Short.class, "toString", short.class);
|
||||
private static final String ANY_NAME = "any/AnyShort";
|
||||
private static final String ANY_NAME_2 = "any.AnyShort";
|
||||
|
||||
private ZenTypeShort() {}
|
||||
|
||||
@Override
|
||||
@@ -30,6 +50,7 @@ public class ZenTypeShort extends ZenType {
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_SHORT)
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -37,6 +58,7 @@ public class ZenTypeShort extends ZenType {
|
||||
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type.getNumberType() != 0
|
||||
|| type == ZenTypeString.INSTANCE
|
||||
|| type == ANY
|
||||
|| canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@@ -128,6 +150,8 @@ public class ZenTypeShort extends ZenType {
|
||||
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
|
||||
} else if (type == STRING) {
|
||||
output.invokeStatic(Short.class, "toString", String.class, short.class);
|
||||
} else if (type == ANY) {
|
||||
output.invokeStatic(getAnyClassName(environment), "valueOf", "(S)" + signature(IAny.class));
|
||||
} else if (!compileCastExpansion(position, environment, type)) {
|
||||
environment.error(position, "cannot cast " + this + " to " + type);
|
||||
}
|
||||
@@ -170,9 +194,394 @@ public class ZenTypeShort extends ZenType {
|
||||
public String getName() {
|
||||
return "short";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionShort(environment), ANY_NAME, Type.SHORT_TYPE));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionInt(position, 0, SHORT);
|
||||
}
|
||||
|
||||
private class AnyDefinitionShort implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
public AnyDefinitionShort(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "S", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(S)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.load(Type.SHORT_TYPE, 0);
|
||||
valueOf.construct(ANY_NAME, "S");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(S)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(Type.SHORT_TYPE, 1);
|
||||
constructor.putField(ANY_NAME, "value", "S");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblCan = new Label();
|
||||
|
||||
output.constant(Type.BYTE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.INT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.LONG_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.FLOAT_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
output.constant(Type.DOUBLE_TYPE);
|
||||
output.loadObject(0);
|
||||
output.ifACmpEq(lblCan);
|
||||
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(SHORT, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblCan);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(SHORT, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "short", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNot();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.iNeg();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iAdd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iSub();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
// StringBuilder builder = new StringBuilder();
|
||||
// builder.append(value);
|
||||
// builder.append(other.asString());
|
||||
// return new AnyString(builder.toString());
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(STRING.getAnyClassName(environment), "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iMul();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iDiv();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iRem();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iAnd();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iOr();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
output.newObject(ANY_NAME);
|
||||
output.dup();
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSHORT);
|
||||
output.iXor();
|
||||
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASBYTE);
|
||||
output.iSub();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "in");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "get []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "set []");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
throwCastException(output, ANY_NAME, "bool");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2b();
|
||||
output.returnType(Type.BYTE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.SHORT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnType(Type.INT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2l();
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2f();
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.i2d();
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.invokeStatic(Short.class, "toString", String.class, short.class);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(Type.SHORT_TYPE);
|
||||
|
||||
getValue(output);
|
||||
output.store(Type.SHORT_TYPE, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(SHORT, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "short", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(Type.SHORT_TYPE);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.constant(IAny.NUM_SHORT);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "short", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "S");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package stanhebben.zenscript.type;
|
||||
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
@@ -15,9 +16,13 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.ANY;
|
||||
import static stanhebben.zenscript.type.ZenType.BOOL;
|
||||
import static stanhebben.zenscript.type.ZenType.STRING;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -122,6 +127,22 @@ public class ZenTypeShortObject extends ZenType {
|
||||
environment.getOutput().invokeVirtual(Short.class, "shortValue", short.class);
|
||||
} else if (type == STRING) {
|
||||
environment.getOutput().invokeVirtual(Short.class, "toString", String.class);
|
||||
} else if (type == ANY) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
Label lblNotNull = new Label();
|
||||
Label lblAfter = new Label();
|
||||
|
||||
output.dup();
|
||||
output.ifNonNull(lblNotNull);
|
||||
output.aConstNull();
|
||||
output.goTo(lblAfter);
|
||||
|
||||
output.label(lblNotNull);
|
||||
output.invokeVirtual(Short.class, "shortValue", short.class);
|
||||
output.invokeStatic(SHORT.getAnyClassName(environment), "valueOf", "(S)" + signature(IAny.class));
|
||||
|
||||
output.label(lblAfter);
|
||||
} else {
|
||||
environment.getOutput().invokeVirtual(Short.class, "shortValue", short.class);
|
||||
SHORT.compileCast(position, environment, type);
|
||||
@@ -132,6 +153,11 @@ public class ZenTypeShortObject extends ZenType {
|
||||
public String getName() {
|
||||
return "short";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
return SHORT.getAnyClassName(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -2,7 +2,11 @@ package stanhebben.zenscript.type;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import stanhebben.zenscript.TypeExpansion;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
@@ -11,16 +15,32 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionCompareGeneric;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionJavaCallVirtual;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.ExpressionStringConcat;
|
||||
import stanhebben.zenscript.expression.ExpressionStringContains;
|
||||
import stanhebben.zenscript.expression.ExpressionStringIndex;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import static stanhebben.zenscript.type.ZenType.BYTE;
|
||||
import stanhebben.zenscript.util.AnyClassWriter;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASINT;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwCastException;
|
||||
import static stanhebben.zenscript.util.AnyClassWriter.throwUnsupportedException;
|
||||
import stanhebben.zenscript.util.IAnyDefinition;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
|
||||
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
|
||||
import stanhebben.zenscript.value.IAny;
|
||||
|
||||
public class ZenTypeString extends ZenType {
|
||||
public static final ZenTypeString INSTANCE = new ZenTypeString();
|
||||
|
||||
private final Type type = Type.getType("java.lang.String");
|
||||
private static final String ANY_NAME = "any/AnyString";
|
||||
private static final String ANY_NAME_2 = "any.AnyString";
|
||||
|
||||
private final Type type = Type.getType(String.class);
|
||||
|
||||
private ZenTypeString() {}
|
||||
|
||||
@@ -31,7 +51,7 @@ public class ZenTypeString extends ZenType {
|
||||
|
||||
@Override
|
||||
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
|
||||
return type == this || type == BOOL || type.getNumberType() > 0 || canCastExpansion(environment, type);
|
||||
return type == this || canCastExpansion(environment, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,7 +63,7 @@ public class ZenTypeString extends ZenType {
|
||||
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
|
||||
if (type == this) {
|
||||
return value;
|
||||
} else if (type == BOOL || type.getNumberType() > 0) {
|
||||
} else if (type == BOOL || type.getNumberType() > 0 || type == ANY) {
|
||||
return new ExpressionAs(position, value, type);
|
||||
} else if (canCastExpansion(environment, type)) {
|
||||
return castExpansion(position, environment, value, type);
|
||||
@@ -99,7 +119,9 @@ public class ZenTypeString extends ZenType {
|
||||
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
|
||||
MethodOutput output = environment.getOutput();
|
||||
|
||||
if (type == BOOL) {
|
||||
if (type == STRING) {
|
||||
// do nothing
|
||||
} else if (type == BOOL) {
|
||||
output.invokeStatic(Boolean.class, "parseBoolean", boolean.class, String.class);
|
||||
} else if (type == ZenTypeBoolObject.INSTANCE) {
|
||||
output.invokeStatic(Boolean.class, "valueOf", Boolean.class, String.class);
|
||||
@@ -157,6 +179,10 @@ public class ZenTypeString extends ZenType {
|
||||
values.add(right.cast(position, environment, this));
|
||||
return new ExpressionStringConcat(position, values);
|
||||
}
|
||||
} else if (operator == OperatorType.INDEXGET) {
|
||||
return new ExpressionStringIndex(position, left, right.cast(position, environment, INT));
|
||||
} else if (operator == OperatorType.CONTAINS) {
|
||||
return new ExpressionStringContains(position, left, right.cast(position, environment, STRING));
|
||||
} else {
|
||||
Expression result = binaryExpansion(position, environment, left, right, operator);
|
||||
if (result == null) {
|
||||
@@ -183,6 +209,10 @@ public class ZenTypeString extends ZenType {
|
||||
@Override
|
||||
public Expression compare(
|
||||
ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, CompareType type) {
|
||||
if (right.getType().canCastImplicit(STRING, environment)) {
|
||||
return new ExpressionCompareGeneric(position, new ExpressionJavaCallVirtual(position, METHOD_ASINT, left, right), type);
|
||||
}
|
||||
|
||||
Expression result = binaryExpansion(position, environment, left, right, OperatorType.COMPARE);
|
||||
if (result == null) {
|
||||
environment.error(position, "cannot compare strings");
|
||||
@@ -208,9 +238,324 @@ public class ZenTypeString extends ZenType {
|
||||
public String getName() {
|
||||
return "string";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
if (!environment.containsClass(ANY_NAME_2)) {
|
||||
environment.putClass(ANY_NAME_2, new byte[0]);
|
||||
environment.putClass(ANY_NAME_2, AnyClassWriter.construct(new AnyDefinitionString(environment), ANY_NAME, type));
|
||||
}
|
||||
|
||||
return ANY_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
return new ExpressionNull(position);
|
||||
}
|
||||
|
||||
private class AnyDefinitionString implements IAnyDefinition {
|
||||
private final IEnvironmentGlobal environment;
|
||||
|
||||
private AnyDefinitionString(IEnvironmentGlobal environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMembers(ClassVisitor output) {
|
||||
output.visitField(Opcodes.ACC_PRIVATE, "value", "Ljava/lang/String;", null, null);
|
||||
|
||||
MethodOutput valueOf = new MethodOutput(output, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "valueOf", "(Ljava/lang/String;)" + signature(IAny.class), null, null);
|
||||
valueOf.start();
|
||||
valueOf.newObject(ANY_NAME);
|
||||
valueOf.dup();
|
||||
valueOf.loadObject(0);
|
||||
valueOf.construct(ANY_NAME, "Ljava/lang/String;");
|
||||
valueOf.returnObject();
|
||||
valueOf.end();
|
||||
|
||||
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/String;)V", null, null);
|
||||
constructor.start();
|
||||
constructor.loadObject(0);
|
||||
constructor.invokeSpecial(internal(Object.class), "<init>", "()V");
|
||||
constructor.loadObject(0);
|
||||
constructor.load(type, 1);
|
||||
constructor.putField(ANY_NAME, "value", "Ljava/lang/String;");
|
||||
constructor.returnType(Type.VOID_TYPE);
|
||||
constructor.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticCanCastImplicit(MethodOutput output) {
|
||||
Label lblOthers = new Label();
|
||||
|
||||
output.constant(type);
|
||||
output.loadObject(0);
|
||||
output.ifACmpNe(lblOthers);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
|
||||
output.label(lblOthers);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCanCastImplicit(STRING, output, environment, 0);
|
||||
}
|
||||
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineStaticAs(MethodOutput output) {
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(STRING, output, environment, 0, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "string", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNot(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "not");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineNeg(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "negate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAdd(MethodOutput output) {
|
||||
defineCat(output);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineSub(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "-");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCat(MethodOutput output) {
|
||||
output.newObject(StringBuilder.class);
|
||||
output.dup();
|
||||
output.invokeSpecial(internal(StringBuilder.class), "<init>", "()V");
|
||||
getValue(output);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
|
||||
output.invokeVirtual(StringBuilder.class, "toString", String.class);
|
||||
output.invokeStatic(ANY_NAME, "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMul(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineDiv(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMod(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAnd(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "&");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineOr(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "|");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineXor(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "^");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineRange(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "..");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCompareTo(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(String.class, "compareTo", int.class, String.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineContains(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASSTRING);
|
||||
output.invokeVirtual(String.class, "contains", boolean.class, CharSequence.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberGet(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberSet(MethodOutput output) {
|
||||
// TODO
|
||||
output.returnType(Type.VOID_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineMemberCall(MethodOutput output) {
|
||||
// TODO
|
||||
output.aConstNull();
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexGet(MethodOutput output) {
|
||||
// return new AnyString(String.substring(other.asInt(), other.asInt() + 1))
|
||||
getValue(output);
|
||||
output.loadObject(1);
|
||||
output.invoke(METHOD_ASINT);
|
||||
output.dup();
|
||||
output.iConst1();
|
||||
output.iAdd();
|
||||
output.invokeVirtual(String.class, "substring", String.class, int.class, int.class);
|
||||
output.invokeStatic(ANY_NAME, "valueOf", "(Ljava/lang/String;)" + signature(IAny.class));
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIndexSet(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "[]=");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineCall(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsBool(MethodOutput output) {
|
||||
// return Boolean.parseBoolean(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Boolean.class, "parseBoolean", boolean.class, String.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsByte(MethodOutput output) {
|
||||
// return Byte.parseByte(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Byte.class, "parseByte", byte.class, String.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsShort(MethodOutput output) {
|
||||
// return Short.parseShort(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Short.class, "parseShort", short.class, String.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsInt(MethodOutput output) {
|
||||
// return Integer.parseInt(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Integer.class, "parseInt", int.class, String.class);
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsLong(MethodOutput output) {
|
||||
// return Integer.parseLong(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Long.class, "parseLong", long.class, String.class);
|
||||
output.returnType(Type.LONG_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsFloat(MethodOutput output) {
|
||||
// return Float.parseFloat(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Float.class, "parseFloat", float.class, String.class);
|
||||
output.returnType(Type.FLOAT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsDouble(MethodOutput output) {
|
||||
// return Double.parseDouble(value);
|
||||
getValue(output);
|
||||
output.invokeStatic(Double.class, "parseDouble", double.class, String.class);
|
||||
output.returnType(Type.DOUBLE_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAsString(MethodOutput output) {
|
||||
getValue(output);
|
||||
output.returnObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineAs(MethodOutput output) {
|
||||
int localValue = output.local(type);
|
||||
|
||||
getValue(output);
|
||||
output.store(type, localValue);
|
||||
TypeExpansion expansion = environment.getExpansion(getName());
|
||||
if (expansion != null) {
|
||||
expansion.compileAnyCast(STRING, output, environment, localValue, 1);
|
||||
}
|
||||
|
||||
throwCastException(output, "string", 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIs(MethodOutput output) {
|
||||
Label lblEq = new Label();
|
||||
|
||||
output.loadObject(1);
|
||||
output.constant(type);
|
||||
output.ifACmpEq(lblEq);
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
output.label(lblEq);
|
||||
output.iConst1();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineGetNumberType(MethodOutput output) {
|
||||
output.iConst0();
|
||||
output.returnInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorSingle(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "iterator");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void defineIteratorMulti(MethodOutput output) {
|
||||
throwUnsupportedException(output, "string", "iterator");
|
||||
}
|
||||
|
||||
private void getValue(MethodOutput output) {
|
||||
output.loadObject(0);
|
||||
output.getField(ANY_NAME, "value", "Ljava/lang/String;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,11 @@ public class ZenTypeVoid extends ZenType {
|
||||
public String getName() {
|
||||
return "void";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnyClassName(IEnvironmentGlobal environment) {
|
||||
throw new UnsupportedOperationException("Cannot convert void to anything, not even any");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression defaultValue(ZenPosition position) {
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
package stanhebben.zenscript.type.expand;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.natives.JavaMethod;
|
||||
import stanhebben.zenscript.util.MethodOutput;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -32,7 +32,7 @@ public class ZenExpandCaster {
|
||||
return method.callStatic(position, environment, expression);
|
||||
}
|
||||
|
||||
public void compile(IEnvironmentMethod environment) {
|
||||
environment.getOutput().invokeStatic(method);
|
||||
public void compile(MethodOutput output) {
|
||||
output.invokeStatic(method);
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,9 @@ public class JavaMethod {
|
||||
public static JavaMethod get(ITypeRegistry types, Class cls, String name, Class... parameterTypes) {
|
||||
try {
|
||||
Method method = cls.getMethod(name, parameterTypes);
|
||||
if (method == null) {
|
||||
throw new RuntimeException("method " + name + " not found in class " + cls.getName());
|
||||
}
|
||||
return new JavaMethod(cls, method, types);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
throw new RuntimeException("method " + name + " not found in class " + cls.getName(), ex);
|
||||
|
||||
@@ -393,6 +393,13 @@ public class MethodOutput {
|
||||
}
|
||||
|
||||
public void invokeStatic(String owner, String name, String descriptor) {
|
||||
if (owner == null)
|
||||
throw new IllegalArgumentException("owner cannot be null");
|
||||
if (name == null)
|
||||
throw new IllegalArgumentException("name cannot be null");
|
||||
if (descriptor == null)
|
||||
throw new IllegalArgumentException("descriptor cannot be null");
|
||||
|
||||
visitor.visitMethodInsn(INVOKESTATIC, owner, name, descriptor);
|
||||
}
|
||||
|
||||
@@ -561,6 +568,14 @@ public class MethodOutput {
|
||||
visitor.visitJumpInsn(IF_ICMPLT, lbl);
|
||||
}
|
||||
|
||||
public void ifACmpEq(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ACMPEQ, lbl);
|
||||
}
|
||||
|
||||
public void ifACmpNe(Label lbl) {
|
||||
visitor.visitJumpInsn(IF_ACMPNE, lbl);
|
||||
}
|
||||
|
||||
public void ifNull(Label lbl) {
|
||||
visitor.visitJumpInsn(IFNULL, lbl);
|
||||
}
|
||||
@@ -601,10 +616,18 @@ public class MethodOutput {
|
||||
visitor.visitFieldInsn(PUTFIELD, internal(owner), name, signature(descriptor));
|
||||
}
|
||||
|
||||
public void getStaticField(String owner, String name, String descriptor) {
|
||||
visitor.visitFieldInsn(GETSTATIC, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void getStaticField(Class owner, Field field) {
|
||||
visitor.visitFieldInsn(GETSTATIC, internal(owner), field.getName(), signature(field.getType()));
|
||||
}
|
||||
|
||||
public void putStaticField(String owner, String name, String descriptor) {
|
||||
visitor.visitFieldInsn(PUTSTATIC, owner, name, descriptor);
|
||||
}
|
||||
|
||||
public void putStaticField(Class owner, Field field) {
|
||||
visitor.visitFieldInsn(PUTSTATIC, internal(owner), field.getName(), signature(field.getType()));
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public class ZenPosition {
|
||||
private final int offset;
|
||||
|
||||
public ZenPosition(ZenParsedFile file, int line, int offset) {
|
||||
if (file != null && line <= 0) throw new IllegalArgumentException("Line must be positive");
|
||||
|
||||
this.file = file;
|
||||
this.line = line;
|
||||
this.offset = offset;
|
||||
|
||||
@@ -9,12 +9,16 @@ package stanhebben.zenscript.util;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import stanhebben.zenscript.compiler.ITypeRegistry;
|
||||
import stanhebben.zenscript.compiler.TypeRegistry;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ZenTypeUtil {
|
||||
public static final ITypeRegistry EMPTY_REGISTRY = new TypeRegistry();
|
||||
|
||||
private static final Map<String, String> SIGNATURE_MAP;
|
||||
|
||||
static {
|
||||
|
||||
@@ -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)");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user