Search in sources :

Example 6 with ZenType

use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.

the class ZenNativeCaster method constructCastingRule.

public void constructCastingRule(ICastingRuleDelegate rules) {
    ZenType type = getReturnType();
    rules.registerCastingRule(type, new CastingRuleVirtualMethod(method));
}
Also used : CastingRuleVirtualMethod(stanhebben.zenscript.type.casting.CastingRuleVirtualMethod) ZenType(stanhebben.zenscript.type.ZenType)

Example 7 with ZenType

use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.

the class StringUtil method methodMatchingError.

/**
 * If a set of methods is available and none matches, this method creates a
 * suitable message.
 *
 * @param methods   matching methods
 * @param arguments calling arguments
 *
 * @return return value
 */
public static String methodMatchingError(List<IJavaMethod> methods, Expression... arguments) {
    if (methods.isEmpty()) {
        return "\u00a7cno method with that name available";
    } else {
        StringBuilder message = new StringBuilder();
        if (methods.size() == 1) {
            message.append("a method ");
        } else {
            message.append(methods.size()).append(" methods ");
        }
        message.append("available but \u00a74none\u00a7r matches the parameters (");
        boolean first = true;
        for (Expression value : arguments) {
            if (first) {
                first = false;
            } else {
                message.append(", ");
            }
            message.append(value.getType().toString());
        }
        message.append(")");
        message.append("\nThis is \u00a7ousually\u00a7r an error in your script, not in the mod");
        methods.forEach(meth -> {
            if (meth instanceof JavaMethod) {
                JavaMethod m = (JavaMethod) meth;
                message.append("\n").append(m.getMethod().getName()).append("(");
                for (int i = 0; i < m.getParameterTypes().length; i++) {
                    ZenType type = m.getParameterTypes()[i];
                    for (int i1 = 0; i1 < m.getMethod().getParameterAnnotations()[i].length; i1++) {
                        Annotation an = m.getMethod().getParameterAnnotations()[i][i1];
                        message.append("\u00a7a").append(an.annotationType().getSimpleName()).append(" ");
                    }
                    message.append("\u00a7r").append(type.toString()).append(", ");
                }
                // Removes last ', ' and closes the bracket
                message.deleteCharAt(message.length() - 1);
                message.deleteCharAt(message.length() - 1);
                message.append(")");
            }
        });
        return message.toString();
    }
}
Also used : Expression(stanhebben.zenscript.expression.Expression) ZenType(stanhebben.zenscript.type.ZenType) Annotation(java.lang.annotation.Annotation)

Example 8 with ZenType

use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.

the class ParsedExpressionCall method compile.

@Override
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
    IPartialExpression cReceiver = receiver.compile(environment, predictedType);
    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, predictedTypes[i]);
        cArguments[i] = cArgument.eval(environment);
    }
    return cReceiver.call(getPosition(), environment, cArguments);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression) ZenType(stanhebben.zenscript.type.ZenType) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Example 9 with ZenType

use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.

the class StatementIf method compile.

@Override
public void compile(IEnvironmentMethod environment) {
    environment.getOutput().position(getPosition());
    Expression cCondition = condition.compile(environment, ZenType.BOOL).eval(environment).cast(getPosition(), environment, ZenType.BOOL);
    ZenType expressionType = cCondition.getType();
    if (expressionType.canCastImplicit(ZenType.BOOL, environment)) {
        Label labelEnd = new Label();
        Label labelElse = onElse == null ? labelEnd : new Label();
        cCondition.compileIf(labelElse, environment);
        onThen.compile(environment);
        MethodOutput methodOutput = environment.getOutput();
        if (onElse != null) {
            methodOutput.goTo(labelEnd);
            methodOutput.label(labelElse);
            onElse.compile(environment);
        }
        methodOutput.label(labelEnd);
    } else {
        environment.error(getPosition(), "condition is not a boolean");
    }
}
Also used : Expression(stanhebben.zenscript.expression.Expression) ParsedExpression(stanhebben.zenscript.parser.expression.ParsedExpression) Label(org.objectweb.asm.Label) ZenType(stanhebben.zenscript.type.ZenType)

Example 10 with ZenType

use of stanhebben.zenscript.type.ZenType in project ZenScript by CraftTweaker.

the class TypeExpansion method compileAnyCast.

public void compileAnyCast(ZenType type, MethodOutput output, IEnvironmentGlobal environment, int localValue, int localClass) {
    if (type == null)
        throw new IllegalArgumentException("type cannot be null");
    Type asmType = type.toASMType();
    if (asmType == null) {
        throw new RuntimeException("type has no asm type");
    }
    for (ZenExpandCaster caster : casters) {
        Label skip = new Label();
        output.loadObject(localClass);
        output.constant(caster.getTarget().toASMType());
        output.ifACmpNe(skip);
        output.load(asmType, localValue);
        caster.compile(output);
        output.returnType(caster.getTarget().toASMType());
        output.label(skip);
    }
    for (ZenExpandCaster caster : casters) {
        String casterAny = caster.getTarget().getAnyClassName(environment);
        if (casterAny == null)
            // TODO: make sure this isn't necessary
            continue;
        Label skip = new Label();
        output.loadObject(localClass);
        output.invokeStatic(casterAny, "rtCanCastImplicit", "(Ljava/lang/Class;)Z");
        output.ifEQ(skip);
        output.load(type.toASMType(), localValue);
        caster.compile(output);
        output.loadObject(localClass);
        output.invokeStatic(casterAny, "rtAs", "(" + caster.getTarget().getSignature() + "Ljava/lang/Class;)Ljava/lang/Object;");
        output.returnType(caster.getTarget().toASMType());
        output.label(skip);
    }
}
Also used : Type(org.objectweb.asm.Type) ZenType(stanhebben.zenscript.type.ZenType)

Aggregations

ZenType (stanhebben.zenscript.type.ZenType)11 Expression (stanhebben.zenscript.expression.Expression)3 ParsedExpression (stanhebben.zenscript.parser.expression.ParsedExpression)3 IPartialExpression (stanhebben.zenscript.expression.partial.IPartialExpression)2 Token (stanhebben.zenscript.parser.Token)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ClassWriter (org.objectweb.asm.ClassWriter)1 Label (org.objectweb.asm.Label)1 Type (org.objectweb.asm.Type)1 Statement (stanhebben.zenscript.statements.Statement)1 SymbolArgument (stanhebben.zenscript.symbols.SymbolArgument)1 CastingRuleDelegateStaticMethod (stanhebben.zenscript.type.casting.CastingRuleDelegateStaticMethod)1 CastingRuleStaticMethod (stanhebben.zenscript.type.casting.CastingRuleStaticMethod)1 CastingRuleVirtualMethod (stanhebben.zenscript.type.casting.CastingRuleVirtualMethod)1 MethodOutput (stanhebben.zenscript.util.MethodOutput)1 ZenPosition (stanhebben.zenscript.util.ZenPosition)1