added Intrinsecs as DenseTable
This commit is contained in:
parent
a79a19cd4f
commit
939896862c
@ -0,0 +1,17 @@
|
|||||||
|
package p.studio.compiler.source.identifiers;
|
||||||
|
|
||||||
|
public class IntrinsicId extends SourceIdentifier {
|
||||||
|
public static final IntrinsicId NONE = new IntrinsicId(-1);
|
||||||
|
|
||||||
|
public IntrinsicId(final int id) {
|
||||||
|
super(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IntrinsicId none() {
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNone() {
|
||||||
|
return this == NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package p.studio.compiler.source.tables;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public record IntrinsicReference(
|
||||||
|
String canonicalName,
|
||||||
|
long canonicalVersion) {
|
||||||
|
|
||||||
|
public IntrinsicReference {
|
||||||
|
canonicalName = Objects.requireNonNull(canonicalName, "canonicalName");
|
||||||
|
if (canonicalName.isBlank()) {
|
||||||
|
throw new IllegalArgumentException("canonicalName must not be blank");
|
||||||
|
}
|
||||||
|
if (canonicalVersion < 0) {
|
||||||
|
throw new IllegalArgumentException("canonicalVersion must be non-negative");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package p.studio.compiler.source.tables;
|
||||||
|
|
||||||
|
import p.studio.compiler.source.identifiers.IntrinsicId;
|
||||||
|
|
||||||
|
public class IntrinsicTable extends InternTable<IntrinsicId, IntrinsicReference> implements IntrinsicTableReader {
|
||||||
|
|
||||||
|
public IntrinsicTable() {
|
||||||
|
super(IntrinsicId::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntrinsicId register(
|
||||||
|
final String canonicalName,
|
||||||
|
final long canonicalVersion) {
|
||||||
|
return register(new IntrinsicReference(canonicalName, canonicalVersion));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
package p.studio.compiler.source.tables;
|
||||||
|
|
||||||
|
import p.studio.compiler.source.identifiers.IntrinsicId;
|
||||||
|
|
||||||
|
public interface IntrinsicTableReader extends InternTableReader<IntrinsicId, IntrinsicReference> {
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
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;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class IntrinsicTableTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldInternEqualIntrinsicReferencesToSameIdentifier() {
|
||||||
|
final var table = new IntrinsicTable();
|
||||||
|
|
||||||
|
final var first = table.register("core.color.pack", 1);
|
||||||
|
final var second = table.register("core.color.pack", 1);
|
||||||
|
final var third = table.register("core.input.poll", 1);
|
||||||
|
|
||||||
|
assertEquals(first, second);
|
||||||
|
assertEquals(2, table.size());
|
||||||
|
assertTrue(table.containsKey(new IntrinsicReference("core.color.pack", 1)));
|
||||||
|
assertEquals("core.input.poll", table.get(third).canonicalName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRejectInvalidIntrinsicReferenceContract() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new IntrinsicReference("", 1));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new IntrinsicReference("core.color.pack", -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user