Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update VirtualMachine.java #3042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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:
}
case SET_WISDOM -> {
amount = stack.pop();
wizard = stack.pop();
setWisdom(wizard, amount);
break;
case SET_HEALTH:
}
case SET_HEALTH -> {
amount = stack.pop();
wizard = stack.pop();
setHealth(wizard, amount);
break;
case GET_HEALTH:
}
case GET_HEALTH -> {
wizard = stack.pop();
stack.push(getHealth(wizard));
break;
case GET_AGILITY:
}
case GET_AGILITY -> {
wizard = stack.pop();
stack.push(getAgility(wizard));
break;
case GET_WISDOM:
}
case GET_WISDOM -> {
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:
}
case DIVIDE -> {
a = stack.pop();
b = stack.pop();
stack.push(b / a);
break;
case PLAY_SOUND:
}
case PLAY_SOUND -> {
wizard = stack.pop();
getWizards()[wizard].playSound();
break;
case SPAWN_PARTICLES:
}
case SPAWN_PARTICLES -> {
wizard = stack.pop();
getWizards()[wizard].spawnParticles();
break;
default:
}
default -> {
throw new IllegalArgumentException("Invalid instruction value");
}
}
LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack());
}
Expand Down
Loading