From 8b6c82d1114320eeca2695f4e2c1db9eca4ab361 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 5 Dec 2026 20:11:56 +0000 Subject: Add equality operation to SQL AST --- hobgoblin/src/output/lang/sql/ast.hob | 11 +++++ .../compiler/output/lang/sql/SqlWriter.java | 20 ++++++++ .../output/lang/sql/ast/SqlBinaryOperation.java | 52 +++++++++++++++++++++ .../output/lang/sql/ast/SqlBinaryOperator.java | 13 ++++++ .../output/lang/sql/ast/SqlExpression.java | 2 +- .../compiler/output/lang/sql/SqlWriterTests.java | 13 ++++++ .../lang/sql/ast/SqlBinaryOperationMatcher.java | 53 ++++++++++++++++++++++ 7 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperator.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperationMatcher.java diff --git a/hobgoblin/src/output/lang/sql/ast.hob b/hobgoblin/src/output/lang/sql/ast.hob index 16e7b64..c10699b 100644 --- a/hobgoblin/src/output/lang/sql/ast.hob +++ b/hobgoblin/src/output/lang/sql/ast.hob @@ -50,10 +50,21 @@ struct SqlPrimaryKeyConstraint { // == Expressions == sum SqlExpression { + variant SqlBinaryOperation; variant SqlIntLiteral; variant SqlRef; } +struct SqlBinaryOperation { + field operator: SqlBinaryOperator; + field left: SqlExpression; + field right: SqlExpression; +} + +enum SqlBinaryOperator { + variant equals; +} + struct SqlIntLiteral { field value: Int64; } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java index 7f0c17a..bc806c6 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java @@ -169,6 +169,10 @@ public class SqlWriter { void writeExpression(SqlExpression expression) throws IOException { switch (expression) { + case SqlBinaryOperation binaryOperation -> { + this.writeBinaryOperation(binaryOperation); + } + case SqlIntLiteral intLiteral -> { this.writeIntLiteral(intLiteral); } @@ -179,6 +183,22 @@ public class SqlWriter { } } + private void writeBinaryOperation(SqlBinaryOperation binaryOperation) throws IOException { + // TODO: handle bracketing/precedence + + this.writeExpression(binaryOperation.left()); + this.writer.write(" "); + this.writeBinaryOperator(binaryOperation.operator()); + this.writer.write(" "); + this.writeExpression(binaryOperation.right()); + } + + private void writeBinaryOperator(SqlBinaryOperator operator) throws IOException { + this.writer.write(switch (operator) { + case EQUALS -> "=="; + }); + } + private void writeIntLiteral(SqlIntLiteral intLiteral) throws IOException { this.writer.write(Long.toString(intLiteral.value())); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java new file mode 100644 index 0000000..8bdde89 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java @@ -0,0 +1,52 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation imports + +public record SqlBinaryOperation( + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator operator, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression left, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression right +) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator.EQUALS, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator operator, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression left, + org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression right + ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation build() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder withOperator(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator operator) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder withLeft(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression left) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder withLeft(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder left) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(operator, left.build(), right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder withRight(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression right) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder withRight(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder right) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder(operator, left, right.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperator.java new file mode 100644 index 0000000..ca13c15 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperator.java @@ -0,0 +1,13 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator imports + +public enum SqlBinaryOperator { + EQUALS + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java index 8479160..5e1387b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports -public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { +public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression build(); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java index 20d6dac..4f9771a 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java @@ -201,6 +201,19 @@ public class SqlWriterTests { // === Expressions === + @Test + public void binaryOperationEquals() throws IOException { + var sql = SqlBinaryOperation.arbitrary() + .withOperator(SqlBinaryOperator.EQUALS) + .withLeft(SqlIntLiteral.arbitrary().withValue(42)) + .withRight(SqlIntLiteral.arbitrary().withValue(47)) + .build(); + + var string = write(writer -> writer.writeExpression(sql)); + + assertThat(string, equalTo("42 == 47")); + } + @Test public void intLiteral() throws IOException { var sql = SqlIntLiteral.arbitrary() diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperationMatcher.java new file mode 100644 index 0000000..fe79201 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperationMatcher.java @@ -0,0 +1,53 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher imports + +public final class SqlBinaryOperationMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher isSqlBinaryOperation() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private SqlBinaryOperationMatcher( + java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher withOperator(org.zwobble.precisely.Matcher operator) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation::operator, (org.zwobble.precisely.Matcher) operator)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher withLeft(org.zwobble.precisely.Matcher left) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation::left, (org.zwobble.precisely.Matcher) left)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher withRight(org.zwobble.precisely.Matcher right) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation::right, (org.zwobble.precisely.Matcher) right)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher body +} -- cgit v1.2.3