Created
March 5, 2022 02:46
-
-
Save yknishidate/8952fac375f654409fb20cba88442834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <array> | |
#include <stack> | |
struct Wizard | |
{ | |
int health; | |
int wisdom; | |
}; | |
constexpr int numWizards = 2; | |
std::array<Wizard, numWizards> wizards; | |
void setHealth(int wizard, int amount) | |
{ | |
std::cout << "setHealth: " << wizard << " " << amount << std::endl; | |
wizards[wizard].health = amount; | |
} | |
void setWisdom(int wizard, int amount) | |
{ | |
std::cout << "setWisdom: " << wizard << " " << amount << std::endl; | |
wizards[wizard].wisdom = amount; | |
} | |
int getHealth(int wizard) | |
{ | |
int amount = wizards[wizard].health; | |
std::cout << "getHealth: " << wizard << " " << amount << std::endl; | |
return amount; | |
} | |
int getWisdom(int wizard) | |
{ | |
int amount = wizards[wizard].wisdom; | |
std::cout << "getWisdom: " << wizard << " " << amount << std::endl; | |
return amount; | |
} | |
enum Instruction | |
{ | |
INST_SET_HEALTH = 0x00, | |
INST_SET_WISDOM = 0x01, | |
INST_GET_HEALTH = 0x10, | |
INST_GET_WISDOM = 0x11, | |
INST_LITERAL = 0x20, | |
INST_ADD = 0x30, | |
}; | |
class VM | |
{ | |
public: | |
void interpret(const std::vector<char> &bytecode) | |
{ | |
for (int i = 0; i < bytecode.size(); i++) | |
{ | |
char instruction = bytecode[i]; | |
switch (instruction) | |
{ | |
case INST_SET_HEALTH: | |
{ | |
int amount = stack.top(); | |
stack.pop(); | |
int wizard = stack.top(); | |
stack.pop(); | |
setHealth(wizard, amount); | |
break; | |
} | |
case INST_SET_WISDOM: | |
{ | |
int amount = stack.top(); | |
stack.pop(); | |
int wizard = stack.top(); | |
stack.pop(); | |
setWisdom(wizard, amount); | |
break; | |
} | |
case INST_GET_HEALTH: | |
{ | |
int wizard = stack.top(); | |
stack.pop(); | |
stack.push(getHealth(wizard)); | |
break; | |
} | |
case INST_GET_WISDOM: | |
{ | |
int wizard = stack.top(); | |
stack.pop(); | |
stack.push(getWisdom(wizard)); | |
break; | |
} | |
case INST_LITERAL: | |
{ | |
int value = bytecode[++i]; | |
stack.push(value); | |
break; | |
} | |
case INST_ADD: | |
{ | |
int b = stack.top(); | |
stack.pop(); | |
int a = stack.top(); | |
stack.pop(); | |
stack.push(a + b); | |
break; | |
} | |
} | |
} | |
} | |
private: | |
std::stack<int> stack; | |
}; | |
int main() | |
{ | |
std::vector<char> bytecode{ | |
// setHealth(0, 100) | |
INST_LITERAL, 0, | |
INST_LITERAL, 100, | |
INST_SET_HEALTH, | |
// setWISDOM(0, 50) | |
INST_LITERAL, 0, | |
INST_LITERAL, 50, | |
INST_SET_WISDOM, | |
// setHealth(0, getHealth(0) + getWisdom(0)) | |
INST_LITERAL, 0, | |
INST_LITERAL, 0, | |
INST_GET_HEALTH, | |
INST_LITERAL, 0, | |
INST_GET_WISDOM, | |
INST_ADD, | |
INST_SET_HEALTH, | |
}; | |
VM vm; | |
vm.interpret(bytecode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Game Programming Patterns, Chapter 11 Bytecode