compiler.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_CALC8_COMPILER_HPP)
  7. #define BOOST_SPIRIT_CALC8_COMPILER_HPP
  8. #include "ast.hpp"
  9. #include "error_handler.hpp"
  10. #include <vector>
  11. #include <map>
  12. #include <boost/function.hpp>
  13. #include <boost/spirit/include/phoenix_core.hpp>
  14. #include <boost/spirit/include/phoenix_function.hpp>
  15. #include <boost/spirit/include/phoenix_operator.hpp>
  16. namespace client { namespace code_gen
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. // The Program
  20. ///////////////////////////////////////////////////////////////////////////
  21. struct program
  22. {
  23. void op(int a);
  24. void op(int a, int b);
  25. void op(int a, int b, int c);
  26. int& operator[](std::size_t i) { return code[i]; }
  27. int const& operator[](std::size_t i) const { return code[i]; }
  28. void clear() { code.clear(); variables.clear(); }
  29. std::size_t size() const { return code.size(); }
  30. std::vector<int> const& operator()() const { return code; }
  31. int nvars() const { return variables.size(); }
  32. int const* find_var(std::string const& name) const;
  33. void add_var(std::string const& name);
  34. void print_variables(std::vector<int> const& stack) const;
  35. void print_assembler() const;
  36. private:
  37. std::map<std::string, int> variables;
  38. std::vector<int> code;
  39. };
  40. ///////////////////////////////////////////////////////////////////////////
  41. // The Compiler
  42. ///////////////////////////////////////////////////////////////////////////
  43. struct compiler
  44. {
  45. typedef bool result_type;
  46. template <typename ErrorHandler>
  47. compiler(client::code_gen::program& program, ErrorHandler& error_handler_)
  48. : program(program)
  49. {
  50. using namespace boost::phoenix::arg_names;
  51. namespace phx = boost::phoenix;
  52. using boost::phoenix::function;
  53. error_handler = function<ErrorHandler>(error_handler_)(
  54. "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
  55. }
  56. bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
  57. bool operator()(unsigned int x) const;
  58. bool operator()(bool x) const;
  59. bool operator()(ast::variable const& x) const;
  60. bool operator()(ast::operation const& x) const;
  61. bool operator()(ast::unary const& x) const;
  62. bool operator()(ast::expression const& x) const;
  63. bool operator()(ast::assignment const& x) const;
  64. bool operator()(ast::variable_declaration const& x) const;
  65. bool operator()(ast::statement_list const& x) const;
  66. bool operator()(ast::statement const& x) const;
  67. bool operator()(ast::if_statement const& x) const;
  68. bool operator()(ast::while_statement const& x) const;
  69. bool start(ast::statement_list const& x) const;
  70. client::code_gen::program& program;
  71. boost::function<
  72. void(int tag, std::string const& what)>
  73. error_handler;
  74. };
  75. }}
  76. #endif