small code improvements

This commit is contained in:
2026-03-24 13:42:16 +00:00
parent 956d3128a9
commit f3be4f7946
8 changed files with 72 additions and 28 deletions
@@ -1,6 +1,7 @@
package p.studio.compiler.pbs;
import p.studio.compiler.models.IRFunction;
import p.studio.compiler.models.IRBackendFile;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
@@ -13,14 +14,14 @@ import java.util.HashSet;
import java.util.Set;
public final class PbsFrontendCompiler {
public ReadOnlyList<IRFunction> compileFile(
public IRBackendFile compileFile(
final FileId fileId,
final String source,
final DiagnosticSink diagnostics) {
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
final var ast = PbsParser.parse(tokens, fileId, diagnostics);
validateFunctionNames(ast, diagnostics);
return lowerFunctions(fileId, ast);
return new IRBackendFile(fileId, lowerFunctions(fileId, ast));
}
private void validateFunctionNames(
@@ -4,10 +4,6 @@ import p.studio.compiler.source.Span;
import p.studio.utilities.structures.ReadOnlyList;
public final class PbsAst {
public sealed interface Statement permits LetStatement, ReturnStatement, ExpressionStatement {
Span span();
}
private PbsAst() {
}
@@ -36,6 +32,10 @@ public final class PbsAst {
Span span) {
}
public sealed interface Statement permits LetStatement, ReturnStatement, ExpressionStatement {
Span span();
}
public record Block(
ReadOnlyList<Statement> statements,
Span span) {
@@ -4,11 +4,9 @@ import lombok.extern.slf4j.Slf4j;
import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.models.IRFunction;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.MutableList;
@Slf4j
public class PBSFrontendPhaseService implements FrontendPhaseService {
@@ -20,7 +18,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
final DiagnosticSink diagnostics,
final LogAggregator logs,
final BuildingIssueSink issues) {
final MutableList<IRFunction> functions = MutableList.create();
final var irBackendAggregator = IRBackend.aggregator();
for (final var pId : ctx.stack.reverseTopologicalOrder) {
final var fileIds = ctx.fileTable.getFiles(pId);
@@ -30,10 +28,11 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
switch (sourceHandle.getExtension()) {
case "pbs": {
sourceHandle.readUtf8().ifPresentOrElse(
utf8Content -> functions.addAll(frontendCompiler
.compileFile(fId, utf8Content, diagnostics)
.stream()
.toList()),
utf8Content -> {
final var irBackendFile = frontendCompiler
.compileFile(fId, utf8Content, diagnostics);
irBackendAggregator.merge(irBackendFile);
},
() -> issues.report(builder -> builder
.error(true)
.message("Failed to read file content: %s".formatted(sourceHandle.toString()))));
@@ -45,10 +44,8 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
}
}
logs.using(log).debug("PBS frontend lowered %d function(s) to IR".formatted(functions.size()));
return IRBackend
.builder()
.functions(functions.toReadOnlyList())
.build();
final var irBackend = irBackendAggregator.emit();
logs.using(log).debug("PBS frontend lowered to IR BE:\n%s".formatted(irBackend));
return irBackend;
}
}