Search in sources :

Example 6 with Expression

use of stanhebben.zenscript.expression.Expression 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 7 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class ParsedExpressionIndex method compile.

@Override
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);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Example 8 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class ParsedExpressionBinary method compile.

@Override
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);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Example 9 with Expression

use of stanhebben.zenscript.expression.Expression 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 10 with Expression

use of stanhebben.zenscript.expression.Expression in project ZenScript by CraftTweaker.

the class ParsedExpressionOpAssign method compile.

@Override
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, predictedType).assign(getPosition(), environment, value);
}
Also used : Expression(stanhebben.zenscript.expression.Expression) IPartialExpression(stanhebben.zenscript.expression.partial.IPartialExpression)

Aggregations

Expression (stanhebben.zenscript.expression.Expression)11 IPartialExpression (stanhebben.zenscript.expression.partial.IPartialExpression)6 ParsedExpression (stanhebben.zenscript.parser.expression.ParsedExpression)4 ZenType (stanhebben.zenscript.type.ZenType)4 Label (org.objectweb.asm.Label)2 SymbolLocal (stanhebben.zenscript.symbols.SymbolLocal)2 Annotation (java.lang.annotation.Annotation)1 Type (org.objectweb.asm.Type)1