summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-12-01 21:31:42 +0000
committerMichael Williamson <mike@zwobble.org>2026-12-01 21:31:42 +0000
commit6aeb1c1fc7ba5e6b17c10d566fc700a49b815f28 (patch)
tree74bcdfe2fa879d7db11fb33a63412099ab29a1ad
parent8ceb2f66323b716aea45b3c908d4393eef7c72d5 (diff)
Don't eval in type checkermain
-rw-r--r--hobgoblin/src/ast/typed.hob2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNode.java30
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java38
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java7
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java17
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java41
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedQueryDefinitionNodeMatcher.java4
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java13
8 files changed, 97 insertions, 55 deletions
diff --git a/hobgoblin/src/ast/typed.hob b/hobgoblin/src/ast/typed.hob
index 7b2b146..ecc1ca9 100644
--- a/hobgoblin/src/ast/typed.hob
+++ b/hobgoblin/src/ast/typed.hob
@@ -60,7 +60,7 @@ struct TypedNativeTypeDefinitionNode {
struct TypedQueryDefinitionNode {
field name: String;
- field relation: Relation;
+ field statements: List[TypedStatementNode];
field docComment: DocComment;
field source: Source;
}
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 dc61d28..b154528 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,42 +7,50 @@ package org.zwobble.hobgoblin.compiler.ast.typed;
public record TypedQueryDefinitionNode(
String name,
- org.zwobble.hobgoblin.compiler.ast.typed.Relation relation,
+ 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("", org.zwobble.hobgoblin.compiler.ast.typed.RelationTable.arbitrary().build(), 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(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
String name,
- org.zwobble.hobgoblin.compiler.ast.typed.Relation relation,
+ 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, relation, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode(name, 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, relation, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, statements, docComment, source);
}
- public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withRelation(org.zwobble.hobgoblin.compiler.ast.typed.Relation relation) {
- return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, relation, 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);
}
- public org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder withRelation(org.zwobble.hobgoblin.compiler.ast.typed.Relation.Builder relation) {
- return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, relation.build(), 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);
+ }
+
+ 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);
}
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, relation, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, 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, relation, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode.Builder(name, 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/interpreter/Interpreter.java b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java
new file mode 100644
index 0000000..445a43d
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java
@@ -0,0 +1,38 @@
+package org.zwobble.hobgoblin.compiler.interpreter;
+
+import org.zwobble.hobgoblin.compiler.ast.typed.*;
+import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType;
+
+import java.util.List;
+
+public class Interpreter<TValue> {
+ private final InterpreterConfig<TValue> config;
+
+ public Interpreter(InterpreterConfig<TValue> config) {
+ this.config = config;
+ }
+
+ public TValue evalStatements(
+ List<TypedStatementNode> statements
+ ) {
+ for (var statement : statements) {
+ switch (statement) {
+ case TypedReturnNode return_ -> {
+ return evalExpression(return_.expression());
+ }
+ }
+ }
+
+ throw new UnsupportedOperationException("TODO: missing return");
+ }
+
+ public TValue evalExpression(
+ TypedExpressionNode expression
+ ) {
+ if (!(expression.type() instanceof TypeLevelValueType typeLevelValue)) {
+ throw new UnsupportedOperationException("TODO");
+ }
+
+ return config.typeLevelValueToValue(typeLevelValue.value());
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java
new file mode 100644
index 0000000..835cb31
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java
@@ -0,0 +1,7 @@
+package org.zwobble.hobgoblin.compiler.interpreter;
+
+import org.zwobble.hobgoblin.compiler.types.TypeLevelValue;
+
+public interface InterpreterConfig<TValue> {
+ TValue typeLevelValueToValue(TypeLevelValue typeLevelValue);
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java
index 4a23fa1..fd4ca0a 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java
@@ -1,15 +1,16 @@
package org.zwobble.hobgoblin.compiler.output.generators.sql;
-import org.zwobble.hobgoblin.compiler.ast.typed.Relation;
-import org.zwobble.hobgoblin.compiler.ast.typed.RelationTable;
-import org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode;
-import org.zwobble.hobgoblin.compiler.ast.typed.TypedTableDefinitionNode;
+import org.zwobble.hobgoblin.compiler.ast.typed.*;
import org.zwobble.hobgoblin.compiler.builtins.NativeTypes;
import org.zwobble.hobgoblin.compiler.errors.InternalError;
+import org.zwobble.hobgoblin.compiler.interpreter.Interpreter;
+import org.zwobble.hobgoblin.compiler.interpreter.InterpreterConfig;
import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.*;
import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo;
import org.zwobble.hobgoblin.compiler.types.SimpleStructType;
+import org.zwobble.hobgoblin.compiler.types.Table;
import org.zwobble.hobgoblin.compiler.types.Type;
+import org.zwobble.hobgoblin.compiler.types.TypeLevelValue;
import org.zwobble.hobgoblin.compiler.util.Casing;
import java.util.List;
@@ -58,7 +59,13 @@ public class SqlGenerator {
TypedQueryDefinitionNode queryDefinition,
TypesInfo typesInfo
) {
- return generateRelation(queryDefinition.relation(), typesInfo);
+ var interpreter = new Interpreter<>(new InterpreterConfig<SqlStatement>() {
+ @Override
+ public SqlStatement typeLevelValueToValue(TypeLevelValue typeLevelValue) {
+ return generateRelation(new RelationTable((Table) typeLevelValue), typesInfo);
+ }
+ });
+ return interpreter.evalStatements(queryDefinition.statements());
}
SqlSelectStatement generateRelation(Relation relation, TypesInfo typesInfo) {
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 3795f4a..f9eb689 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -300,12 +300,16 @@ public class TypeChecker {
UntypedQueryDefinitionNode untyped,
TypeCheckerNamespaceContext context
) {
- var value = evalStatements(untyped.statements(), context);
- var table = (Table) value;
+ // TODO: check return type from query
+ // TODO: check there is a return
+
+ var typedStatements = untyped.statements().stream()
+ .map(untypedStatement -> typeCheckStatement(untypedStatement, context))
+ .toList();
return new TypedQueryDefinitionNode(
untyped.name(),
- new RelationTable(table),
+ typedStatements,
untyped.docComment(),
untyped.source()
);
@@ -703,22 +707,6 @@ public class TypeChecker {
// == Statements ==
- private static TypeLevelValue evalStatements(
- List<UntypedStatementNode> statements,
- TypeCheckerNamespaceContext context
- ) {
- for (var statement : statements) {
- switch (statement) {
- case UntypedReturnNode untypedReturn -> {
- // TODO: test multiple returns.
- return evalExpression(untypedReturn.expression(), context);
- }
- }
- }
-
- throw new UnsupportedOperationException("TODO");
- }
-
static TypedStatementNode typeCheckStatement(
UntypedStatementNode untyped,
TypeCheckerNamespaceContext context
@@ -811,21 +799,6 @@ public class TypeChecker {
);
}
- static TypeLevelValue evalExpression(
- UntypedExpressionNode expression,
- TypeCheckerNamespaceContext context
- ) {
- var typed = typeCheckExpression(expression, Optional.empty(), context);
-
- // TODO: handle not a type-level value
- var type = typed.type();
- if (!(type instanceof TypeLevelValueType(TypeLevelValue value))) {
- throw new UnexpectedTypeError(new TypeSet.TypeLevelValue(), type, expression.source());
- }
-
- return value;
- }
-
private static TypedExpressionNode typeCheckFunctionExpression(
UntypedFunctionExpressionNode untyped,
Optional<Type> typeHint,
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 d9a6bc5..fb6037e 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,9 +36,9 @@ 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 withRelation(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.Relation> relation) {
+ 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("relation", org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode::relation, (org.zwobble.precisely.Matcher) relation));
+ submatchers.add(org.zwobble.precisely.Matchers.has("statements", org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode::statements, (org.zwobble.precisely.Matcher) statements));
return new org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNodeMatcher(submatchers);
}
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 e28e2d5..2e123fa 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java
@@ -10,11 +10,13 @@ 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.RelationTableMatcher.isRelationTable;
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;
import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
+import static org.zwobble.precisely.Matchers.isSequence;
public class TypeCheckerQueryDefinitionTests {
@Test
@@ -37,7 +39,14 @@ public class TypeCheckerQueryDefinitionTests {
assertThat(typed, isTypedQueryDefinitionNode()
.withName(equalTo("allAuthors"))
- .withRelation(isRelationTable().withTable(equalTo(table)))
+ .withStatements(isSequence(
+ isTypedReturnNode()
+ .withExpression(
+ isTypedReferenceNode()
+ .withName(equalTo("authors"))
+ .withType(equalTo(Types.table(table)))
+ )
+ ))
);
}
}