annotation.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_ANNOTATION_HPP)
  7. #define BOOST_SPIRIT_MINIC_ANNOTATION_HPP
  8. #include <map>
  9. #include <boost/variant/apply_visitor.hpp>
  10. #include <boost/type_traits/is_base_of.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include "ast.hpp"
  13. namespace client
  14. {
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // The annotation handler links the AST to a map of iterator positions
  17. // for the purpose of subsequent semantic error handling when the
  18. // program is being compiled.
  19. ///////////////////////////////////////////////////////////////////////////////
  20. template <typename Iterator>
  21. struct annotation
  22. {
  23. template <typename, typename>
  24. struct result { typedef void type; };
  25. std::vector<Iterator>& iters;
  26. annotation(std::vector<Iterator>& iters)
  27. : iters(iters) {}
  28. struct set_id
  29. {
  30. typedef void result_type;
  31. int id;
  32. set_id(int id) : id(id) {}
  33. void operator()(ast::function_call& x) const
  34. {
  35. x.function_name.id = id;
  36. }
  37. void operator()(ast::identifier& x) const
  38. {
  39. x.id = id;
  40. }
  41. template <typename T>
  42. void operator()(T& x) const
  43. {
  44. // no-op
  45. }
  46. };
  47. void operator()(ast::operand& ast, Iterator pos) const
  48. {
  49. int id = iters.size();
  50. iters.push_back(pos);
  51. boost::apply_visitor(set_id(id), ast);
  52. }
  53. void operator()(ast::variable_declaration& ast, Iterator pos) const
  54. {
  55. int id = iters.size();
  56. iters.push_back(pos);
  57. ast.lhs.id = id;
  58. }
  59. void operator()(ast::assignment& ast, Iterator pos) const
  60. {
  61. int id = iters.size();
  62. iters.push_back(pos);
  63. ast.lhs.id = id;
  64. }
  65. void operator()(ast::return_statement& ast, Iterator pos) const
  66. {
  67. int id = iters.size();
  68. iters.push_back(pos);
  69. ast.id = id;
  70. }
  71. void operator()(ast::identifier& ast, Iterator pos) const
  72. {
  73. int id = iters.size();
  74. iters.push_back(pos);
  75. ast.id = id;
  76. }
  77. };
  78. }
  79. #endif