1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #if !defined(BOOST_SPIRIT_CONJURE_AST_HPP)
9 #define BOOST_SPIRIT_CONJURE_AST_HPP
11 #include <boost/config/warning_disable.hpp>
12 #include <boost/variant/recursive_variant.hpp>
13 #include <boost/fusion/include/adapt_struct.hpp>
14 #include <boost/fusion/include/io.hpp>
15 #include <boost/spirit/include/support_extended_variant.hpp>
16 #include <boost/spirit/include/support_attributes.hpp>
17 #include <boost/optional.hpp>
22 namespace client { namespace ast
24 ///////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////
29 int id; // Used to annotate the AST with the iterator position.
30 // This id is used as a key to a map<int, Iterator>
31 // (not really part of the AST.)
39 struct identifier : tagged
41 identifier(std::string const& name = "") : name(name) {}
47 boost::spirit::extended_variant<
52 , boost::recursive_wrapper<expression>
55 primary_expr() : base_type() {}
56 primary_expr(bool val) : base_type(val) {}
57 primary_expr(unsigned int val) : base_type(val) {}
58 primary_expr(identifier const& val) : base_type(val) {}
59 primary_expr(expression const& val) : base_type(val) {}
60 primary_expr(primary_expr const& rhs)
61 : base_type(rhs.get()) {}
66 boost::spirit::extended_variant<
69 , boost::recursive_wrapper<unary_expr>
70 , boost::recursive_wrapper<function_call>
73 operand() : base_type() {}
74 operand(primary_expr const& val) : base_type(val) {}
75 operand(unary_expr const& val) : base_type(val) {}
76 operand(function_call const& val) : base_type(val) {}
77 operand(operand const& rhs)
78 : base_type(rhs.get()) {}
81 struct unary_expr : tagged
83 token_ids::type operator_;
89 token_ids::type operator_;
95 identifier function_name;
96 std::list<expression> args;
102 std::list<operation> rest;
108 token_ids::type operator_;
112 struct variable_declaration
115 boost::optional<expression> rhs;
119 struct while_statement;
120 struct statement_list;
121 struct return_statement;
123 typedef boost::variant<
125 , variable_declaration
127 , boost::recursive_wrapper<if_statement>
128 , boost::recursive_wrapper<while_statement>
129 , boost::recursive_wrapper<return_statement>
130 , boost::recursive_wrapper<statement_list>
131 , boost::recursive_wrapper<expression>
135 struct statement_list : std::list<statement> {};
139 expression condition;
141 boost::optional<statement> else_;
144 struct while_statement
146 expression condition;
150 struct return_statement : tagged
152 boost::optional<expression> expr;
157 std::string return_type;
158 identifier function_name;
159 std::list<identifier> args;
160 boost::optional<statement_list> body;
163 typedef std::list<function> function_list;
165 // print functions for debugging
166 inline std::ostream& operator<<(std::ostream& out, nil)
168 out << "nil"; return out;
171 inline std::ostream& operator<<(std::ostream& out, identifier const& id)
173 out << id.name; return out;
177 BOOST_FUSION_ADAPT_STRUCT(
178 client::ast::unary_expr,
179 (client::token_ids::type, operator_)
180 (client::ast::operand, operand_)
183 BOOST_FUSION_ADAPT_STRUCT(
184 client::ast::operation,
185 (client::token_ids::type, operator_)
186 (client::ast::operand, operand_)
189 BOOST_FUSION_ADAPT_STRUCT(
190 client::ast::function_call,
191 (client::ast::identifier, function_name)
192 (std::list<client::ast::expression>, args)
195 BOOST_FUSION_ADAPT_STRUCT(
196 client::ast::expression,
197 (client::ast::operand, first)
198 (std::list<client::ast::operation>, rest)
201 BOOST_FUSION_ADAPT_STRUCT(
202 client::ast::variable_declaration,
203 (client::ast::identifier, lhs)
204 (boost::optional<client::ast::expression>, rhs)
207 BOOST_FUSION_ADAPT_STRUCT(
208 client::ast::assignment,
209 (client::ast::identifier, lhs)
210 (client::token_ids::type, operator_)
211 (client::ast::expression, rhs)
214 BOOST_FUSION_ADAPT_STRUCT(
215 client::ast::if_statement,
216 (client::ast::expression, condition)
217 (client::ast::statement, then)
218 (boost::optional<client::ast::statement>, else_)
221 BOOST_FUSION_ADAPT_STRUCT(
222 client::ast::while_statement,
223 (client::ast::expression, condition)
224 (client::ast::statement, body)
227 BOOST_FUSION_ADAPT_STRUCT(
228 client::ast::return_statement,
229 (boost::optional<client::ast::expression>, expr)
232 BOOST_FUSION_ADAPT_STRUCT(
233 client::ast::function,
234 (std::string, return_type)
235 (client::ast::identifier, function_name)
236 (std::list<client::ast::identifier>, args)
237 (boost::optional<client::ast::statement_list>, body)