mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-17 10:55:04 +00:00
- Rewrote the whole damn typecasting system. Now uses a more flexible and more structure casting rule system.
- Improved function literals. Function recipes are a thing! - Added mirrored shaped recipes - Fixed a few "any" type - related errors - Added environment to the bracket handlers. Breaks existing bracket handlers!! - Turned JavaMethod into an interface. Can now be used both for existing methods and generated ones. Makes a few things simpler :) - Added creative and adventure getters to player info
This commit is contained in:
1 parent
c67ad22270
commit
154c2242f9
172 files changed
+5400
-2134
No files matched your search
@@ -65,13 +65,10 @@ public class NFA {
|
||||
|
||||
Partial[] partials = new Partial[regexp.length];
|
||||
for (int i = 0; i < regexp.length; i++) {
|
||||
//System.out.println("Processing regexp " + i + ": " + regexp[i]);
|
||||
partials[i] = processRegExp(new CharStream(regexp[i]));
|
||||
partials[i].head.setFinal(finals[i]);
|
||||
initial.addTransition(partials[i].tailLabel, partials[i].tail);
|
||||
}
|
||||
|
||||
//System.out.println("NFA generated");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -230,7 +230,6 @@ public class TokenStream implements Iterator<Token> {
|
||||
if (nextChar < 0 && value.length() == 0) {
|
||||
return; // happens on comments at the end of files
|
||||
}
|
||||
//System.out.println("Value: " + value.length() + " characters: " + value);
|
||||
throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
|
||||
+11
-17
@@ -17,7 +17,6 @@ import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionFloat;
|
||||
import stanhebben.zenscript.expression.ExpressionFunction;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.parser.ParseException;
|
||||
@@ -331,7 +330,6 @@ public abstract class ParsedExpression {
|
||||
}*/
|
||||
case T_ID:
|
||||
return new ParsedExpressionVariable(position, parser.next().getValue());
|
||||
// TODO: implement function values
|
||||
case T_FUNCTION:
|
||||
// function (argname, argname, ...) { ...contents... }
|
||||
parser.next();
|
||||
@@ -360,18 +358,14 @@ public abstract class ParsedExpression {
|
||||
|
||||
parser.required(T_AOPEN, "{ expected");
|
||||
|
||||
Statement[] statements;
|
||||
if (parser.optional(T_ACLOSE) != null) {
|
||||
statements = new Statement[0];
|
||||
} else {
|
||||
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
|
||||
|
||||
List<Statement> statements = new ArrayList<Statement>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statementsAL.add(Statement.read(parser, environment));
|
||||
statements.add(Statement.read(parser, environment, returnType));
|
||||
}
|
||||
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
|
||||
}
|
||||
return new ParsedExpressionValue(position, new ExpressionFunction(position, arguments, returnType, statements));
|
||||
|
||||
return new ParsedExpressionFunction(position, returnType, arguments, statements);
|
||||
case T_LT: {
|
||||
Token start = parser.next();
|
||||
List<Token> tokens = new ArrayList<Token>();
|
||||
@@ -380,7 +374,7 @@ public abstract class ParsedExpression {
|
||||
tokens.add(next);
|
||||
next = parser.next();
|
||||
}
|
||||
IZenSymbol resolved = parser.getEnvironment().getBracketed(tokens);
|
||||
IZenSymbol resolved = parser.getEnvironment().getBracketed(environment, tokens);
|
||||
if (resolved == null) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append('<');
|
||||
@@ -397,7 +391,7 @@ public abstract class ParsedExpression {
|
||||
} else {
|
||||
return new ParsedExpressionValue(
|
||||
start.getPosition(),
|
||||
parser.getEnvironment().getBracketed(tokens).instance(start.getPosition()));
|
||||
parser.getEnvironment().getBracketed(environment, tokens).instance(start.getPosition()));
|
||||
}
|
||||
}
|
||||
case T_SQBROPEN: {
|
||||
@@ -431,7 +425,7 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
}
|
||||
|
||||
return new ParsedExpressionTable(position, keys, values);
|
||||
return new ParsedExpressionMap(position, keys, values);
|
||||
}
|
||||
}
|
||||
case T_TRUE:
|
||||
@@ -463,9 +457,9 @@ public abstract class ParsedExpression {
|
||||
return position;
|
||||
}
|
||||
|
||||
public abstract IPartialExpression compile(IEnvironmentMethod environment);
|
||||
public abstract IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType);
|
||||
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
return compile(environment).eval(environment);
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return compile(environment, predictedType).eval(environment);
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -7,8 +7,9 @@
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionOrOr;
|
||||
import stanhebben.zenscript.expression.ExpressionAndAnd;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -27,10 +28,10 @@ public class ParsedExpressionAndAnd extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionOrOr(
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionAndAnd(
|
||||
getPosition(),
|
||||
left.compile(environment).eval(environment),
|
||||
right.compile(environment).eval(environment));
|
||||
left.compile(environment, predictedType).eval(environment),
|
||||
right.compile(environment, predictedType).eval(environment));
|
||||
}
|
||||
}
|
||||
+43
-6
@@ -10,7 +10,12 @@ import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionArray;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeArray;
|
||||
import stanhebben.zenscript.type.ZenTypeArrayBasic;
|
||||
import stanhebben.zenscript.type.casting.ICastingRule;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -27,20 +32,52 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
ZenType predictedBaseType = null;
|
||||
ZenTypeArrayBasic arrayType = ZenType.ANYARRAY;
|
||||
ICastingRule castingRule = null;
|
||||
|
||||
if (predictedType instanceof ZenTypeArray) {
|
||||
predictedBaseType = ((ZenTypeArray) predictedType).getBaseType();
|
||||
if (predictedType instanceof ZenTypeArrayBasic) {
|
||||
// TODO: allow any kind of array type
|
||||
arrayType = (ZenTypeArrayBasic) predictedType;
|
||||
}
|
||||
} else {
|
||||
// find any[] caster that casts to the given type
|
||||
castingRule = ZenType.ANYARRAY.getCastingRule(predictedType, environment);
|
||||
if (castingRule != null) {
|
||||
if (castingRule.getInputType() instanceof ZenTypeArray) {
|
||||
predictedBaseType = ((ZenTypeArray) castingRule.getInputType()).getBaseType();
|
||||
if (castingRule.getInputType() instanceof ZenTypeArrayBasic) {
|
||||
// TODO: allow any kind of array type
|
||||
arrayType = (ZenTypeArrayBasic) castingRule.getInputType();
|
||||
}
|
||||
} else {
|
||||
environment.error(getPosition(), "Invalid caster - any[] caster but input type is not an array");
|
||||
castingRule = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Expression[] cContents = new Expression[contents.size()];
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
cContents[i] = contents.get(i).compile(environment).eval(environment);
|
||||
cContents[i] = contents.get(i).compile(environment, predictedBaseType).eval(environment);
|
||||
}
|
||||
Expression result = new ExpressionArray(getPosition(), arrayType, cContents);
|
||||
if (castingRule != null) {
|
||||
return new ExpressionAs(getPosition(), result, castingRule);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
return new ExpressionArray(getPosition(), cContents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
|
||||
return contents.get(0).compile(environment).eval(environment);
|
||||
return contents.get(0).compile(environment, predictedType).eval(environment);
|
||||
} else {
|
||||
return compile(environment).eval(environment);
|
||||
return compile(environment, predictedType).eval(environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -26,10 +27,10 @@ public class ParsedExpressionAssign extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
IPartialExpression cLeft = left.compile(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cLeft = left.compile(environment, predictedType);
|
||||
return cLeft
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment).eval(environment));
|
||||
right.compile(environment, cLeft.getType()).eval(environment));
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -30,9 +31,10 @@ public class ParsedExpressionBinary extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: make better predictions
|
||||
Expression cLeft = left.compile(environment, predictedType).eval(environment);
|
||||
Expression cRight = right.compile(environment, predictedType).eval(environment);
|
||||
return cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionBool;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ public class ParsedExpressionBool extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionBool(getPosition(), value);
|
||||
}
|
||||
}
|
||||
+7
-4
@@ -10,6 +10,7 @@ import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -26,13 +27,15 @@ public class ParsedExpressionCall extends ParsedExpression {
|
||||
this.receiver = receiver;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
IPartialExpression cReceiver = receiver.compile(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cReceiver = receiver.compile(environment, null);
|
||||
ZenType[] predictedTypes = cReceiver.predictCallTypes(arguments.size());
|
||||
|
||||
Expression[] cArguments = new Expression[arguments.size()];
|
||||
for (int i = 0; i < cArguments.length; i++) {
|
||||
IPartialExpression cArgument = arguments.get(i).compile(environment);
|
||||
IPartialExpression cArgument = arguments.get(i).compile(environment, predictedTypes[i]);
|
||||
cArguments[i] = cArgument.eval(environment);
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -27,9 +27,9 @@ public class ParsedExpressionCast extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return value
|
||||
.compile(environment)
|
||||
.compile(environment, type)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
}
|
||||
|
||||
+4
-3
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -34,9 +35,9 @@ public class ParsedExpressionCompare extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
Expression cLeft = left.compile(environment, null).eval(environment);
|
||||
Expression cRight = right.compile(environment, null).eval(environment);
|
||||
return cLeft.getType().compare(getPosition(), environment, cLeft, cRight, type);
|
||||
}
|
||||
}
|
||||
+5
-4
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionConditional;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -29,11 +30,11 @@ public class ParsedExpressionConditional extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionConditional(
|
||||
getPosition(),
|
||||
condition.compile(environment).eval(environment),
|
||||
ifThen.compile(environment).eval(environment),
|
||||
ifElse.compile(environment).eval(environment));
|
||||
condition.compile(environment, ZenType.BOOL).eval(environment),
|
||||
ifThen.compile(environment, predictedType).eval(environment),
|
||||
ifElse.compile(environment, predictedType).eval(environment));
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.ExpressionFunction;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionJavaLambda;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeNative;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public class ParsedExpressionFunction extends ParsedExpression {
|
||||
private final ZenType returnType;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final List<Statement> statements;
|
||||
|
||||
public ParsedExpressionFunction(ZenPosition position, ZenType returnType, List<ParsedFunctionArgument> arguments, List<Statement> statements) {
|
||||
super(position);
|
||||
|
||||
this.returnType = returnType;
|
||||
this.arguments = arguments;
|
||||
this.statements = statements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (predictedType != null && predictedType instanceof ZenTypeNative) {
|
||||
System.out.println("Known predicted function type: " + predictedType);
|
||||
|
||||
ZenTypeNative nativeType = (ZenTypeNative) predictedType;
|
||||
Class nativeClass = nativeType.getNativeClass();
|
||||
if (nativeClass.isInterface() && nativeClass.getMethods().length == 1) {
|
||||
// functional interface
|
||||
Method method = nativeClass.getMethods()[0];
|
||||
if (returnType != ZenTypeAny.INSTANCE
|
||||
&& !returnType.canCastImplicit(environment.getType(method.getGenericReturnType()), environment)) {
|
||||
environment.error(getPosition(), "return type not compatible");
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
if (arguments.size() != method.getParameterTypes().length) {
|
||||
environment.error(getPosition(), "number of arguments incorrect");
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
ZenType argumentType = environment.getType(method.getGenericParameterTypes()[i]);
|
||||
if (arguments.get(i).getType() != ZenTypeAny.INSTANCE
|
||||
&& !argumentType.canCastImplicit(arguments.get(i).getType(), environment)) {
|
||||
environment.error(getPosition(), "argument " + i + " doesn't match");
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
return new ExpressionJavaLambda(
|
||||
getPosition(),
|
||||
nativeClass,
|
||||
arguments,
|
||||
statements,
|
||||
environment.getType(nativeClass));
|
||||
} else {
|
||||
environment.error(getPosition(), predictedType.toString() + " is not a functional interface");
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No known predicted type");
|
||||
return new ExpressionFunction(getPosition(), arguments, returnType, statements);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -28,9 +29,10 @@ public class ParsedExpressionIndex extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
Expression cIndex = index.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: improve type prediction for this
|
||||
Expression cValue = value.compile(environment, null).eval(environment);
|
||||
Expression cIndex = index.compile(environment, null).eval(environment);
|
||||
return cValue.getType().binary(getPosition(), environment, cValue, cIndex, OperatorType.INDEXGET);
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -30,10 +31,11 @@ public class ParsedExpressionIndexSet extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
Expression cIndex = index.compile(environment).eval(environment);
|
||||
Expression cSetValue = setValue.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: improve prediction in this expression
|
||||
Expression cValue = value.compile(environment, null).eval(environment);
|
||||
Expression cIndex = index.compile(environment, null).eval(environment);
|
||||
Expression cSetValue = setValue.compile(environment, null).eval(environment);
|
||||
return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue,OperatorType.INDEXSET);
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -21,7 +22,7 @@ public class ParsedExpressionInvalid extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionInvalid(getPosition());
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionInvalid(getPosition(), predictedType == null ? ZenType.ANY : predictedType);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionAs;
|
||||
import stanhebben.zenscript.expression.ExpressionMap;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAssociative;
|
||||
import stanhebben.zenscript.type.casting.ICastingRule;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionMap extends ParsedExpression {
|
||||
private final List<ParsedExpression> keys;
|
||||
private final List<ParsedExpression> values;
|
||||
|
||||
public ParsedExpressionMap(
|
||||
ZenPosition position,
|
||||
List<ParsedExpression> keys,
|
||||
List<ParsedExpression> values) {
|
||||
super(position);
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
ZenType predictedKeyType = null;
|
||||
ZenType predictedValueType = null;
|
||||
ICastingRule castingRule = null;
|
||||
ZenTypeAssociative mapType = ZenType.ANYMAP;
|
||||
|
||||
if (predictedType instanceof ZenTypeAssociative) {
|
||||
ZenTypeAssociative inputType = (ZenTypeAssociative) predictedType;
|
||||
predictedKeyType = inputType.getKeyType();
|
||||
predictedValueType = inputType.getValueType();
|
||||
mapType = inputType;
|
||||
} else {
|
||||
castingRule = ZenType.ANYMAP.getCastingRule(predictedType, environment);
|
||||
if (castingRule != null) {
|
||||
if (castingRule.getInputType() instanceof ZenTypeAssociative) {
|
||||
ZenTypeAssociative inputType = (ZenTypeAssociative) castingRule.getInputType();
|
||||
predictedKeyType = inputType.getKeyType();
|
||||
predictedValueType = inputType.getValueType();
|
||||
mapType = inputType;
|
||||
} else {
|
||||
environment.error(getPosition(), "Caster found for any[any] but its input is not an associative array");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Expression[] cKeys = new Expression[keys.size()];
|
||||
Expression[] cValues = new Expression[values.size()];
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
cKeys[i] = keys.get(i).compileKey(environment, predictedKeyType);
|
||||
cValues[i] = values.get(i).compile(environment, predictedValueType).eval(environment);
|
||||
}
|
||||
|
||||
Expression result = new ExpressionMap(getPosition(), cKeys, cValues, mapType);
|
||||
if (castingRule != null) {
|
||||
return new ExpressionAs(getPosition(), result, castingRule);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,7 @@ public class ParsedExpressionMember extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return value.compile(environment).getMember(getPosition(), environment, member);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return value.compile(environment, null).getMember(getPosition(), environment, member);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -9,6 +9,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -21,7 +22,7 @@ public class ParsedExpressionNull extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionNull(getPosition());
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -30,12 +31,13 @@ public class ParsedExpressionOpAssign extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: validate if the prediction rules are sound
|
||||
Expression cLeft = left.compile(environment, predictedType).eval(environment);
|
||||
Expression cRight = right.compile(environment, cLeft.getType()).eval(environment);
|
||||
|
||||
Expression value = cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
|
||||
return left.compile(environment).assign(getPosition(), environment, value);
|
||||
return left.compile(environment, predictedType).assign(getPosition(), environment, value);
|
||||
}
|
||||
}
|
||||
+18
-3
@@ -7,8 +7,10 @@
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionOrOr;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -27,10 +29,23 @@ public class ParsedExpressionOrOr extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
Expression cLeft = left.compile(environment, predictedType).eval(environment);
|
||||
Expression cRight = right.compile(environment, predictedType).eval(environment);
|
||||
|
||||
ZenType type;
|
||||
if (cRight.getType().canCastImplicit(cLeft.getType(), environment)) {
|
||||
type = cLeft.getType();
|
||||
} else if (cLeft.getType().canCastImplicit(cRight.getType(), environment)) {
|
||||
type = cRight.getType();
|
||||
} else {
|
||||
environment.error(getPosition(), "These types could not be unified: " + cLeft.getType() + " and " + cRight.getType());
|
||||
type = ZenType.ANY;
|
||||
}
|
||||
|
||||
return new ExpressionOrOr(
|
||||
getPosition(),
|
||||
left.compile(environment).eval(environment),
|
||||
right.compile(environment).eval(environment));
|
||||
cLeft.cast(getPosition(), environment, type),
|
||||
cRight.cast(getPosition(), environment, type));
|
||||
}
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionTable;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionTable extends ParsedExpression {
|
||||
private final List<ParsedExpression> keys;
|
||||
private final List<ParsedExpression> values;
|
||||
|
||||
public ParsedExpressionTable(
|
||||
ZenPosition position,
|
||||
List<ParsedExpression> keys,
|
||||
List<ParsedExpression> values) {
|
||||
super(position);
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression[] cKeys = new Expression[keys.size()];
|
||||
Expression[] cValues = new Expression[values.size()];
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
cKeys[i] = keys.get(i).compileKey(environment);
|
||||
cValues[i] = values.get(i).compile(environment).eval(environment);
|
||||
}
|
||||
|
||||
return new ExpressionTable(getPosition(), cKeys, cValues);
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -10,6 +10,7 @@ import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -28,8 +29,9 @@ public class ParsedExpressionUnary extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: improve type predictions?
|
||||
Expression cValue = value.compile(environment, predictedType).eval(environment);
|
||||
return cValue.getType().unary(getPosition(), environment, cValue, operator);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -8,6 +8,7 @@ package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,7 @@ public class ParsedExpressionValue extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -11,6 +11,7 @@ import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
@@ -27,18 +28,24 @@ public class ParsedExpressionVariable extends ParsedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression result = environment.getValue(name, getPosition());
|
||||
if (result == null) {
|
||||
environment.error(getPosition(), "could not find " + name);
|
||||
return new ExpressionInvalid(getPosition());
|
||||
// enable usage of static members of the same type as the predicted type (eg. enum values)
|
||||
IPartialExpression member = predictedType.getStaticMember(getPosition(), environment, name);
|
||||
if (member == null || member.getType().getCastingRule(predictedType, environment) == null) {
|
||||
environment.error(getPosition(), "could not find " + name);
|
||||
return new ExpressionInvalid(getPosition());
|
||||
} else {
|
||||
return member;
|
||||
}
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionString(getPosition(), name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user