From e7025e11b5718393d776e42ec83bef1521bf8a20 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:10:02 +0100 Subject: Compile int literal to stack instruction --- src/main/java/org/shedlang/compiler/stackir/StackCompiler.java | 7 ++++++- .../org/shedlang/compiler/stackir/instructions/Instruction.java | 2 +- .../compiler/stackir/instructions/PushIntInstruction.java | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index a5b2b829..33d8f026 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.stackir; import org.shedlang.compiler.ast.typed.*; import org.shedlang.compiler.stackir.instructions.Instruction; import org.shedlang.compiler.stackir.instructions.PushBoolInstruction; +import org.shedlang.compiler.stackir.instructions.PushIntInstruction; import java.util.List; @@ -20,7 +21,7 @@ public class StackCompiler { compileBoolLiteral(typedBoolLiteralNode); case TypedIntLiteralNode typedIntLiteralNode -> - throw new UnsupportedOperationException("TODO"); + compileIntLiteral(typedIntLiteralNode); case TypedReferenceNode typedReferenceNode -> throw new UnsupportedOperationException("TODO"); @@ -29,4 +30,8 @@ public class StackCompiler { throw new UnsupportedOperationException("TODO"); }; } + + private static List compileIntLiteral(TypedIntLiteralNode node) { + return List.of(new PushIntInstruction(node.value(), node.source())); + } } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index 70c9d7c3..5c5bad79 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.stackir.instructions; -public sealed interface Instruction permits PushBoolInstruction { +public sealed interface Instruction permits PushBoolInstruction, PushIntInstruction { } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java new file mode 100644 index 00000000..aa73d817 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java @@ -0,0 +1,8 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +import java.math.BigInteger; + +public record PushIntInstruction(BigInteger value, Source source) implements Instruction { +} -- cgit v1.2.3