| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | #if !defined(BOOST_SPIRIT_CALC7_VM_HPP) |
| 8 | #define BOOST_SPIRIT_CALC7_VM_HPP |
| 9 | |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace client |
| 13 | { |
| 14 | /////////////////////////////////////////////////////////////////////////// |
| 15 | // The Virtual Machine |
| 16 | /////////////////////////////////////////////////////////////////////////// |
| 17 | enum byte_code |
| 18 | { |
| 19 | op_neg, // negate the top stack entry |
| 20 | op_add, // add top two stack entries |
| 21 | op_sub, // subtract top two stack entries |
| 22 | op_mul, // multiply top two stack entries |
| 23 | op_div, // divide top two stack entries |
| 24 | |
| 25 | op_load, // load a variable |
| 26 | op_store, // store a variable |
| 27 | op_int, // push constant integer into the stack |
| 28 | op_stk_adj // adjust the stack for local variables |
| 29 | }; |
| 30 | |
| 31 | class vmachine |
| 32 | { |
| 33 | public: |
| 34 | |
| 35 | vmachine(unsigned stackSize = 4096) |
| 36 | : stack(stackSize) |
| 37 | , stack_ptr(stack.begin()) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void execute(std::vector<int> const& code); |
| 42 | std::vector<int> const& get_stack() const { return stack; }; |
| 43 | |
| 44 | private: |
| 45 | |
| 46 | std::vector<int> stack; |
| 47 | std::vector<int>::iterator stack_ptr; |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | #endif |
| 52 | |
| 53 | |