From fa0eb2727b12dc38d74228504c49f1a269c32b8c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 29 Aug 2026 22:46:39 +0100 Subject: Add number literals to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 14 ++++++++++ .../lang/typescript/ast/TypescriptExpression.java | 18 ++++++++++++ .../typescript/ast/TypescriptNumberLiteral.java | 32 ++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNumberLiteral.java (limited to 'src/main/java') 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 80ce51b..74556bb 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 @@ -113,6 +113,20 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(";"); } + // == Expressions == + + void writeExpression(TypescriptExpression expression) throws IOException { + switch (expression) { + case TypescriptNumberLiteral numberLiteral -> { + this.writeNumberLiteral(numberLiteral); + } + } + } + + private void writeNumberLiteral(TypescriptNumberLiteral numberLiteral) throws IOException { + this.writer.write(Double.toString(numberLiteral.value())); + } + // == Identifiers == private void writeIdentifier(TypescriptIdentifier name) throws IOException { 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 new file mode 100644 index 0000000..4c04085 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +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 { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNumberLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNumberLiteral.java new file mode 100644 index 0000000..40c2f01 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNumberLiteral.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral imports + +public record TypescriptNumberLiteral( + double value +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder(0); + } + + public record Builder( + double value + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral(value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder withValue(double value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder(value); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral body +} -- cgit v1.2.3