1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*=============================================================================
Copyright (c) 2001-2010 Joel de Guzman
Copyright (c) 2001-2010 Hartmut Kaiser
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A Calculator example demonstrating generation of AST which gets dumped into
// a human readable format afterwards.
//
// [ JDG April 28, 2008 ]
// [ HK April 28, 2008 ]
//
///////////////////////////////////////////////////////////////////////////////
#if !defined(SPIRIT_EXAMPLE_CALC2_AST_APR_30_2008_1011AM)
#define SPIRIT_EXAMPLE_CALC2_AST_APR_30_2008_1011AM
#include <boost/variant.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/spirit/include/karma_domain.hpp>
#include <boost/spirit/include/support_attributes_fwd.hpp>
///////////////////////////////////////////////////////////////////////////////
// Our AST
///////////////////////////////////////////////////////////////////////////////
struct binary_op;
struct unary_op;
struct nil {};
struct expression_ast
{
typedef
boost::variant<
nil // can't happen!
, int
, boost::recursive_wrapper<binary_op>
, boost::recursive_wrapper<unary_op>
>
type;
// expose variant types
typedef type::types types;
// expose variant functionality
int which() const { return expr.which(); }
// constructors
expression_ast()
: expr(nil()) {}
expression_ast(unary_op const& expr)
: expr(expr) {}
expression_ast(binary_op const& expr)
: expr(expr) {}
expression_ast(unsigned int expr)
: expr(expr) {}
expression_ast(type const& expr)
: expr(expr) {}
expression_ast& operator+=(expression_ast const& rhs);
expression_ast& operator-=(expression_ast const& rhs);
expression_ast& operator*=(expression_ast const& rhs);
expression_ast& operator/=(expression_ast const& rhs);
type expr;
};
// expose variant functionality
namespace boost
{
// this function has to live in namespace boost for ADL to correctly find it
template <typename T>
inline T get(expression_ast const& expr)
{
return boost::get<T>(expr.expr);
}
namespace spirit { namespace traits
{
// the specialization below tells Spirit to handle expression_ast as
// if it where a 'real' variant (if used with Spirit.Karma)
template <>
struct not_is_variant<expression_ast, karma::domain>
: mpl::false_ {};
// the specialization of variant_which allows to generically extract
// the current type stored in the given variant like type
template <>
struct variant_which<expression_ast>
{
static int call(expression_ast const& v)
{
return v.which();
}
};
}}
}
///////////////////////////////////////////////////////////////////////////////
struct binary_op
{
binary_op() {}
binary_op(
char op
, expression_ast const& left
, expression_ast const& right)
: op(op), left(left), right(right) {}
char op;
expression_ast left;
expression_ast right;
};
struct unary_op
{
unary_op(
char op
, expression_ast const& right)
: op(op), right(right) {}
char op;
expression_ast right;
};
inline expression_ast& expression_ast::operator+=(expression_ast const& rhs)
{
expr = binary_op('+', expr, rhs);
return *this;
}
inline expression_ast& expression_ast::operator-=(expression_ast const& rhs)
{
expr = binary_op('-', expr, rhs);
return *this;
}
inline expression_ast& expression_ast::operator*=(expression_ast const& rhs)
{
expr = binary_op('*', expr, rhs);
return *this;
}
inline expression_ast& expression_ast::operator/=(expression_ast const& rhs)
{
expr = binary_op('/', expr, rhs);
return *this;
}
// We should be using expression_ast::operator-. There's a bug
// in phoenix type deduction mechanism that prevents us from
// doing so. Phoenix will be switching to BOOST_TYPEOF. In the
// meantime, we will use a phoenix::function below:
template <char Op>
struct unary_expr
{
template <typename T>
struct result { typedef T type; };
expression_ast operator()(expression_ast const& expr) const
{
return unary_op(Op, expr);
}
};
boost::phoenix::function<unary_expr<'+'> > pos;
boost::phoenix::function<unary_expr<'-'> > neg;
#endif
|