vm.hpp 1.5 KB

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