summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-12-03 20:17:32 +0000
committerMichael Williamson <mike@zwobble.org>2026-12-03 20:17:32 +0000
commit6eebe3630f767458597c8f375f01e59a92a2855e (patch)
tree61cfd0caec4b92e233e1bb5f2f7cd90285d1d0fa
parent6aeb1c1fc7ba5e6b17c10d566fc700a49b815f28 (diff)
Minimal type-checking for call nodesmain
-rw-r--r--hobgoblin/src/ast/typed.hob8
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNode.java66
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java21
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNodeMatcher.java59
5 files changed, 153 insertions, 3 deletions
diff --git a/hobgoblin/src/ast/typed.hob b/hobgoblin/src/ast/typed.hob
index ecc1ca9..529f242 100644
--- a/hobgoblin/src/ast/typed.hob
+++ b/hobgoblin/src/ast/typed.hob
@@ -128,6 +128,7 @@ struct TypedReturnNode {
sum TypedExpressionNode {
variant TypedBinaryOperationNode;
+ variant TypedCallNode;
variant TypedFunctionExpressionNode;
variant TypedMemberAccessNode;
variant TypedNumberLiteralNode;
@@ -148,6 +149,13 @@ enum TypedBinaryOperator {
variant equals;
}
+struct TypedCallNode {
+ field receiver: TypedExpressionNode;
+ field args: List[TypedExpressionNode];
+ field type: Type;
+ field source: Source;
+}
+
struct TypedFunctionExpressionNode {
field params: List[TypedParamNode];
field body: TypedExpressionNode;
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNode.java
new file mode 100644
index 0000000..3782f9a
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNode.java
@@ -0,0 +1,66 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode imports
+
+public record TypedCallNode(
+ org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode receiver,
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> args,
+ 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.TypedCallNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.types.HobgoblinNativeTypes.arbitraryType(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode receiver,
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> args,
+ 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.TypedCallNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder withReceiver(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode receiver) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder withReceiver(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder receiver) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver.build(), args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder withArgs(java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> args) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder addArg(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode arg) {
+ var args = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode>(this.args);
+ args.add(arg);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder addArg(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder arg) {
+ var args = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode>(this.args);
+ args.add(arg.build());
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder withType(org.zwobble.hobgoblin.compiler.types.Type type) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder(receiver, args, type, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode body
+}
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 1b5eee1..04342d7 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.TypedFunctionExpressionNode, 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.TypedCallNode, 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/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
index f9eb689..96328a8 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -746,8 +746,8 @@ public class TypeChecker {
yield typeCheckBinaryOperation(binaryOperation, context);
}
- case UntypedCallNode _ -> {
- throw new UnsupportedOperationException("TODO");
+ case UntypedCallNode call -> {
+ yield typeCheckCall(call, context);
}
case UntypedFunctionExpressionNode functionExpression -> {
@@ -799,6 +799,23 @@ public class TypeChecker {
);
}
+ private static TypedExpressionNode typeCheckCall(
+ UntypedCallNode untyped,
+ TypeCheckerNamespaceContext context
+ ) {
+ // TODO: actually do some type-checking
+
+ var receiver = typeCheckExpression(untyped.receiver(), Optional.empty(), context);
+
+ var receiverType = (FunctionType) receiver.type();
+
+ var args = untyped.args().stream()
+ .map(untypedArg -> typeCheckExpression(untypedArg, Optional.empty(), context))
+ .toList();
+
+ return new TypedCallNode(receiver, args, receiverType.returns(), untyped.source());
+ }
+
private static TypedExpressionNode typeCheckFunctionExpression(
UntypedFunctionExpressionNode untyped,
Optional<Type> typeHint,
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNodeMatcher.java
new file mode 100644
index 0000000..98a31ef
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedCallNodeMatcher.java
@@ -0,0 +1,59 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher imports
+
+public final class TypedCallNodeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher isTypedCallNode() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>> submatchers;
+
+ private TypedCallNodeMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher withReceiver(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> receiver) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("receiver", org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode::receiver, (org.zwobble.precisely.Matcher) receiver));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher withArgs(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode>> args) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("args", org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode::args, (org.zwobble.precisely.Matcher) args));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.types.Type> type) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode::type, (org.zwobble.precisely.Matcher) type));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher withSource(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.sources.Source> source) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNode::source, (org.zwobble.precisely.Matcher) source));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedCallNodeMatcher body
+}