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/config/warning_disable.hpp>
11 #include <boost/variant/recursive_variant.hpp>
12 #include <boost/fusion/include/adapt_struct.hpp>
13 #include <boost/fusion/include/io.hpp>
14 #include <boost/optional.hpp>
17 namespace client { namespace ast
19 ///////////////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////////////
24 int id; // Used to annotate the AST with the iterator position.
25 // This id is used as a key to a map<int, Iterator>
26 // (not really part of the AST.)
33 struct variable : tagged
35 variable(std::string const& name = "") : name(name) {}
39 typedef boost::variant<
44 , boost::recursive_wrapper<unary>
45 , boost::recursive_wrapper<expression>
83 std::list<operation> rest;
92 struct variable_declaration
98 struct while_statement;
99 struct statement_list;
101 typedef boost::variant<
104 , boost::recursive_wrapper<if_statement>
105 , boost::recursive_wrapper<while_statement>
106 , boost::recursive_wrapper<statement_list>
110 struct statement_list : std::list<statement> {};
114 expression condition;
116 boost::optional<statement> else_;
119 struct while_statement
121 expression condition;
125 // print functions for debugging
126 inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
127 inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
130 BOOST_FUSION_ADAPT_STRUCT(
132 (client::ast::optoken, operator_)
133 (client::ast::operand, operand_)
136 BOOST_FUSION_ADAPT_STRUCT(
137 client::ast::operation,
138 (client::ast::optoken, operator_)
139 (client::ast::operand, operand_)
142 BOOST_FUSION_ADAPT_STRUCT(
143 client::ast::expression,
144 (client::ast::operand, first)
145 (std::list<client::ast::operation>, rest)
148 BOOST_FUSION_ADAPT_STRUCT(
149 client::ast::variable_declaration,
150 (client::ast::assignment, assign)
153 BOOST_FUSION_ADAPT_STRUCT(
154 client::ast::assignment,
155 (client::ast::variable, lhs)
156 (client::ast::expression, rhs)
159 BOOST_FUSION_ADAPT_STRUCT(
160 client::ast::if_statement,
161 (client::ast::expression, condition)
162 (client::ast::statement, then)
163 (boost::optional<client::ast::statement>, else_)
166 BOOST_FUSION_ADAPT_STRUCT(
167 client::ast::while_statement,
168 (client::ast::expression, condition)
169 (client::ast::statement, body)