Skip to content

Commit 29ff3ca

Browse files
updated to switch expression
1 parent 007dcb1 commit 29ff3ca

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,59 +67,60 @@ public void execute(int[] bytecode) {
6767
for (var i = 0; i < bytecode.length; i++) {
6868
Instruction instruction = Instruction.getInstruction(bytecode[i]);
6969
switch (instruction) {
70-
case LITERAL:
71-
// Read the next byte from the bytecode.
70+
case LITERAL -> { // Read the next byte from the bytecode.
7271
int value = bytecode[++i];
7372
// Push the next value to stack
7473
stack.push(value);
75-
break;
76-
case SET_AGILITY:
74+
}
75+
case SET_AGILITY -> {
7776
var amount = stack.pop();
7877
var wizard = stack.pop();
7978
setAgility(wizard, amount);
80-
break;
81-
case SET_WISDOM:
82-
amount = stack.pop();
83-
wizard = stack.pop();
79+
}
80+
case SET_WISDOM -> {
81+
var amount = stack.pop();
82+
var wizard = stack.pop();
8483
setWisdom(wizard, amount);
85-
break;
86-
case SET_HEALTH:
87-
amount = stack.pop();
88-
wizard = stack.pop();
84+
}
85+
case SET_HEALTH -> {
86+
var amount = stack.pop();
87+
var wizard = stack.pop();
8988
setHealth(wizard, amount);
90-
break;
91-
case GET_HEALTH:
92-
wizard = stack.pop();
89+
}
90+
case GET_HEALTH -> {
91+
var wizard = stack.pop();
9392
stack.push(getHealth(wizard));
94-
break;
95-
case GET_AGILITY:
96-
wizard = stack.pop();
93+
}
94+
case GET_AGILITY -> {
95+
var wizard = stack.pop();
9796
stack.push(getAgility(wizard));
98-
break;
99-
case GET_WISDOM:
100-
wizard = stack.pop();
97+
}
98+
case GET_WISDOM -> {
99+
var wizard = stack.pop();
101100
stack.push(getWisdom(wizard));
102-
break;
103-
case ADD:
101+
}
102+
case ADD -> {
104103
var a = stack.pop();
105104
var b = stack.pop();
106105
stack.push(a + b);
107-
break;
108-
case DIVIDE:
109-
a = stack.pop();
110-
b = stack.pop();
106+
}
107+
case DIVIDE -> {
108+
var a = stack.pop();
109+
var b = stack.pop();
111110
stack.push(b / a);
112-
break;
113-
case PLAY_SOUND:
114-
wizard = stack.pop();
111+
}
112+
case PLAY_SOUND -> {
113+
var wizard = stack.pop();
115114
getWizards()[wizard].playSound();
116-
break;
117-
case SPAWN_PARTICLES:
118-
wizard = stack.pop();
115+
116+
}
117+
case SPAWN_PARTICLES -> {
118+
var wizard = stack.pop();
119119
getWizards()[wizard].spawnParticles();
120-
break;
121-
default:
120+
}
121+
default -> {
122122
throw new IllegalArgumentException("Invalid instruction value");
123+
}
123124
}
124125
LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack());
125126
}

0 commit comments

Comments
 (0)