implements PR026
This commit is contained in:
+2
@@ -247,6 +247,8 @@ public final class PbsModuleVisibilityValidator {
|
||||
|
||||
if (topDecl instanceof PbsAst.StructDecl structDecl) {
|
||||
registerNonFunctionDeclaration(declarations, NonFunctionKind.STRUCT, structDecl.name(), structDecl.span(), nameTable);
|
||||
} else if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
|
||||
registerNonFunctionDeclaration(declarations, NonFunctionKind.STRUCT, builtinTypeDecl.name(), builtinTypeDecl.span(), nameTable);
|
||||
} else if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
|
||||
registerNonFunctionDeclaration(declarations, NonFunctionKind.CONTRACT, contractDecl.name(), contractDecl.span(), nameTable);
|
||||
} else if (topDecl instanceof PbsAst.HostDecl hostDecl) {
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package p.studio.compiler.pbs.stdlib;
|
||||
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class ResourceStdlibEnvironmentResolver implements StdlibEnvironmentResolver {
|
||||
private static final String STDLIB_ROOT = "stdlib";
|
||||
|
||||
@Override
|
||||
public StdlibEnvironment resolve(final int stdlibVersion) {
|
||||
return new ResourceStdlibEnvironment(stdlibVersion);
|
||||
}
|
||||
|
||||
private static final class ResourceStdlibEnvironment implements StdlibEnvironment {
|
||||
private final int stdlibVersion;
|
||||
|
||||
private ResourceStdlibEnvironment(final int stdlibVersion) {
|
||||
this.stdlibVersion = stdlibVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<StdlibModuleSource> resolveModule(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
final var moduleBasePath = moduleBasePath(project, pathSegments);
|
||||
final var mainSource = readResource(moduleBasePath + "/main.pbs");
|
||||
final var barrelSource = readResource(moduleBasePath + "/mod.barrel");
|
||||
if (mainSource.isEmpty() || barrelSource.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(new StdlibModuleSource(
|
||||
project,
|
||||
pathSegments,
|
||||
ReadOnlyList.wrap(java.util.List.of(new StdlibModuleSource.SourceFile("main.pbs", mainSource.get()))),
|
||||
barrelSource.get()));
|
||||
}
|
||||
|
||||
private String moduleBasePath(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
final var pathBuilder = new StringBuilder();
|
||||
pathBuilder.append(STDLIB_ROOT)
|
||||
.append('/')
|
||||
.append(stdlibVersion)
|
||||
.append('/')
|
||||
.append(project);
|
||||
for (final var pathSegment : pathSegments) {
|
||||
pathBuilder.append('/').append(pathSegment);
|
||||
}
|
||||
return pathBuilder.toString();
|
||||
}
|
||||
|
||||
private Optional<String> readResource(final String resourcePath) {
|
||||
final var classLoader = ResourceStdlibEnvironmentResolver.class.getClassLoader();
|
||||
if (classLoader == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
try (InputStream stream = classLoader.getResourceAsStream(resourcePath)) {
|
||||
if (stream == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(new String(stream.readAllBytes(), StandardCharsets.UTF_8));
|
||||
} catch (IOException ignored) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -14,8 +14,8 @@ import p.studio.compiler.pbs.linking.PbsLinkErrors;
|
||||
import p.studio.compiler.pbs.linking.PbsModuleVisibilityValidator;
|
||||
import p.studio.compiler.pbs.parser.PbsBarrelParser;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.pbs.stdlib.EmptyStdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.InterfaceModuleLoader;
|
||||
import p.studio.compiler.pbs.stdlib.ResourceStdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
@@ -42,7 +42,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
private final InterfaceModuleLoader interfaceModuleLoader;
|
||||
|
||||
public PBSFrontendPhaseService() {
|
||||
this(new EmptyStdlibEnvironmentResolver(), new InterfaceModuleLoader());
|
||||
this(new ResourceStdlibEnvironmentResolver(), new InterfaceModuleLoader());
|
||||
}
|
||||
|
||||
PBSFrontendPhaseService(
|
||||
|
||||
Reference in New Issue
Block a user