| 1 | #include <iostream> |
| 2 | #include <vector> |
| 3 | #include <string> |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | struct Instruction { |
| 8 | string _description; |
| 9 | Instruction(string description) : _description(description) {}; |
| 10 | }; |
| 11 | |
| 12 | struct Reader { |
| 13 | virtual Instruction* read() = 0; // take a stream as parameter |
| 14 | }; |
| 15 | |
| 16 | struct SingleInstructionReader : public Reader { |
| 17 | string _description; |
| 18 | SingleInstructionReader(string description) : _description(description) {}; |
| 19 | Instruction* read() { |
| 20 | return new Instruction(_description); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | struct Script { |
| 25 | |
| 26 | Reader* _onlyHandler; |
| 27 | |
| 28 | void registerOpcode(Reader* reader) { |
| 29 | _onlyHandler = reader; |
| 30 | } |
| 31 | |
| 32 | void init() { |
| 33 | registerOpcode(new SingleInstructionReader("zero")); |
| 34 | cout << dispatch()->_description << endl; // leaky |
| 35 | cout << dispatch()->_description << endl; |
| 36 | registerOpcode(new SingleInstructionReader("one")); |
| 37 | cout << dispatch()->_description << endl; |
| 38 | cout << dispatch()->_description << endl; |
| 39 | /* |
| 40 | the idea is to be able to declare most of opcodes in a simple way: |
| 41 | registerOpcode( arr(0x9c, 172), 2, "roomScroll" ); // pop 2 bytes from stack |
| 42 | later on, in the data flow discovery, it will be extended to something like |
| 43 | registerOpcode( arr(0x9c, 172), arr(VAR_X, VAR_Y), "roomScroll", in(VAR_X, VAR_Y), out() ); |
| 44 | where VAR_X/Y is a way to descripe argument (possibly along with types) and return values |
| 45 | */ |
| 46 | } |
| 47 | |
| 48 | Instruction* dispatch() { |
| 49 | return _onlyHandler->read(); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | int main() { |
| 54 | Script s; |
| 55 | s.init(); |
| 56 | } |