feat(pbs): integrate explicit runtime Random handles

This commit is contained in:
2026-10-17 13:51:12 +01:00
parent d1e9259085
commit b62db87245
22 changed files with 895 additions and 8 deletions
@@ -175,7 +175,7 @@ final class PbsRuntimeTypes {
}
int id = switch(canonical(t)) {
case "int" -> 0x10; case "long" -> 0x11; case "float" -> 0x12;
case "bool" -> 0x13; case "str" -> 0x14;
case "bool" -> 0x13; case "str" -> 0x14; case "Random" -> 0x16;
case "color" -> 1; case "vec2" -> 2; case "pixel" -> 3;
default -> { if(enums.containsKey(t.name())) yield 0x10;
throw new UnsupportedOperationException("Unsupported PBS runtime type: "+t.name()); }
@@ -612,7 +612,7 @@ final class PbsDeclarationParser {
}
private PbsToken consumeCallableName() {
if (cursor.check(PbsTokenKind.IDENTIFIER)) {
if (cursor.check(PbsTokenKind.IDENTIFIER) || cursor.check(PbsTokenKind.NEW)) {
return cursor.advance();
}
final var token = cursor.peek();
@@ -41,7 +41,7 @@ final class PbsExprParserContext {
}
PbsToken consumeMemberName(final String message) {
if (cursor.check(PbsTokenKind.IDENTIFIER)) {
if (cursor.check(PbsTokenKind.IDENTIFIER) || cursor.check(PbsTokenKind.NEW)) {
return cursor.advance();
}
final var token = cursor.peek();
@@ -73,6 +73,19 @@ final class PbsFlowExpressionAnalyzer {
private ExprResult analyzeExpressionInternal(
final PbsAst.Expression expression,
final PbsFlowExpressionContext context) {
var result = analyzeExpressionUnchecked(expression, context);
if (context.use() == ExprUse.VALUE && context.expectedType() != null
&& (context.model().isOpaqueBuiltin(result.type()) || context.model().isOpaqueBuiltin(context.expectedType()))
&& !typeOps.compatible(result.type(), context.expectedType())) {
p.studio.compiler.source.diagnostics.Diagnostics.error(context.diagnostics(), "E_SEM_OPAQUE_BUILTIN_TYPE_MISMATCH",
"Opaque builtin references require their exact declared type", expression.span());
}
return result;
}
private ExprResult analyzeExpressionUnchecked(
final PbsAst.Expression expression,
final PbsFlowExpressionContext context) {
if (expression instanceof PbsAst.LambdaExpr lambda) return analyzeLambda(lambda, context);
final var structural = structuralExpressionAnalyzer.analyze(expression, context);
if (structural != null) {
@@ -240,6 +240,12 @@ final class PbsFlowSemanticSupport {
return model;
}
boolean isOpaqueBuiltin(TypeView type) {
if (type == null || !knownBuiltinTypeNames.contains(type.name())) return false;
var declaration = structs.get(type.name());
return declaration != null && declaration.fields().isEmpty();
}
private void registerKnownTopDecl(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
knownStructNames.add(structDecl.name());
@@ -209,6 +209,12 @@ final class PbsFlowStructuralExpressionAnalyzer {
return ExprResult.type(TypeView.optional(payloadExpected == null ? payload : payloadExpected));
}
if (expression instanceof PbsAst.NewExpr newExpr) {
// Fieldless builtins are opaque provider references, not source constructors.
if (model.isOpaqueBuiltin(TypeView.struct(newExpr.typeName()))) {
Diagnostics.error(diagnostics, "E_SEM_OPAQUE_BUILTIN_CONSTRUCTION",
"Opaque builtin must be created by its provider operation", newExpr.span());
return ExprResult.type(TypeView.unknown());
}
if (model.structs.containsKey(newExpr.typeName())) {
return ExprResult.type(TypeView.struct(newExpr.typeName()));
}
@@ -223,6 +229,10 @@ final class PbsFlowStructuralExpressionAnalyzer {
return ExprResult.type(typeOps.typeFromTypeRef(PbsAst.TypeRef.simple(asExpr.contractName(), asExpr.span()), model, null));
}
if (model.contracts.containsKey(asExpr.contractName())) {
if (model.isOpaqueBuiltin(source)) {
Diagnostics.error(diagnostics, "E_SEM_OPAQUE_BUILTIN_TYPE_MISMATCH",
"Opaque builtin references cannot adapt to contracts", asExpr.span());
}
return ExprResult.type(TypeView.contract(asExpr.contractName()));
}
return ExprResult.type(TypeView.unknown());
@@ -0,0 +1,20 @@
[Doc(markdown = """
Opaque runtime-owned generator. Copies share state; equal seeds create independent streams.
""")]
[BuiltinType(name = "Random", version = 1)]
declare builtin type Random() {}
[Doc(markdown = """
Deterministic stateful random operations. Every draw advances the same generator.
Use explicit long seeds; no host entropy or implicit global generator is used.
""")]
declare service random {
[IntrinsicCall(name = "random.new", version = 1)]
fn new(seed: long) -> Random;
[IntrinsicCall(name = "random.next", version = 1)]
fn next(rng: Random) -> long;
[IntrinsicCall(name = "random.nextf", version = 1)]
fn nextf(rng: Random) -> float;
[IntrinsicCall(name = "random.range", version = 1)]
fn range(rng: Random, min: long, max: long) -> long;
}
@@ -0,0 +1,2 @@
pub struct Random;
pub service random;
@@ -0,0 +1,9 @@
import { Random, random } from @sdk:random;
// Copies share the same sequence. Call random.new again for an independent stream.
fn randomExample() -> long {
let generator: Random = random.new(42 as long);
let alias = generator;
random.next(generator);
return random.range(alias, 0 as long, 100 as long);
}