ast.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_AST_HPP)
  7. #define BOOST_SPIRIT_MINIC_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 function_call;
  28. struct expression;
  29. struct identifier : tagged
  30. {
  31. identifier(std::string const& name = "") : name(name) {}
  32. std::string name;
  33. };
  34. typedef boost::variant<
  35. nil
  36. , bool
  37. , unsigned int
  38. , identifier
  39. , boost::recursive_wrapper<unary>
  40. , boost::recursive_wrapper<function_call>
  41. , boost::recursive_wrapper<expression>
  42. >
  43. operand;
  44. enum optoken
  45. {
  46. op_plus,
  47. op_minus,
  48. op_times,
  49. op_divide,
  50. op_positive,
  51. op_negative,
  52. op_not,
  53. op_equal,
  54. op_not_equal,
  55. op_less,
  56. op_less_equal,
  57. op_greater,
  58. op_greater_equal,
  59. op_and,
  60. op_or
  61. };
  62. struct unary
  63. {
  64. optoken operator_;
  65. operand operand_;
  66. };
  67. struct operation
  68. {
  69. optoken operator_;
  70. operand operand_;
  71. };
  72. struct function_call
  73. {
  74. identifier function_name;
  75. std::list<expression> args;
  76. };
  77. struct expression
  78. {
  79. operand first;
  80. std::list<operation> rest;
  81. };
  82. struct assignment
  83. {
  84. identifier lhs;
  85. expression rhs;
  86. };
  87. struct variable_declaration
  88. {
  89. identifier lhs;
  90. boost::optional<expression> rhs;
  91. };
  92. struct if_statement;
  93. struct while_statement;
  94. struct statement_list;
  95. struct return_statement;
  96. typedef boost::variant<
  97. variable_declaration
  98. , assignment
  99. , boost::recursive_wrapper<if_statement>
  100. , boost::recursive_wrapper<while_statement>
  101. , boost::recursive_wrapper<return_statement>
  102. , boost::recursive_wrapper<statement_list>
  103. >
  104. statement;
  105. struct statement_list : std::list<statement> {};
  106. struct if_statement
  107. {
  108. expression condition;
  109. statement then;
  110. boost::optional<statement> else_;
  111. };
  112. struct while_statement
  113. {
  114. expression condition;
  115. statement body;
  116. };
  117. struct return_statement : tagged
  118. {
  119. boost::optional<expression> expr;
  120. };
  121. struct function
  122. {
  123. std::string return_type;
  124. identifier function_name;
  125. std::list<identifier> args;
  126. statement_list body;
  127. };
  128. typedef std::list<function> function_list;
  129. // print functions for debugging
  130. inline std::ostream& operator<<(std::ostream& out, nil)
  131. {
  132. out << "nil"; return out;
  133. }
  134. inline std::ostream& operator<<(std::ostream& out, identifier const& id)
  135. {
  136. out << id.name; return out;
  137. }
  138. }}
  139. BOOST_FUSION_ADAPT_STRUCT(
  140. client::ast::unary,
  141. (client::ast::optoken, operator_)
  142. (client::ast::operand, operand_)
  143. )
  144. BOOST_FUSION_ADAPT_STRUCT(
  145. client::ast::operation,
  146. (client::ast::optoken, operator_)
  147. (client::ast::operand, operand_)
  148. )
  149. BOOST_FUSION_ADAPT_STRUCT(
  150. client::ast::function_call,
  151. (client::ast::identifier, function_name)
  152. (std::list<client::ast::expression>, args)
  153. )
  154. BOOST_FUSION_ADAPT_STRUCT(
  155. client::ast::expression,
  156. (client::ast::operand, first)
  157. (std::list<client::ast::operation>, rest)
  158. )
  159. BOOST_FUSION_ADAPT_STRUCT(
  160. client::ast::variable_declaration,
  161. (client::ast::identifier, lhs)
  162. (boost::optional<client::ast::expression>, rhs)
  163. )
  164. BOOST_FUSION_ADAPT_STRUCT(
  165. client::ast::assignment,
  166. (client::ast::identifier, lhs)
  167. (client::ast::expression, rhs)
  168. )
  169. BOOST_FUSION_ADAPT_STRUCT(
  170. client::ast::if_statement,
  171. (client::ast::expression, condition)
  172. (client::ast::statement, then)
  173. (boost::optional<client::ast::statement>, else_)
  174. )
  175. BOOST_FUSION_ADAPT_STRUCT(
  176. client::ast::while_statement,
  177. (client::ast::expression, condition)
  178. (client::ast::statement, body)
  179. )
  180. BOOST_FUSION_ADAPT_STRUCT(
  181. client::ast::return_statement,
  182. (boost::optional<client::ast::expression>, expr)
  183. )
  184. BOOST_FUSION_ADAPT_STRUCT(
  185. client::ast::function,
  186. (std::string, return_type)
  187. (client::ast::identifier, function_name)
  188. (std::list<client::ast::identifier>, args)
  189. (client::ast::statement_list, body)
  190. )
  191. #endif