ast.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_CONJURE_AST_HPP)
  8. #define BOOST_SPIRIT_CONJURE_AST_HPP
  9. #include <boost/config/warning_disable.hpp>
  10. #include <boost/variant/recursive_variant.hpp>
  11. #include <boost/fusion/include/adapt_struct.hpp>
  12. #include <boost/fusion/include/io.hpp>
  13. #include <boost/optional.hpp>
  14. #include <list>
  15. #include "ids.hpp"
  16. namespace client { namespace ast
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. // The AST
  20. ///////////////////////////////////////////////////////////////////////////
  21. struct tagged
  22. {
  23. int id; // Used to annotate the AST with the iterator position.
  24. // This id is used as a key to a map<int, Iterator>
  25. // (not really part of the AST.)
  26. };
  27. struct nil {};
  28. struct unary;
  29. struct function_call;
  30. struct expression;
  31. struct identifier : tagged
  32. {
  33. identifier(std::string const& name = "") : name(name) {}
  34. std::string name;
  35. };
  36. typedef boost::variant<
  37. nil
  38. , bool
  39. , unsigned int
  40. , identifier
  41. , boost::recursive_wrapper<unary>
  42. , boost::recursive_wrapper<function_call>
  43. , boost::recursive_wrapper<expression>
  44. >
  45. operand;
  46. struct unary
  47. {
  48. token_ids::type operator_;
  49. operand operand_;
  50. };
  51. struct operation
  52. {
  53. token_ids::type operator_;
  54. operand operand_;
  55. };
  56. struct function_call
  57. {
  58. identifier function_name;
  59. std::list<expression> args;
  60. };
  61. struct expression
  62. {
  63. operand first;
  64. std::list<operation> rest;
  65. };
  66. struct assignment
  67. {
  68. identifier lhs;
  69. expression rhs;
  70. };
  71. struct variable_declaration
  72. {
  73. identifier lhs;
  74. boost::optional<expression> rhs;
  75. };
  76. struct if_statement;
  77. struct while_statement;
  78. struct statement_list;
  79. struct return_statement;
  80. typedef boost::variant<
  81. variable_declaration
  82. , assignment
  83. , boost::recursive_wrapper<if_statement>
  84. , boost::recursive_wrapper<while_statement>
  85. , boost::recursive_wrapper<return_statement>
  86. , boost::recursive_wrapper<statement_list>
  87. >
  88. statement;
  89. struct statement_list : std::list<statement> {};
  90. struct if_statement
  91. {
  92. expression condition;
  93. statement then;
  94. boost::optional<statement> else_;
  95. };
  96. struct while_statement
  97. {
  98. expression condition;
  99. statement body;
  100. };
  101. struct return_statement : tagged
  102. {
  103. boost::optional<expression> expr;
  104. };
  105. struct function
  106. {
  107. std::string return_type;
  108. identifier function_name;
  109. std::list<identifier> args;
  110. statement_list body;
  111. };
  112. typedef std::list<function> function_list;
  113. // print functions for debugging
  114. inline std::ostream& operator<<(std::ostream& out, nil)
  115. {
  116. out << "nil"; return out;
  117. }
  118. inline std::ostream& operator<<(std::ostream& out, identifier const& id)
  119. {
  120. out << id.name; return out;
  121. }
  122. }}
  123. BOOST_FUSION_ADAPT_STRUCT(
  124. client::ast::unary,
  125. (client::token_ids::type, operator_)
  126. (client::ast::operand, operand_)
  127. )
  128. BOOST_FUSION_ADAPT_STRUCT(
  129. client::ast::operation,
  130. (client::token_ids::type, operator_)
  131. (client::ast::operand, operand_)
  132. )
  133. BOOST_FUSION_ADAPT_STRUCT(
  134. client::ast::function_call,
  135. (client::ast::identifier, function_name)
  136. (std::list<client::ast::expression>, args)
  137. )
  138. BOOST_FUSION_ADAPT_STRUCT(
  139. client::ast::expression,
  140. (client::ast::operand, first)
  141. (std::list<client::ast::operation>, rest)
  142. )
  143. BOOST_FUSION_ADAPT_STRUCT(
  144. client::ast::variable_declaration,
  145. (client::ast::identifier, lhs)
  146. (boost::optional<client::ast::expression>, rhs)
  147. )
  148. BOOST_FUSION_ADAPT_STRUCT(
  149. client::ast::assignment,
  150. (client::ast::identifier, lhs)
  151. (client::ast::expression, rhs)
  152. )
  153. BOOST_FUSION_ADAPT_STRUCT(
  154. client::ast::if_statement,
  155. (client::ast::expression, condition)
  156. (client::ast::statement, then)
  157. (boost::optional<client::ast::statement>, else_)
  158. )
  159. BOOST_FUSION_ADAPT_STRUCT(
  160. client::ast::while_statement,
  161. (client::ast::expression, condition)
  162. (client::ast::statement, body)
  163. )
  164. BOOST_FUSION_ADAPT_STRUCT(
  165. client::ast::return_statement,
  166. (boost::optional<client::ast::expression>, expr)
  167. )
  168. BOOST_FUSION_ADAPT_STRUCT(
  169. client::ast::function,
  170. (std::string, return_type)
  171. (client::ast::identifier, function_name)
  172. (std::list<client::ast::identifier>, args)
  173. (client::ast::statement_list, body)
  174. )
  175. #endif