From e761d7865111d6fe8fa8177c18490a84a1a1e3e0 Mon Sep 17 00:00:00 2001 From: jeffmorrison Date: Sun, 10 Nov 2024 03:57:42 -0500 Subject: [PATCH] refactor: Updated switch expression (#3030) --- .../com/iluwatar/bytecode/VirtualMachine.java | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java b/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java index 9144288ce65b..2133296ed155 100644 --- a/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java +++ b/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java @@ -67,59 +67,60 @@ public void execute(int[] bytecode) { for (var i = 0; i < bytecode.length; i++) { Instruction instruction = Instruction.getInstruction(bytecode[i]); switch (instruction) { - case LITERAL: - // Read the next byte from the bytecode. + case LITERAL -> { // Read the next byte from the bytecode. int value = bytecode[++i]; // Push the next value to stack stack.push(value); - break; - case SET_AGILITY: + } + case SET_AGILITY -> { var amount = stack.pop(); var wizard = stack.pop(); setAgility(wizard, amount); - break; - case SET_WISDOM: - amount = stack.pop(); - wizard = stack.pop(); + } + case SET_WISDOM -> { + var amount = stack.pop(); + var wizard = stack.pop(); setWisdom(wizard, amount); - break; - case SET_HEALTH: - amount = stack.pop(); - wizard = stack.pop(); + } + case SET_HEALTH -> { + var amount = stack.pop(); + var wizard = stack.pop(); setHealth(wizard, amount); - break; - case GET_HEALTH: - wizard = stack.pop(); + } + case GET_HEALTH -> { + var wizard = stack.pop(); stack.push(getHealth(wizard)); - break; - case GET_AGILITY: - wizard = stack.pop(); + } + case GET_AGILITY -> { + var wizard = stack.pop(); stack.push(getAgility(wizard)); - break; - case GET_WISDOM: - wizard = stack.pop(); + } + case GET_WISDOM -> { + var wizard = stack.pop(); stack.push(getWisdom(wizard)); - break; - case ADD: + } + case ADD -> { var a = stack.pop(); var b = stack.pop(); stack.push(a + b); - break; - case DIVIDE: - a = stack.pop(); - b = stack.pop(); + } + case DIVIDE -> { + var a = stack.pop(); + var b = stack.pop(); stack.push(b / a); - break; - case PLAY_SOUND: - wizard = stack.pop(); + } + case PLAY_SOUND -> { + var wizard = stack.pop(); getWizards()[wizard].playSound(); - break; - case SPAWN_PARTICLES: - wizard = stack.pop(); + + } + case SPAWN_PARTICLES -> { + var wizard = stack.pop(); getWizards()[wizard].spawnParticles(); - break; - default: + } + default -> { throw new IllegalArgumentException("Invalid instruction value"); + } } LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack()); }