- 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

@@ -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) {