format all the things!

This commit is contained in:
jaredlll08 committed 2015-08-31 19:15:35 +02:00
1 parent 54ec75fd3d
commit b36c6dade6
377 files changed
+6833 -6772

No files matched your search

@@ -11,23 +11,23 @@ import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.casting.ICastingRule;
import stanhebben.zenscript.util.ZenPosition;
public abstract class Expression implements IPartialExpression {
public abstract class Expression implements IPartialExpression {
public static final Expression parse(ZenTokener parser, IEnvironmentMethod environment, ZenType predictedType) {
return ParsedExpression.read(parser, environment)
.compile(environment, predictedType)
.eval(environment);
.compile(environment, predictedType)
.eval(environment);
}
private final ZenPosition position;
public Expression(ZenPosition position) {
this.position = position;
}
public ZenPosition getPosition() {
return position;
}
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (getType().equals(type)) {
return this;
@@ -41,9 +41,9 @@ public abstract class Expression implements IPartialExpression {
}
}
}
public abstract void compile(boolean result, IEnvironmentMethod environment);
public void compileIf(Label onElse, IEnvironmentMethod environment) {
if (getType() == ZenType.BOOL) {
compile(true, environment);
@@ -55,42 +55,42 @@ public abstract class Expression implements IPartialExpression {
throw new RuntimeException("cannot compile non-pointer non-boolean value to if condition");
}
}
// #########################################
// ### IPartialExpression implementation ###
// #########################################
@Override
public Expression eval(IEnvironmentGlobal environment) {
return this;
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "not a valid lvalue");
return new ExpressionInvalid(position, getType());
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
return getType().getMember(position, environment, this, name);
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
return getType().call(position, environment, this, values);
}
@Override
public ZenType[] predictCallTypes(int numArguments) {
return getType().predictCallTypes(numArguments);
}
@Override
public IZenSymbol toSymbol() {
return null;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
@@ -9,10 +9,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionAndAnd extends Expression {
private final Expression a;
private final Expression b;
public ExpressionAndAnd(ZenPosition position, Expression a, Expression b) {
super(position);
this.a = a;
this.b = b;
}
@@ -27,9 +27,9 @@ public class ExpressionAndAnd extends Expression {
// if not a: return false
// if not b: return false
// return true
MethodOutput output = environment.getOutput();
Label skip = new Label();
a.compile(true, environment);
output.ifEQ(skip);
@@ -37,7 +37,7 @@ public class ExpressionAndAnd extends Expression {
output.ifEQ(skip);
output.iConst1();
output.label(skip);
if (!result) {
output.pop();
}
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionArgument extends Expression {
private final int id;
private final ZenType type;
public ExpressionArgument(ZenPosition position, int id, ZenType type) {
super(position);
this.id = id;
this.type = type;
}
@@ -27,10 +27,10 @@ public class ExpressionArithmeticBinary extends Expression {
private final OperatorType operator;
private final Expression a;
private final Expression b;
public ExpressionArithmeticBinary(ZenPosition position, OperatorType operator, Expression a, Expression b) {
super(position);
this.operator = operator;
this.a = a;
this.b = b;
@@ -46,10 +46,10 @@ public class ExpressionArithmeticBinary extends Expression {
if (result) {
a.compile(result, environment);
b.compile(result, environment);
ZenType type = a.getType();
MethodOutput output = environment.getOutput();
if (type == ZenType.BOOL) {
switch (operator) {
case AND:
@@ -18,10 +18,10 @@ public class ExpressionArithmeticCompare extends Expression {
private final Expression a;
private final Expression b;
private final CompareType type;
public ExpressionArithmeticCompare(ZenPosition position, CompareType type, Expression a, Expression b) {
super(position);
this.a = a;
this.b = b;
this.type = type;
@@ -36,14 +36,14 @@ public class ExpressionArithmeticCompare extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
a.compile(result, environment);
b.compile(result, environment);
if (result) {
MethodOutput output = environment.getOutput();
if (a.getType() == ZenType.BOOL) {
if (type == CompareType.EQ) {
Label onThen = new Label();
Label onEnd = new Label();
output.ifICmpEQ(onThen);
output.iConst0();
output.goTo(onEnd);
@@ -53,7 +53,7 @@ public class ExpressionArithmeticCompare extends Expression {
} else if (type == CompareType.NE) {
Label onThen = new Label();
Label onEnd = new Label();
output.ifICmpNE(onThen);
output.iConst0();
output.goTo(onEnd);
@@ -66,7 +66,7 @@ public class ExpressionArithmeticCompare extends Expression {
} else {
Label onThen = new Label();
Label onEnd = new Label();
if (a.getType() == ZenTypeLong.INSTANCE) {
output.lCmp();
} else if (a.getType() == ZenTypeFloat.INSTANCE) {
@@ -80,19 +80,31 @@ public class ExpressionArithmeticCompare extends Expression {
} else {
throw new RuntimeException("Unsupported type for arithmetic compare");
}
switch (type) {
case EQ: output.ifICmpEQ(onThen); break;
case NE: output.ifICmpNE(onThen); break;
case LE: output.ifICmpLE(onThen); break;
case GE: output.ifICmpGE(onThen); break;
case LT: output.ifICmpLT(onThen); break;
case GT: output.ifICmpGT(onThen); break;
case EQ:
output.ifICmpEQ(onThen);
break;
case NE:
output.ifICmpNE(onThen);
break;
case LE:
output.ifICmpLE(onThen);
break;
case GE:
output.ifICmpGE(onThen);
break;
case LT:
output.ifICmpLT(onThen);
break;
case GT:
output.ifICmpGT(onThen);
break;
default:
environment.error(getPosition(), "this kind of comparison is not supported on int values");
return;
}
output.iConst0();
output.goTo(onEnd);
output.label(onThen);
@@ -16,10 +16,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionArithmeticUnary extends Expression {
private final Expression base;
private final OperatorType operator;
public ExpressionArithmeticUnary(ZenPosition position, OperatorType operator, Expression base) {
super(position);
this.base = base;
this.operator = operator;
}
@@ -32,7 +32,7 @@ public class ExpressionArithmeticUnary extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
base.compile(result, environment);
MethodOutput output = environment.getOutput();
if (result) {
ZenType type = base.getType();
@@ -69,7 +69,7 @@ public class ExpressionArithmeticUnary extends Expression {
}
}
}
environment.error(getPosition(), "Invalid operation");
}
}
@@ -13,10 +13,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionArray extends Expression {
private final Expression[] contents;
private final ZenTypeArrayBasic type;
public ExpressionArray(ZenPosition position, ZenTypeArrayBasic type, Expression... contents) {
super(position);
this.contents = contents;
this.type = type;
}
@@ -26,14 +26,14 @@ public class ExpressionArray extends Expression {
if (this.type.equals(type)) {
return this;
}
if (type instanceof ZenTypeArrayBasic) {
ZenTypeArrayBasic arrayType = (ZenTypeArrayBasic) type;
Expression[] newContents = new Expression[contents.length];
for (int i = 0; i < contents.length; i++) {
newContents[i] = contents[i].cast(position, environment, arrayType.getBaseType());
}
return new ExpressionArray(getPosition(), arrayType, newContents);
} else {
ICastingRule castingRule = this.type.getCastingRule(type, environment);
@@ -55,11 +55,11 @@ public class ExpressionArray extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
ZenType baseType = type.getBaseType();
Type asmBaseType = type.getBaseType().toASMType();
MethodOutput output = environment.getOutput();
output.constant(contents.length);
output.newArray(asmBaseType);
for (int i = 0; i < contents.length; i++) {
output.dup();
output.constant(i);
@@ -19,10 +19,10 @@ public class ExpressionArrayGet extends Expression {
private final Expression array;
private final Expression index;
private final ZenType baseType;
public ExpressionArrayGet(ZenPosition position, Expression array, Expression index) {
super(position);
this.array = array;
this.index = index;
this.baseType = ((ZenTypeArray) array.getType()).getBaseType();
@@ -37,7 +37,7 @@ public class ExpressionArrayGet extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
array.compile(result, environment);
index.compile(result, environment);
if (result) {
environment.getOutput().arrayLoad(baseType.toASMType());
}
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionArrayLength extends Expression {
private final Expression value;
public ExpressionArrayLength(ZenPosition position, Expression value) {
super(position);
this.value = value;
}
@@ -18,15 +18,15 @@ public class ExpressionArraySet extends Expression {
private final Expression array;
private final Expression index;
private final Expression value;
public ExpressionArraySet(ZenPosition position, Expression array, Expression index, Expression value) {
super(position);
this.array = array;
this.index = index;
this.value = value;
}
@Override
public ZenType getType() {
return ZenType.VOID;
@@ -37,7 +37,7 @@ public class ExpressionArraySet extends Expression {
array.compile(result, environment);
index.compile(result, environment);
value.compile(result, environment);
if (result) {
environment.getOutput().arrayStore(value.getType().toASMType());
}
@@ -9,28 +9,24 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionAs extends Expression {
private final Expression value;
private final ICastingRule castingRule;
public ExpressionAs(ZenPosition position, Expression value, ICastingRule castingRule) {
super(position);
this.value = value;
this.castingRule = castingRule;
}
/*@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType 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 Expression cast(ZenPosition position, IEnvironmentGlobal
* environment, ZenType 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() {
@@ -40,7 +36,7 @@ public class ExpressionAs extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
value.compile(result, environment);
if (result) {
castingRule.compile(environment);
}
@@ -18,13 +18,13 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionBool extends Expression {
private final boolean value;
public ExpressionBool(ZenPosition position, boolean value) {
super(position);
this.value = value;
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type == ZenType.BOOL) {
@@ -21,14 +21,14 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionCallStatic extends Expression {
private final IJavaMethod method;
private final Expression[] arguments;
public ExpressionCallStatic(
ZenPosition position,
IEnvironmentGlobal environment,
IJavaMethod method,
Expression... arguments) {
super(position);
this.method = method;
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
}
@@ -40,14 +40,14 @@ public class ExpressionCallStatic extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
MethodOutput output = environment.getOutput();
MethodOutput output = environment.getOutput();
for (Expression argument : arguments) {
argument.compile(true, environment);
}
method.invokeStatic(output);
if (method.getReturnType() != ZenType.VOID && !result) {
output.pop(method.getReturnType().isLarge());
}
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionCallVirtual extends Expression {
private final IJavaMethod method;
private final Expression receiver;
private final Expression[] arguments;
public ExpressionCallVirtual(
ZenPosition position,
IEnvironmentGlobal environment,
@@ -30,9 +30,9 @@ public class ExpressionCallVirtual extends Expression {
Expression receiver,
Expression... arguments) {
super(position);
this.method = method;
this.receiver = receiver;
this.arguments = JavaMethod.rematch(position, method, environment, arguments);
}
@@ -45,11 +45,11 @@ public class ExpressionCallVirtual extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
receiver.compile(true, environment);
for (Expression argument : arguments) {
argument.compile(true, environment);
}
method.invokeVirtual(environment.getOutput());
if (method.getReturnType() != ZenType.VOID && !result) {
environment.getOutput().pop(method.getReturnType().isLarge());
@@ -21,10 +21,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionCompareGeneric extends Expression {
private final Expression value; // should return a compareTo value (<=>0)
private final CompareType type;
public ExpressionCompareGeneric(ZenPosition position, Expression value, CompareType type) {
super(position);
this.value = value;
this.type = type;
}
@@ -35,24 +35,37 @@ public class ExpressionCompareGeneric extends Expression {
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {;
public void compile(boolean result, IEnvironmentMethod environment) {
;
value.compile(result, environment);
if (result) {
MethodOutput output = environment.getOutput();
Label lblThen = new Label();
Label lblEnd = new Label();
switch (type) {
case LT: output.ifLT(lblThen); break;
case GT: output.ifGT(lblThen); break;
case LE: output.ifLE(lblThen); break;
case GE: output.ifGE(lblThen); break;
case NE: output.ifNE(lblThen); break;
case EQ: output.ifEQ(lblThen); break;
case LT:
output.ifLT(lblThen);
break;
case GT:
output.ifGT(lblThen);
break;
case LE:
output.ifLE(lblThen);
break;
case GE:
output.ifGE(lblThen);
break;
case NE:
output.ifNE(lblThen);
break;
case EQ:
output.ifEQ(lblThen);
break;
default:
throw new RuntimeException();
}
output.iConst0();
output.goTo(lblEnd);
output.label(lblThen);
@@ -9,10 +9,10 @@ public class ExpressionConditional extends Expression {
private final Expression condition;
private final Expression onIf;
private final Expression onElse;
public ExpressionConditional(ZenPosition position, Expression condition, Expression onIf, Expression onElse) {
super(position);
this.condition = condition;
this.onIf = onIf;
this.onElse = onElse;
@@ -27,7 +27,7 @@ public class ExpressionConditional extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
Label lblElse = new Label();
Label lblExit = new Label();
condition.compileIf(lblElse, environment);
onIf.compile(result, environment);
environment.getOutput().goTo(lblExit);
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionFloat extends Expression {
private final double value;
private final ZenType type;
public ExpressionFloat(ZenPosition position, double value, ZenType type) {
super(position);
this.value = value;
this.type = type;
}
@@ -34,8 +34,9 @@ public class ExpressionFloat extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
if (!result)
return;
if (type == ZenTypeFloat.INSTANCE) {
environment.getOutput().constant((float) value);
} else if (type == ZenTypeDouble.INSTANCE) {
@@ -26,18 +26,18 @@ public class ExpressionFunction extends Expression {
private final List<ParsedFunctionArgument> arguments;
private final ZenType returnType;
private final List<Statement> statements;
private final ZenTypeFunction functionType;
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, List<Statement> statements) {
super(position);
System.out.println("Function expression: " + arguments.size() + " arguments");
this.arguments = arguments;
this.returnType = returnType;
this.statements = statements;
ZenType[] argumentTypes = new ZenType[arguments.size()];
for (int i = 0; i < arguments.size(); i++) {
argumentTypes[i] = arguments.get(i).getType();
@@ -70,7 +70,7 @@ public class ExpressionFunction extends Expression {
return new ExpressionInvalid(position);
}
}
return new ExpressionJavaLambda(
position,
nativeClass,
@@ -23,18 +23,19 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionInt extends Expression {
private final long value;
private final ZenType type;
public ExpressionInt(ZenPosition position, long value, ZenType type) {
super(position);
this.value = value;
this.type = type;
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type == this.type) return this;
if (type == this.type)
return this;
switch (type.getNumberType()) {
case ZenType.NUM_BYTE:
case ZenType.NUM_SHORT:
@@ -45,7 +46,7 @@ public class ExpressionInt extends Expression {
case ZenType.NUM_DOUBLE:
return new ExpressionFloat(getPosition(), value, type);
}
return super.cast(position, environment, type);
}
@@ -56,8 +57,9 @@ public class ExpressionInt extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
if (!result)
return;
MethodOutput output = environment.getOutput();
if (type == ZenTypeByte.INSTANCE) {
output.biPush((byte) value);
@@ -18,7 +18,7 @@ public class ExpressionIntegerRange extends Expression {
public ExpressionIntegerRange(ZenPosition position, Expression from, Expression to) {
super(position);
this.from = from;
this.to = to;
}
@@ -19,22 +19,22 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionInvalid extends Expression {
private final ZenType type;
public ExpressionInvalid(ZenPosition position) {
super(position);
type = ZenTypeAny.INSTANCE;
}
public ExpressionInvalid(ZenPosition position, ZenType type) {
super(position);
this.type = type;
// XXX: remove before release
//throw new RuntimeException("Constructing invalid expression");
// throw new RuntimeException("Constructing invalid expression");
}
@Override
public Expression getMember(ZenPosition position, IEnvironmentGlobal errors, String name) {
return new ExpressionInvalid(position);
@@ -31,9 +31,9 @@ public class ExpressionJavaLambda extends Expression {
private final Class interfaceClass;
private final List<ParsedFunctionArgument> arguments;
private final List<Statement> statements;
private final ZenType type;
public ExpressionJavaLambda(
ZenPosition position,
Class interfaceClass,
@@ -41,11 +41,11 @@ public class ExpressionJavaLambda extends Expression {
List<Statement> statements,
ZenType type) {
super(position);
this.interfaceClass = interfaceClass;
this.arguments = arguments;
this.statements = statements;
this.type = type;
}
@@ -56,43 +56,44 @@ public class ExpressionJavaLambda extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
if (!result)
return;
Method method = interfaceClass.getMethods()[0];
// generate class
String clsName = environment.makeClassName();
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, clsName, null, "java/lang/Object", new String[] { internal(interfaceClass) });
MethodOutput constructor = new MethodOutput(cw, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
constructor.start();
constructor.loadObject(0);
constructor.invokeSpecial("java/lang/Object", "<init>", "()V");
constructor.ret();
constructor.end();
MethodOutput output = new MethodOutput(cw, Opcodes.ACC_PUBLIC, method.getName(), descriptor(method), null, null);
IEnvironmentClass environmentClass = new EnvironmentClass(cw, environment);
IEnvironmentMethod environmentMethod = new EnvironmentMethod(output, environmentClass);
for (int i = 0; i < arguments.size(); i++) {
environmentMethod.putValue(
arguments.get(i).getName(),
new SymbolArgument(i + 1, environment.getType(method.getGenericParameterTypes()[i])),
getPosition());
}
output.start();
for (Statement statement : statements) {
statement.compile(environmentMethod);
}
output.end();
environment.putClass(clsName, cw.toByteArray());
// make class instance
environment.getOutput().newObject(clsName);
environment.getOutput().dup();
@@ -24,22 +24,22 @@ public class ExpressionJavaMethodCurled extends Expression {
private final JavaMethod method;
private final ZenType type;
private final Expression receiver;
public ExpressionJavaMethodCurled(
ZenPosition position,
JavaMethod method,
IEnvironmentGlobal environment,
Expression receiver) {
super(position);
this.method = method;
this.receiver = receiver;
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
for (int i = 0; i < method.getMethod().getParameterTypes().length; i++) {
arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i])));
}
this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments);
}
@@ -23,17 +23,17 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionJavaMethodStatic extends Expression {
private final JavaMethod method;
private final ZenType type;
public ExpressionJavaMethodStatic(ZenPosition position, JavaMethod method, IEnvironmentGlobal environment) {
super(position);
this.method = method;
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
for (int i = 0; i < method.getParameterTypes().length; i++) {
arguments.add(new ParsedFunctionArgument("p" + i, environment.getType(method.getMethod().getGenericParameterTypes()[i])));
}
this.type = new ZenTypeFunction(environment.getType(method.getMethod().getGenericReturnType()), arguments);
}
@@ -17,13 +17,13 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionLocalGet extends Expression {
private final SymbolLocal variable;
public ExpressionLocalGet(ZenPosition position, SymbolLocal variable) {
super(position);
this.variable = variable;
}
@Override
public ZenType getType() {
return variable.getType();
@@ -18,14 +18,14 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionLocalSet extends Expression {
private final SymbolLocal variable;
private final Expression value;
public ExpressionLocalSet(ZenPosition position, SymbolLocal variable, Expression value) {
super(position);
this.variable = variable;
this.value = value;
}
@Override
public ZenType getType() {
return variable.getType();
@@ -34,7 +34,7 @@ public class ExpressionLocalSet extends Expression {
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
int local = environment.getLocal(variable);
value.compile(true, environment);
if (result) {
environment.getOutput().dup();
@@ -16,13 +16,13 @@ public class ExpressionMap extends Expression {
private final Expression[] keys;
private final Expression[] values;
private final ZenTypeAssociative type;
public ExpressionMap(ZenPosition position, Expression[] keys, Expression[] values, ZenTypeAssociative type) {
super(position);
this.keys = keys;
this.values = values;
this.type = type;
}
@@ -36,12 +36,12 @@ public class ExpressionMap extends Expression {
if (result) {
ZenType keyType = type.getKeyType();
ZenType valueType = type.getValueType();
MethodOutput output = environment.getOutput();
output.newObject(HashMap.class);
output.dup();
output.invokeSpecial(internal(HashMap.class), "<init>", "()V");
for (int i = 0; i < keys.length; i++) {
output.dup();
keys[i].cast(getPosition(), environment, keyType).compile(true, environment);
@@ -58,23 +58,23 @@ public class ExpressionMap extends Expression {
}
}
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (this.type.equals(type)) {
return this;
}
if (type instanceof ZenTypeAssociative) {
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, associativeType.getKeyType());
newValues[i] = values[i].cast(position, environment, associativeType.getValueType());
}
return new ExpressionMap(getPosition(), newKeys, newValues, associativeType);
} else {
ICastingRule castingRule = this.type.getCastingRule(type, environment);
@@ -20,10 +20,10 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
public class ExpressionMapContains extends Expression {
private final Expression map;
private final Expression key;
public ExpressionMapContains(ZenPosition position, Expression map, Expression key) {
super(position);
this.map = map;
this.key = key;
}
@@ -38,7 +38,7 @@ public class ExpressionMapContains extends Expression {
if (result) {
map.compile(result, environment);
key.compile(result, environment);
environment.getOutput().invokeInterface(internal(Map.class), "containsKey", "()Ljava/lang/Object;");
}
}
@@ -20,15 +20,15 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
public class ExpressionMapIndexGet extends Expression {
private final Expression map;
private final Expression index;
private final ZenType type;
public ExpressionMapIndexGet(ZenPosition position, Expression map, Expression index) {
super(position);
this.map = map;
this.index = index;
type = ((ZenTypeAssociative) map.getType()).getValueType();
}
@@ -21,16 +21,16 @@ public class ExpressionMapIndexSet extends Expression {
private final Expression map;
private final Expression index;
private final Expression value;
private final ZenType type;
public ExpressionMapIndexSet(ZenPosition position, Expression map, Expression index, Expression value) {
super(position);
this.map = map;
this.index = index;
this.value = value;
type = ZenTypeVoid.INSTANCE;
}
@@ -19,10 +19,10 @@ import static stanhebben.zenscript.util.ZenTypeUtil.internal;
*/
public class ExpressionMapSize extends Expression {
private final Expression map;
public ExpressionMapSize(ZenPosition position, Expression map) {
super(position);
this.map = map;
}
@@ -21,7 +21,7 @@ public class ExpressionNull extends Expression {
public ExpressionNull(ZenPosition position) {
super(position);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type.isPointer()) {
@@ -7,10 +7,10 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionOrOr extends Expression {
private final Expression a;
private final Expression b;
public ExpressionOrOr(ZenPosition position, Expression a, Expression b) {
super(position);
this.a = a;
this.b = b;
}
@@ -17,10 +17,10 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionString extends Expression {
private final String value;
public ExpressionString(ZenPosition position, String value) {
super(position);
this.value = value;
}
@@ -19,13 +19,13 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public class ExpressionStringConcat extends Expression {
private final List<Expression> values;
public ExpressionStringConcat(ZenPosition position, List<Expression> values) {
super(position);
this.values = values;
}
public void add(Expression value) {
values.add(value);
}
@@ -39,18 +39,18 @@ public class ExpressionStringConcat extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
MethodOutput output = environment.getOutput();
// Step 1: construct StringBuilder
output.newObject(StringBuilder.class);
output.dup();
output.construct(StringBuilder.class);
// Step 2: concatenate Strings
for (Expression value : values) {
value.compile(true, environment);
output.invoke(StringBuilder.class, "append", StringBuilder.class, String.class);
}
// Step 3: return String
output.invoke(StringBuilder.class, "toString", String.class);
} else {
@@ -17,14 +17,14 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionStringContains extends Expression {
private final Expression haystack;
private final Expression needle;
public ExpressionStringContains(ZenPosition position, Expression haystack, Expression needle) {
super(position);
this.haystack = haystack;
this.needle = needle;
}
@Override
public ZenType getType() {
return ZenType.STRING;
@@ -34,7 +34,7 @@ public class ExpressionStringContains extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
haystack.compile(result, environment);
needle.compile(result, environment);
if (result) {
environment.getOutput().invokeVirtual(String.class, "contains", CharSequence.class);
}
@@ -17,14 +17,14 @@ import stanhebben.zenscript.util.ZenPosition;
public class ExpressionStringIndex extends Expression {
private final Expression source;
private final Expression index;
public ExpressionStringIndex(ZenPosition position, Expression source, Expression index) {
super(position);
this.source = source;
this.index = index;
}
@Override
public ZenType getType() {
return ZenType.STRING;
@@ -34,7 +34,7 @@ public class ExpressionStringIndex extends Expression {
public void compile(boolean result, IEnvironmentMethod environment) {
source.compile(result, environment);
index.compile(result, environment);
if (result) {
environment.getOutput().dup();
environment.getOutput().iConst1();
@@ -21,10 +21,10 @@ public class ExpressionJavaStaticField extends Expression {
private final Class cls;
private final Field field;
private final ITypeRegistry types;
public ExpressionJavaStaticField(ZenPosition position, Class cls, Field field, ITypeRegistry types) {
super(position);
this.cls = cls;
this.field = field;
this.types = types;
@@ -19,18 +19,18 @@ import stanhebben.zenscript.util.ZenPosition;
*/
public interface IPartialExpression {
public Expression eval(IEnvironmentGlobal environment);
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other);
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name);
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);
}
@@ -23,7 +23,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialJavaClass implements IPartialExpression {
private final ZenPosition position;
private final Type cls;
public PartialJavaClass(ZenPosition position, Type cls) {
this.position = position;
this.cls = cls;
@@ -24,7 +24,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialLocal implements IPartialExpression {
private final ZenPosition position;
private final SymbolLocal variable;
public PartialLocal(ZenPosition position, SymbolLocal variable) {
this.position = position;
this.variable = variable;
@@ -22,7 +22,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialPackage implements IPartialExpression {
private final ZenPosition position;
private final SymbolPackage contents;
public PartialPackage(ZenPosition position, SymbolPackage contents) {
this.position = position;
this.contents = contents;
@@ -29,7 +29,7 @@ public class PartialStaticGenerated implements IPartialExpression {
private final String signature;
private final ZenType[] argumentTypes;
private final ZenType returnType;
public PartialStaticGenerated(ZenPosition position, String owner, String method, String signature, ZenType[] argumentTypes, ZenType returnType) {
this.position = position;
this.owner = owner;
@@ -38,7 +38,7 @@ public class PartialStaticGenerated implements IPartialExpression {
this.argumentTypes = argumentTypes;
this.returnType = returnType;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
environment.error(position, "cannot use a function as value");
@@ -63,12 +63,12 @@ public class PartialStaticGenerated implements IPartialExpression {
environment.error(position, "invalid number of arguments");
return new ExpressionInvalid(position);
}
Expression[] arguments = new Expression[argumentTypes.length];
for (int i = 0; i < arguments.length; i++) {
arguments[i] = values[i].cast(position, environment, argumentTypes[i]);
}
return new ExpressionCallStatic(position, environment, JavaMethod.getStatic(owner, method, returnType, argumentTypes), arguments);
}
@@ -24,7 +24,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialStaticGetter implements IPartialExpression {
private final ZenPosition position;
private final IJavaMethod method;
public PartialStaticGetter(ZenPosition position, IJavaMethod method) {
this.position = position;
this.method = method;
@@ -25,7 +25,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialStaticMethod implements IPartialExpression {
private final ZenPosition position;
private final IJavaMethod method;
public PartialStaticMethod(ZenPosition position, IJavaMethod method) {
this.position = position;
this.method = method;
@@ -22,7 +22,7 @@ import stanhebben.zenscript.util.ZenPosition;
public class PartialType implements IPartialExpression {
private final ZenPosition position;
private final ZenType type;
public PartialType(ZenPosition position, ZenType type) {
this.position = position;
this.type = type;
@@ -55,7 +55,7 @@ public class PartialType implements IPartialExpression {
public IZenSymbol toSymbol() {
return new SymbolType(type);
}
@Override
public ZenType getType() {
return null; // not an expression