Skip to content

Commit

Permalink
Feat(wip): Allow Expansions with generic parameters in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Nov 30, 2024
1 parent e782c6a commit d7d345a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum OperatorType {
@interface Expansion {
String value();

//String[] typeParameters() default {};
String typeParameters() default "";
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
public interface JavaRuntimeTypeConverter {
TypeID getType(TypeVariableContext context, AnnotatedType type);

TypeID parseType(String type);
TypeID parseType(ZenCodeType.Expansion type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openzen.zenscript.lexer.ParseException;
import org.openzen.zenscript.lexer.ZSTokenParser;
import org.openzen.zenscript.lexer.ZSTokenType;
import org.openzen.zenscript.parser.definitions.ParsedTypeParameter;
import org.openzen.zenscript.parser.type.IParsedType;

import java.io.IOException;
Expand Down Expand Up @@ -75,7 +76,9 @@ public TypeID getType(TypeVariableContext context, AnnotatedType type) {
}

@Override
public TypeID parseType(String type) {
public TypeID parseType(ZenCodeType.Expansion expansion) {
String type = expansion.value();

for (TypeID value : this.typeByClass.values()) {
if (value.toString().equals(type))
return value;
Expand All @@ -92,7 +95,17 @@ public TypeID parseType(String type) {
IParsedType parsed = IParsedType.parse(tokens);

CompileContext context = new CompileContext(relative ? packageInfo.getPkg() : packageInfo.getRoot(), packageInfo.getPkg(), Collections.emptyList(), Collections.emptyMap(), Collections.emptyList());
return parsed.compile(context);

final ZSTokenParser typeBoundsTokens = ZSTokenParser.create(new LiteralSourceFile("internal", expansion.typeParameters()), null);
TypeParameter[] typeParameters = Optional.ofNullable(ParsedTypeParameter.parseAll(typeBoundsTokens))
.map(parsedTypeParameters -> {
TypeParameter[] compiled = ParsedTypeParameter.getCompiled(parsedTypeParameters);
ParsedTypeParameter.compile(context, compiled, parsedTypeParameters);
return compiled;
})
.orElse(TypeParameter.NONE);

return parsed.compile(context.withGeneric(typeParameters));
} catch (IOException ex) {
throw new AssertionError("Not supposed to happen");
} catch (ParseException ex) {
Expand Down Expand Up @@ -391,6 +404,7 @@ private Method getFunctionalInterfaceMethod(Class<?> functionalInterface) {

private void fillClassMaps() {
typeByClass.put(void.class, BasicTypeID.VOID);
typeByClass.put(Void.class, BasicTypeID.VOID);
typeByClass.put(boolean.class, BasicTypeID.BOOL);
typeByClass.put(byte.class, BasicTypeID.SBYTE);
typeByClass.put(char.class, BasicTypeID.CHAR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public JavaRuntimeClass addClass(Class<?> cls) {
TypeID target = null;
if (kind == JavaClass.Kind.EXPANSION) {
ZenCodeType.Expansion expansion = cls.getAnnotation(ZenCodeType.Expansion.class);
target = typeConverter.parseType(expansion.value());
target = typeConverter.parseType(expansion);
}

ParsedName name = getClassName(cls);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openzen.zenscript.scriptingexample.tests.actual_test.java_native.expansions;

import org.junit.jupiter.api.Test;
import org.openzen.zencode.java.ZenCodeType;
import org.openzen.zenscript.scriptingexample.tests.helpers.ScriptBuilder;
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;

import java.util.Collections;
import java.util.List;

class GenericExpansionTests extends ZenCodeTest {

@Override
public List<String> getRequiredStdLibModules() {
return Collections.singletonList("stdlib");
}

@Override
public List<Class<?>> getRequiredClasses() {
List<Class<?>> requiredClasses = super.getRequiredClasses();
requiredClasses.add(ExpandStringList.class);
return requiredClasses;
}

@Test
void works() {
ScriptBuilder.create()
.add("import stdlib.List;")
.add("var list = ['one', 'two', 'three'] as List<string>;")
.add("println(list.join(','));")
.execute(this);
logger.assertPrintOutputSize(1);
logger.assertPrintOutput(0, "one,two,three");
}

@ZenCodeType.Expansion(value = "stdlib.List<T>", typeParameters = "<T : string>")
public static class ExpandStringList {


@ZenCodeType.Method
public static String join(List<String> list) {
return String.join(",", list);
}
}
}

0 comments on commit d7d345a

Please sign in to comment.