|
Boost Users : |
Subject: Re: [Boost-users] [phoenix][v3] Evaluating a terminal expression in the custom actions class.
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2011-03-02 11:00:32
On Wed, Mar 2, 2011 at 3:03 PM, Alexander Ospin <aospin_at_[hidden]> wrote:
> Is it possible to evaluate terminal and custom_terminal expressions in the
> some custom actions class? For instance how to implement "terminal_eval" to
> print something in the following code?
>
> #include <boost/phoenix/phoenix.hpp>
> #include <iostream>
>
> using namespace boost;
>
> struct actions
> {
> Â Â template <typename Rule, typename Dummy = void> struct when;
> };
>
> template <>
> struct actions::when<phoenix::rule::terminal> :
> Â Â phoenix::call<terminal_eval> {};
>
> int main()
> {
> Â Â int var = 11;
> Â Â phoenix::eval(phoenix::val(var), phoenix::context(int(), actions()));
> Â Â return 0;
> }
>
>
> I've tried to use the following form, but encountered some compiler errors:
> (GCC-4.2.1[FreeBSD], boost and phoenix were taken from SVN, latest versions)
>
> struct terminal_eval
> {
> Â Â typedef void result_type;
> Â Â template <typename Context, typename Expr>
> Â Â result_type operator ()(Context const &ctx, Expr const &expr)
> Â Â {
> Â Â Â Â std::cout << "terminal" << std::endl;
> Â Â }
> };
Hi,
phoenix::call does not work with terminals (which have an arity of 0).
The reason for this is because I didn't want to dictate what should be
passed to the eval function, the possibilities are: the value of the
terminal, or the proto::terminal type itself, the reason i couldn't
decide is because phoenix::call passes along proto expressions to the
to be called function object, and not the result of the evaluation.
You're code will work by doing the following:
template <>
struct actions::when<phoenix::rule::terminal> :
proto::call<terminal_eval(phoenix::_ctx, proto::_)>
{};
or:
template <>
struct actions::when<phoenix::rule::terminal> :
proto::call<terminal_eval(phoenix::_ctx, proto::_value)>
{};
HTH,
Thomas
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net