From 107df69fcf9d53c320165e7f3f946c03e3f5bfc4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 21 Jul 2023 16:52:22 +0100 Subject: Add byte data values --- .../org/shedlang/compiler/patio/ast/PatioSerializer.java | 15 +++++++++++++++ .../shedlang/compiler/patio/ast/types/PatioDataValue.java | 4 ++++ .../compiler/patio/ast/types/PatioDataValueByte.java | 4 ++++ .../compiler/patio/ast/types/PatioDataValues.java | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValue.java create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValueByte.java create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValues.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 8c02d3d3..62e7fa68 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -38,6 +38,14 @@ public class PatioSerializer { serializeTree(tree, builder, 0); } + public static void serializeDataValue( + PatioDataValue value, + StringBuilder builder + ) { + var tree = dataValueToTree(value); + serializeTree(tree, builder, 0); + } + public static void serializeInstructions(List instructions, StringBuilder builder) { var tree = instructionsToTree(instructions); serializeTree(tree, builder, 0); @@ -302,6 +310,13 @@ public class PatioSerializer { }; } + private static Tree dataValueToTree(PatioDataValue value) { + return switch (value) { + case PatioDataValueByte byte_ -> + treeList(treeIdentifier("byte"), treeIdentifier(String.format("0x%02x", byte_.value()))); + }; + } + private static Tree visibilityToTree(PatioVisibility visibility) { return treeIdentifier(switch (visibility) { case PRIVATE -> "private"; diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValue.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValue.java new file mode 100644 index 00000000..5ccf1f21 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValue.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.patio.ast.types; + +public sealed interface PatioDataValue permits PatioDataValueByte { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValueByte.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValueByte.java new file mode 100644 index 00000000..7ca2a997 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValueByte.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.patio.ast.types; + +public record PatioDataValueByte(byte value) implements PatioDataValue { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValues.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValues.java new file mode 100644 index 00000000..88dd0898 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataValues.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.patio.ast.types; + +public class PatioDataValues { + private PatioDataValues() {} + + public static PatioDataValue byte_(int value) { + return new PatioDataValueByte((byte) value); + } +} -- cgit v1.2.3