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));
}
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();
}
}
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);
}
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");
}
}
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);
}
}
Aggregations