diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/AssignmentInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/AssignmentInfo.java index 1a1700939..c1eaeef94 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/AssignmentInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/AssignmentInfo.java @@ -1,5 +1,6 @@ package noppes.npcs.client.gui.util.script.interpreter.field; +import noppes.npcs.client.gui.util.script.interpreter.type.TypeChecker; import noppes.npcs.client.gui.util.script.interpreter.type.TypeInfo; import java.lang.reflect.Field; @@ -134,7 +135,7 @@ public class AssignmentInfo { // Check type compatibility if (targetType != null && sourceType != null) { - if (!isTypeCompatible(sourceType, targetType)) { + if (!TypeChecker.isTypeCompatible(targetType, sourceType)) { this.requiredType = targetType.getSimpleName(); this.providedType = sourceType.getSimpleName(); setError(ErrorType.TYPE_MISMATCH, buildTypeMismatchMessage()); @@ -149,110 +150,6 @@ public class AssignmentInfo { return "Provided type: " + providedType + "\nRequired: " + requiredType; } - /** - * Check if sourceType can be assigned to targetType. - */ - private boolean isTypeCompatible(TypeInfo sourceType, TypeInfo targetType) { - if (sourceType == null || targetType == null) { - return true; // Can't validate, assume compatible - } - - // Same type name - if (sourceType.getFullName().equals(targetType.getFullName())) { - return true; - } - - // Same simple name (for primitives and common types) - if (sourceType.getSimpleName().equals(targetType.getSimpleName())) { - return true; - } - - // Check if sourceType is a subtype of targetType via reflection - if (sourceType.isResolved() && targetType.isResolved()) { - Class sourceClass = sourceType.getJavaClass(); - Class targetClass = targetType.getJavaClass(); - - if (sourceClass != null && targetClass != null) { - // Direct assignability - if (targetClass.isAssignableFrom(sourceClass)) { - return true; - } - - // Primitive widening conversions - if (isPrimitiveWidening(sourceClass, targetClass)) { - return true; - } - - // Boxing/unboxing - if (isBoxingCompatible(sourceClass, targetClass)) { - return true; - } - } - } - - return false; - } - - /** - * Check for primitive widening conversions. - * byte -> short -> int -> long -> float -> double - * char -> int -> long -> float -> double - */ - private boolean isPrimitiveWidening(Class from, Class to) { - if (!from.isPrimitive() || !to.isPrimitive()) { - return false; - } - - if (from == byte.class) { - return to == short.class || to == int.class || to == long.class || - to == float.class || to == double.class; - } - if (from == short.class || from == char.class) { - return to == int.class || to == long.class || to == float.class || to == double.class; - } - if (from == int.class) { - return to == long.class || to == float.class || to == double.class; - } - if (from == long.class) { - return to == float.class || to == double.class; - } - if (from == float.class) { - return to == double.class; - } - return false; - } - - /** - * Check for boxing/unboxing compatibility. - */ - private boolean isBoxingCompatible(Class from, Class to) { - if (from.isPrimitive()) { - Class wrapper = getWrapperClass(from); - if (wrapper != null && to.isAssignableFrom(wrapper)) { - return true; - } - } - if (to.isPrimitive()) { - Class wrapper = getWrapperClass(to); - if (wrapper != null && wrapper.isAssignableFrom(from)) { - return true; - } - } - return false; - } - - private Class getWrapperClass(Class primitive) { - if (primitive == boolean.class) return Boolean.class; - if (primitive == byte.class) return Byte.class; - if (primitive == char.class) return Character.class; - if (primitive == short.class) return Short.class; - if (primitive == int.class) return Integer.class; - if (primitive == long.class) return Long.class; - if (primitive == float.class) return Float.class; - if (primitive == double.class) return Double.class; - return null; - } - private void setError(ErrorType type, String message) { this.errorType = type; this.errorMessage = message; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldAccessInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldAccessInfo.java index 740ae1f46..3e0ae760d 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldAccessInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldAccessInfo.java @@ -1,5 +1,6 @@ package noppes.npcs.client.gui.util.script.interpreter.field; +import noppes.npcs.client.gui.util.script.interpreter.type.TypeChecker; import noppes.npcs.client.gui.util.script.interpreter.type.TypeInfo; /** @@ -94,7 +95,7 @@ public class FieldAccessInfo { // Check return type compatibility with expected type (e.g., assignment LHS) if (expectedType != null && resolvedField != null) { TypeInfo fieldType = resolvedField.getDeclaredType(); - if (fieldType != null && !isTypeCompatible(fieldType, expectedType)) { + if (fieldType != null && !TypeChecker.isTypeCompatible(expectedType, fieldType)) { //extra space is necessary for alignment //setError(ErrorType.TYPE_MISMATCH, "Provided type: " + fieldType.getSimpleName()+ //"\nRequired: " + expectedType.getSimpleName()); @@ -102,34 +103,6 @@ public class FieldAccessInfo { } } - /** - * Check if sourceType can be assigned to targetType. - */ - private boolean isTypeCompatible(TypeInfo sourceType, TypeInfo targetType) { - if (sourceType == null || targetType == null) { - return true; // Can't validate, assume compatible - } - - // Same type - if (sourceType.getFullName().equals(targetType.getFullName())) { - return true; - } - - // Check if sourceType is a subtype of targetType - if (sourceType.isResolved() && targetType.isResolved()) { - Class sourceClass = sourceType.getJavaClass(); - Class targetClass = targetType.getJavaClass(); - - if (sourceClass != null && targetClass != null) { - return targetClass.isAssignableFrom(sourceClass); - } - } - - // Primitive widening/boxing conversions would go here - // For now, just check direct equality - return false; - } - private void setError(ErrorType type, String message) { this.errorType = type; this.errorMessage = message; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java index 21aac4e20..64438c8f9 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java @@ -2,6 +2,7 @@ package noppes.npcs.client.gui.util.script.interpreter.method; import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo; import noppes.npcs.client.gui.util.script.interpreter.token.Token; +import noppes.npcs.client.gui.util.script.interpreter.type.TypeChecker; import noppes.npcs.client.gui.util.script.interpreter.type.TypeInfo; import java.util.ArrayList; @@ -348,7 +349,7 @@ public class MethodCallInfo { TypeInfo argType = arg.getResolvedType(); TypeInfo paramType = para.getDeclaredType(); if (argType != null && paramType != null) { - if (!isTypeCompatible(argType, paramType)) { + if (!TypeChecker.isTypeCompatible(paramType, argType)) { setArgTypeError(i, "Expected " + paramType.getSimpleName() + " but got " + argType.getSimpleName()); } @@ -362,84 +363,13 @@ public class MethodCallInfo { // Check return type compatibility with expected type (e.g., assignment LHS) if (expectedType != null && resolvedMethod != null) { TypeInfo returnType = resolvedMethod.getReturnType(); - if (returnType != null && !isTypeCompatible(returnType, expectedType)) { + if (returnType != null && !TypeChecker.isTypeCompatible(expectedType, returnType)) { // setError(ErrorType.RETURN_TYPE_MISMATCH, // "Required type: " + expectedType.getSimpleName() + ", Provided: " + returnType.getSimpleName()); } } } - /** - * Check if sourceType can be assigned to targetType. - * Handles primitive widening, inheritance, etc. - */ - private boolean isTypeCompatible(TypeInfo sourceType, TypeInfo targetType) { - if (sourceType == null || targetType == null) { - return true; // Can't verify, assume compatible - } - - if (sourceType.equals(targetType)) { - return true; - } - - Class sourceClass = sourceType.getJavaClass(); - Class targetClass = targetType.getJavaClass(); - - if (sourceClass == null || targetClass == null) { - return true; // Can't verify, assume compatible - } - - // Check inheritance - if (targetClass.isAssignableFrom(sourceClass)) { - return true; - } - - // Check primitive widening conversions - if (isPrimitiveWidening(sourceClass, targetClass)) { - return true; - } - - return false; - } - - /** - * Check if widening conversion is valid between primitives. - */ - private boolean isPrimitiveWidening(Class source, Class target) { - // byte -> short -> int -> long -> float -> double - // char -> int -> long -> float -> double - if (target == double.class || target == Double.class) { - return source == float.class || source == Float.class || - source == long.class || source == Long.class || - source == int.class || source == Integer.class || - source == short.class || source == Short.class || - source == byte.class || source == Byte.class || - source == char.class || source == Character.class; - } - if (target == float.class || target == Float.class) { - return source == long.class || source == Long.class || - source == int.class || source == Integer.class || - source == short.class || source == Short.class || - source == byte.class || source == Byte.class || - source == char.class || source == Character.class; - } - if (target == long.class || target == Long.class) { - return source == int.class || source == Integer.class || - source == short.class || source == Short.class || - source == byte.class || source == Byte.class || - source == char.class || source == Character.class; - } - if (target == int.class || target == Integer.class) { - return source == short.class || source == Short.class || - source == byte.class || source == Byte.class || - source == char.class || source == Character.class; - } - if (target == short.class || target == Short.class) { - return source == byte.class || source == Byte.class; - } - return false; - } - @Override public String toString() { StringBuilder sb = new StringBuilder("MethodCallInfo{"); diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java index 3fbd293d4..0327b3c4f 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java @@ -16,16 +16,31 @@ public final class TypeChecker { */ public static boolean isTypeCompatible(TypeInfo expected, TypeInfo actual) { if (expected == null) return true; // void can accept anything (shouldn't happen) - if (actual == null) return true; // null is compatible with any reference type + if (actual == null) return true; // Can't verify, assume compatible + + // Handle null literal - null is compatible with any reference type (non-primitive) + if ("".equals(actual.getFullName())) { + Class expectedClass = expected.getJavaClass(); + if (expectedClass != null && !expectedClass.isPrimitive()) { + return true; // null can be assigned to any reference type + } + // null cannot be assigned to primitive types + return false; + } String expectedName = expected.getSimpleName(); String actualName = actual.getSimpleName(); if (expectedName == null || actualName == null) return true; - // Exact match + // Exact match by simple name if (expectedName.equals(actualName)) return true; + // Exact match by full name + if (expected.getFullName() != null && actual.getFullName() != null) { + if (expected.getFullName().equals(actual.getFullName())) return true; + } + // Primitive widening conversions if (isNumericType(expectedName) && isNumericType(actualName)) { return canWiden(actualName, expectedName); @@ -33,10 +48,26 @@ public final class TypeChecker { // Object type compatibility (check inheritance) if (expected.getJavaClass() != null && actual.getJavaClass() != null) { - return expected.getJavaClass().isAssignableFrom(actual.getJavaClass()); + Class expectedClass = expected.getJavaClass(); + Class actualClass = actual.getJavaClass(); + + // Direct assignability + if (expectedClass.isAssignableFrom(actualClass)) { + return true; + } + + // Primitive widening with Class objects + if (isPrimitiveWidening(actualClass, expectedClass)) { + return true; + } + + // Boxing/unboxing compatibility + if (isBoxingCompatible(actualClass, expectedClass)) { + return true; + } } - // Allow boxed/unboxed conversions + // Allow boxed/unboxed conversions by name if (isPrimitiveOrWrapper(expectedName) && isPrimitiveOrWrapper(actualName)) { return getUnboxedName(expectedName).equals(getUnboxedName(actualName)); } @@ -157,5 +188,68 @@ public final class TypeChecker { if (type == null) return true; return isVoidType(type.getSimpleName()); } + + /** + * Check for primitive widening conversions. + * byte -> short -> int -> long -> float -> double + * char -> int -> long -> float -> double + */ + private static boolean isPrimitiveWidening(Class from, Class to) { + if (!from.isPrimitive() || !to.isPrimitive()) { + return false; + } + + if (from == byte.class) { + return to == short.class || to == int.class || to == long.class || + to == float.class || to == double.class; + } + if (from == short.class || from == char.class) { + return to == int.class || to == long.class || to == float.class || to == double.class; + } + if (from == int.class) { + return to == long.class || to == float.class || to == double.class; + } + if (from == long.class) { + return to == float.class || to == double.class; + } + if (from == float.class) { + return to == double.class; + } + return false; + } + + /** + * Check for boxing/unboxing compatibility. + */ + private static boolean isBoxingCompatible(Class from, Class to) { + if (from.isPrimitive()) { + Class wrapper = getWrapperClassInternal(from); + if (wrapper != null && to.isAssignableFrom(wrapper)) { + return true; + } + } + if (to.isPrimitive()) { + Class wrapper = getWrapperClassInternal(to); + if (wrapper != null && wrapper.isAssignableFrom(from)) { + return true; + } + } + return false; + } + + /** + * Get the wrapper class for a primitive type. + */ + private static Class getWrapperClassInternal(Class primitive) { + if (primitive == boolean.class) return Boolean.class; + if (primitive == byte.class) return Byte.class; + if (primitive == char.class) return Character.class; + if (primitive == short.class) return Short.class; + if (primitive == int.class) return Integer.class; + if (primitive == long.class) return Long.class; + if (primitive == float.class) return Float.class; + if (primitive == double.class) return Double.class; + return null; + } }