added Intrinsecs as DenseTable

This commit is contained in:
bQUARKz 2026-03-07 18:37:16 +00:00
parent a79a19cd4f
commit 939896862c
Signed by: bquarkz
SSH Key Fingerprint: SHA256:Z7dgqoglWwoK6j6u4QC87OveEq74WOhFN+gitsxtkf8
5 changed files with 87 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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");
}
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,6 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.IntrinsicId;
public interface IntrinsicTableReader extends InternTableReader<IntrinsicId, IntrinsicReference> {
}

View File

@ -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));
}
}