Last active
February 17, 2017 11:33
-
-
Save LordAro/49f6cd773f987c94d02fc9360cc52a1a 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
/* static */ const machine::op_map machine::OPERATIONS {{ | |
{op_t::NOP, [](machine* m){(void)m;}}, | |
{op_t::LOAD, &machine::load_func}, | |
{op_t::STORE, &machine::store_func}, | |
{op_t::DUP, [](machine* m){m->stack.push(m->stack.top());}}, | |
{op_t::SWAP, &machine::swap_func}, | |
{op_t::INC, [](machine *m){m->stack.top()++;}}, | |
{op_t::DEC, [](machine *m){m->stack.top()--;}}, | |
{op_t::ADD, [](machine *m){m->binop_func(std::plus<>());}}, | |
{op_t::SUB, [](machine *m){m->binop_func(std::minus<>(), true);}}, | |
// {op_t::ADDC, [](machine *m){m->terminate = true;}}, | |
// {op_t::SUBC, [](machine *m){m->terminate = true;}}, | |
{op_t::AND, [](machine *m){m->binop_func(std::bit_and<>());}}, | |
{op_t::OR, [](machine *m){m->binop_func(std::bit_or<>());}}, | |
{op_t::XOR, [](machine *m){m->binop_func(std::bit_xor<>());}}, | |
{op_t::NOT, [](machine *m){m->stack.top() = ~m->stack.top();}}, | |
{op_t::SHR, [](machine *m){m->binop_func([](uint16_t a, uint16_t b){return a >> b;}, true);}}, | |
{op_t::SHL, [](machine *m){m->binop_func([](uint16_t a, uint16_t b){return a << b;}, true);}}, | |
{op_t::TEQ, [](machine *m){m->terminate = true;}}, | |
{op_t::TGT, [](machine *m){m->terminate = true;}}, | |
{op_t::BRZERO, [](machine *m){m->terminate = true;}}, | |
{op_t::STOP, [](machine *m){m->terminate = true;}}, | |
{op_t::OUT, [](machine *m){std::cout << m->stack.top() << '\n';}}, | |
}}; | |
void machine::set_func(uint16_t v) | |
{ | |
this->stack.push(v); | |
} | |
void machine::load_func() | |
{ | |
uint16_t addr = this->stack.top(); | |
this->stack.pop(); | |
this->stack.push(this->mem.at(addr)); | |
} | |
void machine::store_func() | |
{ | |
uint16_t addr = this->stack.top(); | |
this->stack.pop(); | |
uint16_t val = this->stack.top(); | |
this->stack.pop(); | |
this->mem.at(addr) = val; | |
} | |
void machine::swap_func() | |
{ | |
uint16_t a = this->stack.top(); | |
this->stack.pop(); | |
uint16_t b = this->stack.top(); | |
this->stack.pop(); | |
this->stack.push(a); | |
this->stack.push(b); | |
} | |
void machine::binop_func(std::function<uint16_t(uint16_t, uint16_t)> op, bool invert) | |
{ | |
uint16_t a = this->stack.top(); | |
this->stack.pop(); | |
uint16_t b = this->stack.top(); | |
this->stack.pop(); | |
this->stack.push(invert ? op(b, a) : op(a, b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment