use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.
the class ParsedExpressionOrOr method compile.
@Override
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(), cLeft.cast(getPosition(), environment, type), cRight.cast(getPosition(), environment, type));
}
use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.
the class Statement method read.
public static Statement read(ZenTokener parser, IEnvironmentGlobal environment, ZenType returnType) {
Token next = parser.peek();
switch(next.getType()) {
case T_AOPEN:
{
Token t = parser.next();
ArrayList<Statement> statements = new ArrayList<>();
while (parser.optional(T_ACLOSE) == null) {
statements.add(read(parser, environment, returnType));
}
return new StatementBlock(t.getPosition(), statements);
}
case T_RETURN:
{
parser.next();
ParsedExpression expression = null;
if (parser.peek() != null && !parser.isNext(T_SEMICOLON)) {
expression = ParsedExpression.read(parser, environment);
}
parser.required(T_SEMICOLON, "; expected");
return new StatementReturn(next.getPosition(), returnType, expression);
}
case T_VAR:
case T_VAL:
{
Token start = parser.next();
String name = parser.required(T_ID, "identifier expected").getValue();
ZenType type = null;
ParsedExpression initializer = null;
if (parser.optional(T_AS) != null) {
type = ZenType.read(parser, environment);
}
if (parser.optional(T_ASSIGN) != null) {
initializer = ParsedExpression.read(parser, environment);
}
parser.required(T_SEMICOLON, "; expected");
return new StatementVar(start.getPosition(), name, type, initializer, start.getType() == T_VAL);
}
case T_IF:
{
Token t = parser.next();
ParsedExpression expression = ParsedExpression.read(parser, environment);
Statement onIf = read(parser, environment, returnType);
Statement onElse = null;
if (parser.optional(T_ELSE) != null) {
onElse = read(parser, environment, returnType);
}
return new StatementIf(t.getPosition(), expression, onIf, onElse);
}
case T_FOR:
{
Token t = parser.next();
String name = parser.required(T_ID, "identifier expected").getValue();
List<String> names = new ArrayList<>();
names.add(name);
while (parser.optional(T_COMMA) != null) {
names.add(parser.required(T_ID, "identifier expected").getValue());
}
parser.required(T_IN, "in expected");
ParsedExpression source = ParsedExpression.read(parser, environment);
Statement content = read(parser, environment, null);
return new StatementForeach(t.getPosition(), names.toArray(new String[names.size()]), source, content);
}
case T_WHILE:
{
parser.next();
ParsedExpression condition = ParsedExpression.read(parser, environment);
Statement content = read(parser, environment, null);
return new StatementWhileDo(next.getPosition(), content, condition);
}
case T_VERSION:
{
Token t = parser.next();
parser.required(T_INTVALUE, "integer expected");
parser.required(T_SEMICOLON, "; expected");
return new StatementNull(t.getPosition());
}
case T_BREAK:
{
parser.next();
parser.required(T_SEMICOLON, "; expected");
return new StatementBreak(next.getPosition());
}
}
ZenPosition position = parser.peek().getPosition();
StatementExpression result = new StatementExpression(position, ParsedExpression.read(parser, environment));
parser.required(T_SEMICOLON, "; expected");
return result;
}
use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.
the class ZenExpandCaster method constructCastingRules.
public void constructCastingRules(IEnvironmentGlobal environment, ICastingRuleDelegate rules) {
ZenType type = method.getReturnType();
rules.registerCastingRule(type, new CastingRuleStaticMethod(method));
type.constructCastingRules(environment, new CastingRuleDelegateStaticMethod(rules, method), false);
}
use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.
the class ParsedGlobalValue method parse.
public static ParsedGlobalValue parse(ZenTokener parser, IEnvironmentGlobal environment, String owner, boolean global) {
// Start ("GLOBAL")
Token startingPoint = parser.next();
// Name ("globalName", "test")
String name = parser.required(T_ID, "Global value requires a name!").getValue();
// Type ("as type", "as IItemStack")
ZenType type = ZenType.ANY;
Token nee = parser.optional(T_AS);
if (nee != null) {
type = ZenType.read(parser, environment);
}
// "="
parser.required(T_ASSIGN, "Global values have to be initialized!");
// "value, <minecraft:dirt>"
ParsedExpression value = ParsedExpression.read(parser, environment);
// ";"
parser.required(T_SEMICOLON, "; expected");
// throw it together
return new ParsedGlobalValue(startingPoint.getPosition(), name, type, value, owner, global);
}
use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.
the class ExpressionJavaLambdaSimpleGeneric method compile.
@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 ZenClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, clsName, createMethodSignature(), "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, null, null);
IEnvironmentClass environmentClass = new EnvironmentClass(cw, environment);
IEnvironmentMethod environmentMethod = new EnvironmentMethod(output, environmentClass);
for (int i = 0; i < arguments.size(); i++) {
ZenType typeToPut = arguments.get(i).getType();
if (typeToPut.equals(ZenType.ANY))
typeToPut = environment.getType(method.getGenericParameterTypes()[i]);
if (typeToPut == null)
typeToPut = environment.getType(method.getParameterTypes()[i]);
environmentMethod.putValue(arguments.get(i).getName(), new SymbolArgument(i + 1, typeToPut), getPosition());
}
output.start();
for (Statement statement : statements) {
statement.compile(environmentMethod);
}
output.ret();
output.end();
if (!Objects.equals(genericClass, Object.class)) {
MethodOutput bridge = new MethodOutput(cw, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE, method.getName(), ZenTypeUtil.descriptor(method), null, null);
bridge.loadObject(0);
bridge.loadObject(1);
bridge.checkCast(internal(genericClass));
if (arguments.size() > 1) {
for (int i = 1; i < arguments.size(); ) {
bridge.load(org.objectweb.asm.Type.getType(method.getParameterTypes()[i]), ++i);
}
}
bridge.invokeVirtual(clsName, method.getName(), descriptor);
bridge.returnType(org.objectweb.asm.Type.getReturnType(method));
bridge.end();
}
environment.putClass(clsName, cw.toByteArray());
// make class instance
environment.getOutput().newObject(clsName);
environment.getOutput().dup();
environment.getOutput().construct(clsName);
}
Aggregations