Skip to content

Instantly share code, notes, and snippets.

@roxygen
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxygen/a2276ea1bf180b80efd3 to your computer and use it in GitHub Desktop.
Save roxygen/a2276ea1bf180b80efd3 to your computer and use it in GitHub Desktop.
Staing working on vn bytecode interpreter.
#include <iostream>
#include <string>
#include <vector>
#include <stdint.h>
#include <map>
struct Code {
std::vector<int> code;
std::vector<std::string> strings;
};
enum OPS {
OP_CG = 0x01,
OP_MSG = 0x02,
OP_END = 0x03,
OP_WAITKEY = 0x04,
OP_WIPE = 0x05,
OP_PUSH_INT = 0x06,
OP_PUSH_STR = 0x07,
OP_MOV = 0x08,
OP_ADD = 0x09,
OP_LOADK = 0x10,
OP_INC = 0x11,
OP_DEC = 0x12
};
int
main()
{
Code code;
code.code = {OP_LOADK, 1,200,
OP_INC, 1,
OP_DEC, 1,
OP_ADD, 1,1,1,
OP_CG, 2,1,200,210,300,
OP_MSG, 0,
OP_WIPE, 1,300,
OP_WAITKEY,
OP_END};
code.strings = {u8"Лай ла эй",u8"bg_filename.png",u8"BG01"};
int p = 0;
bool execute = true;
std::vector<int> stack;
std::map<int,int> flag;
int reg[4]; //?
while(p<=code.code.size()&&execute) {
switch(code.code[p++]) {
case OP_CG: {
std::string name = code.strings[code.code[p++]];
std::string file = code.strings[code.code[p++]];
int x = code.code[p++];
int y = code.code[p++];
int z = code.code[p++];
std::cout << "CG "<< name << ' ' << file << ' ' << x <<' ' << y << ' ' << z << std::endl;
} break;
case OP_WIPE: {
int type = code.code[p++];
int time = code.code[p++];
std::cout << "WIPE " << type << ' ' << time << std::endl;
} break;
case OP_MSG: {
std::string message = code.strings[code.code[p++]];
std::cout << "MESSAGE " << message <<std::endl;
} break;
case OP_END: {
execute = false;
std::cout << "END" <<std::endl;
} break;
case OP_WAITKEY: {
std::cout << "WAIT KEY" << std::endl;
} break;
case OP_MOV: {
int flnum = code.code[p++];
int con =code.code[p++];
flag[flnum] = con;
}break;
case OP_ADD: {
int flnum_store = code.code[p++];
int flnum_a = code.code[p++];
int flnum_b = code.code[p++];
flag[flnum_store] = flag[flnum_a] + flag[flnum_b];
std::cout << "ADD " << flnum_store <<' '<<flnum_a<<' '<<flnum_b<< std::endl;
std::cout << "flag["<<flnum_store<<"] = "<< flag[flnum_store] <<std::endl;
} break;
case OP_LOADK: {
int flnum = code.code[p++];
int con = code.code[p++];
flag[flnum] = con; // В регистр flnum загружена константа con
std::cout << "LOADK " << flnum <<' '<<con<< std::endl;
std::cout << "flag["<<flnum<<"] = "<< flag[flnum] <<std::endl;
} break;
case OP_INC: {
int flnum = code.code[p++];
flag[flnum]++;
std::cout << "INC " << flnum << std::endl;
std::cout << "flag["<<flnum<<"] = "<< flag[flnum] <<std::endl;
} break;
case OP_DEC: {
int flnum = code.code[p++];
flag[flnum]--;
std::cout << "DEC " << flnum << std::endl;
std::cout << "flag["<<flnum<<"] = "<< flag[flnum] <<std::endl;
} break;
default:
p++;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment