actions.qbk 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ===============================================================================/]
  8. [section:actions More on Actions]
  9. As you know from the [link phoenix.inside.actor Actors in Detail] section,
  10. Actions are what brings life to a Phoenix expression tree.
  11. When dealing with a Phoenix expression tree, it gets evaluated top-down.
  12. Example:
  13. _1 + 3 * _2
  14. Can be visualized as an AST in the following way:
  15. [$images/simple_ast.png]
  16. In terms of actions this means:
  17. * `rule::plus` is matched
  18. * evaluate left:
  19. * `rule::placeholder` is matched
  20. * evaluate right:
  21. * `rule::multiplies` is matched
  22. * evaluate left:
  23. * `rule::value` is matched
  24. * evaluate right:
  25. * `rule::placeholder` is matched
  26. Every time a rule is matched, an action will be called. The action determines
  27. how the Phoenix AST will be traversed.
  28. [heading Writing an Action]
  29. As mentioned in [link phoenix.inside.actor Actors in Detail] actions are
  30. __proto_primitive_transforms__ for convenience Phoenix provides an abstraction
  31. to this:
  32. template <typename Fun>
  33. struct call;
  34. This is similar to __proto_call__ but does more. It calls the `Fun` function
  35. object passed as template parameter with the `Context` and the children of the
  36. expression associated with the rule.
  37. Lets have an (simplified) example on how to write an evaluation action for
  38. `rule::plus`:
  39. struct plus_eval
  40. {
  41. typedef int result_type;
  42. template <typename Lhs, typename Rhs, typename Context>
  43. result_type operator()(Lhs const& lhs, Rhs const &rhs, Context & ctx)
  44. {
  45. return eval(lhs, ctx) + eval(rhs, ctx);
  46. }
  47. };
  48. template <>
  49. struct default_actions::when<rule::plus>
  50. : call<plus_eval>
  51. {};
  52. That's it. When evaluating a `plus` expression, the `plus_eval` callable gets
  53. called with the left hand side and right hand side expression and the associated
  54. Context.
  55. [*But there is more:] As Actions /can/ be full fletched __proto_transforms__, you can
  56. in fact use any proto expression you can imagine as the action. Phoenix predifines a
  57. set of callables and transform to deal with the Context information passed along and
  58. of course every Phoenix expression can be used as a Phoenix grammar or
  59. __proto_pass_through_transform__.
  60. [variablelist
  61. [
  62. [`functional::context(Env, Actions)`]
  63. [A __proto_callable__ that creates a new context out of the `Env` and `Actions` parameter]
  64. ]
  65. [
  66. [`functional::env(Context)`]
  67. [A __proto_callable__ that returns the environment out of the `Context` parameter]
  68. ]
  69. [
  70. [`functional::actions(Context)`]
  71. [A __proto_callable__ that returns the actions out of the `Context` parameter]
  72. ]
  73. [
  74. [`_context`]
  75. [A __proto_primitive_transform__ that returns the current context]
  76. ]
  77. [
  78. [`_env`]
  79. [A __proto_primitive_transform__ that returns the current environment]
  80. ]
  81. [
  82. [`_actions`]
  83. [A __proto_primitive_transform__ that returns the current actions]
  84. ]
  85. [
  86. [`context(env, actions)`]
  87. [A regular function that creates a context]
  88. ]
  89. [
  90. [`env(ctx)`]
  91. [A regular function that returns the environment from the given context]
  92. ]
  93. [
  94. [`actions(ctx)`]
  95. [A regular function that returns the actions from the given context]
  96. ]
  97. ]
  98. Phoenix is equipped with a predefined set of expressions, rules and actions to
  99. make all the stuff work you learned in the __phoenix_starter_kit__ and __phoenix_modules__
  100. sections. See the [link phoenix.inside.rules next section] for more details!
  101. [endsect]