implements PR020

This commit is contained in:
2026-03-24 13:42:22 +00:00
parent eb469fd68c
commit d2287a0a58
4 changed files with 198 additions and 6 deletions
@@ -1,6 +1,7 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.lexer.LexErrors;
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
@@ -44,10 +45,11 @@ class PbsFrontendCompilerTest {
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(0), source, diagnostics);
final var fileBackend = compiler.compileFile(new FileId(0), source, diagnostics);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
assertEquals(0, fileBackend.functions().size());
}
@Test
@@ -59,9 +61,42 @@ class PbsFrontendCompilerTest {
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(0), source, diagnostics);
final var fileBackend = compiler.compileFile(new FileId(0), source, diagnostics);
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
assertEquals(2, fileBackend.functions().size());
}
@Test
void shouldNotLowerWhenSyntaxErrorsExist() {
final var source = """
$
fn run() -> int { return 1; }
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(0), source, diagnostics);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(LexErrors.E_LEX_INVALID_CHAR.name())));
assertEquals(0, fileBackend.functions().size());
}
@Test
void shouldNotLowerWhenStaticSemanticsErrorsExist() {
final var source = """
fn run(x: int) -> int { return x; }
fn run(y: int) -> int { return y; }
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(0), source, diagnostics);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
assertEquals(0, fileBackend.functions().size());
}
}
@@ -21,6 +21,7 @@ import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PBSFrontendPhaseServiceTest {
@@ -61,13 +62,115 @@ class PBSFrontendPhaseServiceTest {
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
new PBSFrontendPhaseService().compile(
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().anyMatch(d -> d.getCode().equals(PbsLinkErrors.E_LINK_INVALID_BARREL_FILENAME.name())));
assertEquals(0, irBackend.getFunctions().size());
}
@Test
void shouldLowerOnlyModulesThatPassAdmissionGates() throws IOException {
final var projectRoot = tempDir.resolve("project-mixed");
final var sourceRoot = projectRoot.resolve("src");
final var validModulePath = sourceRoot.resolve("valid");
final var invalidModulePath = sourceRoot.resolve("invalid");
Files.createDirectories(validModulePath);
Files.createDirectories(invalidModulePath);
final var validSource = validModulePath.resolve("source.pbs");
final var validBarrel = validModulePath.resolve("mod.barrel");
final var invalidSource = invalidModulePath.resolve("source.pbs");
Files.writeString(validSource, """
fn good(v: int) -> int {
return v;
}
""");
Files.writeString(validBarrel, "pub fn good(v: int) -> int;");
Files.writeString(invalidSource, """
fn bad(v: int) -> int {
return v;
}
""");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("core")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, validSource, fileTable);
registerFile(projectId, projectRoot, validBarrel, fileTable);
registerFile(projectId, projectRoot, invalidSource, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_MISSING_BARREL.name())));
assertEquals(1, irBackend.getFunctions().size());
assertEquals("good", irBackend.getFunctions().getFirst().name());
}
@Test
void shouldLowerAllSourcesWhenNoAdmissionErrorsExist() throws IOException {
final var projectRoot = tempDir.resolve("project-valid");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("math");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
fn run() -> int { return 1; }
fn sum(a: int, b: int) -> int { return a + b; }
""");
Files.writeString(modBarrel, """
pub fn run() -> int;
pub fn sum(a: int, b: int) -> int;
""");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("core")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, sourceFile, fileTable);
registerFile(projectId, projectRoot, modBarrel, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.isEmpty());
assertEquals(2, irBackend.getFunctions().size());
}
private void registerFile(