implements PR-O4.1
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package p.studio.compiler.source.identifiers;
|
||||
|
||||
public class CallableId extends SourceIdentifier {
|
||||
public static final CallableId NONE = new CallableId(-1);
|
||||
|
||||
public CallableId(final int id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public static CallableId none() {
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package p.studio.compiler.source.tables;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record CallableSignatureRef(
|
||||
String moduleKey,
|
||||
String callableName,
|
||||
int arity,
|
||||
String typeShape) {
|
||||
|
||||
public CallableSignatureRef {
|
||||
moduleKey = moduleKey == null ? "" : moduleKey;
|
||||
callableName = Objects.requireNonNull(callableName, "callableName");
|
||||
typeShape = typeShape == null ? "" : typeShape;
|
||||
if (callableName.isBlank()) {
|
||||
throw new IllegalArgumentException("callableName must not be blank");
|
||||
}
|
||||
if (arity < 0) {
|
||||
throw new IllegalArgumentException("arity must be non-negative");
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package p.studio.compiler.source.tables;
|
||||
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
|
||||
public class CallableTable extends InternTable<CallableId, CallableSignatureRef> implements CallableTableReader {
|
||||
|
||||
public CallableTable() {
|
||||
super(CallableId::new);
|
||||
}
|
||||
|
||||
public CallableId register(
|
||||
final String moduleKey,
|
||||
final String callableName,
|
||||
final int arity,
|
||||
final String typeShape) {
|
||||
return register(new CallableSignatureRef(moduleKey, callableName, arity, typeShape));
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package p.studio.compiler.source.tables;
|
||||
|
||||
import p.studio.compiler.source.identifiers.CallableId;
|
||||
|
||||
public interface CallableTableReader extends InternTableReader<CallableId, CallableSignatureRef> {
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
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 CallableTableTest {
|
||||
|
||||
@Test
|
||||
void shouldInternEqualCallableSignaturesToSameIdentifier() {
|
||||
final var table = new CallableTable();
|
||||
|
||||
final var first = table.register("app/main", "draw", 2, "(simple:int,simple:int)->unit");
|
||||
final var second = table.register("app/main", "draw", 2, "(simple:int,simple:int)->unit");
|
||||
final var third = table.register("app/main", "draw", 1, "(simple:int)->unit");
|
||||
|
||||
assertEquals(first, second);
|
||||
assertEquals(2, table.size());
|
||||
assertEquals(1, table.get(third).arity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidCallableSignatureReferenceContract() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new CallableSignatureRef("app/main", "", 1, "(unit)->unit"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new CallableSignatureRef("app/main", "draw", -1, "(unit)->unit"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user