Skip to content

Commit

Permalink
Merge pull request #168 from ZenCodeLang/feature/refactor-crafttweake…
Browse files Browse the repository at this point in the history
…r-stuff

Feature/refactor crafttweaker stuff
  • Loading branch information
stanhebben authored Aug 30, 2024
2 parents 4a1126a + c27ca1a commit 4014692
Show file tree
Hide file tree
Showing 48 changed files with 267 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public GenericName(String name, TypeID[] arguments) {
public static Optional<TypeID> getInnerType(TypeID type, List<GenericName> name, int index, List<ExpansionSymbol> expansions) {
while (index < name.size()) {
GenericName innerName = name.get(index++);
ResolvedType members = type.resolve(expansions);
ResolvedType members = type.resolve();
Optional<TypeID> inner = members
.findInnerType(innerName.name)
.map(t -> DefinitionTypeID.create(t, innerName.arguments));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
import org.openzen.zenscript.codemodel.compilation.ResolvedType;
import org.openzen.zenscript.codemodel.definition.*;
import org.openzen.zenscript.codemodel.generic.TypeParameter;
import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol;
import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance;
import org.openzen.zenscript.codemodel.member.*;
import org.openzen.zenscript.codemodel.type.BasicTypeID;
import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol;
import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType;
import org.openzen.zenscript.codemodel.type.member.InterfaceResolvedType;
import org.openzen.zenscript.codemodel.type.member.MemberSet;
import org.openzen.zenscript.codemodel.type.member.SubclassResolvedType;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public abstract class HighLevelDefinition extends Taggable implements TypeSymbol {
public final CodePosition position;
Expand Down Expand Up @@ -144,17 +144,13 @@ public String getName() {
}

@Override
public ResolvedType resolve(TypeID[] typeArguments, List<ExpansionSymbol> expansions) {
MemberSet.Builder members = MemberSet.create();
public ResolvedType resolve(TypeID type, TypeID[] typeArguments) {
MemberSet.Builder members = MemberSet.create(type);
GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments);
TypeID type = DefinitionTypeID.create(this, typeArguments);
for (IDefinitionMember member : this.members) {
member.registerTo(type, members, mapper);
}
List<ResolvedType> interfaceExpansions = new ArrayList<>();
for (IDefinitionMember member : this.members) {
interfaceExpansions.addAll(member.resolveExpansions(expansions));
}
List<TypeID> interfaces = this.members.stream().map(IDefinitionMember::asImplementation).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());

members.method(new MethodInstance(BuiltinMethodSymbol.OBJECT_SAME, new FunctionHeader(BasicTypeID.BOOL, type), type));
members.method(new MethodInstance(BuiltinMethodSymbol.OBJECT_NOTSAME, new FunctionHeader(BasicTypeID.BOOL, type), type));
Expand All @@ -163,11 +159,15 @@ public ResolvedType resolve(TypeID[] typeArguments, List<ExpansionSymbol> expans

if (superType != null) {
TypeID instancedSuperType = mapper.map(superType);
ResolvedType superResolved = instancedSuperType.resolve(expansions);
ResolvedType superResolved = instancedSuperType.resolve();
resolved = new SubclassResolvedType(superResolved, resolved, superType);
}

return ExpandedResolvedType.of(resolved, interfaceExpansions);
if (interfaces.isEmpty()) {
return resolved;
} else {
return new InterfaceResolvedType(resolved, interfaces);
}
}

protected void resolveAdditional(TypeID type, MemberSet.Builder members, GenericMapper mapper) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
import org.openzen.zenscript.codemodel.globals.IGlobal;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType;

import java.util.*;

Expand Down Expand Up @@ -79,12 +78,7 @@ public Optional<IGlobal> findGlobal(String name) {

@Override
public ResolvedType resolve(TypeID type) {
ResolvedType base = type.resolve(expansions);
List<ResolvedType> resolutions = new ArrayList<>();
for (ExpansionSymbol expansion : expansions) {
expansion.resolve(type).ifPresent(resolutions::add);
}
return ExpandedResolvedType.of(base, resolutions);
return type.resolve().withExpansions(expansions);
}

@Override
Expand All @@ -108,7 +102,7 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> name) {
.flatMap(t -> t.getType(position, this, name.get(0).arguments))
.flatMap(t -> {
for (int i = 1; i < name.size(); i++) {
Optional<TypeSymbol> inner = t.resolve(expansions).findInnerType(name.get(i).name);
Optional<TypeSymbol> inner = t.resolve().findInnerType(name.get(i).name);
if (inner.isPresent()) {
t = DefinitionTypeID.create(inner.get(), name.get(i).arguments);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openzen.zencode.shared.CompileError;
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.compilation.expression.WrappedCompilingExpression;
import org.openzen.zenscript.codemodel.expression.ArrayExpression;
import org.openzen.zenscript.codemodel.expression.CallArguments;
import org.openzen.zenscript.codemodel.expression.Expression;
Expand Down Expand Up @@ -225,24 +226,31 @@ private static <T extends AnyMethod> MatchedCallArguments<T> matchNormal(
new CallArguments(CastedExpression.Level.INVALID, expansionTypeArguments, typeArguments, Expression.NONE));
}

CastedExpression[] providedArguments = IntStream.range(0, arguments.length)
.mapToObj(i -> arguments[i].cast(CastedEval.implicit(compiler, position, header.getParameterType(false, i))))
CastedExpression[] providedArguments = IntStream.range(0, header.parameters.length)
.mapToObj(i -> {
CompilingExpression argument;
if (i < arguments.length) {
// parameter provided
argument = arguments[i];
} else if (header.getParameter(false, i).defaultValue != null) {
// default value
argument = new WrappedCompilingExpression(compiler, header.getParameter(false, i).defaultValue);
} else {
// invalid
return CastedExpression.invalid(compiler.at(position).invalid(CompileErrors.missingParameter(header.getParameter(false, i).name)));
}
return argument.cast(CastedEval.implicit(compiler, position, header.getParameterType(false, i)));
})
.toArray(CastedExpression[]::new);

Expression[] expressions = new Expression[header.parameters.length];
IntStream.range(0, providedArguments.length).forEach(i -> expressions[i] = providedArguments[i].value);
IntStream.range(providedArguments.length, header.parameters.length).forEach(i -> expressions[i] = header.getParameter(false, i).defaultValue);

CastedExpression.Level level = Stream.of(providedArguments)
.map(e -> e.level)
.max(Comparator.naturalOrder())
.orElse(CastedExpression.Level.EXACT);

boolean containsNull = Stream.of(expressions).anyMatch(Objects::isNull);

return new MatchedCallArguments<>(
instancedMethod,
new CallArguments(containsNull ? CastedExpression.Level.INVALID : level, expansionTypeArguments, typeArguments, expressions)
new CallArguments(level, expansionTypeArguments, typeArguments, Stream.of(providedArguments).map(casted -> casted.value).toArray(Expression[]::new))
);
}

Expand Down Expand Up @@ -299,12 +307,12 @@ private static <T extends AnyMethod> Optional<TypeID[]> inferTypeArguments(
) {
int providedTypeArguments = typeArguments == null ? 0 : typeArguments.length;

if (providedTypeArguments == 0 && method.getHeader().typeParameters.length == 0) {
return Optional.of(TypeID.NONE);
if (providedTypeArguments == method.getHeader().typeParameters.length) {
return Optional.of(typeArguments != null ? typeArguments : TypeID.NONE);
}

if (providedTypeArguments != 0 || method.getHeader().typeParameters.length == 0) {
return Optional.of(typeArguments == null ? TypeID.NONE : typeArguments);
if(providedTypeArguments != 0 && providedTypeArguments != method.getHeader().typeParameters.length) {
return Optional.empty();
}

// attempt to infer type arguments from the return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import org.openzen.zenscript.codemodel.expression.CallArguments;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public interface ResolvedType {
TypeID getType();

StaticCallable getConstructor();

Optional<StaticCallable> findImplicitConstructor();
Expand Down Expand Up @@ -73,6 +78,14 @@ default boolean canCastImplicitlyTo(TypeID target) {

Optional<StaticCallable> findStaticOperator(OperatorType operator);

default ResolvedType withExpansions(List<ExpansionSymbol> expansions) {
List<ResolvedType> resolutions = new ArrayList<>();
for (ExpansionSymbol expansion : expansions) {
expansion.resolve(getType()).ifPresent(resolutions::add);
}
return ExpandedResolvedType.of(this, resolutions);
}

interface SwitchMember {
SwitchValue toSwitchValue(List<CompilingVariable> bindings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.Collections;
import java.util.Optional;

public class LocalTypeImpl implements LocalType {
Expand Down Expand Up @@ -46,7 +45,7 @@ public Optional<StaticCallable> superCall() {
return Optional.empty();

if (resolvedSuper == null)
resolvedSuper = thisType.getSuperType().resolve(Collections.emptyList());
resolvedSuper = thisType.getSuperType().resolve();

return Optional.ofNullable(resolvedSuper)
.map(super_ -> super_.getConstructor().map(constructor -> new SuperCallable(thisType.getSuperType(), constructor)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void addDefaultMembers() {
boolean hasNoConstructor = members.stream().noneMatch(IDefinitionMember::isConstructor);

Optional<MethodInstance> superConstructor = Optional.ofNullable(getSuperType())
.flatMap(t -> t.resolve(Collections.emptyList()).getConstructor().getSingleOverload())
.flatMap(t -> t.resolve().getConstructor().getSingleOverload())
.flatMap(AnyMethod::asMethod);

if (hasNoConstructor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Optional<ResolvedType> resolve(TypeID expandingType) {
return Optional.empty();

TypeID[] expansionTypeArguments = Stream.of(typeParameters).map(mapping::get).toArray(TypeID[]::new);
MemberSet.Builder resolution = MemberSet.create();
MemberSet.Builder resolution = MemberSet.create(expandingType);
GenericMapper mapper = new GenericMapper(mapping, expansionTypeArguments);
for (IDefinitionMember member : members)
member.registerTo(expandingType, resolution, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.compilation.ResolvedType;
import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
import java.util.Optional;

public final class ParameterSuperBound implements TypeParameterBound {
Expand All @@ -21,7 +19,7 @@ public String getCanonical() {
}

@Override
public Optional<ResolvedType> resolveMembers(List<ExpansionSymbol> expansions) {
public Optional<ResolvedType> resolveMembers() {
return Optional.empty();
}

Expand All @@ -39,6 +37,11 @@ public TypeParameterBound instance(GenericMapper mapper) {
return new ParameterSuperBound(translated);
}

@Override
public Optional<TypeID> asType() {
return Optional.empty();
}

@Override
public <T> T accept(GenericParameterBoundVisitor<T> visitor) {
return visitor.visitSuper(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.compilation.ResolvedType;
import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
import java.util.Optional;

public final class ParameterTypeBound implements TypeParameterBound {
Expand All @@ -24,8 +22,8 @@ public String getCanonical() {
}

@Override
public Optional<ResolvedType> resolveMembers(List<ExpansionSymbol> expansions) {
return Optional.of(type.resolve(expansions));
public Optional<ResolvedType> resolveMembers() {
return Optional.of(type.resolve());
}

@Override
Expand All @@ -38,6 +36,11 @@ public TypeParameterBound instance(GenericMapper mapper) {
return new ParameterTypeBound(position, type.instance(mapper));
}

@Override
public Optional<TypeID> asType() {
return Optional.of(type);
}

@Override
public <T> T accept(GenericParameterBoundVisitor<T> visitor) {
return visitor.visitType(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.compilation.ResolvedType;
import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
import java.util.Optional;

public interface TypeParameterBound {
Expand All @@ -15,11 +13,13 @@ public interface TypeParameterBound {

<C, R> R accept(C context, GenericParameterBoundVisitorWithContext<C, R> visitor);

Optional<ResolvedType> resolveMembers(List<ExpansionSymbol> expansions);
Optional<ResolvedType> resolveMembers();

boolean matches(TypeID type);

TypeParameterBound instance(GenericMapper mapper);

Optional<TypeID> asType();

String getCanonical();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -20,7 +19,7 @@ public interface TypeSymbol extends DefinitionSymbol {

String getName();

ResolvedType resolve(TypeID[] typeArguments, List<ExpansionSymbol> expansions);
ResolvedType resolve(TypeID type, TypeID[] typeArguments);

Optional<TypeSymbol> getOuter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

public interface IDefinitionMember {
CodePosition getPosition();
Expand All @@ -27,8 +28,8 @@ public interface IDefinitionMember {

void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapper mapper);

default List<ResolvedType> resolveExpansions(List<ExpansionSymbol> expansions) {
return Collections.emptyList();
default Optional<TypeID> asImplementation() {
return Optional.empty();
}

<T> T accept(MemberVisitor<T> visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ public void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapp
}

@Override
public List<ResolvedType> resolveExpansions(List<ExpansionSymbol> expansions) {
return expansions.stream()
.map(expansion -> expansion.resolve(type))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
public Optional<TypeID> asImplementation() {
return Optional.of(type);
}

@Override
Expand Down
Loading

0 comments on commit 4014692

Please sign in to comment.