fix(compiler): isolate PBS conformance tests from common pipeline

This commit is contained in:
2026-10-16 11:56:55 +01:00
parent a04214a705
commit c4e1b9055d
5 changed files with 10 additions and 4 deletions
@@ -0,0 +1,124 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.models.SourceKind;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsRuntimeAlignmentTest {
private static String sdk(String path) throws Exception {
try(var stream = PbsFrontendCompiler.class.getResourceAsStream("/game/stdlib/1/"+path+"/main.pbs")) {
assertNotNull(stream); return new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
}
}
private static BytecodeModule compile(String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();
var fileId = new FileId(0);
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,fileId,diagnostics),fileId,diagnostics,p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(fileId,ast,diagnostics,SourceKind.SDK_INTERFACE,
new p.studio.compiler.source.identifiers.ModuleId(0),p.studio.utilities.structures.ReadOnlyList.empty(),
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(), ()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
var backend=IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new p.studio.compiler.source.identifiers.ModuleId(0)).executableFunctions(file.executableFunctions())
.globals(file.globals()).registry(file.registry()).intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
var program=new LowerToIRVMService().lower(backend);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of(System.getProperty("pbs.registryConformanceDirectory"));Files.createDirectories(directory);
Files.write(directory.resolve("pbs-"+name+".pbx"),module.serialize());
return module;
}
@Test void numericValuesAndExplicitConversionsReachProductionPbx() throws Exception {
compile("numeric", """
declare service Probe {
fn widen(value: int) -> long { return value as long; }
fn contextual() -> long { return 1; }
fn receive(value: long) -> long { return value; }
fn main() -> long {
let large: long = 2147483648;
let maximum: long = 9223372036854775807;
Probe.contextual();
Probe.receive(2147483648);
return large + Probe.widen(7);
}
}
""");
compile("narrow", "declare service Probe { fn main() -> int { let value: long = 2147483648; return value as int; } }");
compile("float", "declare service Probe { fn main() -> float { return 1.5 + 2.25; } }");
compile("overflow", "declare service Probe { fn main() -> int { return 2147483647 + 1; } }");
}
@Test void allInputQueriesCompileFromThePublishedFacade() throws Exception {
var input=sdk("sdk/input");
for (var button : new String[]{"Up","Down","Left","Right","A","B","X","Y","L","R","Start","Select"}) {
for(var query:new String[]{"pressed","released","down","hold"}) {
var type=query.equals("hold")?"long":"bool";
compile("pad-"+button.toLowerCase()+"-"+query,input+"\ndeclare service Probe { fn select(button: Button) -> Button { return button; } fn main() -> "+type+" { let button = Probe.select(Button."+button+"); return Pad."+query+"(button); } }");
compile("pad-"+button.toLowerCase()+"-"+query+"-constant", input+"\ndeclare service Probe { fn main() -> "+type+" { return Pad."+query+"(Button."+button+"); } }");
}
}
for(var query:new String[]{"pressed","released","down","hold","x","y"}) {
var type=java.util.Set.of("hold","x","y").contains(query)?"long":"bool";
compile("touch-"+query,input+"\ndeclare service Probe { fn main() -> "+type+" { return Touch."+query+"(); } }");
}
}
@Test void completeBuiltinAndTupleGroupsReachProductionPbx() throws Exception {
compile("vec2", """
[BuiltinType(name = "vec2", version = 1)]
declare builtin type Vector(pub x: float, pub y: float) {}
[BuiltinConst(target = "vec2", name = "zero", version = 1)]
declare const ZERO: Vector;
declare service Probe {
fn echo(value: Vector) -> Vector { return value; }
fn main() -> Vector { let vector = Probe.echo(ZERO); return vector; }
}
""");
compile("tuple", """
declare service Probe {
fn pair() -> (x: int, y: long) { return (x: 7, y: 8 as long); }
fn main() -> (x: int, y: long) { let pair = Probe.pair(); return pair; }
}
""");
compile("named", """
declare global trace: int = 0;
declare service Probe {
fn x() -> int { trace = trace * 10 + 1; return 10; }
fn y() -> int { trace = trace * 10 + 2; return 20; }
fn combine(x: int, y: int) -> int { return x * 100 + y; }
fn main() -> int {
trace = 0;
let value = Probe.combine apply (y: Probe.y(), x: Probe.x());
return trace * 10000 + value;
}
}
""");
compile("color", sdk("core/color") + "declare service Probe { fn echo(value: Color) -> Color { return value; } fn main() -> Color { return Probe.echo(RED); } }");
compile("pixel", sdk("core/color") + """
[BuiltinType(name = "pixel", version = 1)]
declare builtin type Pixel(pub x: long, pub y: long, pub color: Color) {}
declare service Probe {
fn echo(value: Pixel) -> Pixel { return value; }
fn main() -> Pixel { let value = new Pixel(12, -34, RED); return Probe.echo(value); }
}
""");
compile("string", "declare service Probe { fn echo(value: str) -> str { return value; } fn main() -> str { return Probe.echo(\"hello\"); } }");
compile("division", "declare service Probe { fn main() -> int { return 1 / 0; } }");
compile("minimum", "declare service Probe { fn main() -> long { return -9223372036854775808; } }");
}
@Test void wrongNumericAndButtonTypesAreRejected() throws Exception {
for(var expression:new String[]{"1","1 as long","true","Other.A","\"A\""}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),sdk("sdk/input")+"\ndeclare enum Other(A); declare service Probe { fn main() -> bool { return Pad.pressed("+expression+"); } }",diagnostics,SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.hasErrors());
}
for(var source:new String[]{"fn main() -> long { let x: long = 1; let y: int = 1; return x + y; }", "fn main() -> int { return 2147483648; }", "fn main() -> long { return 9223372036854775808; }"}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),source,diagnostics);
assertTrue(diagnostics.hasErrors());
}
}
}