Skip to content

Commit

Permalink
Fix resolving fully qualified names
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Oct 11, 2024
1 parent aa11ee4 commit 6e9bd58
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> name) {
});
}

@Override
public Optional<ZSPackage> getRootPackage(String name) {
return rootPackage.getOptional(name);
}

@Override
public Optional<AnnotationDefinition> resolveAnnotation(List<GenericName> name) {
if (name.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public static CompileError cannotUseTypeAsValue() {
return new CompileError(CompileExceptionCode.TYPE_NOT_VALUE, "Type cannot be used as value");
}

public static CompileError cannotUsePackageAsValue() {
return new CompileError(CompileExceptionCode.PACKAGE_NOT_VALUE, "Package cannot be used as value");
}

public static CompileError associativeKeyCannotHaveTypeParameters() {
return new CompileError(CompileExceptionCode.INVALID_ASSOC_KEY,
"Associative array key cannot have type parameters");
Expand Down Expand Up @@ -599,4 +603,8 @@ public static CompileError incompleteImplementation(List<MethodSymbol> unimpleme

return new CompileError(CompileExceptionCode.INCOMPLETE_IMPLEMENTATION, text);
}

public static CompileError noMemberInPackage(String fullName, String name) {
return new CompileError(CompileExceptionCode.NO_SUCH_MEMBER, "No member " + name + " in package " + fullName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.GenericName;
import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
import org.openzen.zenscript.codemodel.generic.TypeParameter;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.type.*;
Expand Down Expand Up @@ -32,6 +33,8 @@ public interface TypeBuilder {

Optional<TypeID> resolve(CodePosition position, List<GenericName> name);

Optional<ZSPackage> getRootPackage(String name);

Optional<AnnotationDefinition> resolveAnnotation(List<GenericName> name);

ExpressionCompiler getDefaultValueCompiler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.openzen.zenscript.codemodel.compilation.expression;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.GenericName;
import org.openzen.zenscript.codemodel.compilation.*;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.Optional;

public class PackageCompilingExpression implements CompilingExpression {
private final ExpressionCompiler compiler;
private final ZSPackage pkg;
private final CodePosition position;

public PackageCompilingExpression(ExpressionCompiler compiler, CodePosition position, ZSPackage pkg) {
this.compiler = compiler;
this.position = position;
this.pkg = pkg;
}

@Override
public Expression eval() {
return compiler.at(position).invalid(CompileErrors.cannotUsePackageAsValue());
}

@Override
public CastedExpression cast(CastedEval cast) {
return CastedExpression.invalid(position, CompileErrors.cannotUsePackageAsValue());
}

@Override
public boolean canConstructAs(TypeID type) {
return false;
}

@Override
public Optional<CompilingCallable> call() {
return Optional.empty();
}

@Override
public CompilingExpression getMember(CodePosition position, GenericName name) {
Optional<TypeID> asType = pkg.getType(name);
if (asType.isPresent()) {
return new TypeCompilingExpression(compiler, position, asType.get());
}

Optional<ZSPackage> asPackage = pkg.getOptional(name.name);
if (asPackage.isPresent()) {
return new PackageCompilingExpression(compiler, position, asPackage.get());
}

return new InvalidCompilingExpression(compiler, position, CompileErrors.noMemberInPackage(pkg.fullName, name.name));
}

@Override
public CompilingExpression assign(CompilingExpression value) {
return new InvalidCompilingExpression(compiler, position, CompileErrors.cannotUsePackageAsValue());
}

@Override
public Expression as(TypeID type) {
return compiler.at(position).invalid(CompileErrors.cannotUsePackageAsValue(), type);
}

@Override
public void collect(SSAVariableCollector collector) {

}

@Override
public void linkVariables(CodeBlockStatement.VariableLinker linker) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
import org.openzen.zenscript.codemodel.compilation.ExpressionCompiler;
import org.openzen.zenscript.codemodel.compilation.TypeBuilder;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
import org.openzen.zenscript.codemodel.generic.TypeParameter;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.type.*;
Expand Down Expand Up @@ -74,6 +75,11 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> name) {
return AbstractTypeBuilder.this.resolve(position, name);
}

@Override
public Optional<ZSPackage> getRootPackage(String name) {
return AbstractTypeBuilder.this.getRootPackage(name);
}

@Override
public Optional<AnnotationDefinition> resolveAnnotation(List<GenericName> name) {
return AbstractTypeBuilder.this.resolveAnnotation(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalExpression;
import org.openzen.zenscript.codemodel.compilation.impl.capture.LocalThisExpression;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.expression.modifiable.ModifiableExpression;
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
Expand Down Expand Up @@ -92,6 +93,11 @@ public Optional<CompilingExpression> resolve(CodePosition position, GenericName
return asType.map(type -> new TypeCompilingExpression(this, position, type));
}

Optional<ZSPackage> asPackage = types.getRootPackage(name.name);
if (asPackage.isPresent()) {
return Optional.of(new PackageCompilingExpression(this, position, asPackage.get()));
}

return context.findGlobal(name.name)
.map(g -> g.getExpression(position, name.arguments).compile(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openzen.zenscript.codemodel.compilation.impl.compiler.MemberCompilerImpl;
import org.openzen.zenscript.codemodel.compilation.impl.compiler.StatementCompilerImpl;
import org.openzen.zenscript.codemodel.context.CompilingPackage;
import org.openzen.zenscript.codemodel.definition.ZSPackage;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
Expand Down Expand Up @@ -90,6 +91,11 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> name) {
return context.resolve(position, name);
}

@Override
public Optional<ZSPackage> getRootPackage(String name) {
return context.getRootPackage(name);
}

@Override
public Optional<AnnotationDefinition> resolveAnnotation(List<GenericName> name) {
return context.resolveAnnotation(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public enum CompileExceptionCode {
USING_STATIC_ON_INSTANCE,
CANNOT_ASSIGN,
UNAVAILABLE_IN_CLOSURE,
USING_PACKAGE_AS_EXPRESSION,
USING_PACKAGE_AS_CALL_TARGET,
USING_TYPE_AS_EXPRESSION,
MEMBER_NO_SETTER,
MEMBER_NO_GETTER,
MEMBER_NOT_STATIC,
Expand Down Expand Up @@ -86,6 +83,7 @@ public enum CompileExceptionCode {
INVALID_ASSOC_KEY,
DIFFERENT_EXCEPTIONS,
TYPE_NOT_VALUE,
PACKAGE_NOT_VALUE,
TRY_CONVERT_WITHOUT_RESULT,
TRY_CONVERT_WITHOUT_THROW,
TYPE_ARGUMENTS_NOT_ALLOWED,
Expand Down

0 comments on commit 6e9bd58

Please sign in to comment.