- Rewrote the whole damn typecasting system. Now uses a more flexible and more structure casting rule system.

- Improved function literals. Function recipes are a thing!
- Added mirrored shaped recipes
- Fixed a few "any" type - related errors
- Added environment to the bracket handlers. Breaks existing bracket handlers!!
- Turned JavaMethod into an interface. Can now be used both for existing methods and generated ones. Makes a few things simpler :)
- Added creative and adventure getters to player info
This commit is contained in:
Stan Hebben committed 2014-08-20 11:59:49 +02:00
1 parent c67ad22270
commit 154c2242f9
172 files changed
+5400 -2134

No files matched your search

@@ -7,8 +7,7 @@
package stanhebben.zenscript;
import java.util.List;
import stanhebben.zenscript.IZenErrorLogger;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.TypeRegistry;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.symbols.IZenSymbol;
@@ -24,7 +23,7 @@ public interface IZenCompileEnvironment {
public IZenSymbol getDollar(String name);
public IZenSymbol getBracketed(List<Token> tokens);
public IZenSymbol getBracketed(IEnvironmentGlobal environment, List<Token> tokens);
public TypeRegistry getTypeRegistry();
@@ -17,16 +17,21 @@ import stanhebben.zenscript.annotations.ZenMethodStatic;
import stanhebben.zenscript.annotations.ZenOperator;
import stanhebben.zenscript.annotations.ZenSetter;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.compiler.ITypeRegistry;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionCallStatic;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
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;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
* Type expansions provide additional members for existing types. They can
@@ -77,7 +82,7 @@ public class TypeExpansion {
for (Annotation annotation : method.getAnnotations()) {
if (annotation instanceof ZenCaster) {
checkStatic(method);
casters.add(new ZenExpandCaster(new JavaMethod(cls, method, types)));
casters.add(new ZenExpandCaster(new JavaMethod(method, types)));
} else if (annotation instanceof ZenGetter) {
checkStatic(method);
ZenGetter getterAnnotation = (ZenGetter) annotation;
@@ -86,7 +91,7 @@ public class TypeExpansion {
if (!members.containsKey(name)) {
members.put(name, new ZenExpandMember(type, name));
}
members.get(name).setGetter(new JavaMethod(cls, method, types));
members.get(name).setGetter(new JavaMethod(method, types));
} else if (annotation instanceof ZenSetter) {
checkStatic(method);
ZenSetter setterAnnotation = (ZenSetter) annotation;
@@ -95,7 +100,7 @@ public class TypeExpansion {
if (!members.containsKey(name)) {
members.put(name, new ZenExpandMember(type, name));
}
members.get(name).setSetter(new JavaMethod(cls, method, types));
members.get(name).setSetter(new JavaMethod(method, types));
} else if (annotation instanceof ZenOperator) {
checkStatic(method);
ZenOperator operatorAnnotation = (ZenOperator) annotation;
@@ -107,7 +112,7 @@ public class TypeExpansion {
} else {
unaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
case ADD:
@@ -128,7 +133,7 @@ public class TypeExpansion {
} else {
binaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
case INDEXSET:
@@ -137,7 +142,7 @@ public class TypeExpansion {
} else {
trinaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
}
@@ -150,7 +155,7 @@ public class TypeExpansion {
if (!members.containsKey(methodName)) {
members.put(methodName, new ZenExpandMember(type, methodName));
}
members.get(methodName).addMethod(new JavaMethod(cls, method, types));
members.get(methodName).addMethod(new JavaMethod(method, types));
} else if (annotation instanceof ZenMethodStatic) {
checkStatic(method);
ZenMethodStatic methodAnnotation = (ZenMethodStatic) annotation;
@@ -160,12 +165,24 @@ public class TypeExpansion {
if (!staticMembers.containsKey(methodName)) {
staticMembers.put(methodName, new ZenExpandMember(type, methodName));
}
staticMembers.get(methodName).addMethod(new JavaMethod(cls, method, types));
staticMembers.get(methodName).addMethod(new JavaMethod(method, types));
}
}
}
}
/**
* Registers all casting rules to the given delegate.
*
* @param environment compilation environment
* @param rules target delegate
*/
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules) {
for (ZenExpandCaster caster : casters) {
caster.constructCastingRules(environment, rules);
}
}
/**
* Retrieves a caster from this expansion. May return null if no suitable
* caster was available.
@@ -209,7 +226,7 @@ public class TypeExpansion {
OperatorType operator) {
for (ZenNativeOperator op : unaryOperators) {
if (op.getOperator() == operator) {
return op.getMethod().callStatic(position, environment, value);
return new ExpressionCallStatic(position, environment, op.getMethod(), value);
}
}
@@ -235,7 +252,7 @@ public class TypeExpansion {
OperatorType operator) {
for (ZenNativeOperator op : binaryOperators) {
if (op.getOperator() == operator) {
return op.getMethod().callStatic(position, environment, left, right);
return new ExpressionCallStatic(position, environment, op.getMethod(), left, right);
}
}
@@ -263,7 +280,7 @@ public class TypeExpansion {
OperatorType operator) {
for (ZenNativeOperator op : trinaryOperators) {
if (op.getOperator() == operator) {
return op.getMethod().callStatic(position, environment, first, second, third);
return new ExpressionCallStatic(position, environment, op.getMethod(), first, second, third);
}
}
@@ -312,6 +329,7 @@ public class TypeExpansion {
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");
@@ -340,7 +358,8 @@ public class TypeExpansion {
output.ifEQ(skip);
output.load(type.toASMType(), localValue);
caster.compile(output);
output.invokeStatic(casterAny, "rtAs", "(Ljava/lang/Class;)Z");
output.loadObject(localClass);
output.invokeStatic(casterAny, "rtAs", "(" + caster.getTarget().getSignature() + "Ljava/lang/Class;)Ljava/lang/Object;");
output.returnType(caster.getTarget().toASMType());
output.label(skip);
}
@@ -374,6 +393,25 @@ public class TypeExpansion {
}
}
public boolean compileAnyUnary(MethodOutput output, OperatorType type, IEnvironmentMethod environment) {
for (ZenNativeOperator operator : unaryOperators) {
if (operator.getOperator() == type) {
ZenType returnType = operator.getMethod().getReturnType();
output.loadObject(0);
operator.getMethod().invokeStatic(output);
output.invokeStatic(
returnType.getAnyClassName(environment),
"valueOf",
"(" + returnType.getSignature() + ")" + ZenType.ANY.getSignature());
output.returnObject();
return true;
}
}
return false;
}
// #######################
// ### Private methods ###
// #######################
@@ -50,8 +50,9 @@ public class ZenModule {
* @param mainFileName main filename (used for debug info)
* @param scripts scripts to compile
* @param environmentGlobal global compile environment
* @param debug enable debug mode (outputs classes to generated directory)
*/
public static void compileScripts(String mainFileName, List<ZenParsedFile> scripts, IEnvironmentGlobal environmentGlobal) {
public static void compileScripts(String mainFileName, List<ZenParsedFile> scripts, IEnvironmentGlobal environmentGlobal, boolean debug) {
ClassWriter clsMain = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
clsMain.visitSource(mainFileName, null);
@@ -117,6 +118,7 @@ public class ZenModule {
if (script.getStatements().size() > 0) {
MethodOutput scriptOutput = new MethodOutput(clsScript, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "__script__", "()V", null, null);
IEnvironmentMethod functionMethod = new EnvironmentMethod(scriptOutput, environmentScript);
//scriptOutput.enableDebug();
scriptOutput.start();
for (Statement statement : script.getStatements()) {
statement.compile(functionMethod);
@@ -143,6 +145,28 @@ public class ZenModule {
constructor.visitMaxs(0, 0);
constructor.visitEnd();
// debug: output classes
if (debug) {
try {
File outputDir = new File("generated");
outputDir.mkdir();
for (String className : environmentGlobal.getClassNames()) {
File outputFile = new File(outputDir, className.replace('.', '/') + ".class");
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
FileOutputStream output = new FileOutputStream(outputFile);
output.write(environmentGlobal.getClass(className));
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
environmentGlobal.putClass("__ZenMain__", clsMain.toByteArray());
}
@@ -176,7 +200,7 @@ public class ZenModule {
List<ZenParsedFile> files = new ArrayList<ZenParsedFile>();
files.add(file);
compileScripts(filename, files, environmentGlobal);
compileScripts(filename, files, environmentGlobal, false);
// debug: output classes
File outputDir = new File("generated");
@@ -238,18 +262,7 @@ public class ZenModule {
}
String filename = file.getName();
compileScripts(filename, files, environmentGlobal);
// debug: output classes
File outputDir = new File("generated");
outputDir.mkdir();
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
File outputFile = new File(outputDir, entry.getKey().replace('.', '/') + ".class");
FileOutputStream output = new FileOutputStream(outputFile);
output.write(entry.getValue());
output.close();
}
compileScripts(filename, files, environmentGlobal, true);
return new ZenModule(classes, baseClassLoader);
}
@@ -126,7 +126,7 @@ public class ZenParsedFile {
}
functions.put(function.getName(), function);
} else {
statements.add(Statement.read(tokener, environmentScript));
statements.add(Statement.read(tokener, environmentScript, null));
}
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.compiler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ClassVisitor;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -90,4 +91,14 @@ public class EnvironmentClass implements IEnvironmentClass {
public void warning(ZenPosition position, String message) {
global.warning(position, message);
}
@Override
public Set<String> getClassNames() {
return global.getClassNames();
}
@Override
public byte[] getClass(String name) {
return global.getClass(name);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.compiler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import stanhebben.zenscript.IZenErrorLogger;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -103,4 +104,14 @@ public class EnvironmentGlobal implements IEnvironmentGlobal {
local.put(name, value);
}
}
@Override
public Set<String> getClassNames() {
return classes.keySet();
}
@Override
public byte[] getClass(String name) {
return classes.get(name);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.compiler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ClassVisitor;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -107,4 +108,14 @@ public class EnvironmentMethod implements IEnvironmentMethod {
public void putValue(String name, IZenSymbol value) {
local.put(name, value);
}
@Override
public Set<String> getClassNames() {
return environment.getClassNames();
}
@Override
public byte[] getClass(String name) {
return environment.getClass(name);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.compiler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ClassVisitor;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -106,4 +107,14 @@ public class EnvironmentScope implements IEnvironmentMethod {
public void warning(ZenPosition position, String message) {
outer.warning(position, message);
}
@Override
public Set<String> getClassNames() {
return outer.getClassNames();
}
@Override
public byte[] getClass(String name) {
return outer.getClass(name);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.compiler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import stanhebben.zenscript.IZenCompileEnvironment;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -86,4 +87,14 @@ public class EnvironmentScript implements IEnvironmentGlobal {
public void warning(ZenPosition position, String message) {
parent.warning(position, message);
}
@Override
public Set<String> getClassNames() {
return parent.getClassNames();
}
@Override
public byte[] getClass(String name) {
return parent.getClass(name);
}
}
@@ -6,6 +6,7 @@
package stanhebben.zenscript.compiler;
import java.util.Set;
import stanhebben.zenscript.IZenErrorLogger;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.expression.partial.IPartialExpression;
@@ -26,6 +27,10 @@ public interface IEnvironmentGlobal extends ITypeRegistry, IZenErrorLogger {
public boolean containsClass(String name);
public Set<String> getClassNames();
public byte[] getClass(String name);
public void putClass(String name, byte[] data);
public IPartialExpression getValue(String name, ZenPosition position);
@@ -67,7 +67,7 @@ public class ParsedFunction {
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
while (parser.optional(T_ACLOSE) == null) {
statementsAL.add(Statement.read(parser, environment));
statementsAL.add(Statement.read(parser, environment, type));
}
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
}
@@ -8,13 +8,13 @@ import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.parser.expression.ParsedExpression;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.ZenPosition;
public abstract class Expression implements IPartialExpression {
public static final Expression parse(ZenTokener parser, IEnvironmentMethod environment) {
public static final Expression parse(ZenTokener parser, IEnvironmentMethod environment, ZenType predictedType) {
return ParsedExpression.read(parser, environment)
.compile(environment)
.compile(environment, predictedType)
.eval(environment);
}
@@ -28,13 +28,17 @@ public abstract class Expression implements IPartialExpression {
return position;
}
public abstract ZenType getType();
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (getType() == type) {
return this;
} else {
return getType().cast(position, environment, this, type);
ICastingRule castingRule = getType().getCastingRule(type, environment);
if (castingRule == null) {
environment.error(position, "Cannot cast " + this.getType() + " to " + type);
return new ExpressionInvalid(position, type);
} else {
return new ExpressionAs(position, this, castingRule);
}
}
}
@@ -77,6 +81,11 @@ public abstract class Expression implements IPartialExpression {
return getType().call(position, environment, this, values);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return getType().predictCallTypes(numArguments);
}
@Override
public IZenSymbol toSymbol() {
return null;
@@ -5,8 +5,8 @@ import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAny;
import stanhebben.zenscript.type.ZenTypeArrayBasic;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
@@ -14,21 +14,19 @@ public class ExpressionArray extends Expression {
private final Expression[] contents;
private final ZenTypeArrayBasic type;
public ExpressionArray(ZenPosition position, Expression... contents) {
public ExpressionArray(ZenPosition position, ZenTypeArrayBasic type, Expression... contents) {
super(position);
this.contents = contents;
type = new ZenTypeArrayBasic(ZenTypeAny.INSTANCE);
}
public ExpressionArray(ZenPosition position, Expression[] contents, ZenTypeArrayBasic type) {
super(position);
this.contents = contents;
this.type = type;
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (this.type.equals(type)) {
return this;
}
if (type instanceof ZenTypeArrayBasic) {
ZenTypeArrayBasic arrayType = (ZenTypeArrayBasic) type;
Expression[] newContents = new Expression[contents.length];
@@ -36,12 +34,15 @@ public class ExpressionArray extends Expression {
newContents[i] = contents[i].cast(position, environment, arrayType.getBaseType());
}
return new ExpressionArray(getPosition(), newContents, arrayType);
} else if (this.type.canCastImplicit(type, environment)) {
return this.type.cast(position, environment, this, type);
return new ExpressionArray(getPosition(), arrayType, newContents);
} else {
environment.error(position, "cannot cast " + this.type + " to " + type);
return new ExpressionInvalid(position, type);
ICastingRule castingRule = this.type.getCastingRule(type, environment);
if (castingRule == null) {
environment.error(position, "cannot cast " + this.type + " to " + type);
return new ExpressionInvalid(position, type);
} else {
return new ExpressionAs(position, this, castingRule);
}
}
}
@@ -3,34 +3,46 @@ package stanhebben.zenscript.expression;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.ZenPosition;
public class ExpressionAs extends Expression {
private final Expression value;
private final ZenType type;
private final ICastingRule castingRule;
public ExpressionAs(ZenPosition position, Expression value, ZenType type) {
public ExpressionAs(ZenPosition position, Expression value, ICastingRule castingRule) {
super(position);
this.value = value;
this.type = type;
this.castingRule = castingRule;
}
@Override
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
return this.type.cast(position, environment, this, type);
}
if (castingRule.getResultingType().equals(type)) {
return this;
}
ICastingRule newCastingRule = type.getCastingRule(type, environment);
if (newCastingRule == null) {
environment.error(position, "Cannot cast " + getType() + " to " + type);
return new ExpressionInvalid(position, type);
} else {
return new ExpressionAs(position, this, newCastingRule);
}
}*/
@Override
public ZenType getType() {
return type;
return castingRule.getResultingType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
value.compile(result, environment);
if (result) {
value.getType().compileCast(getPosition(), environment, type);
castingRule.compile(environment);
}
}
}
@@ -30,7 +30,7 @@ public class ExpressionBool extends Expression {
if (type == ZenType.BOOL) {
return this;
} else {
return ZenType.BOOL.cast(position, environment, this, type);
return super.cast(position, environment, type);
}
}
@@ -6,8 +6,10 @@
package stanhebben.zenscript.expression;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
@@ -16,18 +18,19 @@ import stanhebben.zenscript.util.ZenPosition;
*
* @author Stanneke
*/
public class ExpressionJavaCallStatic extends Expression {
private final JavaMethod method;
public class ExpressionCallStatic extends Expression {
private final IJavaMethod method;
private final Expression[] arguments;
public ExpressionJavaCallStatic(
public ExpressionCallStatic(
ZenPosition position,
JavaMethod method,
IEnvironmentGlobal environment,
IJavaMethod method,
Expression... arguments) {
super(position);
this.method = method;
this.arguments = arguments;
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
}
@Override
@@ -43,7 +46,7 @@ public class ExpressionJavaCallStatic extends Expression {
argument.compile(true, environment);
}
output.invokeStatic(method);
method.invokeStatic(output);
if (method.getReturnType() != ZenType.VOID && !result) {
output.pop(method.getReturnType().isLarge());
@@ -6,8 +6,10 @@
package stanhebben.zenscript.expression;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
@@ -15,15 +17,16 @@ import stanhebben.zenscript.util.ZenPosition;
*
* @author Stanneke
*/
public class ExpressionJavaCallVirtual extends Expression {
private final JavaMethod method;
public class ExpressionCallVirtual extends Expression {
private final IJavaMethod method;
private final Expression receiver;
private final Expression[] arguments;
public ExpressionJavaCallVirtual(
public ExpressionCallVirtual(
ZenPosition position,
JavaMethod method,
IEnvironmentGlobal environment,
IJavaMethod method,
Expression receiver,
Expression... arguments) {
super(position);
@@ -31,7 +34,7 @@ public class ExpressionJavaCallVirtual extends Expression {
this.method = method;
this.receiver = receiver;
this.arguments = arguments;
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
}
@Override
@@ -47,7 +50,7 @@ public class ExpressionJavaCallVirtual extends Expression {
argument.compile(true, environment);
}
environment.getOutput().invoke(method);
method.invokeVirtual(environment.getOutput());
if (method.getReturnType() != ZenType.VOID && !result) {
environment.getOutput().pop(method.getReturnType().isLarge());
}
@@ -25,11 +25,11 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionFunction extends Expression {
private final List<ParsedFunctionArgument> arguments;
private final ZenType returnType;
private final Statement[] statements;
private final List<Statement> statements;
private final ZenTypeFunction functionType;
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, Statement[] statements) {
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, List<Statement> statements) {
super(position);
System.out.println("Function expression: " + arguments.size() + " arguments");
@@ -95,6 +95,7 @@ public class ExpressionFunction extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: implement
// TODO: make sure the function is compiled properly
throw new UnsupportedOperationException("not yet implemented");
}
}
@@ -7,10 +7,8 @@
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.ZenTypeAny;
import stanhebben.zenscript.util.ZenPosition;
@@ -32,6 +30,8 @@ public class ExpressionInvalid extends Expression {
super(position);
this.type = type;
//throw new RuntimeException("Constructing invalid expression");
}
@Override
@@ -1,59 +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.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionJavaCallStaticGenerated extends Expression {
private final String target;
private final String name;
private final String descriptor;
private final ZenType returnZType;
private final Expression[] arguments;
public ExpressionJavaCallStaticGenerated(
ZenPosition position,
String target, String name, String descriptor,
ZenType returnZType,
Expression... arguments) {
super(position);
this.target = target;
this.name = name;
this.descriptor = descriptor;
this.returnZType = returnZType;
this.arguments = arguments;
}
@Override
public ZenType getType() {
return returnZType;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
MethodOutput output = environment.getOutput();
for (Expression argument : arguments) {
argument.compile(true, environment);
}
output.invokeStatic(target, name, descriptor);
if (!descriptor.endsWith("V") && !result) {
output.pop();
}
}
}
@@ -30,7 +30,7 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
public class ExpressionJavaLambda extends Expression {
private final Class interfaceClass;
private final List<ParsedFunctionArgument> arguments;
private final Statement[] statements;
private final List<Statement> statements;
private final ZenType type;
@@ -38,7 +38,7 @@ public class ExpressionJavaLambda extends Expression {
ZenPosition position,
Class interfaceClass,
List<ParsedFunctionArgument> arguments,
Statement[] statements,
List<Statement> statements,
ZenType type) {
super(position);
@@ -7,25 +7,17 @@ import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAny;
import stanhebben.zenscript.type.ZenTypeAssociative;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
public class ExpressionTable extends Expression {
public class ExpressionMap extends Expression {
private final Expression[] keys;
private final Expression[] values;
private final ZenTypeAssociative type;
public ExpressionTable(ZenPosition position, Expression[] keys, Expression[] values) {
super(position);
this.keys = keys;
this.values = values;
type = new ZenTypeAssociative(ZenTypeAny.INSTANCE, ZenTypeAny.INSTANCE);
}
public ExpressionTable(ZenPosition position, Expression[] keys, Expression[] values, ZenTypeAssociative type) {
public ExpressionMap(ZenPosition position, Expression[] keys, Expression[] values, ZenTypeAssociative type) {
super(position);
this.keys = keys;
@@ -69,22 +61,29 @@ public class ExpressionTable extends Expression {
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (this.type.equals(type)) {
return this;
}
if (type instanceof ZenTypeAssociative) {
ZenTypeAssociative keyType = (ZenTypeAssociative) type;
ZenTypeAssociative associativeType = (ZenTypeAssociative) type;
Expression[] newKeys = new Expression[keys.length];
Expression[] newValues = new Expression[values.length];
for (int i = 0; i < keys.length; i++) {
newKeys[i] = keys[i].cast(position, environment, keyType.getKeyType());
newValues[i] = values[i].cast(position, environment, keyType.getValueType());
newKeys[i] = keys[i].cast(position, environment, associativeType.getKeyType());
newValues[i] = values[i].cast(position, environment, associativeType.getValueType());
}
return new ExpressionTable(getPosition(), newKeys, newValues, keyType);
} else if (this.type.canCastImplicit(type, environment)) {
return this.type.cast(position, environment, this, type);
return new ExpressionMap(getPosition(), newKeys, newValues, associativeType);
} else {
environment.error(position, "cannot cast " + this.type + " to " + type);
return new ExpressionInvalid(position, type);
ICastingRule castingRule = this.type.getCastingRule(type, environment);
if (castingRule == null) {
environment.error(position, "cannot cast " + this.type + " to " + type);
return new ExpressionInvalid(position, type);
} else {
return new ExpressionAs(position, this, castingRule);
}
}
}
}
@@ -17,7 +17,7 @@ public class ExpressionOrOr extends Expression {
@Override
public ZenType getType() {
return a.getType(); // TODO: combine both types
return a.getType();
}
@Override
@@ -26,7 +26,11 @@ public interface IPartialExpression {
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values);
public ZenType[] predictCallTypes(int numArguments);
public IZenSymbol toSymbol();
public ZenType getType();
public ZenType toType(IEnvironmentGlobal environment);
}
@@ -57,8 +57,18 @@ public class PartialJavaClass implements IPartialExpression {
return new SymbolJavaClass(cls);
}
@Override
public ZenType getType() {
return null;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
return environment.getType(cls);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return new ZenType[numArguments];
}
}
@@ -55,11 +55,21 @@ public class PartialLocal implements IPartialExpression {
return variable.getType().call(position, environment, eval(environment), values);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return variable.getType().predictCallTypes(numArguments);
}
@Override
public IZenSymbol toSymbol() {
return variable;
}
@Override
public ZenType getType() {
return variable.getType();
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
@@ -57,11 +57,21 @@ public class PartialPackage implements IPartialExpression {
return new ExpressionInvalid(position);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return new ZenType[numArguments];
}
@Override
public IZenSymbol toSymbol() {
return null; // not supposed to be used as symbol
}
@Override
public ZenType getType() {
return null;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
@@ -6,14 +6,16 @@
package stanhebben.zenscript.expression.partial;
import java.util.Arrays;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionCallStatic;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionJavaCallStaticGenerated;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolZenStaticMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -67,7 +69,12 @@ public class PartialStaticGenerated implements IPartialExpression {
arguments[i] = values[i].cast(position, environment, argumentTypes[i]);
}
return new ExpressionJavaCallStaticGenerated(position, owner, method, signature, returnType, arguments);
return new ExpressionCallStatic(position, environment, JavaMethod.getStatic(owner, method, returnType, argumentTypes), arguments);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return Arrays.copyOf(argumentTypes, numArguments);
}
@Override
@@ -75,6 +82,11 @@ public class PartialStaticGenerated implements IPartialExpression {
return new SymbolZenStaticMethod(owner, method, signature, argumentTypes, returnType);
}
@Override
public ZenType getType() {
return returnType;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
@@ -9,11 +9,12 @@ package stanhebben.zenscript.expression.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionCallStatic;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaStaticGetter;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -22,16 +23,16 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class PartialStaticGetter implements IPartialExpression {
private final ZenPosition position;
private final JavaMethod method;
private final IJavaMethod method;
public PartialStaticGetter(ZenPosition position, JavaMethod method) {
public PartialStaticGetter(ZenPosition position, IJavaMethod method) {
this.position = position;
this.method = method;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
return method.callStatic(position, environment);
return new ExpressionCallStatic(position, environment, method);
}
@Override
@@ -51,11 +52,21 @@ public class PartialStaticGetter implements IPartialExpression {
return new ExpressionInvalid(position);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return new ZenType[numArguments];
}
@Override
public IZenSymbol toSymbol() {
return new SymbolJavaStaticGetter(method);
}
@Override
public ZenType getType() {
return method.getReturnType();
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
@@ -6,14 +6,16 @@
package stanhebben.zenscript.expression.partial;
import java.util.Arrays;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionCallStatic;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaStaticMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -22,9 +24,9 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class PartialStaticMethod implements IPartialExpression {
private final ZenPosition position;
private final JavaMethod method;
private final IJavaMethod method;
public PartialStaticMethod(ZenPosition position, JavaMethod method) {
public PartialStaticMethod(ZenPosition position, IJavaMethod method) {
this.position = position;
this.method = method;
}
@@ -50,18 +52,28 @@ public class PartialStaticMethod implements IPartialExpression {
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
if (method.accepts(environment, values)) {
return method.callStatic(position, environment, values);
return new ExpressionCallStatic(position, environment, method, values);
} else {
environment.error(position, "parameter count mismatch");
return new ExpressionInvalid(position, method.getReturnType());
}
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return Arrays.copyOf(method.getParameterTypes(), numArguments);
}
@Override
public IZenSymbol toSymbol() {
return new SymbolJavaStaticMethod(method);
}
@Override
public ZenType getType() {
return method.getReturnType();
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
return ZenType.ANY;
@@ -55,6 +55,16 @@ public class PartialType implements IPartialExpression {
public IZenSymbol toSymbol() {
return new SymbolType(type);
}
@Override
public ZenType getType() {
return null; // not an expression
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return new ZenType[numArguments];
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
@@ -65,13 +65,10 @@ public class NFA {
Partial[] partials = new Partial[regexp.length];
for (int i = 0; i < regexp.length; i++) {
//System.out.println("Processing regexp " + i + ": " + regexp[i]);
partials[i] = processRegExp(new CharStream(regexp[i]));
partials[i].head.setFinal(finals[i]);
initial.addTransition(partials[i].tailLabel, partials[i].tail);
}
//System.out.println("NFA generated");
}
/**
@@ -230,7 +230,6 @@ public class TokenStream implements Iterator<Token> {
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(file, line, lineOffset, (char)nextChar);
}
} catch (IOException ex) {
@@ -17,7 +17,6 @@ import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionFloat;
import stanhebben.zenscript.expression.ExpressionFunction;
import stanhebben.zenscript.expression.ExpressionInt;
import stanhebben.zenscript.expression.ExpressionString;
import stanhebben.zenscript.parser.ParseException;
@@ -331,7 +330,6 @@ public abstract class ParsedExpression {
}*/
case T_ID:
return new ParsedExpressionVariable(position, parser.next().getValue());
// TODO: implement function values
case T_FUNCTION:
// function (argname, argname, ...) { ...contents... }
parser.next();
@@ -360,18 +358,14 @@ public abstract class ParsedExpression {
parser.required(T_AOPEN, "{ expected");
Statement[] statements;
if (parser.optional(T_ACLOSE) != null) {
statements = new Statement[0];
} else {
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
List<Statement> statements = new ArrayList<Statement>();
if (parser.optional(T_ACLOSE) == null) {
while (parser.optional(T_ACLOSE) == null) {
statementsAL.add(Statement.read(parser, environment));
statements.add(Statement.read(parser, environment, returnType));
}
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
}
return new ParsedExpressionValue(position, new ExpressionFunction(position, arguments, returnType, statements));
return new ParsedExpressionFunction(position, returnType, arguments, statements);
case T_LT: {
Token start = parser.next();
List<Token> tokens = new ArrayList<Token>();
@@ -380,7 +374,7 @@ public abstract class ParsedExpression {
tokens.add(next);
next = parser.next();
}
IZenSymbol resolved = parser.getEnvironment().getBracketed(tokens);
IZenSymbol resolved = parser.getEnvironment().getBracketed(environment, tokens);
if (resolved == null) {
StringBuilder builder = new StringBuilder();
builder.append('<');
@@ -397,7 +391,7 @@ public abstract class ParsedExpression {
} else {
return new ParsedExpressionValue(
start.getPosition(),
parser.getEnvironment().getBracketed(tokens).instance(start.getPosition()));
parser.getEnvironment().getBracketed(environment, tokens).instance(start.getPosition()));
}
}
case T_SQBROPEN: {
@@ -431,7 +425,7 @@ public abstract class ParsedExpression {
}
}
return new ParsedExpressionTable(position, keys, values);
return new ParsedExpressionMap(position, keys, values);
}
}
case T_TRUE:
@@ -463,9 +457,9 @@ public abstract class ParsedExpression {
return position;
}
public abstract IPartialExpression compile(IEnvironmentMethod environment);
public abstract IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType);
public Expression compileKey(IEnvironmentMethod environment) {
return compile(environment).eval(environment);
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
return compile(environment, predictedType).eval(environment);
}
}
@@ -7,8 +7,9 @@
package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.ExpressionOrOr;
import stanhebben.zenscript.expression.ExpressionAndAnd;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -27,10 +28,10 @@ public class ParsedExpressionAndAnd extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
return new ExpressionOrOr(
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionAndAnd(
getPosition(),
left.compile(environment).eval(environment),
right.compile(environment).eval(environment));
left.compile(environment, predictedType).eval(environment),
right.compile(environment, predictedType).eval(environment));
}
}
@@ -10,7 +10,12 @@ import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArray;
import stanhebben.zenscript.expression.ExpressionAs;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeArray;
import stanhebben.zenscript.type.ZenTypeArrayBasic;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -27,20 +32,52 @@ public class ParsedExpressionArray extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
ZenType predictedBaseType = null;
ZenTypeArrayBasic arrayType = ZenType.ANYARRAY;
ICastingRule castingRule = null;
if (predictedType instanceof ZenTypeArray) {
predictedBaseType = ((ZenTypeArray) predictedType).getBaseType();
if (predictedType instanceof ZenTypeArrayBasic) {
// TODO: allow any kind of array type
arrayType = (ZenTypeArrayBasic) predictedType;
}
} else {
// find any[] caster that casts to the given type
castingRule = ZenType.ANYARRAY.getCastingRule(predictedType, environment);
if (castingRule != null) {
if (castingRule.getInputType() instanceof ZenTypeArray) {
predictedBaseType = ((ZenTypeArray) castingRule.getInputType()).getBaseType();
if (castingRule.getInputType() instanceof ZenTypeArrayBasic) {
// TODO: allow any kind of array type
arrayType = (ZenTypeArrayBasic) castingRule.getInputType();
}
} else {
environment.error(getPosition(), "Invalid caster - any[] caster but input type is not an array");
castingRule = null;
}
}
}
Expression[] cContents = new Expression[contents.size()];
for (int i = 0; i < contents.size(); i++) {
cContents[i] = contents.get(i).compile(environment).eval(environment);
cContents[i] = contents.get(i).compile(environment, predictedBaseType).eval(environment);
}
Expression result = new ExpressionArray(getPosition(), arrayType, cContents);
if (castingRule != null) {
return new ExpressionAs(getPosition(), result, castingRule);
} else {
return result;
}
return new ExpressionArray(getPosition(), cContents);
}
@Override
public Expression compileKey(IEnvironmentMethod environment) {
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
return contents.get(0).compile(environment).eval(environment);
return contents.get(0).compile(environment, predictedType).eval(environment);
} else {
return compile(environment).eval(environment);
return compile(environment, predictedType).eval(environment);
}
}
}
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -26,10 +27,10 @@ public class ParsedExpressionAssign extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
IPartialExpression cLeft = left.compile(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
IPartialExpression cLeft = left.compile(environment, predictedType);
return cLeft
.assign(getPosition(), environment,
right.compile(environment).eval(environment));
right.compile(environment, cLeft.getType()).eval(environment));
}
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -30,9 +31,10 @@ public class ParsedExpressionBinary extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cLeft = left.compile(environment).eval(environment);
Expression cRight = right.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
// TODO: make better predictions
Expression cLeft = left.compile(environment, predictedType).eval(environment);
Expression cRight = right.compile(environment, predictedType).eval(environment);
return cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.ExpressionBool;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -25,7 +26,7 @@ public class ParsedExpressionBool extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionBool(getPosition(), value);
}
}
@@ -10,6 +10,7 @@ import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -26,13 +27,15 @@ public class ParsedExpressionCall extends ParsedExpression {
this.receiver = receiver;
this.arguments = arguments;
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
IPartialExpression cReceiver = receiver.compile(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
IPartialExpression cReceiver = receiver.compile(environment, null);
ZenType[] predictedTypes = cReceiver.predictCallTypes(arguments.size());
Expression[] cArguments = new Expression[arguments.size()];
for (int i = 0; i < cArguments.length; i++) {
IPartialExpression cArgument = arguments.get(i).compile(environment);
IPartialExpression cArgument = arguments.get(i).compile(environment, predictedTypes[i]);
cArguments[i] = cArgument.eval(environment);
}
@@ -27,9 +27,9 @@ public class ParsedExpressionCast extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return value
.compile(environment)
.compile(environment, type)
.eval(environment)
.cast(getPosition(), environment, type);
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -34,9 +35,9 @@ public class ParsedExpressionCompare extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cLeft = left.compile(environment).eval(environment);
Expression cRight = right.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
Expression cLeft = left.compile(environment, null).eval(environment);
Expression cRight = right.compile(environment, null).eval(environment);
return cLeft.getType().compare(getPosition(), environment, cLeft, cRight, type);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.ExpressionConditional;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -29,11 +30,11 @@ public class ParsedExpressionConditional extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionConditional(
getPosition(),
condition.compile(environment).eval(environment),
ifThen.compile(environment).eval(environment),
ifElse.compile(environment).eval(environment));
condition.compile(environment, ZenType.BOOL).eval(environment),
ifThen.compile(environment, predictedType).eval(environment),
ifElse.compile(environment, predictedType).eval(environment));
}
}
@@ -0,0 +1,83 @@
/*
* 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.parser.expression;
import java.lang.reflect.Method;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.expression.ExpressionFunction;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionJavaLambda;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.statements.Statement;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAny;
import stanhebben.zenscript.type.ZenTypeNative;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stan
*/
public class ParsedExpressionFunction extends ParsedExpression {
private final ZenType returnType;
private final List<ParsedFunctionArgument> arguments;
private final List<Statement> statements;
public ParsedExpressionFunction(ZenPosition position, ZenType returnType, List<ParsedFunctionArgument> arguments, List<Statement> statements) {
super(position);
this.returnType = returnType;
this.arguments = arguments;
this.statements = statements;
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
if (predictedType != null && predictedType instanceof ZenTypeNative) {
System.out.println("Known predicted function type: " + predictedType);
ZenTypeNative nativeType = (ZenTypeNative) predictedType;
Class nativeClass = nativeType.getNativeClass();
if (nativeClass.isInterface() && nativeClass.getMethods().length == 1) {
// functional interface
Method method = nativeClass.getMethods()[0];
if (returnType != ZenTypeAny.INSTANCE
&& !returnType.canCastImplicit(environment.getType(method.getGenericReturnType()), environment)) {
environment.error(getPosition(), "return type not compatible");
return new ExpressionInvalid(getPosition());
}
if (arguments.size() != method.getParameterTypes().length) {
environment.error(getPosition(), "number of arguments incorrect");
return new ExpressionInvalid(getPosition());
}
for (int i = 0; i < arguments.size(); i++) {
ZenType argumentType = environment.getType(method.getGenericParameterTypes()[i]);
if (arguments.get(i).getType() != ZenTypeAny.INSTANCE
&& !argumentType.canCastImplicit(arguments.get(i).getType(), environment)) {
environment.error(getPosition(), "argument " + i + " doesn't match");
return new ExpressionInvalid(getPosition());
}
}
return new ExpressionJavaLambda(
getPosition(),
nativeClass,
arguments,
statements,
environment.getType(nativeClass));
} else {
environment.error(getPosition(), predictedType.toString() + " is not a functional interface");
return new ExpressionInvalid(getPosition());
}
} else {
System.out.println("No known predicted type");
return new ExpressionFunction(getPosition(), arguments, returnType, statements);
}
}
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -28,9 +29,10 @@ public class ParsedExpressionIndex extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cValue = value.compile(environment).eval(environment);
Expression cIndex = index.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
// TODO: improve type prediction for this
Expression cValue = value.compile(environment, null).eval(environment);
Expression cIndex = index.compile(environment, null).eval(environment);
return cValue.getType().binary(getPosition(), environment, cValue, cIndex, OperatorType.INDEXGET);
}
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -30,10 +31,11 @@ public class ParsedExpressionIndexSet extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cValue = value.compile(environment).eval(environment);
Expression cIndex = index.compile(environment).eval(environment);
Expression cSetValue = setValue.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
// TODO: improve prediction in this expression
Expression cValue = value.compile(environment, null).eval(environment);
Expression cIndex = index.compile(environment, null).eval(environment);
Expression cSetValue = setValue.compile(environment, null).eval(environment);
return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue,OperatorType.INDEXSET);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -21,7 +22,7 @@ public class ParsedExpressionInvalid extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
return new ExpressionInvalid(getPosition());
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionInvalid(getPosition(), predictedType == null ? ZenType.ANY : predictedType);
}
}
@@ -0,0 +1,79 @@
/*
* 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.parser.expression;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionAs;
import stanhebben.zenscript.expression.ExpressionMap;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAssociative;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ParsedExpressionMap extends ParsedExpression {
private final List<ParsedExpression> keys;
private final List<ParsedExpression> values;
public ParsedExpressionMap(
ZenPosition position,
List<ParsedExpression> keys,
List<ParsedExpression> values) {
super(position);
this.keys = keys;
this.values = values;
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
ZenType predictedKeyType = null;
ZenType predictedValueType = null;
ICastingRule castingRule = null;
ZenTypeAssociative mapType = ZenType.ANYMAP;
if (predictedType instanceof ZenTypeAssociative) {
ZenTypeAssociative inputType = (ZenTypeAssociative) predictedType;
predictedKeyType = inputType.getKeyType();
predictedValueType = inputType.getValueType();
mapType = inputType;
} else {
castingRule = ZenType.ANYMAP.getCastingRule(predictedType, environment);
if (castingRule != null) {
if (castingRule.getInputType() instanceof ZenTypeAssociative) {
ZenTypeAssociative inputType = (ZenTypeAssociative) castingRule.getInputType();
predictedKeyType = inputType.getKeyType();
predictedValueType = inputType.getValueType();
mapType = inputType;
} else {
environment.error(getPosition(), "Caster found for any[any] but its input is not an associative array");
}
}
}
Expression[] cKeys = new Expression[keys.size()];
Expression[] cValues = new Expression[values.size()];
for (int i = 0; i < keys.size(); i++) {
cKeys[i] = keys.get(i).compileKey(environment, predictedKeyType);
cValues[i] = values.get(i).compile(environment, predictedValueType).eval(environment);
}
Expression result = new ExpressionMap(getPosition(), cKeys, cValues, mapType);
if (castingRule != null) {
return new ExpressionAs(getPosition(), result, castingRule);
} else {
return result;
}
}
}
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -26,7 +27,7 @@ public class ParsedExpressionMember extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
return value.compile(environment).getMember(getPosition(), environment, member);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return value.compile(environment, null).getMember(getPosition(), environment, member);
}
}
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -21,7 +22,7 @@ public class ParsedExpressionNull extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionNull(getPosition());
}
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -30,12 +31,13 @@ public class ParsedExpressionOpAssign extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cLeft = left.compile(environment).eval(environment);
Expression cRight = right.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
// TODO: validate if the prediction rules are sound
Expression cLeft = left.compile(environment, predictedType).eval(environment);
Expression cRight = right.compile(environment, cLeft.getType()).eval(environment);
Expression value = cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
return left.compile(environment).assign(getPosition(), environment, value);
return left.compile(environment, predictedType).assign(getPosition(), environment, value);
}
}
@@ -7,8 +7,10 @@
package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionOrOr;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -27,10 +29,23 @@ public class ParsedExpressionOrOr extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
Expression cLeft = left.compile(environment, predictedType).eval(environment);
Expression cRight = right.compile(environment, predictedType).eval(environment);
ZenType type;
if (cRight.getType().canCastImplicit(cLeft.getType(), environment)) {
type = cLeft.getType();
} else if (cLeft.getType().canCastImplicit(cRight.getType(), environment)) {
type = cRight.getType();
} else {
environment.error(getPosition(), "These types could not be unified: " + cLeft.getType() + " and " + cRight.getType());
type = ZenType.ANY;
}
return new ExpressionOrOr(
getPosition(),
left.compile(environment).eval(environment),
right.compile(environment).eval(environment));
cLeft.cast(getPosition(), environment, type),
cRight.cast(getPosition(), environment, type));
}
}
@@ -1,46 +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.parser.expression;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionTable;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ParsedExpressionTable extends ParsedExpression {
private final List<ParsedExpression> keys;
private final List<ParsedExpression> values;
public ParsedExpressionTable(
ZenPosition position,
List<ParsedExpression> keys,
List<ParsedExpression> values) {
super(position);
this.keys = keys;
this.values = values;
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression[] cKeys = new Expression[keys.size()];
Expression[] cValues = new Expression[values.size()];
for (int i = 0; i < keys.size(); i++) {
cKeys[i] = keys.get(i).compileKey(environment);
cValues[i] = values.get(i).compile(environment).eval(environment);
}
return new ExpressionTable(getPosition(), cKeys, cValues);
}
}
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -28,8 +29,9 @@ public class ParsedExpressionUnary extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
Expression cValue = value.compile(environment).eval(environment);
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
// TODO: improve type predictions?
Expression cValue = value.compile(environment, predictedType).eval(environment);
return cValue.getType().unary(getPosition(), environment, cValue, operator);
}
}
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -26,7 +27,7 @@ public class ParsedExpressionValue extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
return value;
}
}
@@ -11,6 +11,7 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionString;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -27,18 +28,24 @@ public class ParsedExpressionVariable extends ParsedExpression {
}
@Override
public IPartialExpression compile(IEnvironmentMethod environment) {
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
IPartialExpression result = environment.getValue(name, getPosition());
if (result == null) {
environment.error(getPosition(), "could not find " + name);
return new ExpressionInvalid(getPosition());
// enable usage of static members of the same type as the predicted type (eg. enum values)
IPartialExpression member = predictedType.getStaticMember(getPosition(), environment, name);
if (member == null || member.getType().getCastingRule(predictedType, environment) == null) {
environment.error(getPosition(), "could not find " + name);
return new ExpressionInvalid(getPosition());
} else {
return member;
}
} else {
return result;
}
}
@Override
public Expression compileKey(IEnvironmentMethod environment) {
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
return new ExpressionString(getPosition(), name);
}
}
@@ -13,14 +13,14 @@ import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
public abstract class Statement {
public static Statement read(ZenTokener parser, IEnvironmentGlobal environment) {
public static Statement read(ZenTokener parser, IEnvironmentGlobal environment, ZenType returnType) {
Token next = parser.peek();
switch (next.getType()) {
case T_AOPEN: {
Token t = parser.next();
ArrayList<Statement> statements = new ArrayList<Statement>();
while (parser.optional(T_ACLOSE) == null) {
statements.add(read(parser, environment));
statements.add(read(parser, environment, returnType));
}
return new StatementBlock(t.getPosition(), statements);
}
@@ -28,7 +28,7 @@ public abstract class Statement {
parser.next();
ParsedExpression expression = ParsedExpression.read(parser, environment);
parser.required(T_SEMICOLON, "; expected");
return new StatementReturn(next.getPosition(), expression);
return new StatementReturn(next.getPosition(), returnType, expression);
}
case T_VAR:
case T_VAL: {
@@ -49,10 +49,10 @@ public abstract class Statement {
case T_IF: {
Token t = parser.next();
ParsedExpression expression = ParsedExpression.read(parser, environment);
Statement onIf = read(parser, environment);
Statement onIf = read(parser, environment, returnType);
Statement onElse = null;
if (parser.optional(T_ELSE) != null) {
onElse = read(parser, environment);
onElse = read(parser, environment, returnType);
}
return new StatementIf(t.getPosition(), expression, onIf, onElse);
}
@@ -68,7 +68,7 @@ public abstract class Statement {
parser.required(T_IN, "in expected");
ParsedExpression source = ParsedExpression.read(parser, environment);
Statement content = read(parser, environment);
Statement content = read(parser, environment, null);
return new StatementForeach(
t.getPosition(),
names.toArray(new String[names.size()]),
@@ -16,7 +16,7 @@ public class StatementExpression extends Statement {
@Override
public void compile(IEnvironmentMethod environment) {
environment.getOutput().position(getPosition());
expression.compile(environment)
expression.compile(environment, null)
.eval(environment)
.compile(false, environment);
}
@@ -26,7 +26,7 @@ public class StatementForeach extends Statement {
@Override
public void compile(IEnvironmentMethod environment) {
Expression cList = list.compile(environment).eval(environment);
Expression cList = list.compile(environment, ZenType.ANYARRAY).eval(environment);
ZenType listType = cList.getType();
IZenIterator iterator = listType.makeIterator(varnames.length, environment);
@@ -5,7 +5,6 @@ import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.parser.expression.ParsedExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
@@ -26,7 +25,7 @@ public class StatementIf extends Statement {
public void compile(IEnvironmentMethod environment) {
environment.getOutput().position(getPosition());
Expression cCondition = condition.compile(environment).eval(environment);
Expression cCondition = condition.compile(environment, ZenType.BOOL).eval(environment);
ZenType expressionType = cCondition.getType();
if (expressionType.canCastImplicit(ZenType.BOOL, environment)) {
Label labelEnd = new Label();
@@ -4,14 +4,17 @@ import org.objectweb.asm.Type;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.parser.expression.ParsedExpression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
public class StatementReturn extends Statement {
private final ZenType returnType;
private final ParsedExpression expression;
public StatementReturn(ZenPosition position, ParsedExpression expression) {
public StatementReturn(ZenPosition position, ZenType returnType, ParsedExpression expression) {
super(position);
this.returnType = returnType;
this.expression = expression;
}
@@ -31,7 +34,7 @@ public class StatementReturn extends Statement {
if (expression == null) {
environment.getOutput().ret();
} else {
Expression cExpression = expression.compile(environment).eval(environment);
Expression cExpression = expression.compile(environment, returnType).eval(environment);
cExpression.compile(true, environment);
Type returnType = cExpression.getType().toASMType();
@@ -39,7 +39,7 @@ public class StatementVar extends Statement {
Expression cInitializer = initializer == null
? null
: initializer.compile(environment).eval(environment);
: initializer.compile(environment, type).eval(environment);
ZenType cType = type == null ? (cInitializer == null ? ZenTypeAny.INSTANCE : cInitializer.getType()) : type;
SymbolLocal symbol = new SymbolLocal(cType, isFinal);
@@ -8,7 +8,7 @@ package stanhebben.zenscript.symbols;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.expression.partial.PartialStaticGetter;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -16,9 +16,9 @@ import stanhebben.zenscript.util.ZenPosition;
* @author Stanneke
*/
public class SymbolJavaStaticGetter implements IZenSymbol {
private final JavaMethod method;
private final IJavaMethod method;
public SymbolJavaStaticGetter(JavaMethod method) {
public SymbolJavaStaticGetter(IJavaMethod method) {
this.method = method;
}
@@ -8,7 +8,7 @@ package stanhebben.zenscript.symbols;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.expression.partial.PartialStaticMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -16,9 +16,9 @@ import stanhebben.zenscript.util.ZenPosition;
* @author Stanneke
*/
public class SymbolJavaStaticMethod implements IZenSymbol {
private final JavaMethod method;
private final IJavaMethod method;
public SymbolJavaStaticMethod(JavaMethod method) {
public SymbolJavaStaticMethod(IJavaMethod method) {
this.method = method;
}
@@ -1,6 +1,8 @@
package stanhebben.zenscript.type;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.Type;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.ZenTokener;
@@ -12,22 +14,38 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.parser.ParseException;
import stanhebben.zenscript.parser.Token;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleDelegateMap;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.expand.ZenExpandCaster;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.EMPTY_REGISTRY;
public abstract class ZenType {
public static final ZenTypeAny ANY = ZenTypeAny.INSTANCE;
public static final ZenTypeBool BOOL = new ZenTypeBool();
public static final ZenTypeBoolObject BOOLOBJECT = ZenTypeBoolObject.INSTANCE;
public static final ZenTypeByte BYTE = ZenTypeByte.INSTANCE;
public static final ZenTypeByteObject BYTEOBJECT = ZenTypeByteObject.INSTANCE;
public static final ZenTypeShort SHORT = ZenTypeShort.INSTANCE;
public static final ZenTypeShortObject SHORTOBJECT = ZenTypeShortObject.INSTANCE;
public static final ZenTypeInt INT = ZenTypeInt.INSTANCE;
public static final ZenTypeIntObject INTOBJECT = ZenTypeIntObject.INSTANCE;
public static final ZenTypeLong LONG = ZenTypeLong.INSTANCE;
public static final ZenTypeLongObject LONGOBJECT = ZenTypeLongObject.INSTANCE;
public static final ZenTypeFloat FLOAT = ZenTypeFloat.INSTANCE;
public static final ZenTypeFloatObject FLOATOBJECT = ZenTypeFloatObject.INSTANCE;
public static final ZenTypeDouble DOUBLE = ZenTypeDouble.INSTANCE;
public static final ZenTypeDoubleObject DOUBLEOBJECT = ZenTypeDoubleObject.INSTANCE;
public static final ZenTypeString STRING = ZenTypeString.INSTANCE;
public static final ZenTypeVoid VOID = ZenTypeVoid.INSTANCE;
public static final ZenTypeNull NULL = ZenTypeNull.INSTANCE;
public static final ZenTypeArrayBasic ANYARRAY = new ZenTypeArrayBasic(ANY);
public static final ZenTypeAssociative ANYMAP = new ZenTypeAssociative(ANY, ANY);
public static final int NUM_BYTE = 1;
public static final int NUM_SHORT = 2;
public static final int NUM_INT = 3;
@@ -35,6 +53,54 @@ public abstract class ZenType {
public static final int NUM_FLOAT = 5;
public static final int NUM_DOUBLE = 6;
protected static final IJavaMethod BOOL_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "valueOf", boolean.class);
protected static final IJavaMethod BYTE_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Byte.class, "valueOf", byte.class);
protected static final IJavaMethod SHORT_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Short.class, "valueOf", short.class);
protected static final IJavaMethod INT_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "valueOf", int.class);
protected static final IJavaMethod LONG_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Long.class, "valueOf", long.class);
protected static final IJavaMethod FLOAT_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Float.class, "valueOf", float.class);
protected static final IJavaMethod DOUBLE_VALUEOF = JavaMethod.get(EMPTY_REGISTRY, Double.class, "valueOf", double.class);
protected static final IJavaMethod BOOL_VALUE = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "booleanValue");
protected static final IJavaMethod BYTE_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "byteValue");
protected static final IJavaMethod SHORT_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "shortValue");
protected static final IJavaMethod INT_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "intValue");
protected static final IJavaMethod LONG_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "longValue");
protected static final IJavaMethod FLOAT_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "floatValue");
protected static final IJavaMethod DOUBLE_VALUE = JavaMethod.get(EMPTY_REGISTRY, Number.class, "doubleValue");
protected static final IJavaMethod BOOL_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "toString", boolean.class);
protected static final IJavaMethod BYTE_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Byte.class, "toString", byte.class);
protected static final IJavaMethod SHORT_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Short.class, "toString", short.class);
protected static final IJavaMethod INT_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "toString", int.class);
protected static final IJavaMethod LONG_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Long.class, "toString", long.class);
protected static final IJavaMethod FLOAT_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Float.class, "toString", float.class);
protected static final IJavaMethod DOUBLE_TOSTRING_STATIC = JavaMethod.get(EMPTY_REGISTRY, Double.class, "toString", double.class);
protected static final IJavaMethod BOOL_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "toString");
protected static final IJavaMethod BYTE_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Byte.class, "toString");
protected static final IJavaMethod SHORT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Short.class, "toString");
protected static final IJavaMethod INT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "toString");
protected static final IJavaMethod LONG_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Long.class, "toString");
protected static final IJavaMethod FLOAT_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Float.class, "toString");
protected static final IJavaMethod DOUBLE_TOSTRING = JavaMethod.get(EMPTY_REGISTRY, Double.class, "toString");
protected static final IJavaMethod PARSE_BOOL = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "parseBoolean", String.class);
protected static final IJavaMethod PARSE_BYTE = JavaMethod.get(EMPTY_REGISTRY, Byte.class, "parseByte", String.class);
protected static final IJavaMethod PARSE_SHORT = JavaMethod.get(EMPTY_REGISTRY, Short.class, "parseShort", String.class);
protected static final IJavaMethod PARSE_INT = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "parseInt", String.class);
protected static final IJavaMethod PARSE_LONG = JavaMethod.get(EMPTY_REGISTRY, Long.class, "parseLong", String.class);
protected static final IJavaMethod PARSE_FLOAT = JavaMethod.get(EMPTY_REGISTRY, Float.class, "parseFloat", String.class);
protected static final IJavaMethod PARSE_DOUBLE = JavaMethod.get(EMPTY_REGISTRY, Double.class, "parseDouble", String.class);
protected static final IJavaMethod PARSE_BOOL_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Boolean.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_BYTE_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Byte.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_SHORT_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Short.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_INT_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Integer.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_LONG_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Long.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_FLOAT_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Float.class, "valueOf", String.class);
protected static final IJavaMethod PARSE_DOUBLE_OBJECT = JavaMethod.get(EMPTY_REGISTRY, Double.class, "valueOf", String.class);
public static ZenType parse(String type, IEnvironmentGlobal environment) {
try {
ZenTokener parser = new ZenTokener(type, environment.getEnvironment());
@@ -99,7 +165,6 @@ public abstract class ZenType {
}
if (partial != null) {
base = partial.toType(environment);
System.out.println("Parsed type " + base.getName());
}
break;
default:
@@ -118,6 +183,8 @@ public abstract class ZenType {
return base;
}
private Map<ZenType, ICastingRule> castingRules = null;
public abstract Expression unary(
ZenPosition position,
IEnvironmentGlobal environment,
@@ -163,15 +230,35 @@ public abstract class ZenType {
Expression receiver,
Expression... arguments);
public abstract Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type);
public ZenType[] predictCallTypes(int numArguments) {
return new ZenType[numArguments];
}
public abstract void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters);
public abstract IZenIterator makeIterator(
int numValues,
IEnvironmentMethod methodOutput);
public abstract boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment);
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
if (castingRules == null) {
castingRules = new HashMap<ZenType, ICastingRule>();
constructCastingRules(environment, new CastingRuleDelegateMap(castingRules), true);
}
return castingRules.get(type);
}
public final boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
if (equals(type))
return true;
return getCastingRule(type, environment) != null;
}
public abstract boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment);
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return canCastImplicit(type, environment);
}
public abstract Class toJavaClass();
@@ -183,7 +270,17 @@ public abstract class ZenType {
public abstract boolean isPointer();
public abstract void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type);
public final void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (equals(type))
return;
ICastingRule castingRule = getCastingRule(type, environment);
if (castingRule != null) {
castingRule.compile(environment);
} else {
throw new RuntimeException("Cannot cast " + getName() + " to " + type.getName());
}
}
public abstract String getAnyClassName(IEnvironmentGlobal environment);
@@ -251,6 +348,13 @@ public abstract class ZenType {
return null;
}
protected void constructExpansionCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules) {
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
expansion.constructCastingRules(environment, rules);
}
}
protected Expression castExpansion(
ZenPosition position,
IEnvironmentGlobal environment,
@@ -1,24 +1,30 @@
package stanhebben.zenscript.type;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.ExpressionCallVirtual;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingAnyBool;
import stanhebben.zenscript.type.casting.CastingAnyByte;
import stanhebben.zenscript.type.casting.CastingAnyDouble;
import stanhebben.zenscript.type.casting.CastingAnyFloat;
import stanhebben.zenscript.type.casting.CastingAnyInt;
import stanhebben.zenscript.type.casting.CastingAnyLong;
import stanhebben.zenscript.type.casting.CastingAnyShort;
import stanhebben.zenscript.type.casting.CastingAnyString;
import stanhebben.zenscript.type.casting.CastingRuleAnyAs;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
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;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
@@ -30,205 +36,6 @@ import stanhebben.zenscript.value.IAny;
public class ZenTypeAny extends ZenType {
public static final ZenTypeAny INSTANCE = new ZenTypeAny();
private static final Map<String, MethodCompiler> CASTERS;
static {
CASTERS = new HashMap<String, MethodCompiler>();
CASTERS.put("bool", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asBool", boolean.class);
}
});
CASTERS.put("byte", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asByte", byte.class);
}
});
CASTERS.put("short", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asShort", short.class);
}
});
CASTERS.put("int", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asInt", int.class);
}
});
CASTERS.put("long", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asLong", long.class);
}
});
CASTERS.put("float", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asFloat", float.class);
}
});
CASTERS.put("double", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
output.invokeInterface(IAny.class, "asDouble", double.class);
}
});
CASTERS.put("string", new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asString", String.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Boolean.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asBool", boolean.class);
output.invokeStatic(Boolean.class, "valueOf", Boolean.class, boolean.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Byte.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asByte", boolean.class);
output.invokeStatic(Byte.class, "valueOf", Byte.class, byte.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Short.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asShort", short.class);
output.invokeStatic(Short.class, "valueOf", Short.class, short.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Integer.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asInt", int.class);
output.invokeStatic(Integer.class, "valueOf", Integer.class, int.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Long.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asLong", long.class);
output.invokeStatic(Long.class, "valueOf", Long.class, long.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Float.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asFloat", float.class);
output.invokeStatic(Float.class, "valueOf", Float.class, float.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(Double.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asDouble", double.class);
output.invokeStatic(Double.class, "valueOf", Double.class, double.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
CASTERS.put(String.class.getName(), new MethodCompiler() {
@Override
public void compile(MethodOutput output) {
Label lblNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNull(lblNull);
output.invokeInterface(IAny.class, "asString", String.class);
output.goTo(lblAfter);
output.label(lblNull);
output.pop();
output.aConstNull();
output.label(lblAfter);
}
});
}
private ZenTypeAny() {}
@Override
@@ -247,12 +54,40 @@ public class ZenTypeAny extends ZenType {
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
return null; // TODO: handle iteration on any-values
}
@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return true;
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
ICastingRule base = super.getCastingRule(type, environment);
if (base == null) {
return new CastingRuleAnyAs(type);
} else {
return base;
}
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BOOL, CastingAnyBool.INSTANCE);
rules.registerCastingRule(BOOLOBJECT, new CastingRuleNullableStaticMethod(BOOL_VALUEOF, CastingAnyBool.INSTANCE));
rules.registerCastingRule(BYTE, CastingAnyByte.INSTANCE);
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(BYTE_VALUEOF, CastingAnyByte.INSTANCE));
rules.registerCastingRule(SHORT, CastingAnyShort.INSTANCE);
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(SHORT_VALUEOF, CastingAnyShort.INSTANCE));
rules.registerCastingRule(INT, CastingAnyInt.INSTANCE);
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(INT_VALUEOF, CastingAnyInt.INSTANCE));
rules.registerCastingRule(LONG, CastingAnyLong.INSTANCE);
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(LONG_VALUEOF, CastingAnyLong.INSTANCE));
rules.registerCastingRule(FLOAT, CastingAnyFloat.INSTANCE);
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(FLOAT_VALUEOF, CastingAnyFloat.INSTANCE));
rules.registerCastingRule(DOUBLE, CastingAnyDouble.INSTANCE);
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(DOUBLE_VALUEOF, CastingAnyDouble.INSTANCE));
rules.registerCastingRule(STRING, CastingAnyString.INSTANCE);
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return true;
@@ -282,26 +117,14 @@ public class ZenTypeAny extends ZenType {
public boolean isPointer() {
return true;
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType toType) {
if (CASTERS.containsKey(toType.getName())) {
CASTERS.get(toType.getName()).compile(environment.getOutput());
} else {
if (!compileCastExpansion(position, environment, toType)) {
environment.getOutput().constant(toType);
environment.getOutput().invokeInterface(IAny.class, "as", Class.class, Object.class);
}
}
}
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
switch (operator) {
case NEG:
return new ExpressionJavaCallVirtual(position, METHOD_NEG, value);
return new ExpressionCallVirtual(position, environment, METHOD_NEG, value);
case NOT:
return new ExpressionJavaCallVirtual(position, METHOD_NOT, value);
return new ExpressionCallVirtual(position, environment, METHOD_NOT, value);
default:
return new ExpressionInvalid(position, ZenTypeAny.INSTANCE);
}
@@ -311,33 +134,33 @@ public class ZenTypeAny extends ZenType {
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
switch (operator) {
case ADD:
return new ExpressionJavaCallVirtual(position, METHOD_ADD, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_ADD, left, right.cast(position, environment, ANY));
case CAT:
return new ExpressionJavaCallVirtual(position, METHOD_CAT, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_CAT, left, right.cast(position, environment, ANY));
case SUB:
return new ExpressionJavaCallVirtual(position, METHOD_SUB, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_SUB, left, right.cast(position, environment, ANY));
case MUL:
return new ExpressionJavaCallVirtual(position, METHOD_MUL, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_MUL, left, right.cast(position, environment, ANY));
case DIV:
return new ExpressionJavaCallVirtual(position, METHOD_DIV, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_DIV, left, right.cast(position, environment, ANY));
case MOD:
return new ExpressionJavaCallVirtual(position, METHOD_MOD, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_MOD, left, right.cast(position, environment, ANY));
case AND:
return new ExpressionJavaCallVirtual(position, METHOD_AND, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_AND, left, right.cast(position, environment, ANY));
case OR:
return new ExpressionJavaCallVirtual(position, METHOD_OR, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_OR, left, right.cast(position, environment, ANY));
case XOR:
return new ExpressionJavaCallVirtual(position, METHOD_XOR, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_XOR, left, right.cast(position, environment, ANY));
case CONTAINS:
return new ExpressionJavaCallVirtual(position, METHOD_CONTAINS, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_CONTAINS, left, right.cast(position, environment, ANY));
case INDEXGET:
return new ExpressionJavaCallVirtual(position, METHOD_INDEXGET, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_INDEXGET, left, right.cast(position, environment, ANY));
case RANGE:
return new ExpressionJavaCallVirtual(position, METHOD_RANGE, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_RANGE, left, right.cast(position, environment, ANY));
case COMPARE:
return new ExpressionJavaCallVirtual(position, METHOD_COMPARETO, left, right.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, METHOD_COMPARETO, left, right.cast(position, environment, ANY));
case MEMBERGETTER:
return new ExpressionJavaCallVirtual(position, METHOD_MEMBERGET, left, right.cast(position, environment, STRING));
return new ExpressionCallVirtual(position, environment, METHOD_MEMBERGET, left, right.cast(position, environment, STRING));
default:
return new ExpressionInvalid(position, ANY);
}
@@ -347,9 +170,9 @@ public class ZenTypeAny extends ZenType {
public Expression trinary(ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
switch (operator) {
case INDEXSET:
return new ExpressionJavaCallVirtual(position, METHOD_INDEXSET, first, second.cast(position, environment, ANY), third.cast(position, environment, ANY));
return new ExpressionCallVirtual(position, environment, 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));
return new ExpressionCallVirtual(position, environment, METHOD_MEMBERSET, first, second.cast(position, environment, STRING), third.cast(position, environment, ANY));
default:
return new ExpressionInvalid(position, ANY);
}
@@ -357,9 +180,12 @@ public class ZenTypeAny extends ZenType {
@Override
public Expression compare(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, CompareType type) {
Expression comparator = JavaMethod
.get(environment, IAny.class, "compareTo", IAny.class)
.callVirtual(position, environment, left, right);
Expression comparator = new ExpressionCallVirtual(
position,
environment,
JavaMethod.get(environment, IAny.class, "compareTo", IAny.class),
left,
right);
return new ExpressionCompareGeneric(
position,
@@ -369,20 +195,21 @@ public class ZenTypeAny extends ZenType {
@Override
public Expression call(ZenPosition position, IEnvironmentGlobal environment, Expression receiver, Expression... arguments) {
return JavaMethod
.get(environment, IAny.class, "call", IAny[].class)
.callVirtual(position, environment, receiver, arguments);
return new ExpressionCallVirtual(
position,
environment,
JavaMethod.get(environment, IAny.class, "call", IAny[].class),
receiver,
arguments);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type.equals(this)) return value;
if (canCastExpansion(environment, type)) {
return castExpansion(position, environment, value, type);
} else {
return new ExpressionAs(position, value, type);
public ZenType[] predictCallTypes(int numArguments) {
ZenType[] result = new ZenType[numArguments];
for (int i = 0; i < result.length; i++) {
result[i] = ANY;
}
return result;
}
@Override
@@ -44,19 +44,6 @@ public abstract class ZenTypeArray extends ZenType {
return name;
}
@Override
public final boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
if (equals(type) || canCastExpansion(environment, type)) {
return true;
}
if (type instanceof ZenTypeArray) {
return base.canCastImplicit(((ZenTypeArray) type).base, environment);
}
return false;
}
@Override
public final boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return equals(type) || canCastExpansion(environment, type);
@@ -1,7 +1,5 @@
package stanhebben.zenscript.type;
import java.util.Arrays;
import java.util.List;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
@@ -11,6 +9,11 @@ import stanhebben.zenscript.expression.ExpressionArrayGet;
import stanhebben.zenscript.expression.ExpressionArrayLength;
import stanhebben.zenscript.expression.ExpressionArraySet;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleArrayArray;
import stanhebben.zenscript.type.casting.CastingRuleArrayList;
import stanhebben.zenscript.type.casting.CastingRuleDelegateArray;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
@@ -41,12 +44,32 @@ public class ZenTypeArrayBasic extends ZenTypeArray {
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (equals(type)) return value;
return castExpansion(position, environment, value, type);
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
ICastingRule base = super.getCastingRule(type, environment);
if (base == null && getBaseType() == ANY && type instanceof ZenTypeArray) {
ZenType toBaseType = ((ZenTypeArray)type).getBaseType();
if (type instanceof ZenTypeArrayBasic) {
return new CastingRuleArrayArray(ANY.getCastingRule(toBaseType, environment), this, (ZenTypeArrayBasic) type);
} else if (type instanceof ZenTypeArrayList) {
return new CastingRuleArrayList(ANY.getCastingRule(toBaseType, environment), this, (ZenTypeArrayList) type);
} else {
throw new RuntimeException("Invalid array type: " + type);
}
} else {
return base;
}
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
ICastingRuleDelegate arrayRules = new CastingRuleDelegateArray(rules, this);
getBaseType().constructCastingRules(environment, arrayRules, followCasters);
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
@Override
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
if (numValues == 1) {
@@ -71,7 +94,7 @@ public class ZenTypeArrayBasic extends ZenTypeArray {
@Override
public Class toJavaClass() {
try {
return Class.forName("[" + getBaseType().toJavaClass().getName());
return Class.forName("[L" + getBaseType().toJavaClass().getName() + ";");
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
@@ -81,68 +104,7 @@ public class ZenTypeArrayBasic extends ZenTypeArray {
public String getSignature() {
return "[" + getBaseType().getSignature();
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType toClass) {
MethodOutput output = environment.getOutput();
if (compileCastExpansion(position, environment, toClass)) {
// OK
} else if (toClass instanceof ZenTypeArrayList) {
// convert array to list
// Arrays.asList(value);
output.invokeStatic(Arrays.class, "asList", List.class, Object[].class);
} else if (toClass instanceof ZenTypeArrayBasic) {
// convert elements
if (toClass.equals(this)) {
// do nothing
} else {
ZenType component = ((ZenTypeArrayBasic) toClass).getBaseType();
Type componentType = component.toASMType();
int result = output.local(componentType);
output.dup();
output.arrayLength();
output.newArray(componentType);
output.storeObject(result);
output.iConst0();
Label lbl = new Label();
output.label(lbl);
// stack: original index
output.dupX1();
output.dupX1();
output.arrayLoad(componentType);
// stack: original index value
getBaseType().compileCast(position, environment, toClass);
output.loadObject(result);
output.dupX2();
output.dupX2();
output.arrayStore(componentType);
output.pop();
// stack: original index
output.iConst1();
output.iAdd();
output.dupX1();
output.arrayLength();
output.ifICmpGE(lbl);
output.pop();
output.pop();
output.loadObject(result);
}
} else {
// TODO: error
}
}
@Override
public IPartialExpression getMemberLength(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value) {
return new ExpressionArrayLength(position, value.eval(environment));
@@ -7,15 +7,15 @@
package stanhebben.zenscript.type;
import java.util.List;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleDelegateList;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.iterator.IteratorIterable;
import stanhebben.zenscript.type.iterator.IteratorList;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
@@ -38,12 +38,15 @@ public class ZenTypeArrayList extends ZenTypeArray {
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (equals(type)) return value;
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
ICastingRuleDelegate arrayRules = new CastingRuleDelegateList(rules, this);
getBaseType().constructCastingRules(environment, arrayRules, followCasters);
return castExpansion(position, environment, value, type);
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
@Override
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
if (numValues == 1) {
@@ -74,74 +77,16 @@ public class ZenTypeArrayList extends ZenTypeArray {
public String getSignature() {
return signature(List.class);
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
if (equals(type)) {
return;
}
if (compileCastExpansion(position, environment, type)) {
// OK
} else if (type instanceof ZenTypeArrayList) {
// TODO: implement
} else if (type instanceof ZenTypeArrayBasic) {
// convert elements
ZenType component = ((ZenTypeArrayBasic) type).getBaseType();
Type componentType = component.toASMType();
Type baseType = getBaseType().toASMType();
int result = output.local(componentType);
output.dup();
output.arrayLength();
output.newArray(baseType);
output.storeObject(result);
output.iConst0();
Label lbl = new Label();
output.label(lbl);
// stack: original index
output.dupX1();
output.dupX1();
output.arrayLoad(componentType);
// stack: original index value
getBaseType().compileCast(position, environment, type);
output.loadObject(result);
output.dupX2();
output.dupX2();
output.arrayStore(baseType);
output.pop();
// stack: original index
output.iConst1();
output.iAdd();
output.dupX1();
output.arrayLength();
output.ifICmpGE(lbl);
output.pop();
output.pop();
output.loadObject(result);
} else {
// TODO: error
}
}
@Override
public Expression indexGet(ZenPosition position, IEnvironmentGlobal environment, Expression array, Expression index) {
// TODO: implement
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Expression indexSet(ZenPosition position, IEnvironmentGlobal environment, Expression array, Expression index, Expression value) {
// TODO: implement
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@@ -22,6 +22,9 @@ import stanhebben.zenscript.expression.ExpressionMapSize;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.ExpressionString;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleMap;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.iterator.IteratorMap;
import stanhebben.zenscript.type.iterator.IteratorMapKeys;
import stanhebben.zenscript.util.ZenPosition;
@@ -32,24 +35,39 @@ import static stanhebben.zenscript.util.ZenTypeUtil.signature;
* @author Stanneke
*/
public class ZenTypeAssociative extends ZenType {
private final ZenType base;
private final ZenType key;
private final ZenType valueType;
private final ZenType keyType;
private final String name;
public ZenTypeAssociative(ZenType base, ZenType key) {
this.base = base;
this.key = key;
public ZenTypeAssociative(ZenType valueType, ZenType keyType) {
this.valueType = valueType;
this.keyType = keyType;
name = base.getName() + "[" + key.getName() + "]";
name = valueType.getName() + "[" + keyType.getName() + "]";
}
public ZenType getValueType() {
return base;
return valueType;
}
public ZenType getKeyType() {
return key;
return keyType;
}
@Override
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
ICastingRule base = super.getCastingRule(type, environment);
if (base == null && type instanceof ZenTypeAssociative && keyType == ANY && valueType == ANY) {
ZenTypeAssociative aType = (ZenTypeAssociative) type;
return new CastingRuleMap(
ANY.getCastingRule(aType.keyType, environment),
ANY.getCastingRule(aType.valueType, environment),
this,
aType);
} else {
return base;
}
}
@Override
@@ -71,9 +89,9 @@ public class ZenTypeAssociative extends ZenType {
@Override
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
if (operator == OperatorType.CONTAINS) {
return new ExpressionMapContains(position, left, right.cast(position, environment, key));
return new ExpressionMapContains(position, left, right.cast(position, environment, keyType));
} else if (operator == OperatorType.INDEXGET) {
return new ExpressionMapIndexGet(position, left, right.cast(position, environment, key));
return new ExpressionMapIndexGet(position, left, right.cast(position, environment, keyType));
} else {
Expression result = binaryExpansion(position, environment, left, right, operator);
if (result == null) {
@@ -91,8 +109,8 @@ public class ZenTypeAssociative extends ZenType {
return new ExpressionMapIndexSet(
position,
first,
second.cast(position, environment, key),
third.cast(position, environment, base));
second.cast(position, environment, keyType),
third.cast(position, environment, valueType));
} else {
Expression result = trinaryExpansion(position, environment, first, second, third, operator);
if (result == null) {
@@ -119,16 +137,16 @@ public class ZenTypeAssociative extends ZenType {
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
if (name.equals("length")) {
return new ExpressionMapSize(position, value.eval(environment));
} else if (STRING.canCastImplicit(key, environment)) {
} else if (STRING.canCastImplicit(keyType, environment)) {
return new ExpressionMapIndexGet(
position,
value.eval(environment),
new ExpressionString(position, name).cast(position, environment, key));
new ExpressionString(position, name).cast(position, environment, keyType));
} else {
IPartialExpression result = memberExpansion(position, environment, value.eval(environment), name);
if (result == null) {
environment.error(position, "this array is not indexable with strings");
return new ExpressionInvalid(position, base);
return new ExpressionInvalid(position, valueType);
} else {
return result;
}
@@ -153,14 +171,12 @@ public class ZenTypeAssociative extends ZenType {
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type == this) {
return value;
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
return castExpansion(position, environment, value, type);
}
@Override
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
if (numValues == 1) {
@@ -172,11 +188,6 @@ public class ZenTypeAssociative extends ZenType {
}
}
@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this || canCastAssociative(type, environment) || canCastExpansion(environment, type);
}
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this || canCastAssociative(type, environment) || canCastExpansion(environment, type);
@@ -207,15 +218,6 @@ public class ZenTypeAssociative extends ZenType {
return true;
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (equals(type)) {
// nothing to do
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
@Override
public String getName() {
return name;
@@ -13,12 +13,14 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBOOL;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
@@ -33,7 +35,6 @@ import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
public class ZenTypeBool extends ZenType {
//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;";
@@ -44,18 +45,31 @@ public class ZenTypeBool extends ZenType {
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
return null;
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(BOOL_TOSTRING_STATIC));
rules.registerCastingRule(BOOLOBJECT, new CastingRuleStaticMethod(BOOL_VALUEOF));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(
getAnyClassName(environment), "valueOf", ANY, BOOL
)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == BOOL || type == ANY || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
}
}*/
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type == BOOL || type == ANY || type == ZenTypeString.INSTANCE || canCastExpansion(environment, type);
return canCastImplicit(type, environment);
}
@Override
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type == BOOL || type == ZenTypeBoolObject.INSTANCE || type == STRING || type == ANY) {
return new ExpressionAs(position, value, type);
@@ -64,7 +78,7 @@ public class ZenTypeBool extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -140,7 +154,7 @@ public class ZenTypeBool extends ZenType {
environment.error(position, "cannot call a boolean value");
return new ExpressionInvalid(position, ZenTypeAny.INSTANCE);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
IPartialExpression result = memberExpansion(position, environment, value.eval(environment), name);
@@ -168,7 +182,7 @@ public class ZenTypeBool extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -181,7 +195,7 @@ public class ZenTypeBool extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "Cannot compile bool to " + type);
}
}
}*/
@Override
public String getName() {
@@ -321,7 +335,7 @@ public class ZenTypeBool extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, boolean.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -349,7 +363,7 @@ public class ZenTypeBool extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBOOL);
METHOD_ASBOOL.invokeVirtual(output);
output.iAnd();
valueOf(output);
output.returnObject();
@@ -361,7 +375,7 @@ public class ZenTypeBool extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBOOL);
METHOD_ASBOOL.invokeVirtual(output);
output.iOr();
valueOf(output);
output.returnObject();
@@ -373,7 +387,7 @@ public class ZenTypeBool extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iXor();
valueOf(output);
output.returnObject();
@@ -388,7 +402,7 @@ public class ZenTypeBool extends ZenType {
public void defineCompareTo(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBOOL);
METHOD_ASBOOL.invokeVirtual(output);
output.iSub();
output.returnInt();
}
@@ -6,22 +6,22 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BOOL;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -66,20 +66,32 @@ public class ZenTypeBoolObject extends ZenType {
public Expression call(ZenPosition position, IEnvironmentGlobal environment, Expression receiver, Expression... arguments) {
return BOOL.call(position, environment, receiver.cast(position, environment, BOOL), arguments);
}
@Override
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
return BOOL.makeIterator(numValues, methodOutput);
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BOOL, new CastingRuleVirtualMethod(BOOL_VALUE));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(BOOL, BOOL_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(JavaMethod.getStatic(
BOOL.getAnyClassName(environment),
"valueOf",
ANY,
BOOL
), new CastingRuleVirtualMethod(BOOL_VALUE)));
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return BOOL.canCastImplicit(type, environment);
}
}*/
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return BOOL.canCastExplicit(type, environment);
return canCastImplicit(type, environment);
}
@Override
@@ -107,7 +119,7 @@ public class ZenTypeBoolObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -135,7 +147,7 @@ public class ZenTypeBoolObject extends ZenType {
environment.getOutput().invokeVirtual(Boolean.class, "booleanValue", boolean.class);
BOOL.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -147,7 +159,7 @@ public class ZenTypeBoolObject extends ZenType {
return BOOL.getAnyClassName(environment);
}
@Override
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type == BOOL || type == ZenTypeBoolObject.INSTANCE || type == STRING) {
return new ExpressionAs(position, value, type);
@@ -156,7 +168,7 @@ public class ZenTypeBoolObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Expression defaultValue(ZenPosition position) {
@@ -13,11 +13,17 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.ZenTypeAny.*;
import stanhebben.zenscript.type.casting.CastingRuleI2D;
import stanhebben.zenscript.type.casting.CastingRuleI2F;
import stanhebben.zenscript.type.casting.CastingRuleI2L;
import stanhebben.zenscript.type.casting.CastingRuleNone;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASBYTE;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
@@ -44,6 +50,28 @@ public class ZenTypeByte extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF));
rules.registerCastingRule(SHORT, new CastingRuleNone(BYTE, SHORT));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF));
rules.registerCastingRule(INT, new CastingRuleNone(BYTE, INT));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF));
rules.registerCastingRule(LONG, new CastingRuleI2L(null));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleI2L(null)));
rules.registerCastingRule(FLOAT, new CastingRuleI2F(null));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF, new CastingRuleI2F(null)));
rules.registerCastingRule(DOUBLE, new CastingRuleI2D(null));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF, new CastingRuleI2D(null)));
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(BYTE_TOSTRING_STATIC));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, BYTE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
int itype = type.getNumberType();
return itype != 0
@@ -69,7 +97,7 @@ public class ZenTypeByte extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -112,7 +140,7 @@ public class ZenTypeByte extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -150,7 +178,7 @@ public class ZenTypeByte extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -316,7 +344,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -328,7 +356,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iSub();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -346,7 +374,7 @@ public class ZenTypeByte extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -359,7 +387,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iMul();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -371,7 +399,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -383,7 +411,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iRem();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -395,7 +423,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iAnd();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -407,7 +435,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iOr();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -419,7 +447,7 @@ public class ZenTypeByte extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iXor();
output.invokeSpecial(ANY_NAME, "<init>", "(B)V");
output.returnObject();
@@ -436,7 +464,7 @@ public class ZenTypeByte extends ZenType {
public void defineCompareTo(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.iSub();
output.returnInt();
}
@@ -586,7 +614,7 @@ public class ZenTypeByte extends ZenType {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASBYTE.invokeVirtual(output);
output.ifICmpNE(lblNope);
output.iConst1();
@@ -6,23 +6,22 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +73,40 @@ public class ZenTypeByteObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(BYTEOBJECT, BYTE_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, BYTE),
new CastingRuleVirtualMethod(BYTE_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return BYTE.canCastImplicit(type, environment);
}
@@ -92,7 +125,7 @@ public class ZenTypeByteObject extends ZenType {
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return BYTE.canCastExplicit(type, environment);
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +152,7 @@ public class ZenTypeByteObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +180,7 @@ public class ZenTypeByteObject extends ZenType {
environment.getOutput().invokeVirtual(Byte.class, "byteValue", byte.class);
BYTE.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -13,13 +13,23 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTEOBJECT;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUEOF;
import static stanhebben.zenscript.type.ZenType.FLOAT;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.type.casting.CastingRuleD2F;
import stanhebben.zenscript.type.casting.CastingRuleD2I;
import stanhebben.zenscript.type.casting.CastingRuleD2L;
import stanhebben.zenscript.type.casting.CastingRuleI2B;
import stanhebben.zenscript.type.casting.CastingRuleI2S;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASDOUBLE;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
@@ -47,6 +57,28 @@ public class ZenTypeDouble extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleI2B(new CastingRuleD2I(null)));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF, new CastingRuleI2B(new CastingRuleD2I(null))));
rules.registerCastingRule(SHORT, new CastingRuleI2S(new CastingRuleD2I(null)));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF, new CastingRuleI2S(new CastingRuleD2I(null))));
rules.registerCastingRule(INT, new CastingRuleD2I(null));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF, new CastingRuleD2I(null)));
rules.registerCastingRule(LONG, new CastingRuleD2L(null));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleD2L(null)));
rules.registerCastingRule(FLOAT, new CastingRuleD2F(null));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF, new CastingRuleD2F(null)));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF));
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(DOUBLE_TOSTRING_STATIC));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, DOUBLE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_FLOAT)
|| type == STRING
@@ -71,7 +103,7 @@ public class ZenTypeDouble extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -114,7 +146,7 @@ public class ZenTypeDouble extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -158,7 +190,7 @@ public class ZenTypeDouble extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -324,7 +356,7 @@ public class ZenTypeDouble extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
output.returnObject();
@@ -336,7 +368,7 @@ public class ZenTypeDouble extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dSub();
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
output.returnObject();
@@ -354,7 +386,7 @@ public class ZenTypeDouble extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, double.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -367,7 +399,7 @@ public class ZenTypeDouble extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dMul();
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
output.returnObject();
@@ -379,7 +411,7 @@ public class ZenTypeDouble extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
output.returnObject();
@@ -391,7 +423,7 @@ public class ZenTypeDouble extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dRem();
output.invokeSpecial(ANY_NAME, "<init>", "(D)V");
output.returnObject();
@@ -422,7 +454,7 @@ public class ZenTypeDouble extends ZenType {
// return Double.compare(x, y)
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.invokeStatic(Float.class, "compare", int.class, double.class, double.class);
output.returnInt();
}
@@ -581,7 +613,7 @@ public class ZenTypeDouble extends ZenType {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASDOUBLE);
METHOD_ASDOUBLE.invokeVirtual(output);
output.dCmp();
output.ifNE(lblNope);
@@ -6,23 +6,25 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUE;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +76,43 @@ public class ZenTypeDoubleObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(
BYTE_VALUEOF,
new CastingRuleVirtualMethod(BYTE_VALUE)));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(DOUBLEOBJECT, DOUBLE_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, DOUBLE),
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return DOUBLE.canCastImplicit(type, environment);
}
@@ -92,7 +131,7 @@ public class ZenTypeDoubleObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +158,7 @@ public class ZenTypeDoubleObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +186,7 @@ public class ZenTypeDoubleObject extends ZenType {
environment.getOutput().invokeVirtual(Double.class, "doubleValue", double.class);
DOUBLE.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -13,13 +13,23 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTEOBJECT;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUEOF;
import static stanhebben.zenscript.type.ZenType.LONG;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.type.casting.CastingRuleF2D;
import stanhebben.zenscript.type.casting.CastingRuleF2I;
import stanhebben.zenscript.type.casting.CastingRuleF2L;
import stanhebben.zenscript.type.casting.CastingRuleI2B;
import stanhebben.zenscript.type.casting.CastingRuleI2S;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASFLOAT;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
@@ -47,6 +57,28 @@ public class ZenTypeFloat extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleI2B(new CastingRuleF2I(null)));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF, new CastingRuleI2B(new CastingRuleF2I(null))));
rules.registerCastingRule(SHORT, new CastingRuleI2S(new CastingRuleF2I(null)));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF, new CastingRuleI2S(new CastingRuleF2I(null))));
rules.registerCastingRule(INT, new CastingRuleF2I(null));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF, new CastingRuleF2I(null)));
rules.registerCastingRule(LONG, new CastingRuleF2L(null));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleF2L(null)));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF));
rules.registerCastingRule(DOUBLE, new CastingRuleF2D(null));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF, new CastingRuleF2D(null)));
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(FLOAT_TOSTRING_STATIC));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, FLOAT)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return (type.getNumberType() != 0)
|| type == ZenTypeString.INSTANCE
@@ -71,7 +103,7 @@ public class ZenTypeFloat extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -109,7 +141,7 @@ public class ZenTypeFloat extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -153,7 +185,7 @@ public class ZenTypeFloat extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -319,7 +351,7 @@ public class ZenTypeFloat extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
output.returnObject();
@@ -331,7 +363,7 @@ public class ZenTypeFloat extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fSub();
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
output.returnObject();
@@ -349,7 +381,7 @@ public class ZenTypeFloat extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, float.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -362,7 +394,7 @@ public class ZenTypeFloat extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fMul();
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
output.returnObject();
@@ -374,7 +406,7 @@ public class ZenTypeFloat extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
output.returnObject();
@@ -386,7 +418,7 @@ public class ZenTypeFloat extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fRem();
output.invokeSpecial(ANY_NAME, "<init>", "(F)V");
output.returnObject();
@@ -417,7 +449,7 @@ public class ZenTypeFloat extends ZenType {
// return Float.compare(x, y)
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.invokeStatic(Float.class, "compare", int.class, float.class, float.class);
output.returnInt();
}
@@ -576,7 +608,7 @@ public class ZenTypeFloat extends ZenType {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASFLOAT);
METHOD_ASFLOAT.invokeVirtual(output);
output.fCmp();
output.ifICmpNE(lblNope);
@@ -6,23 +6,25 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUE;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +76,43 @@ public class ZenTypeFloatObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(
BYTE_VALUEOF,
new CastingRuleVirtualMethod(BYTE_VALUE)));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(FLOATOBJECT, FLOAT_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, FLOAT),
new CastingRuleVirtualMethod(FLOAT_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return FLOAT.canCastImplicit(type, environment);
}
@@ -92,7 +131,7 @@ public class ZenTypeFloatObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +158,7 @@ public class ZenTypeFloatObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +186,7 @@ public class ZenTypeFloatObject extends ZenType {
environment.getOutput().invokeVirtual(Float.class, "floatValue", float.class);
FLOAT.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -6,7 +6,11 @@
package stanhebben.zenscript.type;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
@@ -17,6 +21,9 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleMatchedFunction;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -27,6 +34,7 @@ public class ZenTypeFunction extends ZenType {
private final ZenType returnType;
private final ZenType[] argumentTypes;
private final String name;
private final Map<ZenType, CastingRuleMatchedFunction> implementedInterfaces = new HashMap<ZenType, CastingRuleMatchedFunction>();
public ZenTypeFunction(ZenType returnType, List<ParsedFunctionArgument> arguments) {
this.returnType = returnType;
@@ -85,20 +93,64 @@ public class ZenTypeFunction extends ZenType {
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
return null;
}
@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return equals(type) || canCastToNative(type) || canCastExpansion(environment, type); // TODO: LATER: expand
}
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return equals(type) || canCastToNative(type) || canCastExpansion(environment, type); // TODO: LATER: expand
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
throw new UnsupportedOperationException("not supported yet");
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
if (implementedInterfaces.containsKey(type)) {
return implementedInterfaces.get(type);
}
Class cls = type.toJavaClass();
System.out.println("Can cast this function to " + cls.getName() + "?");
if (cls.isInterface() && cls.getMethods().length == 1) {
// this is a functional interface
// do the method signatures match?
Method method = cls.getMethods()[0];
ZenType methodReturnType = environment.getType(method.getGenericReturnType());
ICastingRule returnCastingRule = null;
if (!returnType.equals(methodReturnType)) {
returnCastingRule = returnType.getCastingRule(environment.getType(method.getGenericReturnType()), environment);
if (returnCastingRule == null) {
System.out.println("Return types don't match");
return null;
}
}
java.lang.reflect.Type[] methodParameters = method.getGenericParameterTypes();
if (methodParameters.length < argumentTypes.length) {
System.out.println("Argument count doesn't match");
return null;
}
ICastingRule[] argumentCastingRules = new ICastingRule[argumentTypes.length];
for (int i = 0; i < argumentCastingRules.length; i++) {
ZenType argumentType = environment.getType(methodParameters[i]);
if (!argumentType.equals(argumentTypes[i])) {
argumentCastingRules[i] = argumentType.getCastingRule(argumentTypes[i], environment);
if (argumentCastingRules[i] == null) {
System.out.println("Argument " + i + " doesn't match");
System.out.println("Cannot cast " + argumentType.getName() + " to " + argumentTypes[i].getName());
return null;
}
}
}
CastingRuleMatchedFunction castingRule = new CastingRuleMatchedFunction(this, type, returnCastingRule, argumentCastingRules);
implementedInterfaces.put(type, castingRule);
System.out.println("Can cast this function");
return castingRule;
}
return null;
}
@Override
@@ -150,6 +202,11 @@ public class ZenTypeFunction extends ZenType {
ZenPosition position, IEnvironmentGlobal environment, Expression receiver, Expression... arguments) {
return null; // TODO: complete
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return Arrays.copyOf(argumentTypes, numArguments);
}
@Override
public Class toJavaClass() {
@@ -157,13 +214,6 @@ public class ZenTypeFunction extends ZenType {
return null;
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
@Override
public String getName() {
return name;
@@ -173,21 +223,4 @@ public class ZenTypeFunction extends ZenType {
public Expression defaultValue(ZenPosition position) {
return new ExpressionNull(position);
}
private boolean canCastToNative(ZenType type) {
if (!(type instanceof ZenTypeNative)) return false;
//System.out.println("Check cast to native type " + type.getName());
ZenTypeNative ntype = (ZenTypeNative) type;
if (ntype.getNativeClass().isInterface() && ntype.getNativeClass().getMethods().length == 1) {
// ta-da! we got a functional interface
// does it match?
// TODO: expand
return true; // assume for now it is
}
System.out.println("Cannot cast");
return false;
}
}
@@ -13,14 +13,23 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.BYTEOBJECT;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUEOF;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.type.casting.CastingRuleI2B;
import stanhebben.zenscript.type.casting.CastingRuleI2D;
import stanhebben.zenscript.type.casting.CastingRuleI2F;
import stanhebben.zenscript.type.casting.CastingRuleI2L;
import stanhebben.zenscript.type.casting.CastingRuleI2S;
import stanhebben.zenscript.type.casting.CastingRuleNone;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
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;
@@ -45,8 +54,30 @@ public class ZenTypeInt extends ZenType {
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
return null;
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleI2B(null));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF));
rules.registerCastingRule(SHORT, new CastingRuleI2S(null));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF));
rules.registerCastingRule(LONG, new CastingRuleI2L(null));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleI2L(null)));
rules.registerCastingRule(FLOAT, new CastingRuleI2F(null));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF, new CastingRuleI2F(null)));
rules.registerCastingRule(DOUBLE, new CastingRuleI2D(null));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF, new CastingRuleI2D(null)));
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(INT_TOSTRING_STATIC));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, INT)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_INT)
|| type == ZenTypeString.INSTANCE
@@ -71,7 +102,7 @@ public class ZenTypeInt extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -109,7 +140,7 @@ public class ZenTypeInt extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -149,7 +180,7 @@ public class ZenTypeInt extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -320,7 +351,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -332,7 +363,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iSub();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -350,7 +381,7 @@ public class ZenTypeInt extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -363,7 +394,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iMul();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -375,7 +406,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -387,7 +418,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASINT.invokeVirtual(output);
output.iRem();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -399,7 +430,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iAnd();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -411,7 +442,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iOr();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -423,7 +454,7 @@ public class ZenTypeInt extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iXor();
output.invokeSpecial(ANY_NAME, "<init>", "(I)V");
output.returnObject();
@@ -440,7 +471,7 @@ public class ZenTypeInt extends ZenType {
public void defineCompareTo(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.iSub();
output.returnInt();
}
@@ -597,7 +628,7 @@ public class ZenTypeInt extends ZenType {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.ifICmpNE(lblNope);
output.iConst1();
@@ -6,23 +6,25 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUE;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +76,43 @@ public class ZenTypeIntObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(
BYTE_VALUEOF,
new CastingRuleVirtualMethod(BYTE_VALUE)));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(INTOBJECT, INT_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, INT),
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return INT.canCastImplicit(type, environment);
}
@@ -92,7 +131,7 @@ public class ZenTypeIntObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +158,7 @@ public class ZenTypeIntObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +186,7 @@ public class ZenTypeIntObject extends ZenType {
environment.getOutput().invokeVirtual(Integer.class, "intValue", int.class);
INT.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -16,9 +16,11 @@ import stanhebben.zenscript.compiler.ITypeRegistry;
import stanhebben.zenscript.compiler.TypeRegistry;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionJavaCallVirtual;
import stanhebben.zenscript.expression.ExpressionCallVirtual;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.IJavaMethod;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
@@ -32,8 +34,8 @@ import stanhebben.zenscript.value.IntRange;
public class ZenTypeIntRange extends ZenType {
public static final ZenTypeIntRange INSTANCE = new ZenTypeIntRange();
private final JavaMethod methodFrom;
private final JavaMethod methodTo;
private final IJavaMethod methodFrom;
private final IJavaMethod methodTo;
private ZenTypeIntRange() {
ITypeRegistry dummy = new TypeRegistry();
@@ -73,9 +75,9 @@ public class ZenTypeIntRange extends ZenType {
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, IPartialExpression value, String name) {
if (name.equals("from")) {
return new ExpressionJavaCallVirtual(position, methodFrom, value.eval(environment));
return new ExpressionCallVirtual(position, environment, methodFrom, value.eval(environment));
} else if (name.equals("to")) {
return new ExpressionJavaCallVirtual(position, methodTo, value.eval(environment));
return new ExpressionCallVirtual(position, environment, methodTo, value.eval(environment));
} else {
environment.error(position, "no such member " + name + " in int range");
return new ExpressionInvalid(position);
@@ -93,8 +95,13 @@ public class ZenTypeIntRange extends ZenType {
environment.error(position, "int ranges cannot be called");
return new ExpressionInvalid(position);
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
}
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type == this) {
return value;
@@ -102,7 +109,7 @@ public class ZenTypeIntRange extends ZenType {
environment.error(position, "cannot cast int ranges to any other type");
return new ExpressionInvalid(position);
}
}*/
@Override
public IZenIterator makeIterator(int numValues, IEnvironmentMethod methodOutput) {
@@ -113,7 +120,7 @@ public class ZenTypeIntRange extends ZenType {
}
}
@Override
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this;
}
@@ -121,7 +128,7 @@ public class ZenTypeIntRange extends ZenType {
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this;
}
}*/
@Override
public Class toJavaClass() {
@@ -148,10 +155,10 @@ public class ZenTypeIntRange extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}*/
@Override
public String getName() {
@@ -175,7 +182,7 @@ public class ZenTypeIntRange extends ZenType {
MethodOutput output = method.getOutput();
output.dup(); // copy IntRange reference
output.invoke(methodFrom);
methodFrom.invokeVirtual(output);
output.storeInt(locals[0]);
}
@@ -189,7 +196,7 @@ public class ZenTypeIntRange extends ZenType {
MethodOutput output = method.getOutput();
output.dup(); // copy IntRange reference
output.invoke(methodTo);
methodTo.invokeVirtual(output);
output.loadInt(locals[0]);
output.iinc(locals[0]);
@@ -13,13 +13,24 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.BYTEOBJECT;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUEOF;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.type.casting.CastingRuleI2B;
import stanhebben.zenscript.type.casting.CastingRuleI2D;
import stanhebben.zenscript.type.casting.CastingRuleI2F;
import stanhebben.zenscript.type.casting.CastingRuleI2L;
import stanhebben.zenscript.type.casting.CastingRuleI2S;
import stanhebben.zenscript.type.casting.CastingRuleL2F;
import stanhebben.zenscript.type.casting.CastingRuleL2I;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASLONG;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
@@ -47,6 +58,28 @@ public class ZenTypeLong extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleI2B(new CastingRuleL2I(null)));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF, new CastingRuleI2B(new CastingRuleL2I(null))));
rules.registerCastingRule(SHORT, new CastingRuleI2S(new CastingRuleL2I(null)));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF, new CastingRuleI2S(new CastingRuleL2I(null))));
rules.registerCastingRule(INT, new CastingRuleL2I(null));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleI2L(null)));
rules.registerCastingRule(FLOAT, new CastingRuleL2F(null));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF, new CastingRuleI2F(null)));
rules.registerCastingRule(DOUBLE, new CastingRuleI2D(null));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF, new CastingRuleI2D(null)));
rules.registerCastingRule(STRING, new CastingRuleStaticMethod(LONG_TOSTRING_STATIC));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, LONG)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_LONG)
|| type == ZenTypeString.INSTANCE
@@ -71,7 +104,7 @@ public class ZenTypeLong extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -109,7 +142,7 @@ public class ZenTypeLong extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(stanhebben.zenscript.util.ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -153,7 +186,7 @@ public class ZenTypeLong extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -198,7 +231,7 @@ public class ZenTypeLong extends ZenType {
@Override
public String getName() {
return "name";
return "long";
}
@Override
@@ -329,7 +362,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -341,7 +374,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lSub();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -359,7 +392,7 @@ public class ZenTypeLong extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, long.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -372,7 +405,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lMul();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -384,7 +417,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -396,7 +429,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lRem();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -408,7 +441,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lAnd();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -420,7 +453,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lOr();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -432,7 +465,7 @@ public class ZenTypeLong extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lXor();
output.invokeSpecial(ANY_NAME, "<init>", "(J)V");
output.returnObject();
@@ -450,7 +483,7 @@ public class ZenTypeLong extends ZenType {
// return Long.compare(x, y)
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.invokeStatic(Long.class, "compare", int.class, long.class, long.class);
output.returnInt();
}
@@ -603,13 +636,13 @@ public class ZenTypeLong extends ZenType {
public void defineEquals(MethodOutput output) {
Label lblNope = new Label();
output.loadObject(1);
output.loadObject(0);
output.instanceOf(IAny.NAME);
output.ifEQ(lblNope);
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASLONG);
METHOD_ASLONG.invokeVirtual(output);
output.lCmp();
output.ifICmpNE(lblNope);
@@ -6,23 +6,25 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUE;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +76,43 @@ public class ZenTypeLongObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(
BYTE_VALUEOF,
new CastingRuleVirtualMethod(BYTE_VALUE)));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(LONGOBJECT, LONG_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, LONG),
new CastingRuleVirtualMethod(LONG_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return LONG.canCastImplicit(type, environment);
}
@@ -92,7 +131,7 @@ public class ZenTypeLongObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +158,7 @@ public class ZenTypeLongObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +186,7 @@ public class ZenTypeLongObject extends ZenType {
environment.getOutput().invokeVirtual(Long.class, "longValue", long.class);
LONG.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -15,12 +15,11 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.objectweb.asm.ClassWriter;
import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import stanhebben.zenscript.ZenRuntimeException;
import stanhebben.zenscript.TypeExpansion;
import stanhebben.zenscript.annotations.ZenCaster;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.ZenClass;
@@ -36,16 +35,18 @@ import stanhebben.zenscript.annotations.ZenSetter;
import stanhebben.zenscript.annotations.ZenMethod;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.compiler.EnvironmentMethod;
import stanhebben.zenscript.compiler.ITypeRegistry;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
import stanhebben.zenscript.expression.ExpressionAs;
import stanhebben.zenscript.expression.ExpressionCallVirtual;
import stanhebben.zenscript.expression.ExpressionCompareGeneric;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.ExpressionString;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleNone;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.iterator.IteratorIterable;
import stanhebben.zenscript.type.iterator.IteratorList;
import stanhebben.zenscript.type.iterator.IteratorMap;
@@ -54,8 +55,13 @@ import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.type.natives.ZenNativeCaster;
import stanhebben.zenscript.type.natives.ZenNativeMember;
import stanhebben.zenscript.type.natives.ZenNativeOperator;
import stanhebben.zenscript.util.AnyClassWriter;
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;
@@ -70,6 +76,8 @@ public class ZenTypeNative extends ZenType {
private static final int ITERATOR_MAP = 3;
private final Class cls;
private final String anyName;
private final String anyName2;
private final List<ZenTypeNative> implementing;
private final Map<String, ZenNativeMember> members;
private final Map<String, ZenNativeMember> staticMembers;
@@ -94,6 +102,9 @@ public class ZenTypeNative extends ZenType {
binaryOperators = new ArrayList<ZenNativeOperator>();
unaryOperators = new ArrayList<ZenNativeOperator>();
implementing = new ArrayList<ZenTypeNative>();
anyName2 = cls.getName() + "Any";
anyName = anyName2.replace('.', '/');
}
public void complete(ITypeRegistry types) {
@@ -149,8 +160,7 @@ public class ZenTypeNative extends ZenType {
for (Annotation annotation : method.getAnnotations()) {
if (annotation instanceof ZenCaster) {
Class output = method.getReturnType();
casters.add(new ZenNativeCaster(cls, output, method.getName()));
casters.add(new ZenNativeCaster(JavaMethod.get(types, method)));
isMethod = false;
} else if (annotation instanceof ZenGetter) {
ZenGetter getterAnnotation = (ZenGetter) annotation;
@@ -159,7 +169,7 @@ public class ZenTypeNative extends ZenType {
if (!members.containsKey(name)) {
members.put(name, new ZenNativeMember());
}
members.get(name).setGetter(new JavaMethod(cls, method, types));
members.get(name).setGetter(new JavaMethod(method, types));
isMethod = false;
} else if (annotation instanceof ZenSetter) {
ZenSetter setterAnnotation = (ZenSetter) annotation;
@@ -168,12 +178,12 @@ public class ZenTypeNative extends ZenType {
if (!members.containsKey(name)) {
members.put(name, new ZenNativeMember());
}
members.get(name).setSetter(new JavaMethod(cls, method, types));
members.get(name).setSetter(new JavaMethod(method, types));
isMethod = false;
} else if (annotation instanceof ZenMemberGetter) {
binaryOperators.add(new ZenNativeOperator(OperatorType.MEMBERGETTER, new JavaMethod(cls, method, types)));
binaryOperators.add(new ZenNativeOperator(OperatorType.MEMBERGETTER, new JavaMethod(method, types)));
} else if (annotation instanceof ZenMemberSetter) {
trinaryOperators.add(new ZenNativeOperator(OperatorType.MEMBERSETTER, new JavaMethod(cls, method, types)));
trinaryOperators.add(new ZenNativeOperator(OperatorType.MEMBERSETTER, new JavaMethod(method, types)));
} else if (annotation instanceof ZenOperator) {
ZenOperator operatorAnnotation = (ZenOperator) annotation;
switch (operatorAnnotation.value()) {
@@ -184,7 +194,7 @@ public class ZenTypeNative extends ZenType {
} else {
unaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
case ADD:
@@ -205,7 +215,7 @@ public class ZenTypeNative extends ZenType {
} else {
binaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
case INDEXSET:
@@ -214,7 +224,7 @@ public class ZenTypeNative extends ZenType {
} else {
trinaryOperators.add(new ZenNativeOperator(
operatorAnnotation.value(),
new JavaMethod(cls, method, types)));
new JavaMethod(method, types)));
}
break;
}
@@ -234,12 +244,12 @@ public class ZenTypeNative extends ZenType {
if (!staticMembers.containsKey(methodName)) {
staticMembers.put(methodName, new ZenNativeMember());
}
staticMembers.get(methodName).addMethod(new JavaMethod(cls, method, types));
staticMembers.get(methodName).addMethod(new JavaMethod(method, types));
} else {
if (!members.containsKey(methodName)) {
members.put(methodName, new ZenNativeMember());
}
members.get(methodName).addMethod(new JavaMethod(cls, method, types));
members.get(methodName).addMethod(new JavaMethod(method, types));
}
}
}
@@ -273,7 +283,12 @@ public class ZenTypeNative extends ZenType {
@Override
public String getAnyClassName(IEnvironmentGlobal global) {
return null;
if (!global.containsClass(anyName2)) {
global.putClass(anyName2, new byte[0]);
//global.putClass(anyName2, AnyClassWriter.construct(new AnyNativeDefinition(global), anyName2, toASMType()));
}
return anyName;
}
@Override
@@ -287,6 +302,7 @@ public class ZenTypeNative extends ZenType {
}
}
}
if (member == null) {
Expression evalue = value.eval(environment);
IPartialExpression member2 = memberExpansion(position, environment, evalue, name);
@@ -376,34 +392,48 @@ public class ZenTypeNative extends ZenType {
}
return null;
}
@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this
|| canCastImplicit(cls, type)
|| canCastExpansion(environment, type);
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
if (cls.getSuperclass() != null) {
ZenType superType = environment.getType(cls.getSuperclass());
rules.registerCastingRule(superType, new CastingRuleNone(this, superType));
superType.constructCastingRules(environment, rules, followCasters);
}
for (Class iface : cls.getInterfaces()) {
ZenType ifaceType = environment.getType(iface);
rules.registerCastingRule(ifaceType, new CastingRuleNone(this, ifaceType));
ifaceType.constructCastingRules(environment, rules, followCasters);
}
if (followCasters) {
for (ZenNativeCaster caster : casters) {
// TODO: implement
}
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
expansion.constructCastingRules(environment, rules);
}
}
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(JavaMethod.getStatic(
getAnyClassName(environment),
"valueOf",
ANY,
this)));
}
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this
|| canCastImplicit(cls, type)
|| canCastImplicit(type, cls)
|| canCastExpansion(environment, type);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
if (type == this || canCastImplicit(cls, type)) {
return value;
}
if (canCastExpansion(environment, type)) {
return castExpansion(position, environment, value, type);
} else {
return new ExpressionAs(position, value, type);
}
|| canCastImplicit(type, environment)
|| type.canCastExplicit(this, environment);
}
@Override
@@ -431,400 +461,11 @@ public class ZenTypeNative extends ZenType {
return true;
}
/**
* Can we cast the given class to the given type implicitly?
*
* @param cls class to be casted
* @param type type to cast to
* @return true if casting is possible
*/
private static boolean canCastImplicit(Class cls, ZenType type) {
if (isEqual(cls, type)) return true;
if (type == BOOL) return true;
if (cls.getSuperclass() != null) {
if (canCastImplicit(cls.getSuperclass(), type)) return true;
}
for (Class iface : cls.getInterfaces()) {
if (canCastImplicit(iface, type)) return true;
}
return false;
}
/**
* Can we cast the given type to the given class implicitly?
*
* @param type type to be casted
* @param cls type to cast to
* @return true if casting is possible
*/
private static boolean canCastImplicit(ZenType type, Class cls) {
if (isEqual(cls, type)) return true;
if (type == BOOL) return true;
if (type instanceof ZenTypeNative) {
Class clsFrom = ((ZenTypeNative) type).cls;
return cls.isAssignableFrom(clsFrom);
}
return false;
}
private static boolean isEqual(Class cls, ZenType type) {
String signature = getSignature(cls);
return type.getSignature().equals(signature);
}
private static String getSignature(Class cls) {
String signature = signature(cls);
if (Number.class.isAssignableFrom(cls)) {
if (cls == Byte.class) {
signature = "B";
} else if (cls == Short.class) {
signature = "S";
} else if (cls == Integer.class) {
signature = "I";
} else if (cls == Long.class) {
signature = "J";
} else if (cls == Float.class) {
signature = "F";
} else if (cls == Double.class) {
signature = "D";
}
}
return signature;
}
@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType toType) {
if (toType == ZenTypeAny.INSTANCE) {
String anyClassName = this.cls.getName() + "__Any";
String anySignature = signature(IAny.class);
String toSignature = "L" + anyClassName + "";
if (!environment.containsClass(anyClassName)) {
environment.putClass(anyClassName, new byte[0]); // prevent recursive issues
ClassWriter newClass = new ClassWriter(COMPUTE_FRAMES);
newClass.visit(
Opcodes.V1_6,
Opcodes.ACC_PUBLIC,
anyClassName,
"L" + anyClassName + ";",
signature(Object.class),
new String[] { anySignature });
newClass.visitField(Opcodes.ACC_PRIVATE, "value", null, className, null);
// static valueOf method
MethodOutput valueOfMethod = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
"valueOf",
"(" + getSignature() + ")L" + anyClassName + ";",
null,
null);
valueOfMethod.start();
valueOfMethod.newObject(anyClassName);
valueOfMethod.loadObject(0);
valueOfMethod.construct(anyClassName, getSignature());
valueOfMethod.returnObject();
valueOfMethod.end();
// single constructor
MethodOutput constructorMethod = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"<init>",
"(" + getSignature() + ")V",
null,
null);
constructorMethod.start();
constructorMethod.loadObject(1);
constructorMethod.loadObject(0);
constructorMethod.putField(toSignature, "value", null);
constructorMethod.end();
// implementation for each of the interface methods
MethodOutput methodNot = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"not",
"()" + anySignature,
null,
null);
methodNot.start();
compileAnyUnary(
anySignature,
OperatorType.NOT,
new EnvironmentMethod(methodNot, environment));
methodNot.end();
MethodOutput methodNeg = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"neg",
"()" + anySignature,
null,
null);
methodNeg.start();
compileAnyUnary(
anySignature,
OperatorType.NEG,
new EnvironmentMethod(methodNeg, environment));
methodNeg.end();
MethodOutput methodAdd = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"add",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodAdd.start();
compileAnyBinary(
anySignature,
OperatorType.ADD,
new EnvironmentMethod(methodAdd, environment));
methodAdd.end();
MethodOutput methodSub = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"sub",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodSub.start();
compileAnyBinary(
anySignature,
OperatorType.SUB,
new EnvironmentMethod(methodSub, environment));
methodSub.end();
MethodOutput methodCat = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"cat",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodCat.start();
compileAnyBinary(
anySignature,
OperatorType.CAT,
new EnvironmentMethod(methodCat, environment));
methodCat.end();
MethodOutput methodMul = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"mul",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodMul.start();
compileAnyBinary(
anySignature,
OperatorType.ADD,
new EnvironmentMethod(methodMul, environment));
methodMul.end();
MethodOutput methodDiv = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"div",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodDiv.start();
compileAnyBinary(
anySignature,
OperatorType.DIV,
new EnvironmentMethod(methodDiv, environment));
methodDiv.end();
MethodOutput methodMod = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"mod",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodMod.start();
compileAnyBinary(
anySignature,
OperatorType.MOD,
new EnvironmentMethod(methodMod, environment));
methodMod.end();
MethodOutput methodAnd = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"and",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodAnd.start();
compileAnyBinary(
anySignature,
OperatorType.AND,
new EnvironmentMethod(methodAnd, environment));
methodAnd.end();
MethodOutput methodOr = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"or",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodOr.start();
compileAnyBinary(
anySignature,
OperatorType.OR,
new EnvironmentMethod(methodOr, environment));
methodOr.end();
MethodOutput methodXor = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"xor",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodXor.start();
compileAnyBinary(
anySignature,
OperatorType.XOR,
new EnvironmentMethod(methodXor, environment));
methodXor.end();
MethodOutput methodRange = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"range",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodRange.start();
compileAnyBinary(
anySignature,
OperatorType.RANGE,
new EnvironmentMethod(methodRange, environment));
methodRange.end();
MethodOutput methodContains = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"contains",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodContains.start();
compileAnyBinary(
anySignature,
OperatorType.CONTAINS,
new EnvironmentMethod(methodContains, environment));
methodContains.end();
MethodOutput methodIndexSet = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"indexSet",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodIndexSet.start();
compileAnyBinary(
anySignature,
OperatorType.INDEXSET,
new EnvironmentMethod(methodIndexSet, environment));
methodIndexSet.end();
MethodOutput methodCompare = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"compareTo",
"(" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodCompare.start();
compileAnyBinary(
anySignature,
OperatorType.COMPARE,
new EnvironmentMethod(methodCompare, environment));
methodCompare.end();
MethodOutput methodMember = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"method",
"(" + signature(String.class) + ")" + anySignature,
null,
null);
methodMember.start();
compileAnyMember(anySignature, new EnvironmentMethod(methodMember, environment));
methodMember.end();
MethodOutput methodCall = new MethodOutput(
newClass,
Opcodes.ACC_PUBLIC,
"call",
"([" + signature(IAny.class) + ")" + anySignature,
null,
null);
methodCall.start();
// TODO: complete
methodCall.aConstNull();
methodCall.returnObject();
methodCall.end();
// TODO: asXXX, iterators
environment.putClass(anyClassName, newClass.toByteArray());
}
return;
} else if (toType instanceof ZenTypeNative) {
Class toCls = toType.toJavaClass();
if (toCls.isAssignableFrom(cls)) {
// nothing to do
return;
}
}
MethodOutput output = environment.getOutput();
if (toType == BOOL) {
Label lbl = new Label();
output.iConst0();
output.ifNull(lbl);
output.iConst1();
output.label(lbl);
return;
}
for (ZenNativeCaster caster : casters) {
if (caster.getCasterClass() == toType.toJavaClass()) {
caster.compile(output);
return;
}
}
for (ZenNativeCaster caster : casters) {
if (toType.toJavaClass().isAssignableFrom(caster.getCasterClass())) {
caster.compile(output);
return;
}
}
if (compileCastExpansion(position, environment, toType)) {
return;
}
output.checkCast(toType.toJavaClass());
}
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
for (ZenNativeOperator unaryOperator : unaryOperators) {
if (unaryOperator.getOperator() == operator) {
return unaryOperator
.getMethod()
.callVirtual(position, environment, value);
return new ExpressionCallVirtual(position, environment, unaryOperator.getMethod(), value);
}
}
@@ -836,9 +477,7 @@ public class ZenTypeNative extends ZenType {
public Expression binary(ZenPosition position, IEnvironmentGlobal environment, Expression left, Expression right, OperatorType operator) {
for (ZenNativeOperator binaryOperator : binaryOperators) {
if (binaryOperator.getOperator() == operator) {
return binaryOperator
.getMethod()
.callVirtual(position, environment, left, right);
return new ExpressionCallVirtual(position, environment, binaryOperator.getMethod(), left, right);
}
}
@@ -850,9 +489,7 @@ public class ZenTypeNative extends ZenType {
public Expression trinary(ZenPosition position, IEnvironmentGlobal environment, Expression first, Expression second, Expression third, OperatorType operator) {
for (ZenNativeOperator trinaryOperator : trinaryOperators) {
if (trinaryOperator.getOperator() == operator) {
return trinaryOperator
.getMethod()
.callVirtual(position, environment, first, second, third);
return new ExpressionCallVirtual(position, environment, trinaryOperator.getMethod(), first, second, third);
}
}
@@ -865,9 +502,7 @@ public class ZenTypeNative extends ZenType {
if (type == CompareType.EQ || type == CompareType.NE) {
for (ZenNativeOperator binaryOperator : binaryOperators) {
if (binaryOperator.getOperator() == OperatorType.EQUALS) {
Expression result = binaryOperator
.getMethod()
.callVirtual(position, environment, left, right);
Expression result = new ExpressionCallVirtual(position, environment, binaryOperator.getMethod(), left, right);
if (type == CompareType.EQ) {
return result;
} else {
@@ -883,9 +518,16 @@ public class ZenTypeNative extends ZenType {
@Override
public Expression call(
ZenPosition position, IEnvironmentGlobal environment, Expression receiver, Expression... arguments) {
// TODO: support functional interfaces
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
// TODO: support functional interface
return new ZenType[numArguments];
}
private boolean hasBinary(ZenType type, OperatorType operator) {
for (ZenNativeOperator binaryOperator : binaryOperators) {
if (binaryOperator.getOperator() == operator) {
@@ -896,7 +538,7 @@ public class ZenTypeNative extends ZenType {
return false;
}
private void compileAnyUnary(String anySignature, OperatorType operator, IEnvironmentMethod environment) {
/*private void compileAnyUnary(String anySignature, OperatorType operator, IEnvironmentMethod environment) {
List<ZenNativeOperator> operators = new ArrayList<ZenNativeOperator>();
for (ZenNativeOperator unary : this.unaryOperators) {
if (unary.getOperator() == operator) {
@@ -961,7 +603,7 @@ public class ZenTypeNative extends ZenType {
MethodOutput output = environment.getOutput();
output.aConstNull();
output.returnObject();
}
}*/
@Override
public String getName() {
@@ -992,4 +634,313 @@ public class ZenTypeNative extends ZenType {
}
}
}
private class AnyNativeDefinition implements IAnyDefinition {
private final IEnvironmentGlobal environment;
public AnyNativeDefinition(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(anyName2);
valueOf.dup();
valueOf.load(toASMType(), 0);
valueOf.construct(anyName2, "L" + cls.getName() + ";");
valueOf.returnObject();
valueOf.end();
MethodOutput constructor = new MethodOutput(output, Opcodes.ACC_PUBLIC, "<init>", "(L" + cls.getName() + ";)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(anyName2, "value", "L" + cls.getName() + ";");
constructor.returnType(Type.VOID_TYPE);
constructor.end();
}
@Override
public void defineStaticCanCastImplicit(MethodOutput output) {
// Class
// if (cls.isAssignableFrom(param)) return true;
output.constant(Type.getType(cls));
output.loadObject(1);
output.invokeVirtual(Class.class, "isAssignableFrom", boolean.class, Class.class);
Label lblNotAssignable = new Label();
output.ifEQ(lblNotAssignable);
output.iConst1();
output.returnInt();
output.label(lblNotAssignable);
for (ZenNativeCaster caster : casters) {
caster.compileAnyCanCastImplicit(ZenTypeNative.this, output, environment, 0);
}
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
expansion.compileAnyCanCastImplicit(ZenTypeNative.this, output, environment, 0);
}
output.iConst0();
output.returnInt();
}
@Override
public void defineStaticAs(MethodOutput output) {
output.constant(Type.getType(cls));
output.loadObject(1);
output.invokeVirtual(Class.class, "isAssignableFrom", boolean.class, Class.class);
Label lblNotAssignable = new Label();
output.ifEQ(lblNotAssignable);
output.loadObject(0);
output.returnObject();
output.label(lblNotAssignable);
for (ZenNativeCaster caster : casters) {
caster.compileAnyCast(ZenTypeNative.this, output, environment, 0, 1);
}
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
expansion.compileAnyCast(ZenTypeNative.this, output, environment, 0, 1);
}
throwCastException(output, "float", 1);
}
@Override
public void defineNot(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "not");
/*for (ZenNativeOperator unaryOperator : unaryOperators) {
if (unaryOperator.getOperator() == OperatorType.NOT) {
}
}
TypeExpansion expansion = environment.getExpansion(getName());
if (expansion != null) {
if (expansion.compileAnyUnary(output, OperatorType.NOT)) {
}
}*/
}
@Override
public void defineNeg(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "negate");
}
@Override
public void defineAdd(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "+");
}
@Override
public void defineSub(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "-");
}
@Override
public void defineCat(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "~");
}
@Override
public void defineMul(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "*");
}
@Override
public void defineDiv(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "/");
}
@Override
public void defineMod(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "%");
}
@Override
public void defineAnd(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "&");
}
@Override
public void defineOr(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "|");
}
@Override
public void defineXor(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "^");
}
@Override
public void defineRange(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "..");
}
@Override
public void defineCompareTo(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "compare");
}
@Override
public void defineContains(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "in");
}
@Override
public void defineMemberGet(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "member get");
}
@Override
public void defineMemberSet(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "member set");
}
@Override
public void defineMemberCall(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "member call");
}
@Override
public void defineIndexGet(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "index get");
}
@Override
public void defineIndexSet(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "index set");
}
@Override
public void defineCall(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "call");
}
@Override
public void defineAsBool(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asBool");
}
@Override
public void defineAsByte(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asByte");
}
@Override
public void defineAsShort(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asShort");
}
@Override
public void defineAsInt(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asInt");
}
@Override
public void defineAsLong(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asLong");
}
@Override
public void defineAsFloat(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asFloat");
}
@Override
public void defineAsDouble(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asDouble");
}
@Override
public void defineAsString(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "asString");
}
@Override
public void defineAs(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "as");
}
@Override
public void defineIs(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "is");
}
@Override
public void defineGetNumberType(MethodOutput output) {
output.iConst0();
output.returnInt();
}
@Override
public void defineIteratorSingle(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "iterator");
}
@Override
public void defineIteratorMulti(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "iterator");
}
@Override
public void defineEquals(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "equals");
}
@Override
public void defineHashCode(MethodOutput output) {
// TODO: implement
throwUnsupportedException(output, getName(), "hashCode");
}
}
}
@@ -15,6 +15,9 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionNull;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.CastingRuleNone;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -29,6 +32,20 @@ public class ZenTypeNull extends ZenType {
}
@Override
public ICastingRule getCastingRule(ZenType type, IEnvironmentGlobal environment) {
if (type.isPointer()) {
return new CastingRuleNone(this, type);
} else {
return null;
}
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
}
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
environment.error(position, "null has not operators");
@@ -77,20 +94,20 @@ public class ZenTypeNull extends ZenType {
return null;
}
@Override
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type.isPointer();
}
}*/
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type.isPointer();
}
@Override
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
return value;
}
}*/
@Override
public Class toJavaClass() {
@@ -117,10 +134,10 @@ public class ZenTypeNull extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
// nothing to do
}
}*/
@Override
public String getName() {
@@ -13,14 +13,22 @@ import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionArithmeticBinary;
import stanhebben.zenscript.expression.ExpressionArithmeticCompare;
import stanhebben.zenscript.expression.ExpressionArithmeticUnary;
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.BYTEOBJECT;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUEOF;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.type.casting.CastingRuleI2B;
import stanhebben.zenscript.type.casting.CastingRuleI2D;
import stanhebben.zenscript.type.casting.CastingRuleI2F;
import stanhebben.zenscript.type.casting.CastingRuleI2L;
import stanhebben.zenscript.type.casting.CastingRuleNone;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
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;
@@ -45,8 +53,28 @@ public class ZenTypeShort extends ZenType {
public IZenIterator makeIterator(int numValues, IEnvironmentMethod environment) {
return null;
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleI2B(null));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleStaticMethod(BYTE_VALUEOF));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleStaticMethod(SHORT_VALUEOF));
rules.registerCastingRule(INT, new CastingRuleNone(SHORT, INT));
rules.registerCastingRule(INTOBJECT, new CastingRuleStaticMethod(INT_VALUEOF));
rules.registerCastingRule(LONG, new CastingRuleI2L(null));
rules.registerCastingRule(LONGOBJECT, new CastingRuleStaticMethod(LONG_VALUEOF, new CastingRuleI2L(null)));
rules.registerCastingRule(FLOAT, new CastingRuleI2F(null));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleStaticMethod(FLOAT_VALUEOF, new CastingRuleI2F(null)));
rules.registerCastingRule(DOUBLE, new CastingRuleI2D(null));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleStaticMethod(DOUBLE_VALUEOF, new CastingRuleI2D(null)));
rules.registerCastingRule(ANY, new CastingRuleStaticMethod(JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, BYTE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return (type.getNumberType() != 0 && type.getNumberType() >= NUM_SHORT)
|| type == ZenTypeString.INSTANCE
@@ -56,10 +84,7 @@ public class ZenTypeShort extends ZenType {
@Override
public boolean canCastExplicit(ZenType type, IEnvironmentGlobal environment) {
return type.getNumberType() != 0
|| type == ZenTypeString.INSTANCE
|| type == ANY
|| canCastExpansion(environment, type);
return canCastImplicit(type, environment);
}
@Override
@@ -71,7 +96,7 @@ public class ZenTypeShort extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -116,7 +141,7 @@ public class ZenTypeShort extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -155,7 +180,7 @@ public class ZenTypeShort extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(ZenPosition position, IEnvironmentGlobal environment, Expression value, OperatorType operator) {
@@ -326,7 +351,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iAdd();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -338,7 +363,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iSub();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -356,7 +381,7 @@ public class ZenTypeShort extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, int.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -369,7 +394,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iMul();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -381,7 +406,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iDiv();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -393,7 +418,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iRem();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -405,7 +430,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iAnd();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -417,7 +442,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iOr();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -429,7 +454,7 @@ public class ZenTypeShort extends ZenType {
output.dup();
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.iXor();
output.invokeSpecial(ANY_NAME, "<init>", "(S)V");
output.returnObject();
@@ -446,7 +471,7 @@ public class ZenTypeShort extends ZenType {
public void defineCompareTo(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASBYTE);
METHOD_ASSHORT.invokeVirtual(output);
output.iSub();
output.returnInt();
}
@@ -602,7 +627,7 @@ public class ZenTypeShort extends ZenType {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSHORT);
METHOD_ASSHORT.invokeVirtual(output);
output.ifICmpNE(lblNope);
output.iConst1();
@@ -6,23 +6,25 @@
package stanhebben.zenscript.type;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.BYTE;
import static stanhebben.zenscript.type.ZenType.BYTE_VALUE;
import static stanhebben.zenscript.type.ZenType.STRING;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleNullableVirtualMethod;
import stanhebben.zenscript.type.casting.CastingRuleVirtualMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.signature;
import stanhebben.zenscript.value.IAny;
/**
*
@@ -74,6 +76,43 @@ public class ZenTypeShortObject extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BYTE, new CastingRuleVirtualMethod(BYTE_VALUE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(
BYTE_VALUEOF,
new CastingRuleVirtualMethod(BYTE_VALUE)));
rules.registerCastingRule(SHORT, new CastingRuleVirtualMethod(SHORT_VALUE));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(
SHORT_VALUEOF,
new CastingRuleVirtualMethod(SHORT_VALUE)));
rules.registerCastingRule(INT, new CastingRuleVirtualMethod(INT_VALUE));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(
INT_VALUEOF,
new CastingRuleVirtualMethod(INT_VALUE)));
rules.registerCastingRule(LONG, new CastingRuleVirtualMethod(LONG_VALUE));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(
LONG_VALUEOF,
new CastingRuleVirtualMethod(LONG_VALUE)));
rules.registerCastingRule(FLOAT, new CastingRuleVirtualMethod(FLOAT_VALUE));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(
FLOAT_VALUEOF,
new CastingRuleVirtualMethod(FLOAT_VALUE)));
rules.registerCastingRule(DOUBLE, new CastingRuleVirtualMethod(DOUBLE_VALUE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(
DOUBLE_VALUEOF,
new CastingRuleVirtualMethod(DOUBLE_VALUE)));
rules.registerCastingRule(STRING, new CastingRuleNullableVirtualMethod(SHORTOBJECT, SHORT_TOSTRING));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(
JavaMethod.getStatic(getAnyClassName(environment), "valueOf", ANY, SHORT),
new CastingRuleVirtualMethod(SHORT_VALUE)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return SHORT.canCastImplicit(type, environment);
}
@@ -92,7 +131,7 @@ public class ZenTypeShortObject extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Class toJavaClass() {
@@ -119,7 +158,7 @@ public class ZenTypeShortObject extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -147,7 +186,7 @@ public class ZenTypeShortObject extends ZenType {
environment.getOutput().invokeVirtual(Short.class, "shortValue", short.class);
SHORT.compileCast(position, environment, type);
}
}
}*/
@Override
public String getName() {
@@ -12,16 +12,19 @@ import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.ExpressionCallVirtual;
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.type.casting.CastingRuleNullableStaticMethod;
import stanhebben.zenscript.type.casting.CastingRuleStaticMethod;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.AnyClassWriter;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASINT;
import static stanhebben.zenscript.util.AnyClassWriter.METHOD_ASSTRING;
@@ -50,6 +53,34 @@ public class ZenTypeString extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
rules.registerCastingRule(BOOL, new CastingRuleStaticMethod(PARSE_BOOL));
rules.registerCastingRule(BOOLOBJECT, new CastingRuleNullableStaticMethod(PARSE_BOOL_OBJECT));
rules.registerCastingRule(BYTE, new CastingRuleStaticMethod(PARSE_BYTE));
rules.registerCastingRule(BYTEOBJECT, new CastingRuleNullableStaticMethod(PARSE_BYTE_OBJECT));
rules.registerCastingRule(SHORT, new CastingRuleStaticMethod(PARSE_SHORT));
rules.registerCastingRule(SHORTOBJECT, new CastingRuleNullableStaticMethod(PARSE_SHORT_OBJECT));
rules.registerCastingRule(INT, new CastingRuleStaticMethod(PARSE_INT));
rules.registerCastingRule(INTOBJECT, new CastingRuleNullableStaticMethod(PARSE_INT_OBJECT));
rules.registerCastingRule(LONG, new CastingRuleStaticMethod(PARSE_LONG));
rules.registerCastingRule(LONGOBJECT, new CastingRuleNullableStaticMethod(PARSE_LONG_OBJECT));
rules.registerCastingRule(FLOAT, new CastingRuleStaticMethod(PARSE_FLOAT));
rules.registerCastingRule(FLOATOBJECT, new CastingRuleNullableStaticMethod(PARSE_FLOAT_OBJECT));
rules.registerCastingRule(DOUBLE, new CastingRuleStaticMethod(PARSE_DOUBLE));
rules.registerCastingRule(DOUBLEOBJECT, new CastingRuleNullableStaticMethod(PARSE_DOUBLE_OBJECT));
rules.registerCastingRule(ANY, new CastingRuleNullableStaticMethod(JavaMethod.getStatic(
getAnyClassName(environment),
"valueOf",
ANY,
STRING
)));
if (followCasters) {
constructExpansionCastingRules(environment, rules);
}
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this || type == ANY || canCastExpansion(environment, type);
}
@@ -70,7 +101,7 @@ public class ZenTypeString extends ZenType {
} else {
return new ExpressionAs(position, value, type);
}
}
}*/
@Override
public Type toASMType() {
@@ -115,7 +146,7 @@ public class ZenTypeString extends ZenType {
return true;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
MethodOutput output = environment.getOutput();
@@ -154,7 +185,7 @@ public class ZenTypeString extends ZenType {
} else if (!compileCastExpansion(position, environment, type)) {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}*/
@Override
public Expression unary(
@@ -212,7 +243,7 @@ public class ZenTypeString extends ZenType {
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);
return new ExpressionCompareGeneric(position, new ExpressionCallVirtual(position, environment, METHOD_ASINT, left, right), type);
}
Expression result = binaryExpansion(position, environment, left, right, OperatorType.COMPARE);
@@ -241,6 +272,12 @@ public class ZenTypeString extends ZenType {
return "string";
}
/**
* Returns the Java class name.
*
* @param environment
* @return
*/
@Override
public String getAnyClassName(IEnvironmentGlobal environment) {
if (!environment.containsClass(ANY_NAME_2)) {
@@ -345,7 +382,7 @@ public class ZenTypeString extends ZenType {
getValue(output);
output.invokeVirtual(StringBuilder.class, "append", StringBuilder.class, String.class);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
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));
@@ -391,7 +428,7 @@ public class ZenTypeString extends ZenType {
public void defineCompareTo(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
output.invokeVirtual(String.class, "compareTo", int.class, String.class);
output.returnInt();
}
@@ -400,7 +437,7 @@ public class ZenTypeString extends ZenType {
public void defineContains(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
output.invokeVirtual(String.class, "contains", boolean.class, CharSequence.class);
output.returnInt();
}
@@ -430,7 +467,7 @@ public class ZenTypeString extends ZenType {
// return new AnyString(String.substring(other.asInt(), other.asInt() + 1))
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASINT);
METHOD_ASINT.invokeVirtual(output);
output.dup();
output.iConst1();
output.iAdd();
@@ -564,7 +601,7 @@ public class ZenTypeString extends ZenType {
public void defineEquals(MethodOutput output) {
getValue(output);
output.loadObject(1);
output.invoke(METHOD_ASSTRING);
METHOD_ASSTRING.invokeVirtual(output);
output.invokeVirtual(String.class, "equals", boolean.class, Object.class);
output.returnInt();
}
@@ -14,6 +14,7 @@ import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.partial.IPartialExpression;
import stanhebben.zenscript.type.casting.ICastingRuleDelegate;
import stanhebben.zenscript.util.ZenPosition;
/**
@@ -50,6 +51,11 @@ public class ZenTypeVoid extends ZenType {
}
@Override
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules, boolean followCasters) {
}
/*@Override
public boolean canCastImplicit(ZenType type, IEnvironmentGlobal environment) {
return type == this || canCastExpansion(environment, type);
}
@@ -63,7 +69,7 @@ public class ZenTypeVoid extends ZenType {
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, Expression value, ZenType type) {
environment.error(position, "cannot cast void to other type");
return new ExpressionInvalid(position, type);
}
}*/
@Override
public Type toASMType() {
@@ -85,7 +91,7 @@ public class ZenTypeVoid extends ZenType {
return false;
}
@Override
/*@Override
public void compileCast(ZenPosition position, IEnvironmentMethod environment, ZenType type) {
if (type == this) {
// nothing to do
@@ -94,7 +100,7 @@ public class ZenTypeVoid extends ZenType {
environment.error(position, "cannot cast " + this + " to " + type);
}
}
}
}*/
@Override
public Expression unary(
@@ -0,0 +1,39 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
/**
*
* @author Stan
*/
public abstract class BaseCastingRule implements ICastingRule {
private final ICastingRule baseRule;
public BaseCastingRule(ICastingRule baseRule) {
this.baseRule = baseRule;
}
@Override
public final void compile(IEnvironmentMethod method) {
if (baseRule != null)
baseRule.compile(method);
compileInner(method);
}
@Override
public final ZenType getInputType() {
return baseRule == null ? getInnerInputType() : baseRule.getInputType();
}
public abstract ZenType getInnerInputType();
public abstract void compileInner(IEnvironmentMethod method);
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyBool implements ICastingRule {
public static final CastingAnyBool INSTANCE = new CastingAnyBool();
private CastingAnyBool() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asBool", boolean.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.BOOL;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyByte implements ICastingRule {
public static final CastingAnyByte INSTANCE = new CastingAnyByte();
private CastingAnyByte() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asByte", byte.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.BYTE;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyDouble implements ICastingRule {
public static final CastingAnyDouble INSTANCE = new CastingAnyDouble();
private CastingAnyDouble() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asDouble", double.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.DOUBLE;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyFloat implements ICastingRule {
public static final CastingAnyFloat INSTANCE = new CastingAnyFloat();
private CastingAnyFloat() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asFloat", float.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.FLOAT;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyInt implements ICastingRule {
public static final CastingAnyInt INSTANCE = new CastingAnyInt();
private CastingAnyInt() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asInt", int.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.INT;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyLong implements ICastingRule {
public static final CastingAnyLong INSTANCE = new CastingAnyLong();
private CastingAnyLong() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asLong", long.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.LONG;
}
}
@@ -0,0 +1,36 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyShort implements ICastingRule {
public static final CastingAnyShort INSTANCE = new CastingAnyShort();
private CastingAnyShort() {}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().invokeInterface(IAny.class, "asShort", short.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.SHORT;
}
}
@@ -0,0 +1,51 @@
/*
* 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.casting;
import org.objectweb.asm.Label;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingAnyString implements ICastingRule {
public static final CastingAnyString INSTANCE = new CastingAnyString();
private CastingAnyString() {}
@Override
public void compile(IEnvironmentMethod method) {
MethodOutput output = method.getOutput();
Label lblNonNull = new Label();
Label lblAfter = new Label();
output.dup();
output.ifNonNull(lblNonNull);
output.pop();
output.aConstNull();
output.goTo(lblAfter);
output.label(lblNonNull);
output.invokeInterface(IAny.class, "asString", String.class);
output.label(lblAfter);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return ZenType.STRING;
}
}
@@ -0,0 +1,39 @@
/*
* 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.casting;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.value.IAny;
/**
*
* @author Stan
*/
public class CastingRuleAny implements ICastingRule {
private final ZenType toType;
public CastingRuleAny(ZenType toType) {
this.toType = toType;
}
@Override
public void compile(IEnvironmentMethod method) {
method.getOutput().constant(toType);
method.getOutput().invokeInterface(IAny.class, "as", Class.class, Object.class);
}
@Override
public ZenType getInputType() {
return ZenType.ANY;
}
@Override
public ZenType getResultingType() {
return toType;
}
}
Loaded 100 of 172 files, more files were not shown because too many files have changed in this diff. Show more