summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperation.java52
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperator.java13
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java13
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlBinaryOperationMatcher.java53
6 files changed, 152 insertions, 1 deletions
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
@@ -202,6 +202,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()
.withValue(42)
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<java.lang.Object> {
+ 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation>> submatchers;
+
+ private SqlBinaryOperationMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation>> 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.output.lang.sql.ast.SqlBinaryOperation.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperationMatcher withOperator(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperator> operator) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression> left) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression> right) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlBinaryOperation>>(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
+}