1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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_MINIC_AST_HPP)
8 #define BOOST_SPIRIT_MINIC_AST_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>
16 namespace client { namespace ast
18 ///////////////////////////////////////////////////////////////////////////
20 ///////////////////////////////////////////////////////////////////////////
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.)
33 struct identifier : tagged
35 identifier(std::string const& name = "") : name(name) {}
39 typedef boost::variant<
44 , boost::recursive_wrapper<unary>
45 , boost::recursive_wrapper<function_call>
46 , boost::recursive_wrapper<expression>
83 identifier function_name;
84 std::list<expression> args;
90 std::list<operation> rest;
99 struct variable_declaration
102 boost::optional<expression> rhs;
106 struct while_statement;
107 struct statement_list;
108 struct return_statement;
110 typedef boost::variant<
113 , boost::recursive_wrapper<if_statement>
114 , boost::recursive_wrapper<while_statement>
115 , boost::recursive_wrapper<return_statement>
116 , boost::recursive_wrapper<statement_list>
120 struct statement_list : std::list<statement> {};
124 expression condition;
126 boost::optional<statement> else_;
129 struct while_statement
131 expression condition;
135 struct return_statement : tagged
137 boost::optional<expression> expr;
142 std::string return_type;
143 identifier function_name;
144 std::list<identifier> args;
148 typedef std::list<function> function_list;
150 // print functions for debugging
151 inline std::ostream& operator<<(std::ostream& out, nil)
153 out << "nil"; return out;
156 inline std::ostream& operator<<(std::ostream& out, identifier const& id)
158 out << id.name; return out;
162 BOOST_FUSION_ADAPT_STRUCT(
164 (client::ast::optoken, operator_)
165 (client::ast::operand, operand_)
168 BOOST_FUSION_ADAPT_STRUCT(
169 client::ast::operation,
170 (client::ast::optoken, operator_)
171 (client::ast::operand, operand_)
174 BOOST_FUSION_ADAPT_STRUCT(
175 client::ast::function_call,
176 (client::ast::identifier, function_name)
177 (std::list<client::ast::expression>, args)
180 BOOST_FUSION_ADAPT_STRUCT(
181 client::ast::expression,
182 (client::ast::operand, first)
183 (std::list<client::ast::operation>, rest)
186 BOOST_FUSION_ADAPT_STRUCT(
187 client::ast::variable_declaration,
188 (client::ast::identifier, lhs)
189 (boost::optional<client::ast::expression>, rhs)
192 BOOST_FUSION_ADAPT_STRUCT(
193 client::ast::assignment,
194 (client::ast::identifier, lhs)
195 (client::ast::expression, rhs)
198 BOOST_FUSION_ADAPT_STRUCT(
199 client::ast::if_statement,
200 (client::ast::expression, condition)
201 (client::ast::statement, then)
202 (boost::optional<client::ast::statement>, else_)
205 BOOST_FUSION_ADAPT_STRUCT(
206 client::ast::while_statement,
207 (client::ast::expression, condition)
208 (client::ast::statement, body)
211 BOOST_FUSION_ADAPT_STRUCT(
212 client::ast::return_statement,
213 (boost::optional<client::ast::expression>, expr)
216 BOOST_FUSION_ADAPT_STRUCT(
217 client::ast::function,
218 (std::string, return_type)
219 (client::ast::identifier, function_name)
220 (std::list<client::ast::identifier>, args)
221 (client::ast::statement_list, body)