ast.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_AST_HPP)
  7. #define BOOST_SPIRIT_CALC8_AST_HPP
  8. #include <boost/config/warning_disable.hpp>
  9. #include <boost/variant/recursive_variant.hpp>
  10. #include <boost/fusion/include/adapt_struct.hpp>
  11. #include <boost/fusion/include/io.hpp>
  12. #include <boost/optional.hpp>
  13. #include <list>
  14. namespace client { namespace ast
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // The AST
  18. ///////////////////////////////////////////////////////////////////////////
  19. struct tagged
  20. {
  21. int id; // Used to annotate the AST with the iterator position.
  22. // This id is used as a key to a map<int, Iterator>
  23. // (not really part of the AST.)
  24. };
  25. struct nil {};
  26. struct unary;
  27. struct expression;
  28. struct variable : tagged
  29. {
  30. variable(std::string const& name = "") : name(name) {}
  31. std::string name;
  32. };
  33. typedef boost::variant<
  34. nil
  35. , bool
  36. , unsigned int
  37. , variable
  38. , boost::recursive_wrapper<unary>
  39. , boost::recursive_wrapper<expression>
  40. >
  41. operand;
  42. enum optoken
  43. {
  44. op_plus,
  45. op_minus,
  46. op_times,
  47. op_divide,
  48. op_positive,
  49. op_negative,
  50. op_not,
  51. op_equal,
  52. op_not_equal,
  53. op_less,
  54. op_less_equal,
  55. op_greater,
  56. op_greater_equal,
  57. op_and,
  58. op_or
  59. };
  60. struct unary
  61. {
  62. optoken operator_;
  63. operand operand_;
  64. };
  65. struct operation
  66. {
  67. optoken operator_;
  68. operand operand_;
  69. };
  70. struct expression
  71. {
  72. operand first;
  73. std::list<operation> rest;
  74. };
  75. struct assignment
  76. {
  77. variable lhs;
  78. expression rhs;
  79. };
  80. struct variable_declaration
  81. {
  82. assignment assign;
  83. };
  84. struct if_statement;
  85. struct while_statement;
  86. struct statement_list;
  87. typedef boost::variant<
  88. variable_declaration
  89. , assignment
  90. , boost::recursive_wrapper<if_statement>
  91. , boost::recursive_wrapper<while_statement>
  92. , boost::recursive_wrapper<statement_list>
  93. >
  94. statement;
  95. struct statement_list : std::list<statement> {};
  96. struct if_statement
  97. {
  98. expression condition;
  99. statement then;
  100. boost::optional<statement> else_;
  101. };
  102. struct while_statement
  103. {
  104. expression condition;
  105. statement body;
  106. };
  107. // print functions for debugging
  108. inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
  109. inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
  110. }}
  111. BOOST_FUSION_ADAPT_STRUCT(
  112. client::ast::unary,
  113. (client::ast::optoken, operator_)
  114. (client::ast::operand, operand_)
  115. )
  116. BOOST_FUSION_ADAPT_STRUCT(
  117. client::ast::operation,
  118. (client::ast::optoken, operator_)
  119. (client::ast::operand, operand_)
  120. )
  121. BOOST_FUSION_ADAPT_STRUCT(
  122. client::ast::expression,
  123. (client::ast::operand, first)
  124. (std::list<client::ast::operation>, rest)
  125. )
  126. BOOST_FUSION_ADAPT_STRUCT(
  127. client::ast::variable_declaration,
  128. (client::ast::assignment, assign)
  129. )
  130. BOOST_FUSION_ADAPT_STRUCT(
  131. client::ast::assignment,
  132. (client::ast::variable, lhs)
  133. (client::ast::expression, rhs)
  134. )
  135. BOOST_FUSION_ADAPT_STRUCT(
  136. client::ast::if_statement,
  137. (client::ast::expression, condition)
  138. (client::ast::statement, then)
  139. (boost::optional<client::ast::statement>, else_)
  140. )
  141. BOOST_FUSION_ADAPT_STRUCT(
  142. client::ast::while_statement,
  143. (client::ast::expression, condition)
  144. (client::ast::statement, body)
  145. )
  146. #endif