Skip to content

Commit

Permalink
Merge pull request #184 from ZenCodeLang/fix/general-issues
Browse files Browse the repository at this point in the history
Fix/general issues
  • Loading branch information
stanhebben authored Jan 31, 2025
2 parents 3e18e7a + 2382bab commit f7a20a4
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static <T extends AnyMethod> MatchedCallArguments<T> match(
if (!matchedVarArg.isPresent()) {
return matchedWidening;
} else {
return Stream.of(matchedVarArg.get(), matchedWidening).min(Comparator.comparing(match -> match.arguments.level)).orElseThrow(() -> new IllegalStateException("Should never happen"));
return Stream.of(matchedWidening, matchedVarArg.get()).min(Comparator.comparing(match -> match.arguments.level)).orElseThrow(() -> new IllegalStateException("Should never happen"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,24 @@ public Expression eval() {

ResolvedType resolved = compiler.resolve(type);

/*Optional<Expression> asGetter = resolved.findStaticGetter(name.name)
.map(getter -> getter.call(compiler, position, TypeID.NONE));
if (asGetter.isPresent()) {
return asGetter.get();
}*/

Optional<StaticCallable> staticGetter = resolved.findStaticGetter(name.name);
if(staticGetter.isPresent()){
return staticGetter.get().call(compiler, position, TypeID.NONE);
Optional<Expression> byGetter = resolved
.findStaticGetter(name.name)
.map(getter -> getter.call(compiler, position, TypeID.NONE));

if (byGetter.isPresent()) {
return byGetter.get();
}

Optional<CompilableExpression> contextMember = resolved.getContextMember(name.name);
if(contextMember.isPresent()){
if (contextMember.isPresent()) {
return contextMember.get().compile(compiler).eval();
}
return compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name));

// return resolved.findStaticGetter(name.name)
// .map(getter -> getter.call(compiler, position, TypeID.NONE))
// .orElseGet(() ->
// resolved.getContextMember(name.name)
// .map(member -> member.compile(compiler).eval())
// .orElseGet(() -> compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name))));

return resolved
.findField(name.name)
.filter(ResolvedType.Field::isStatic)
.map(field -> field.getStatic(compiler.at(position)))
.orElseGet(() -> compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public boolean equals(Object obj) {
final DefinitionTypeID other = (DefinitionTypeID) obj;
return this.definition == other.definition
&& Arrays.deepEquals(this.typeArguments, other.typeArguments)
&& Objects.equals(outer, this.outer);
&& Objects.equals(outer, other.outer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
final OptionalTypeID other = (OptionalTypeID) obj;
return this.baseType == other.baseType;
return this.baseType.equals(other.baseType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ScriptingEngine(ScriptingEngineLogger logger) {
}

public ScriptingEngine(ScriptingEngineLogger logger, Function<String, InputStream> resourceGetter) {
this(logger, resourceGetter, "stdlib", "math", "collections", "uuid");
this(logger, resourceGetter, "stdlib", "math", "collections", "uuid", "javalib");
}

public ScriptingEngine(ScriptingEngineLogger logger, Function<String, InputStream> resourceGetter, String... stdLibModulesToRegister) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import org.openzen.zencode.java.TypeVariableContext;
import org.openzen.zencode.java.module.JavaAnnotatedType;
import org.openzen.zencode.java.module.JavaNativePackageInfo;
import org.openzen.zencode.java.module.JavaRuntimeClass;
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zencode.shared.LiteralSourceFile;
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.Modifiers;
import org.openzen.zenscript.codemodel.ModuleSpace;
import org.openzen.zenscript.codemodel.compilation.CompileContext;
import org.openzen.zenscript.codemodel.definition.ClassDefinition;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
Expand Down Expand Up @@ -68,15 +66,10 @@ public TypeID getType(TypeVariableContext context, AnnotatedType type) {
} else if (type.isAnnotationPresent(ZenCodeType.USize.class)) {
result = BasicTypeID.USIZE;
} else {
boolean unsigned = type.isAnnotationPresent(ZenCodeType.Unsigned.class);
result = loadType(context, JavaAnnotatedType.of(type), unsigned);
result = loadType(context, JavaAnnotatedType.of(type));
}

boolean isOptional = type.isAnnotationPresent(ZenCodeType.Nullable.class);
if (isOptional && !result.isOptional())
result = new OptionalTypeID(result);

return result;
return optionallyWrap(type, result);
}

@Override
Expand Down Expand Up @@ -106,19 +99,19 @@ public TypeID parseType(String type) {
}
}

private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boolean unsigned) {
private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type) {
final JavaAnnotatedType.ElementType elementType = type.getElementType();

try {
switch (elementType) {
case ANNOTATED_PARAMETERIZED_TYPE:
return loadAnnotatedParameterizedType(context, (AnnotatedParameterizedType) type.getAnnotatedElement(), unsigned);
return loadAnnotatedParameterizedType(context, (AnnotatedParameterizedType) type.getAnnotatedElement());
case ANNOTATED_TYPE:
return loadAnnotatedType(context, (AnnotatedType) type.getAnnotatedElement(), unsigned);
return loadAnnotatedType(context, (AnnotatedType) type.getAnnotatedElement());
case CLASS:
return loadClass(context, (Class<?>) type.getType(), unsigned);
return loadClass(context, (Class<?>) type.getType());
case GENERIC_ARRAY:
return loadGenericArray(context, (GenericArrayType) type.getType(), unsigned);
return loadGenericArray(context, (GenericArrayType) type.getType());
case PARAMETERIZED_TYPE:
return loadParameterizedType(context, (ParameterizedType) type.getType());
case TYPE_VARIABLE:
Expand All @@ -133,56 +126,52 @@ private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boo
throw new IllegalArgumentException("Invalid type " + elementType + ": not yet implemented or foolery");
}

private TypeSymbol loadRawType(TypeVariableContext context, JavaAnnotatedType type, boolean unsigned) {
private TypeSymbol loadRawType(JavaAnnotatedType type) {
final JavaAnnotatedType.ElementType elementType = type.getElementType();

try {
switch (elementType) {
case CLASS:
return findType((Class<?>) type.getType());
default:
throw new IllegalArgumentException("Didn't expect to get this kind of type: " + type);
if (Objects.requireNonNull(elementType) == JavaAnnotatedType.ElementType.CLASS) {
return findType((Class<?>) type.getType());
}
throw new IllegalArgumentException("Didn't expect to get this kind of type: " + type);
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException("Unable to analyze type: " + type, e);
}
}

private TypeID loadAnnotatedParameterizedType(TypeVariableContext context, AnnotatedParameterizedType type, boolean unsigned) {
private TypeID loadAnnotatedParameterizedType(TypeVariableContext context, AnnotatedParameterizedType type) {
final ParameterizedType parameterizedType = this.getTypeIfValid(JavaAnnotatedType.of(type.getType()), JavaAnnotatedType.ElementType.PARAMETERIZED_TYPE);
final JavaAnnotatedType rawType = JavaAnnotatedType.of(parameterizedType.getRawType());

final JavaAnnotatedType[] actualTypeArguments = JavaAnnotatedType.arrayOf(type.getAnnotatedActualTypeArguments());
final TypeID[] codeParameters = new TypeID[actualTypeArguments.length];

for (int i = 0; i < actualTypeArguments.length; i++) {
codeParameters[i] = this.loadType(context, actualTypeArguments[i], false);
codeParameters[i] = this.loadType(context, actualTypeArguments[i]);
}

if (rawType.getElementType() == JavaAnnotatedType.ElementType.CLASS) {
//noinspection SuspiciousMethodCalls
if (specialTypes.containsKey(rawType.getType())) {
//noinspection SuspiciousMethodCalls
return specialTypes.get(rawType.getType()).apply(codeParameters);
}

final TypeSymbol rawTypeSymbol = loadRawType(context, rawType, unsigned);
final TypeSymbol rawTypeSymbol = loadRawType(rawType);
return DefinitionTypeID.create(rawTypeSymbol, codeParameters);
}
return this.loadType(context, JavaAnnotatedType.of(type), unsigned);
return this.loadType(context, JavaAnnotatedType.of(type));
}

private TypeID loadAnnotatedType(TypeVariableContext context, AnnotatedType type, boolean unsigned) {
private TypeID loadAnnotatedType(TypeVariableContext context, AnnotatedType type) {
final JavaAnnotatedType annotatedType = JavaAnnotatedType.of(type.getType());
return this.loadType(context, annotatedType, unsigned);
TypeID result = this.loadType(context, annotatedType);
return optionallyWrap(type, result);
}

private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean unsigned) {
if (unsigned) {
return unsignedByClass.computeIfAbsent(type, it -> {
throw new IllegalArgumentException("This class cannot be used as unsigned: " + it);
});
}
private TypeID loadClass(TypeVariableContext context, Class<?> type) {
if (type.isArray()) {
final TypeID baseType = this.loadType(context, JavaAnnotatedType.of(type.getComponentType()), false);
final TypeID baseType = this.loadType(context, JavaAnnotatedType.of(type.getComponentType()));
return new ArrayTypeID(baseType);
}
if (type.isAnnotationPresent(FunctionalInterface.class)) {
Expand All @@ -205,9 +194,9 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean uns
return DefinitionTypeID.create(definition, typeParameters.toArray(TypeID.NONE));
}

private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type, boolean unsigned) {
private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type) {
final JavaAnnotatedType componentType = JavaAnnotatedType.of(type.getGenericComponentType());
final TypeID baseType = this.loadType(context, componentType, unsigned);
final TypeID baseType = this.loadType(context, componentType);
return new ArrayTypeID(baseType);
}

Expand All @@ -221,7 +210,7 @@ private TypeID loadParameterizedType(TypeVariableContext context, ParameterizedT

TypeID[] codeParameters = new TypeID[typeArguments.length];
for (int i = 0; i < typeArguments.length; i++) {
codeParameters[i] = this.loadType(context, typeArguments[i], false);
codeParameters[i] = this.loadType(context, typeArguments[i]);
}

if (rawType == Map.class) {
Expand Down Expand Up @@ -342,7 +331,7 @@ private TypeID loadFunctionalInterface(TypeVariableContext loadContext, Class<?>
Map<TypeParameter, TypeID> mapping = new HashMap<>();
TypeVariable<?>[] javaParameters = cls.getTypeParameters();
for (int i = 0; i < parameters.length; i++) {
mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i], false));
mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i]));
}

header = header.withGenericArguments(new GenericMapper(mapping, TypeID.NONE));
Expand Down Expand Up @@ -469,4 +458,11 @@ private void fillSpecialTypes() {
specialTypes.put(ToLongFunction.class, args -> new FunctionTypeID(new FunctionHeader(BasicTypeID.LONG, args[0])));
specialTypes.put(UnaryOperator.class, args -> new FunctionTypeID(new FunctionHeader(args[0], args[0])));
}

private TypeID optionallyWrap(AnnotatedType type, TypeID result) {
boolean isOptional = type.isAnnotationPresent(ZenCodeType.Nullable.class);
if (isOptional && !result.isOptional())
result = new OptionalTypeID(result);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
import org.openzen.zenscript.codemodel.type.ArrayTypeID;
import org.openzen.zenscript.codemodel.type.BasicTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol;
import org.openzen.zenscript.javashared.JavaClass;
Expand Down Expand Up @@ -213,9 +214,9 @@ private void loadMethods() {
if (class_.cls.isEnum()) {
Stream.of(
BuiltinMethodSymbol.ENUM_NAME,
BuiltinMethodSymbol.ENUM_ORDINAL,
BuiltinMethodSymbol.ENUM_ORDINAL
//BuiltinMethodSymbol.ENUM_VALUES,
BuiltinMethodSymbol.ENUM_COMPARE
// BuiltinMethodSymbol.ENUM_COMPARE
).forEach(method -> methods
.computeIfAbsent(method.getID(), x -> new ArrayList<>())
.add(method)
Expand All @@ -232,6 +233,18 @@ private void loadMethods() {
throw new IllegalStateException("We found an enum class without values() method: " + class_.cls.getCanonicalName(), exception);
}

try {
MethodID id = MethodID.operator(OperatorType.COMPARE);
FunctionHeader header = new FunctionHeader(BasicTypeID.INT, target);
Method method = class_.cls.getMethod("compareTo", Enum.class);
JavaRuntimeMethod runtimeMethod = new JavaRuntimeMethod(class_, target, method, id, header, false, false);
methods.computeIfAbsent(id, x -> new ArrayList<>()).add(runtimeMethod);
class_.module.getCompiled().setMethodInfo(runtimeMethod, runtimeMethod);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException("Error while registering Enum#compareTo for: " + class_.cls.getCanonicalName(), exception);
}


}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#output: ========
#output: default value here
#output: ========
#debug
function test(args...: string[] = ["default value here"]): void {
for item in args println(item);
}

println("========");
test();
println("========");
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#output: ========
#output: provided value
#output: ========
#debug
function test(args...: string[] = ["default value here"]): void {
for item in args println(item);
}

println("========");
test("provided value");
println("========");

0 comments on commit f7a20a4

Please sign in to comment.