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_CALC8_AST_HPP)
8 #define BOOST_SPIRIT_CALC8_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.)
32 struct variable : tagged
34 variable(std::string const& name = "") : name(name) {}
38 typedef boost::variant<
43 , boost::recursive_wrapper<unary>
44 , boost::recursive_wrapper<expression>
82 std::list<operation> rest;
91 struct variable_declaration
97 struct while_statement;
98 struct statement_list;
100 typedef boost::variant<
103 , boost::recursive_wrapper<if_statement>
104 , boost::recursive_wrapper<while_statement>
105 , boost::recursive_wrapper<statement_list>
109 struct statement_list : std::list<statement> {};
113 expression condition;
115 boost::optional<statement> else_;
118 struct while_statement
120 expression condition;
124 // print functions for debugging
125 inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
126 inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
129 BOOST_FUSION_ADAPT_STRUCT(
131 (client::ast::optoken, operator_)
132 (client::ast::operand, operand_)
135 BOOST_FUSION_ADAPT_STRUCT(
136 client::ast::operation,
137 (client::ast::optoken, operator_)
138 (client::ast::operand, operand_)
141 BOOST_FUSION_ADAPT_STRUCT(
142 client::ast::expression,
143 (client::ast::operand, first)
144 (std::list<client::ast::operation>, rest)
147 BOOST_FUSION_ADAPT_STRUCT(
148 client::ast::variable_declaration,
149 (client::ast::assignment, assign)
152 BOOST_FUSION_ADAPT_STRUCT(
153 client::ast::assignment,
154 (client::ast::variable, lhs)
155 (client::ast::expression, rhs)
158 BOOST_FUSION_ADAPT_STRUCT(
159 client::ast::if_statement,
160 (client::ast::expression, condition)
161 (client::ast::statement, then)
162 (boost::optional<client::ast::statement>, else_)
165 BOOST_FUSION_ADAPT_STRUCT(
166 client::ast::while_statement,
167 (client::ast::expression, condition)
168 (client::ast::statement, body)