From 5257c230be9bbbb1dad4d053d80405d29369a1c7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 09:33:49 +0100 Subject: Add assignment operations to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 18 ++++++++ .../typescript/ast/TypescriptBinaryOperation.java | 52 +++++++++++++++++++++ .../typescript/ast/TypescriptBinaryOperator.java | 13 ++++++ .../lang/typescript/ast/TypescriptExpression.java | 2 +- .../lang/typescript/TypescriptWriterTests.java | 47 +++++++++++++------ .../ast/TypescriptBinaryOperationMatcher.java | 53 ++++++++++++++++++++++ 6 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperation.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperator.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java index 6cd116e..ddad0b1 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -170,6 +170,10 @@ public class TypescriptWriter implements AutoCloseable { void writeExpression(TypescriptExpression expression) throws IOException { switch (expression) { + case TypescriptBinaryOperation binaryOperation -> { + this.writeBinaryOperation(binaryOperation); + } + case TypescriptNumberLiteral numberLiteral -> { this.writeNumberLiteral(numberLiteral); } @@ -180,6 +184,20 @@ public class TypescriptWriter implements AutoCloseable { } } + private void writeBinaryOperation(TypescriptBinaryOperation binaryOperation) throws IOException { + this.writeExpression(binaryOperation.left()); + this.writer.write(" "); + this.writeBinaryOperator(binaryOperation.operator()); + this.writer.write(" "); + this.writeExpression(binaryOperation.right()); + } + + private void writeBinaryOperator(TypescriptBinaryOperator operator) throws IOException { + this.writer.write(switch (operator) { + case ASSIGN -> "="; + }); + } + private void writeNumberLiteral(TypescriptNumberLiteral numberLiteral) throws IOException { this.writer.write(Double.toString(numberLiteral.value())); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperation.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperation.java new file mode 100644 index 0000000..35e9bed --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperation.java @@ -0,0 +1,52 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation imports + +public record TypescriptBinaryOperation( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator operator, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression left, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression right +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator.ASSIGN, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator operator, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression left, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression right + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder withOperator(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator operator) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder withLeft(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression left) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder withLeft(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder left) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(operator, left.build(), right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder withRight(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression right) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(operator, left, right); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder withRight(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder right) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder(operator, left, right.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperator.java new file mode 100644 index 0000000..f9264b5 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperator.java @@ -0,0 +1,13 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator imports + +public enum TypescriptBinaryOperator { + ASSIGN + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java index 3996e2c..99dc933 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports -public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef { +public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression build(); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java index 82b76f0..690a676 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java @@ -118,7 +118,7 @@ public class TypescriptWriterTests { @Test public void expressionStatement() throws IOException { var typescript = TypescriptExpressionStatement.arbitrary() - .withExpression(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withExpression(num(42)) .build(); var string = write(writer -> writer.writeStatement(typescript)); @@ -146,11 +146,11 @@ public class TypescriptWriterTests { var typescript = TypescriptClassConstructor.arbitrary() .addParam(TypescriptParam.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) + .withType(ref("number")) ) .addParam(TypescriptParam.arbitrary() .withName(id("b")) - .withType(TypescriptRef.arbitrary().withName(id("string"))) + .withType(ref("string")) ) .build(); @@ -178,7 +178,7 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithExplicitType() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) + .withType(ref("number")) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -190,7 +190,7 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withInitialValue(num(42)) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -202,8 +202,8 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithTypeAndInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) - .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withType(ref("number")) + .withInitialValue(num(42)) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -211,15 +211,32 @@ public class TypescriptWriterTests { assertThat(string, equalTo("a: number = 42.0;")); } + private static TypescriptRef ref(String value) { + return TypescriptRef.arbitrary().withName(id(value)).build(); + } + // == Expressions == + // === Binary operations === + + @Test + public void assign() throws IOException { + var typescript = TypescriptBinaryOperation.arbitrary() + .withOperator(TypescriptBinaryOperator.ASSIGN) + .withLeft(ref("x")) + .withRight(num(42)) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("x = 42.0")); + } + // === Number literals === @Test public void numberLiteral() throws IOException { - var typescript = TypescriptNumberLiteral.arbitrary() - .withValue(42) - .build(); + var typescript = num(42); var string = write(writer -> writer.writeExpression(typescript)); @@ -230,9 +247,7 @@ public class TypescriptWriterTests { @Test public void reference() throws IOException { - var typescript = TypescriptRef.arbitrary() - .withName(id("x")) - .build(); + var typescript = ref("x"); var string = write(writer -> writer.writeExpression(typescript)); @@ -245,6 +260,12 @@ public class TypescriptWriterTests { return TypescriptIdentifier.of(name); } + private static TypescriptNumberLiteral num(int value) { + return TypescriptNumberLiteral.arbitrary() + .withValue(value) + .build(); + } + private String write(TypescriptWriter.Write write) throws IOException { return write(write, Optional.empty()); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java new file mode 100644 index 0000000..a46c68d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java @@ -0,0 +1,53 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports + +public final class TypescriptBinaryOperationMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher isTypescriptBinaryOperation() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptBinaryOperationMatcher( + 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.typescript.ast.TypescriptBinaryOperation.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher 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.typescript.ast.TypescriptBinaryOperation::operator, (org.zwobble.precisely.Matcher) operator)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher 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.typescript.ast.TypescriptBinaryOperation::left, (org.zwobble.precisely.Matcher) left)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher 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.typescript.ast.TypescriptBinaryOperation::right, (org.zwobble.precisely.Matcher) right)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body +} -- cgit v1.2.3