implements PR-05.0.4

This commit is contained in:
2026-03-24 13:42:29 +00:00
parent 0149b85e7d
commit 19f293d8ea
10 changed files with 221 additions and 51 deletions
@@ -0,0 +1,7 @@
package p.studio.compiler.source.identifiers;
public class CallableShapeId extends SourceIdentifier {
public CallableShapeId(final int id) {
super(id);
}
}
@@ -0,0 +1,7 @@
package p.studio.compiler.source.identifiers;
public class TypeSurfaceId extends SourceIdentifier {
public TypeSurfaceId(final int id) {
super(id);
}
}
@@ -0,0 +1,15 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.TypeSurfaceId;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.Objects;
public record CallableShapeRef(
ReadOnlyList<TypeSurfaceId> parameterTypeSurfaces,
TypeSurfaceId outputTypeSurface) {
public CallableShapeRef {
parameterTypeSurfaces = parameterTypeSurfaces == null ? ReadOnlyList.empty() : parameterTypeSurfaces;
outputTypeSurface = Objects.requireNonNull(outputTypeSurface, "outputTypeSurface");
}
}
@@ -0,0 +1,18 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.CallableShapeId;
import p.studio.compiler.source.identifiers.TypeSurfaceId;
import p.studio.utilities.structures.ReadOnlyList;
public class CallableShapeTable extends InternTable<CallableShapeId, CallableShapeRef> {
public CallableShapeTable() {
super(CallableShapeId::new);
}
public CallableShapeId register(
final ReadOnlyList<TypeSurfaceId> parameterTypeSurfaces,
final TypeSurfaceId outputTypeSurface) {
return register(new CallableShapeRef(parameterTypeSurfaces, outputTypeSurface));
}
}
@@ -0,0 +1,13 @@
package p.studio.compiler.source.tables;
import java.util.Objects;
public record TypeSurfaceRef(
String surface) {
public TypeSurfaceRef {
surface = Objects.requireNonNull(surface, "surface");
if (surface.isBlank()) {
throw new IllegalArgumentException("surface must not be blank");
}
}
}
@@ -0,0 +1,14 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.TypeSurfaceId;
public class TypeSurfaceTable extends InternTable<TypeSurfaceId, TypeSurfaceRef> {
public TypeSurfaceTable() {
super(TypeSurfaceId::new);
}
public TypeSurfaceId register(final String surface) {
return register(new TypeSurfaceRef(surface));
}
}
@@ -0,0 +1,25 @@
package p.studio.compiler.source.tables;
import org.junit.jupiter.api.Test;
import p.studio.utilities.structures.ReadOnlyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CallableShapeTableTest {
@Test
void shouldInternEqualShapesToSameIdentifier() {
final var surfaces = new TypeSurfaceTable();
final var shapes = new CallableShapeTable();
final var intType = surfaces.register("simple:int");
final var unitType = surfaces.register("unit");
final var first = shapes.register(ReadOnlyList.from(intType, intType), unitType);
final var second = shapes.register(ReadOnlyList.from(intType, intType), unitType);
final var third = shapes.register(ReadOnlyList.from(intType), unitType);
assertEquals(first, second);
assertEquals(2, shapes.size());
assertEquals(1, shapes.get(third).parameterTypeSurfaces().size());
}
}
@@ -0,0 +1,27 @@
package p.studio.compiler.source.tables;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class TypeSurfaceTableTest {
@Test
void shouldInternEqualSurfacesToSameIdentifier() {
final var table = new TypeSurfaceTable();
final var first = table.register("simple:int");
final var second = table.register("simple:int");
final var third = table.register("result<error>simple:int");
assertEquals(first, second);
assertEquals(2, table.size());
assertEquals("result<error>simple:int", table.get(third).surface());
}
@Test
void shouldRejectInvalidSurfaceContract() {
assertThrows(IllegalArgumentException.class, () -> new TypeSurfaceRef(""));
}
}