From 5ef36d57009702a38722c6af72fa94ae5ff0778b Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 21 Jul 2025 17:23:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20R=5FPUSH=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 RPushCommand 类实现 Command 接口,用于处理引用类型推入操作 - 支持将字符串字面量等引用类型数据推入操作栈 --- .../vm/commands/ref/control/RPushCommand.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/org/jcnc/snow/vm/commands/ref/control/RPushCommand.java diff --git a/src/main/java/org/jcnc/snow/vm/commands/ref/control/RPushCommand.java b/src/main/java/org/jcnc/snow/vm/commands/ref/control/RPushCommand.java new file mode 100644 index 0000000..1386018 --- /dev/null +++ b/src/main/java/org/jcnc/snow/vm/commands/ref/control/RPushCommand.java @@ -0,0 +1,63 @@ +package org.jcnc.snow.vm.commands.ref.control; + +import org.jcnc.snow.vm.interfaces.Command; +import org.jcnc.snow.vm.module.CallStack; +import org.jcnc.snow.vm.module.LocalVariableStore; +import org.jcnc.snow.vm.module.OperandStack; + +/** + * The {@code RPushCommand} class implements the {@link Command} interface and represents the + * reference push instruction ({@code R_PUSH}) in the virtual machine. + * + *

+ * This instruction pushes a reference object, such as a String literal, onto the operand stack. + *

+ * + *

Instruction format: {@code R_PUSH }

+ * + * + *

Behavior:

+ * + */ +public final class RPushCommand implements Command { + + /** + * Executes the {@code R_PUSH} instruction, pushing a reference (such as a string literal) + * onto the operand stack. + * + * @param parts The instruction parameters. {@code parts[0]} is the operator ("R_PUSH"), + * {@code parts[1..]} are the parts of the literal to be concatenated and pushed. + * @param pc The current program counter value, indicating the instruction address being executed. + * @param stack The operand stack manager. The literal will be pushed onto this stack. + * @param lvs The local variable store. (Not used in this instruction.) + * @param cs The call stack manager. (Not used in this instruction.) + * @return The next program counter value ({@code pc + 1}), pointing to the next instruction. + * @throws IllegalStateException if the instruction is missing required parameters. + */ + @Override + public int execute(String[] parts, int pc, + OperandStack stack, + LocalVariableStore lvs, + CallStack cs) { + + if (parts.length < 2) + throw new IllegalStateException("R_PUSH missing parameter"); + + StringBuilder sb = new StringBuilder(); + for (int i = 1; i < parts.length; i++) { + if (i > 1) sb.append(' '); + sb.append(parts[i]); + } + stack.push(sb.toString()); + return pc + 1; + } +}