aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-02-05 18:48:01 +0000
committerMichael Williamson <mike@zwobble.org>2025-02-05 18:48:01 +0000
commit765d7579b96f558ffc6232b707d471850664a11a (patch)
tree1deb393601bb77d8adf10a6c1c2b8eda30c2585d /src/main/java
parent89394d0fab076765b637b0fe643d8f8950639817 (diff)
Add native types
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java13
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructedType.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructibleType.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/NativeType.java5
-rw-r--r--src/main/java/org/shedlang/compiler/types/Scope.java1
-rw-r--r--src/main/java/org/shedlang/compiler/types/SimpleNativeType.java13
-rw-r--r--src/main/java/org/shedlang/compiler/types/Type.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeParam.java4
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeSubstitution.java17
-rw-r--r--src/main/java/org/shedlang/compiler/types/Types.java13
10 files changed, 69 insertions, 3 deletions
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java b/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java
new file mode 100644
index 00000000..24653b3d
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java
@@ -0,0 +1,13 @@
+package org.shedlang.compiler.types;
+
+import java.util.List;
+
+public record ConstructedNativeType(
+ TypeConstructor<SimpleNativeType> constructor,
+ List<? extends TypeLevelValue> args
+) implements ConstructedType<SimpleNativeType>, NativeType {
+ @Override
+ public SimpleNativeType rawType() {
+ return genericType();
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedType.java b/src/main/java/org/shedlang/compiler/types/ConstructedType.java
index e49b3520..8a51d6b0 100644
--- a/src/main/java/org/shedlang/compiler/types/ConstructedType.java
+++ b/src/main/java/org/shedlang/compiler/types/ConstructedType.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.types;
import java.util.List;
import java.util.stream.Collectors;
-public sealed interface ConstructedType<T extends ConstructibleType> extends Type permits ConstructedStructType, ConstructedSumType {
+public sealed interface ConstructedType<T extends ConstructibleType> extends Type permits ConstructedNativeType, ConstructedStructType, ConstructedSumType {
TypeConstructor<T> constructor();
List<? extends TypeLevelValue> args();
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructibleType.java b/src/main/java/org/shedlang/compiler/types/ConstructibleType.java
index 164b2b8d..c9283467 100644
--- a/src/main/java/org/shedlang/compiler/types/ConstructibleType.java
+++ b/src/main/java/org/shedlang/compiler/types/ConstructibleType.java
@@ -1,5 +1,5 @@
package org.shedlang.compiler.types;
-public sealed interface ConstructibleType extends Type permits SimpleStructType, SimpleSumType {
+public sealed interface ConstructibleType extends Type permits SimpleNativeType, SimpleStructType, SimpleSumType {
String name();
}
diff --git a/src/main/java/org/shedlang/compiler/types/NativeType.java b/src/main/java/org/shedlang/compiler/types/NativeType.java
new file mode 100644
index 00000000..815b8274
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/types/NativeType.java
@@ -0,0 +1,5 @@
+package org.shedlang.compiler.types;
+
+public sealed interface NativeType extends Type permits ConstructedNativeType, SimpleNativeType {
+ SimpleNativeType rawType();
+}
diff --git a/src/main/java/org/shedlang/compiler/types/Scope.java b/src/main/java/org/shedlang/compiler/types/Scope.java
index b7fb0ecd..b0141916 100644
--- a/src/main/java/org/shedlang/compiler/types/Scope.java
+++ b/src/main/java/org/shedlang/compiler/types/Scope.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.types;
public sealed interface Scope {
record Function(NamespaceName namespaceName, String name) implements Scope {}
+ record Native(NativeType type) implements Scope {}
record Struct(SimpleStructType type) implements Scope {}
record Sum(SimpleSumType type) implements Scope {}
}
diff --git a/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java
new file mode 100644
index 00000000..a2acde62
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java
@@ -0,0 +1,13 @@
+package org.shedlang.compiler.types;
+
+public record SimpleNativeType(NamespaceName namespaceName, String name) implements ConstructibleType, NativeType {
+ @Override
+ public String describe() {
+ return namespaceName.toString() + "." + name;
+ }
+
+ @Override
+ public SimpleNativeType rawType() {
+ return this;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/types/Type.java b/src/main/java/org/shedlang/compiler/types/Type.java
index d2607906..a05304c8 100644
--- a/src/main/java/org/shedlang/compiler/types/Type.java
+++ b/src/main/java/org/shedlang/compiler/types/Type.java
@@ -6,5 +6,5 @@ package org.shedlang.compiler.types;
* found through auxiliary data.
*/
public sealed interface Type extends TypeLevelValue
- permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType {
+ permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, NativeType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType {
}
diff --git a/src/main/java/org/shedlang/compiler/types/TypeParam.java b/src/main/java/org/shedlang/compiler/types/TypeParam.java
index 45262cc4..6d311940 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeParam.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeParam.java
@@ -9,6 +9,10 @@ public record TypeParam(Scope scope, String name) implements Type, TypeLevelPara
return new TypeParam(new Scope.Function(namespaceName, functionName), name);
}
+ public static TypeParam forNativeType(SimpleNativeType nativeType, String name) {
+ return new TypeParam(new Scope.Native(nativeType), name);
+ }
+
public static TypeParam forStructType(SimpleStructType structType, String name) {
return new TypeParam(new Scope.Struct(structType), name);
}
diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
index 1aca81c7..cefe3d0f 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
@@ -39,6 +39,9 @@ public class TypeSubstitution {
case BottomType bottomType ->
type;
+ case ConstructedNativeType constructedNativeType ->
+ substituteConstructedNativeType(constructedNativeType, map);
+
case ConstructedStructType constructedType ->
substituteConstructedStructType(constructedType, map);
@@ -54,6 +57,9 @@ public class TypeSubstitution {
case NamespaceType namespaceType ->
type;
+ case NativeType nativeType ->
+ type;
+
case StringType stringType ->
type;
@@ -89,6 +95,17 @@ public class TypeSubstitution {
};
}
+ private static ConstructedNativeType substituteConstructedNativeType(
+ ConstructedNativeType constructedType,
+ TypeSubstitutionMap map
+ ) {
+ return substituteConstructedType(
+ ConstructedNativeType::new,
+ constructedType,
+ map
+ );
+ }
+
private static ConstructedStructType substituteConstructedStructType(
ConstructedStructType constructedType,
TypeSubstitutionMap map
diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java
index a8c3aef5..ea4e7053 100644
--- a/src/main/java/org/shedlang/compiler/types/Types.java
+++ b/src/main/java/org/shedlang/compiler/types/Types.java
@@ -21,6 +21,10 @@ public class Types {
return new NamespaceType(namespaceName);
}
+ public static SimpleNativeType nativeType(NamespaceName namespaceName, String name) {
+ return new SimpleNativeType(namespaceName, name);
+ }
+
public static SimpleStructType struct(NamespaceName namespaceName, String name) {
return new SimpleStructType(namespaceName, name);
}
@@ -62,6 +66,8 @@ public class Types {
List<? extends TypeLevelValue> args
) {
return switch (constructor.genericType()) {
+ case SimpleNativeType nativeType ->
+ constructedNativeType((TypeConstructor<SimpleNativeType>) constructor, args);
case SimpleStructType structType ->
constructedStructType((TypeConstructor<SimpleStructType>) constructor, args);
case SimpleSumType sumType ->
@@ -69,6 +75,13 @@ public class Types {
};
}
+ public static ConstructedNativeType constructedNativeType(
+ TypeConstructor<SimpleNativeType> constructor,
+ List<? extends TypeLevelValue> args
+ ) {
+ return new ConstructedNativeType(constructor, args);
+ }
+
public static ConstructedStructType constructedStructType(
TypeConstructor<SimpleStructType> constructor,
List<? extends TypeLevelValue> args