Added projects and basic build scripts. Project for scripting engine, API, Main 1.7 version and one for each mod supported in 1.7.

This commit is contained in:
Stan Hebben committed 2014-06-09 20:33:30 +02:00
1 parent 219106f392
commit 38b78ce305
281 files changed
+25090 -49

No files matched your search

@@ -0,0 +1,93 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.ZenParser;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
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.util.ZenPosition;
public abstract class Expression implements IPartialExpression {
public static final Expression parse(ZenParser parser, IEnvironmentMethod environment) {
return ParsedExpression.read(parser, environment)
.compile(environment)
.eval(environment);
}
private final ZenPosition position;
public Expression(ZenPosition position) {
this.position = position;
}
public ZenPosition getPosition() {
return position;
}
public abstract ZenType getType();
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (getType() == type) {
return this;
} else if (getType().canCastImplicit(type, environment)) {
return new ExpressionAs(position, this, type);
} else {
environment.error(position, "Cannot cast " + getType() + " to " + type);
return new ExpressionInvalid(position, type);
}
}
public abstract void compile(boolean result, IEnvironmentMethod environment);
public void compileIf(Label onElse, IEnvironmentMethod environment) {
if (getType() == ZenTypeBool.INSTANCE) {
compile(true, environment);
environment.getOutput().ifEQ(onElse);
} else if (getType().isPointer()) {
compile(true, environment);
environment.getOutput().ifNull(onElse);
} else {
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 IZenSymbol toSymbol() {
return null;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
return ZenType.ANY;
}
}
@@ -0,0 +1,53 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.MethodOutput;
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;
}
@Override
public ZenType getType() {
return a.getType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// 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);
b.compile(true, environment);
output.ifEQ(skip);
output.iConst1();
output.label(skip);
if (!result) {
output.pop();
}
}
@Override
public void compileIf(Label onElse, IEnvironmentMethod environment) {
a.compile(true, environment);
environment.getOutput().ifEQ(onElse);
b.compile(true, environment);
environment.getOutput().ifEQ(onElse);
}
}
@@ -0,0 +1,37 @@
/*
* 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.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
environment.getOutput().load(type.toASMType(), id);
}
}
@@ -0,0 +1,173 @@
/*
* 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.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.type.ZenTypeByte;
import stanhebben.zenscript.type.ZenTypeDouble;
import stanhebben.zenscript.type.ZenTypeFloat;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.type.ZenTypeLong;
import stanhebben.zenscript.type.ZenTypeShort;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return a.getType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
a.compile(result, environment);
b.compile(result, environment);
ZenType type = a.getType();
MethodOutput output = environment.getOutput();
if (type == ZenTypeBool.INSTANCE) {
switch (operator) {
case AND:
output.iAnd();
break;
case OR:
output.iOr();
break;
case XOR:
output.iXor();
break;
default:
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
}
} else if (type == ZenTypeByte.INSTANCE || type == ZenTypeShort.INSTANCE || type == ZenTypeInt.INSTANCE) {
switch (operator) {
case ADD:
output.iAdd();
break;
case SUB:
output.iSub();
break;
case MUL:
output.iMul();
break;
case DIV:
output.iDiv();
break;
case MOD:
output.iRem();
break;
case AND:
output.iAnd();
break;
case OR:
output.iOr();
break;
case XOR:
output.iXor();
break;
default:
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
}
} else if (type == ZenTypeLong.INSTANCE) {
switch (operator) {
case ADD:
output.lAdd();
break;
case SUB:
output.lSub();
break;
case MUL:
output.lMul();
break;
case DIV:
output.lDiv();
break;
case MOD:
output.lRem();
break;
case AND:
output.lAnd();
break;
case OR:
output.lOr();
break;
case XOR:
output.lXor();
break;
default:
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
}
} else if (type == ZenTypeFloat.INSTANCE) {
switch (operator) {
case ADD:
output.fAdd();
break;
case SUB:
output.fSub();
break;
case MUL:
output.fMul();
break;
case DIV:
output.fDiv();
break;
case MOD:
output.fRem();
break;
default:
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
}
} else if (type == ZenTypeDouble.INSTANCE) {
switch (operator) {
case ADD:
output.dAdd();
break;
case SUB:
output.dSub();
break;
case MUL:
output.dMul();
break;
case DIV:
output.dDiv();
break;
case MOD:
output.dRem();
break;
default:
throw new RuntimeException("Unsupported operator on " + type + ": " + operator);
}
} else {
throw new RuntimeException("Internal compilation error: " + type + " is not a supported arithmetic type");
}
} else {
a.compile(result, environment);
b.compile(result, environment);
}
}
}
@@ -0,0 +1,104 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.type.ZenTypeByte;
import stanhebben.zenscript.type.ZenTypeDouble;
import stanhebben.zenscript.type.ZenTypeFloat;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.type.ZenTypeLong;
import stanhebben.zenscript.type.ZenTypeShort;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
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;
}
@Override
public ZenType getType() {
return ZenTypeBool.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
a.compile(result, environment);
b.compile(result, environment);
if (result) {
MethodOutput output = environment.getOutput();
if (a.getType() == ZenTypeBool.INSTANCE) {
if (type == CompareType.EQ) {
Label onThen = new Label();
Label onEnd = new Label();
output.ifICmpEQ(onThen);
output.iConst0();
output.goTo(onEnd);
output.label(onThen);
output.iConst1();
output.label(onEnd);
} else if (type == CompareType.NE) {
Label onThen = new Label();
Label onEnd = new Label();
output.ifICmpNE(onThen);
output.iConst0();
output.goTo(onEnd);
output.label(onThen);
output.iConst1();
output.label(onEnd);
} else {
environment.error(getPosition(), "this kind of comparison is not supported on bool values");
}
} else {
Label onThen = new Label();
Label onEnd = new Label();
if (a.getType() == ZenTypeLong.INSTANCE) {
output.lCmp();
} else if (a.getType() == ZenTypeFloat.INSTANCE) {
output.fCmp();
} else if (a.getType() == ZenTypeDouble.INSTANCE) {
output.dCmp();
} else if (a.getType() == ZenTypeByte.INSTANCE
|| a.getType() == ZenTypeShort.INSTANCE
|| a.getType() == ZenTypeInt.INSTANCE) {
// nothing to do
} 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;
default:
environment.error(getPosition(), "this kind of comparison is not supported on int values");
return;
}
output.iConst0();
output.goTo(onEnd);
output.label(onThen);
output.iConst1();
output.label(onEnd);
}
}
}
}
@@ -0,0 +1,75 @@
package stanhebben.zenscript.expression;
import stanhebben.zenscript.annotations.OperatorType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.type.ZenTypeByte;
import stanhebben.zenscript.type.ZenTypeDouble;
import stanhebben.zenscript.type.ZenTypeFloat;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.type.ZenTypeLong;
import stanhebben.zenscript.type.ZenTypeShort;
import stanhebben.zenscript.util.MethodOutput;
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;
}
@Override
public ZenType getType() {
return base.getType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
base.compile(result, environment);
MethodOutput output = environment.getOutput();
if (result) {
ZenType type = base.getType();
if (type == ZenTypeBool.INSTANCE) {
if (operator == OperatorType.NOT) {
output.iNot();
return;
}
} else if (type == ZenTypeByte.INSTANCE || type == ZenTypeShort.INSTANCE || type == ZenTypeInt.INSTANCE) {
if (operator == OperatorType.NOT) {
output.iNot();
return;
} else if (operator == OperatorType.NEG) {
output.iNeg();
return;
}
} else if (type == ZenTypeLong.INSTANCE) {
if (operator == OperatorType.NOT) {
output.lNot();
return;
} else if (operator == OperatorType.NEG) {
output.lNeg();
return;
}
} else if (type == ZenTypeFloat.INSTANCE) {
if (operator == OperatorType.NEG) {
output.fNeg();
return;
}
} else if (type == ZenTypeDouble.INSTANCE) {
if (operator == OperatorType.NEG) {
output.fNeg();
return;
}
}
}
environment.error(getPosition(), "Invalid operation");
}
}
@@ -0,0 +1,68 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Type;
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.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
public class ExpressionArray extends Expression {
private final Expression[] contents;
private final ZenTypeArrayBasic type;
public ExpressionArray(ZenPosition position, 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 (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(), newContents, arrayType);
} else if (this.type.canCastImplicit(type, environment)) {
return this.type.cast(position, environment, this, type);
} else {
environment.error(position, "cannot cast " + this.type + " to " + type);
return new ExpressionInvalid(position, type);
}
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
Type baseType = type.getBaseType().toASMType();
MethodOutput output = environment.getOutput();
output.constant(contents.length);
output.newArray(baseType);
for (int i = 0; i < contents.length; i++) {
output.dup();
output.constant(i);
contents[i].compile(result, environment);
output.arrayStore(baseType);
}
}
}
@@ -0,0 +1,45 @@
/*
* 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.type.ZenTypeArray;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stan
*/
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();
}
@Override
public ZenType getType() {
return baseType;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
array.compile(result, environment);
index.compile(result, environment);
if (result) {
environment.getOutput().arrayLoad(baseType.toASMType());
}
}
}
@@ -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.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionArrayLength extends Expression {
private final Expression value;
public ExpressionArrayLength(ZenPosition position, Expression value) {
super(position);
this.value = value;
}
@Override
public ZenType getType() {
return ZenTypeInt.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
environment.getOutput().arrayLength();
}
}
@@ -0,0 +1,45 @@
/*
* 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.ZenPosition;
/**
*
* @author Stan
*/
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;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
array.compile(result, environment);
index.compile(result, environment);
value.compile(result, environment);
if (result) {
environment.getOutput().arrayStore(value.getType().toASMType());
}
}
}
@@ -0,0 +1,39 @@
package stanhebben.zenscript.expression;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
public class ExpressionAs extends Expression {
private final Expression value;
private final ZenType type;
public ExpressionAs(ZenPosition position, Expression value, ZenType type) {
super(position);
this.value = value;
this.type = type;
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (!this.type.canCastExplicit(type, environment)) {
environment.error(position, "cannot cast " + this.type + " to " + type);
}
return new ExpressionAs(getPosition(), this, type);
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
value.compile(result, environment);
if (result) {
value.getType().compileCast(getPosition(), environment, type);
}
}
}
@@ -0,0 +1,71 @@
/*
* 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 org.objectweb.asm.Label;
import stanhebben.zenscript.compiler.EnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.compiler.EnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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 == ZenTypeBool.INSTANCE) {
return this;
} else if (type.canCastExplicit(type, environment)) {
return new ExpressionAs(position, this, type);
} else {
environment.error(position, "Cannot cast a bool constant to " + type.toString());
return new ExpressionInvalid(position, type);
}
}
@Override
public Expression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
environment.error(position, "Bool constants do not have members");
return new ExpressionInvalid(position, ZenTypeBool.INSTANCE);
}
@Override
public ZenType getType() {
return ZenTypeBool.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
if (value) {
environment.getOutput().iConst1();
} else {
environment.getOutput().iConst0();
}
}
}
@Override
public void compileIf(Label onElse, IEnvironmentMethod environment) {
if (!value) {
environment.getOutput().goTo(onElse);
}
}
}
@@ -0,0 +1,63 @@
/*
* 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 org.objectweb.asm.Label;
import stanhebben.zenscript.annotations.CompareType;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return ZenTypeBool.INSTANCE;
}
@Override
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;
default:
throw new RuntimeException();
}
output.iConst0();
output.goTo(lblEnd);
output.label(lblThen);
output.iConst1();
output.label(lblEnd);
}
}
}
@@ -0,0 +1,38 @@
package stanhebben.zenscript.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
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;
}
@Override
public ZenType getType() {
return onIf.getType(); // TODO: improve
}
@Override
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);
environment.getOutput().label(lblElse);
onElse.compile(result, environment);
environment.getOutput().label(lblExit);
}
}
@@ -0,0 +1,47 @@
/*
* 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.type.ZenTypeDouble;
import stanhebben.zenscript.type.ZenTypeFloat;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
if (type == ZenTypeFloat.INSTANCE) {
environment.getOutput().constant((float) value);
} else if (type == ZenTypeDouble.INSTANCE) {
environment.getOutput().constant(value);
} else {
// TODO: internal error
}
}
}
@@ -0,0 +1,98 @@
/*
* 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 java.lang.reflect.Method;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.statements.Statement;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAny;
import stanhebben.zenscript.type.ZenTypeFunction;
import stanhebben.zenscript.type.ZenTypeNative;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionFunction extends Expression {
private final List<ParsedFunctionArgument> arguments;
private final ZenType returnType;
private final Statement[] statements;
private final ZenTypeFunction functionType;
public ExpressionFunction(ZenPosition position, List<ParsedFunctionArgument> arguments, ZenType returnType, Statement[] statements) {
super(position);
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();
}
functionType = new ZenTypeFunction(returnType, argumentTypes);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type instanceof ZenTypeNative) {
ZenTypeNative nativeType = (ZenTypeNative) type;
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(position, "return type not compatible");
return new ExpressionInvalid(position);
}
if (arguments.size() != method.getParameterTypes().length) {
environment.error(position, "number of arguments incorrect");
return new ExpressionInvalid(position);
}
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(position, "argument " + i + " doesn't match");
return new ExpressionInvalid(position);
}
}
return new ExpressionJavaLambda(
position,
nativeClass,
arguments,
statements,
environment.getType(nativeClass));
} else {
environment.error(position, type.toString() + " is not a functional interface");
return new ExpressionInvalid(position);
}
} else {
environment.error(position, "Cannot cast a function literal to " + type.toString());
return new ExpressionInvalid(position);
}
}
@Override
public ZenType getType() {
return functionType;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: implement
throw new UnsupportedOperationException("not yet implemented");
}
}
@@ -0,0 +1,74 @@
/*
* 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.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeByte;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.type.ZenTypeLong;
import stanhebben.zenscript.type.ZenTypeShort;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
switch (type.getNumberType()) {
case ZenType.NUM_BYTE:
case ZenType.NUM_SHORT:
case ZenType.NUM_INT:
case ZenType.NUM_LONG:
return new ExpressionInt(getPosition(), value, type);
case ZenType.NUM_FLOAT:
case ZenType.NUM_DOUBLE:
return new ExpressionFloat(getPosition(), value, type);
}
return super.cast(position, environment, type);
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
MethodOutput output = environment.getOutput();
if (type == ZenTypeByte.INSTANCE) {
output.biPush((byte) value);
} else if (type == ZenTypeShort.INSTANCE) {
output.siPush((short) value);
} else if (type == ZenTypeInt.INSTANCE) {
output.constant((int) value);
} else if (type == ZenTypeLong.INSTANCE) {
output.constant(value);
} else {
// TODO: internal error
}
}
}
@@ -0,0 +1,35 @@
/*
* To change this template, 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.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionIntegerRange extends Expression {
private final Expression from;
private final Expression to;
public ExpressionIntegerRange(ZenPosition position, Expression from, Expression to) {
super(position);
this.from = from;
this.to = to;
}
@Override
public ZenType getType() {
return null; // TODO: implement
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: implement
}
}
@@ -0,0 +1,63 @@
/*
* 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 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;
/**
*
* @author Stanneke
*/
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;
}
@Override
public Expression getMember(ZenPosition position, IEnvironmentGlobal errors, String name) {
return new ExpressionInvalid(position);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal errors, ZenType type) {
return new ExpressionInvalid(position, type);
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
environment.getOutput().aConstNull();
}
}
@Override
public void compileIf(Label onElse, IEnvironmentMethod environment) {
environment.getOutput().goTo(onElse);
}
}
@@ -0,0 +1,52 @@
/*
* 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.type.natives.JavaMethod;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionJavaCallStatic extends Expression {
private final JavaMethod method;
private final Expression[] arguments;
public ExpressionJavaCallStatic(
ZenPosition position,
JavaMethod method,
Expression... arguments) {
super(position);
this.method = method;
this.arguments = arguments;
}
@Override
public ZenType getType() {
return method.getReturnType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
MethodOutput output = environment.getOutput();
for (Expression argument : arguments) {
argument.compile(true, environment);
}
output.invokeStatic(method);
if (method.getReturnType() != ZenType.VOID && !result) {
output.pop(method.getReturnType().isLarge());
}
}
}
@@ -0,0 +1,59 @@
/*
* 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();
}
}
}
@@ -0,0 +1,55 @@
/*
* 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.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionJavaCallVirtual extends Expression {
private final JavaMethod method;
private final Expression receiver;
private final Expression[] arguments;
public ExpressionJavaCallVirtual(
ZenPosition position,
JavaMethod method,
Expression receiver,
Expression... arguments) {
super(position);
this.method = method;
this.receiver = receiver;
this.arguments = arguments;
}
@Override
public ZenType getType() {
return method.getReturnType();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
receiver.compile(true, environment);
for (Expression argument : arguments) {
argument.compile(true, environment);
}
environment.getOutput().invoke(method);
if (method.getReturnType() != ZenType.VOID && !result) {
environment.getOutput().pop(method.getReturnType().isLarge());
}
}
}
@@ -0,0 +1,100 @@
/*
* 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 java.lang.reflect.Method;
import java.util.List;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import stanhebben.zenscript.compiler.EnvironmentClass;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.compiler.EnvironmentMethod;
import stanhebben.zenscript.compiler.IEnvironmentClass;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.statements.Statement;
import stanhebben.zenscript.symbols.SymbolArgument;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.descriptor;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
*/
public class ExpressionJavaLambda extends Expression {
private final Class interfaceClass;
private final List<ParsedFunctionArgument> arguments;
private final Statement[] statements;
private final ZenType type;
public ExpressionJavaLambda(
ZenPosition position,
Class interfaceClass,
List<ParsedFunctionArgument> arguments,
Statement[] statements,
ZenType type) {
super(position);
this.interfaceClass = interfaceClass;
this.arguments = arguments;
this.statements = statements;
this.type = type;
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (!result) return;
Method method = interfaceClass.getMethods()[0];
// generate class
String clsName = environment.makeClassName();
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
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])));
}
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();
environment.getOutput().construct(clsName);
}
}
@@ -0,0 +1,55 @@
/*
* 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 java.util.ArrayList;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeFunction;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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);
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: compile
}
}
@@ -0,0 +1,49 @@
/*
* 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 java.util.ArrayList;
import java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeFunction;
import stanhebben.zenscript.type.natives.JavaMethod;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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);
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: compile
}
}
@@ -0,0 +1,37 @@
/*
* 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.symbols.SymbolLocal;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
int local = environment.getLocal(variable);
environment.getOutput().load(variable.getType().toASMType(), local);
}
}
@@ -0,0 +1,44 @@
/*
* 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.symbols.SymbolLocal;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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();
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
int local = environment.getLocal(variable);
value.compile(true, environment);
if (result) {
environment.getOutput().dup();
}
environment.getOutput().store(variable.getType().toASMType(), local);
}
}
@@ -0,0 +1,45 @@
/*
* 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 java.util.Map;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeBool;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return ZenTypeBool.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
map.compile(result, environment);
key.compile(result, environment);
environment.getOutput().invokeInterface(internal(Map.class), "containsKey", "()Ljava/lang/Object;");
}
}
}
@@ -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.expression;
import java.util.Map;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAssociative;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
*/
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();
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
map.compile(result, environment);
index.compile(result, environment);
environment.getOutput().invokeInterface(
internal(Map.class),
"get",
"(Ljava/lang/Object;)Ljava/lang/Object;");
}
}
}
@@ -0,0 +1,53 @@
/*
* 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 java.util.Map;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeVoid;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
*/
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;
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
map.compile(result, environment);
index.compile(result, environment);
environment.getOutput().invokeInterface(
internal(Map.class),
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
}
}
}
@@ -0,0 +1,41 @@
/*
* 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 java.util.Map;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeInt;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
/**
*
* @author Stanneke
*/
public class ExpressionMapSize extends Expression {
private final Expression map;
public ExpressionMapSize(ZenPosition position, Expression map) {
super(position);
this.map = map;
}
@Override
public ZenType getType() {
return ZenTypeInt.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
map.compile(true, environment);
environment.getOutput().invokeInterface(internal(Map.class), "size", "()I");
}
}
}
@@ -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.expression;
import org.objectweb.asm.Label;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeNull;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionNull extends Expression {
public ExpressionNull(ZenPosition position) {
super(position);
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type.isPointer()) {
return this;
} else {
environment.error(position, "Cannot convert null to " + type);
return new ExpressionInvalid(position);
}
}
@Override
public ZenType getType() {
return ZenTypeNull.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
environment.getOutput().aConstNull();
}
}
@Override
public void compileIf(Label onElse, IEnvironmentMethod environment) {
environment.getOutput().goTo(onElse);
}
}
@@ -0,0 +1,27 @@
package stanhebben.zenscript.expression;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
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;
}
@Override
public ZenType getType() {
return a.getType(); // TODO: combine both types
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
// TODO: implement
}
}
@@ -0,0 +1,38 @@
/*
* 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.type.ZenTypeString;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class ExpressionString extends Expression {
private final String value;
public ExpressionString(ZenPosition position, String value) {
super(position);
this.value = value;
}
@Override
public ZenType getType() {
return ZenTypeString.INSTANCE;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
environment.getOutput().constant(value);
}
}
}
@@ -0,0 +1,62 @@
/*
* 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 java.util.List;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeString;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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);
}
@Override
public ZenType getType() {
return ZenTypeString.INSTANCE;
}
@Override
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 {
for (Expression value : values) {
value.compile(false, environment);
}
}
}
}
@@ -0,0 +1,84 @@
package stanhebben.zenscript.expression;
import java.util.HashMap;
import java.util.Map;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.type.ZenTypeAny;
import stanhebben.zenscript.type.ZenTypeAssociative;
import stanhebben.zenscript.util.MethodOutput;
import stanhebben.zenscript.util.ZenPosition;
import static stanhebben.zenscript.util.ZenTypeUtil.internal;
public class ExpressionTable extends Expression {
private final Expression[] keys;
private final Expression[] values;
private final ZenType 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, ZenType type) {
super(position);
this.keys = keys;
this.values = values;
this.type = type;
}
@Override
public ZenType getType() {
return type;
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
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].compile(true, environment);
values[i].compile(true, environment);
output.invokeInterface(internal(Map.class), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
output.pop();
}
} else {
for (Expression key : keys) {
key.compile(false, environment);
}
for (Expression value : values) {
value.compile(false, environment);
}
}
}
@Override
public Expression cast(ZenPosition position, IEnvironmentGlobal environment, ZenType type) {
if (type instanceof ZenTypeAssociative) {
ZenTypeAssociative keyType = (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());
}
return new ExpressionTable(getPosition(), newKeys, newValues, type);
} else {
return super.cast(position, environment, type);
}
}
}
@@ -0,0 +1,44 @@
/*
* 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.partial;
import java.lang.reflect.Field;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.compiler.ITypeRegistry;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stan
*/
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;
}
@Override
public ZenType getType() {
return types.getType(field.getGenericType());
}
@Override
public void compile(boolean result, IEnvironmentMethod environment) {
if (result) {
environment.getOutput().getStaticField(cls, field);
}
}
}
@@ -0,0 +1,32 @@
/*
* 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.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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 IZenSymbol toSymbol();
public ZenType toType(IEnvironmentGlobal environment);
}
@@ -0,0 +1,64 @@
/*
* 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.partial;
import java.lang.reflect.Type;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolJavaClass;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
environment.error(position, "cannot use class name as expression");
return new ExpressionInvalid(position);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "cannot assign to a class name");
return new ExpressionInvalid(position);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
return environment.getType(cls).getStaticMember(position, environment, name);
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
environment.error(position, "cannot call a class name");
return new ExpressionInvalid(position);
}
@Override
public IZenSymbol toSymbol() {
return new SymbolJavaClass(cls);
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
return environment.getType(cls);
}
}
@@ -0,0 +1,68 @@
/*
* 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.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.expression.ExpressionLocalGet;
import stanhebben.zenscript.expression.ExpressionLocalSet;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolLocal;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
return new ExpressionLocalGet(position, variable);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
return variable.getType().getMember(position, environment, this, name);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
if (variable.isFinal()) {
environment.error(position, "value cannot be changed");
return new ExpressionInvalid(position);
} else {
return new ExpressionLocalSet(position, variable, other);
}
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
return variable.getType().call(position, environment, eval(environment), values);
}
@Override
public IZenSymbol toSymbol() {
return variable;
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
return ZenType.ANY;
}
}
@@ -0,0 +1,70 @@
/*
* 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.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
import stanhebben.zenscript.expression.ExpressionInvalid;
import stanhebben.zenscript.symbols.IZenSymbol;
import stanhebben.zenscript.symbols.SymbolPackage;
import stanhebben.zenscript.type.ZenType;
import stanhebben.zenscript.util.ZenPosition;
/**
*
* @author Stanneke
*/
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;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
environment.error(position, "Cannot use package name as expression");
return new ExpressionInvalid(position);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "Cannot assign to a package");
return new ExpressionInvalid(position);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
IZenSymbol member = contents.get(name);
if (member == null) {
environment.error(position, "No such member: " + name);
return new ExpressionInvalid(position);
} else {
return member.instance(position);
}
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
environment.error(position, "cannot call a package");
return new ExpressionInvalid(position);
}
@Override
public IZenSymbol toSymbol() {
return null; // not supposed to be used as symbol
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
return ZenType.ANY;
}
}
@@ -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.expression.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
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.util.ZenPosition;
/**
*
* @author Stan
*/
public class PartialStaticGenerated implements IPartialExpression {
private final ZenPosition position;
private final String owner;
private final String method;
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;
this.method = method;
this.signature = signature;
this.argumentTypes = argumentTypes;
this.returnType = returnType;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
environment.error(position, "cannot use a function as value");
return new ExpressionInvalid(position);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "cannot assign to a function");
return new ExpressionInvalid(position);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
environment.error(position, "functions don't have members");
return new ExpressionInvalid(position);
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
if (values.length != argumentTypes.length) {
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 ExpressionJavaCallStaticGenerated(position, owner, method, signature, returnType, arguments);
}
@Override
public IZenSymbol toSymbol() {
return new SymbolZenStaticMethod(owner, method, signature, argumentTypes, returnType);
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
return ZenType.ANY;
}
}
@@ -0,0 +1,64 @@
/*
* 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.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
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.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class PartialStaticGetter implements IPartialExpression {
private final ZenPosition position;
private final JavaMethod method;
public PartialStaticGetter(ZenPosition position, JavaMethod method) {
this.position = position;
this.method = method;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
return method.callStatic(position, environment);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "cannot alter this final");
return new ExpressionInvalid(position);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
return method.getReturnType().getMember(position, environment, this, name);
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
environment.error(position, "value cannot be called");
return new ExpressionInvalid(position);
}
@Override
public IZenSymbol toSymbol() {
return new SymbolJavaStaticGetter(method);
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
environment.error(position, "not a valid type");
return ZenType.ANY;
}
}
@@ -0,0 +1,69 @@
/*
* 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.partial;
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
import stanhebben.zenscript.compiler.IEnvironmentMethod;
import stanhebben.zenscript.expression.Expression;
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.util.ZenPosition;
/**
*
* @author Stanneke
*/
public class PartialStaticMethod implements IPartialExpression {
private final ZenPosition position;
private final JavaMethod method;
public PartialStaticMethod(ZenPosition position, JavaMethod method) {
this.position = position;
this.method = method;
}
@Override
public Expression eval(IEnvironmentGlobal environment) {
environment.error(position, "not a valid expression");
return new ExpressionInvalid(position);
}
@Override
public Expression assign(ZenPosition position, IEnvironmentGlobal environment, Expression other) {
environment.error(position, "cannot alter this final");
return new ExpressionInvalid(position);
}
@Override
public IPartialExpression getMember(ZenPosition position, IEnvironmentGlobal environment, String name) {
environment.error(position, "methods have no members");
return new ExpressionInvalid(position);
}
@Override
public Expression call(ZenPosition position, IEnvironmentMethod environment, Expression... values) {
if (method.accepts(environment, values)) {
return method.callStatic(position, environment, values);
} else {
environment.error(position, "parameter count mismatch");
return new ExpressionInvalid(position, method.getReturnType());
}
}
@Override
public IZenSymbol toSymbol() {
return new SymbolJavaStaticMethod(method);
}
@Override
public ZenType toType(IEnvironmentGlobal environment) {
return ZenType.ANY;
}
}