mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-15 22:15:02 +00:00
refactor: Updated switch expression (#3030)
This commit is contained in:
1 parent
094027e0f7
commit
e761d78651
1 file changed
+36
-35
@@ -67,59 +67,60 @@ public class VirtualMachine {
|
|||||||
for (var i = 0; i < bytecode.length; i++) {
|
for (var i = 0; i < bytecode.length; i++) {
|
||||||
Instruction instruction = Instruction.getInstruction(bytecode[i]);
|
Instruction instruction = Instruction.getInstruction(bytecode[i]);
|
||||||
switch (instruction) {
|
switch (instruction) {
|
||||||
case LITERAL:
|
case LITERAL -> { // Read the next byte from the bytecode.
|
||||||
// Read the next byte from the bytecode.
|
|
||||||
int value = bytecode[++i];
|
int value = bytecode[++i];
|
||||||
// Push the next value to stack
|
// Push the next value to stack
|
||||||
stack.push(value);
|
stack.push(value);
|
||||||
break;
|
}
|
||||||
case SET_AGILITY:
|
case SET_AGILITY -> {
|
||||||
var amount = stack.pop();
|
var amount = stack.pop();
|
||||||
var wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
setAgility(wizard, amount);
|
setAgility(wizard, amount);
|
||||||
break;
|
}
|
||||||
case SET_WISDOM:
|
case SET_WISDOM -> {
|
||||||
amount = stack.pop();
|
var amount = stack.pop();
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
setWisdom(wizard, amount);
|
setWisdom(wizard, amount);
|
||||||
break;
|
}
|
||||||
case SET_HEALTH:
|
case SET_HEALTH -> {
|
||||||
amount = stack.pop();
|
var amount = stack.pop();
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
setHealth(wizard, amount);
|
setHealth(wizard, amount);
|
||||||
break;
|
}
|
||||||
case GET_HEALTH:
|
case GET_HEALTH -> {
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
stack.push(getHealth(wizard));
|
stack.push(getHealth(wizard));
|
||||||
break;
|
}
|
||||||
case GET_AGILITY:
|
case GET_AGILITY -> {
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
stack.push(getAgility(wizard));
|
stack.push(getAgility(wizard));
|
||||||
break;
|
}
|
||||||
case GET_WISDOM:
|
case GET_WISDOM -> {
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
stack.push(getWisdom(wizard));
|
stack.push(getWisdom(wizard));
|
||||||
break;
|
}
|
||||||
case ADD:
|
case ADD -> {
|
||||||
var a = stack.pop();
|
var a = stack.pop();
|
||||||
var b = stack.pop();
|
var b = stack.pop();
|
||||||
stack.push(a + b);
|
stack.push(a + b);
|
||||||
break;
|
}
|
||||||
case DIVIDE:
|
case DIVIDE -> {
|
||||||
a = stack.pop();
|
var a = stack.pop();
|
||||||
b = stack.pop();
|
var b = stack.pop();
|
||||||
stack.push(b / a);
|
stack.push(b / a);
|
||||||
break;
|
}
|
||||||
case PLAY_SOUND:
|
case PLAY_SOUND -> {
|
||||||
wizard = stack.pop();
|
var wizard = stack.pop();
|
||||||
getWizards()[wizard].playSound();
|
getWizards()[wizard].playSound();
|
||||||
break;
|
|
||||||
case SPAWN_PARTICLES:
|
}
|
||||||
wizard = stack.pop();
|
case SPAWN_PARTICLES -> {
|
||||||
|
var wizard = stack.pop();
|
||||||
getWizards()[wizard].spawnParticles();
|
getWizards()[wizard].spawnParticles();
|
||||||
break;
|
}
|
||||||
default:
|
default -> {
|
||||||
throw new IllegalArgumentException("Invalid instruction value");
|
throw new IllegalArgumentException("Invalid instruction value");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack());
|
LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user