compiler.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_MINIC_COMPILER_HPP)
  7. #define BOOST_SPIRIT_MINIC_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/shared_ptr.hpp>
  14. #include <boost/spirit/include/phoenix_core.hpp>
  15. #include <boost/spirit/include/phoenix_function.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. namespace client { namespace code_gen
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. // The Function
  21. ///////////////////////////////////////////////////////////////////////////
  22. struct function
  23. {
  24. function(std::vector<int>& code, int nargs)
  25. : code(code), address(code.size()), size_(0), nargs_(nargs) {}
  26. void op(int a);
  27. void op(int a, int b);
  28. void op(int a, int b, int c);
  29. int& operator[](std::size_t i) { return code[address+i]; }
  30. int const& operator[](std::size_t i) const { return code[address+i]; }
  31. std::size_t size() const { return size_; }
  32. std::size_t get_address() const { return address; }
  33. int nargs() const { return nargs_; }
  34. int nvars() const { return variables.size(); }
  35. int const* find_var(std::string const& name) const;
  36. void add_var(std::string const& name);
  37. void link_to(std::string const& name, std::size_t address);
  38. void print_assembler() const;
  39. private:
  40. std::map<std::string, int> variables;
  41. std::map<std::size_t, std::string> function_calls;
  42. std::vector<int>& code;
  43. std::size_t address;
  44. std::size_t size_;
  45. std::size_t nargs_;
  46. };
  47. ///////////////////////////////////////////////////////////////////////////
  48. // The Compiler
  49. ///////////////////////////////////////////////////////////////////////////
  50. struct compiler
  51. {
  52. typedef bool result_type;
  53. template <typename ErrorHandler>
  54. compiler(ErrorHandler& error_handler_)
  55. : current(0)
  56. {
  57. using namespace boost::phoenix::arg_names;
  58. namespace phx = boost::phoenix;
  59. using boost::phoenix::function;
  60. error_handler = function<ErrorHandler>(error_handler_)(
  61. "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
  62. }
  63. bool operator()(ast::nil) { BOOST_ASSERT(0); return false; }
  64. bool operator()(unsigned int x);
  65. bool operator()(bool x);
  66. bool operator()(ast::identifier const& x);
  67. bool operator()(ast::operation const& x);
  68. bool operator()(ast::unary const& x);
  69. bool operator()(ast::function_call const& x);
  70. bool operator()(ast::expression const& x);
  71. bool operator()(ast::assignment const& x);
  72. bool operator()(ast::variable_declaration const& x);
  73. bool operator()(ast::statement_list const& x);
  74. bool operator()(ast::statement const& x);
  75. bool operator()(ast::if_statement const& x);
  76. bool operator()(ast::while_statement const& x);
  77. bool operator()(ast::return_statement const& x);
  78. bool operator()(ast::function const& x);
  79. bool operator()(ast::function_list const& x);
  80. void print_assembler() const;
  81. boost::shared_ptr<code_gen::function>
  82. find_function(std::string const& name) const;
  83. std::vector<int>& get_code() { return code; }
  84. std::vector<int> const& get_code() const { return code; }
  85. private:
  86. typedef std::map<std::string, boost::shared_ptr<code_gen::function> > function_table;
  87. std::vector<int> code;
  88. code_gen::function* current;
  89. std::string current_function_name;
  90. function_table functions;
  91. bool void_return;
  92. boost::function<
  93. void(int tag, std::string const& what)>
  94. error_handler;
  95. };
  96. }}
  97. #endif