summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-11-29 11:03:05 +0000
committerMichael Williamson <mike@zwobble.org>2026-11-29 11:03:05 +0000
commitdaf09ab1ba05768625fa7beb370f73e5021d53a1 (patch)
tree19df32237ddb650da4a578c4e96ae1744986ba3b /src/main/java
parent2dd1a64b5fe1c0ff276008a3655c472fb132f8c3 (diff)
Type check body of function expression
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java66
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java44
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java20
4 files changed, 129 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java
index 887b2b4..1b5eee1 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java
@@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.ast.typed;
// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode imports
// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode imports
-public sealed interface TypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNode {
+public sealed interface TypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNode {
public interface Builder {
public org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java
new file mode 100644
index 0000000..0c6652d
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java
@@ -0,0 +1,66 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode imports
+
+public record TypedFunctionExpressionNode(
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params,
+ org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body,
+ org.zwobble.hobgoblin.compiler.types.Type type,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode {
+ public static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.types.HobgoblinNativeTypes.arbitraryType(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params,
+ org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body,
+ org.zwobble.hobgoblin.compiler.types.Type type,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+ ) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder {
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withParams(java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder addParam(org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode param) {
+ var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode>(this.params);
+ params.add(param);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder addParam(org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder param) {
+ var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode>(this.params);
+ params.add(param.build());
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withBody(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withBody(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder body) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body.build(), type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withType(org.zwobble.hobgoblin.compiler.types.Type type) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java
new file mode 100644
index 0000000..cf4f6aa
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java
@@ -0,0 +1,44 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode imports
+
+public record TypedParamNode(
+ String name,
+ org.zwobble.hobgoblin.compiler.types.Type type,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+) {
+ public static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder("", org.zwobble.hobgoblin.compiler.types.HobgoblinNativeTypes.arbitraryType(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ String name,
+ org.zwobble.hobgoblin.compiler.types.Type type,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+ ) {
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode(name, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withType(org.zwobble.hobgoblin.compiler.types.Type type) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
index 741b8d4..d4b9c1c 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -714,8 +714,8 @@ public class TypeChecker {
throw new UnsupportedOperationException("TODO");
}
- case UntypedFunctionExpressionNode _ -> {
- throw new UnsupportedOperationException("TODO");
+ case UntypedFunctionExpressionNode functionExpression -> {
+ yield typeCheckFunctionExpression(functionExpression, typeHint, context);
}
case UntypedMemberAccessNode memberAccess -> {
@@ -778,6 +778,22 @@ public class TypeChecker {
return value;
}
+ private static TypedExpressionNode typeCheckFunctionExpression(
+ UntypedFunctionExpressionNode untyped,
+ Optional<Type> typeHint,
+ TypeCheckerNamespaceContext context
+ ) {
+ var body = typeCheckExpression(untyped.body(), Optional.empty(), context);
+
+ return new TypedFunctionExpressionNode(
+ List.of(),
+ body,
+ // TODO: proper type
+ NativeTypes.INT_32,
+ untyped.source()
+ );
+ }
+
private static TypedExpressionNode typeCheckMemberAccess(
UntypedMemberAccessNode untyped,
TypeCheckerNamespaceContext context