diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:42:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-12-05 21:42:53 +0000 |
| commit | d536d0535af4a3b88ec356a6d1b561d6942334f2 (patch) | |
| tree | 331a3084f4fb492a3778de2a7a8141935b9ccb06 /src | |
| parent | 5a646ae3ae92b9756e005b38e40f9fd8476cdc7c (diff) | |
Add params to typed query definition nodes
Diffstat (limited to 'src')
4 files changed, 41 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNode.java index b154528..05ef9c9 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNode.java @@ -7,50 +7,68 @@ package org.zwobble.hobgoblin.compiler.ast.typed; public record TypedQueryDefinitionNode( String name, + java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params, java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode> statements, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source ) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceStatementNode { public static org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder("", java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder("", java.util.List.of(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); } public record Builder( String name, + java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params, java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode> statements, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source ) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceStatementNode.Builder { public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode build() { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withName(String name) { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withParams(java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode> params) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.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.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.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.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withStatements(java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode> statements) { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder addStatement(org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode statement) { var statements = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode>(this.statements); statements.add(statement); - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder addStatement(org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode.Builder statement) { var statements = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode>(this.statements); statements.add(statement.build()); - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) { - return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, params, statements, docComment, source); } // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder 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 abeb74d..debcc63 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -303,9 +303,11 @@ public class TypeChecker { // TODO: check return type from query // TODO: check there is a return + var typedParams = new ArrayList<TypedParamNode>(); var bodyContextDeclaring = context.enter(); for (var untypedParam : untyped.params()) { var typedParam = typeCheckMetaType(untypedParam.type(), context); + typedParams.add(new TypedParamNode(untypedParam.name(), typedParam.value(), untyped.source())); bodyContextDeclaring.define(untypedParam.name(), typedParam.value(), untypedParam.source()); } var bodyContext = bodyContextDeclaring.endDeclarations(); @@ -316,6 +318,7 @@ public class TypeChecker { return new TypedQueryDefinitionNode( untyped.name(), + typedParams, typedStatements, untyped.docComment(), untyped.source() diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNodeMatcher.java index fb6037e..133368f 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNodeMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNodeMatcher.java @@ -36,6 +36,12 @@ public final class TypedQueryDefinitionNodeMatcher implements org.zwobble.precis return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher withParams(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode>> params) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode::params, (org.zwobble.precisely.Matcher) params)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher withStatements(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.ast.typed.TypedStatementNode>> statements) { var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode>>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("statements", org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode::statements, (org.zwobble.precisely.Matcher) statements)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java index c8344c2..c016bc1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java @@ -9,6 +9,7 @@ import org.zwobble.hobgoblin.compiler.types.NamespaceName; import org.zwobble.hobgoblin.compiler.types.Table; import org.zwobble.hobgoblin.compiler.types.Types; +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher.isTypedParamNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher.isTypedQueryDefinitionNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReturnNodeMatcher.isTypedReturnNode; @@ -71,6 +72,11 @@ public class TypeCheckerQueryDefinitionTests { assertThat(typed, isTypedQueryDefinitionNode() .withName(equalTo("q")) + .withParams(isSequence( + isTypedParamNode() + .withName(equalTo("a")) + .withType(equalTo(NativeTypes.STRING)) + )) .withStatements(isSequence( isTypedReturnNode() .withExpression( |
