Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
expression07_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__PARSER__EXPRESSION_GRAMMAR07_DEF__HPP__
2 #define STAN__GM__PARSER__EXPRESSION_GRAMMAR07_DEF__HPP__
3 
4 #include <cstddef>
5 #include <iomanip>
6 #include <iostream>
7 #include <istream>
8 #include <map>
9 #include <set>
10 #include <sstream>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include <stdexcept>
15 
16 #include <boost/spirit/include/qi.hpp>
17 // FIXME: get rid of unused include
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_function.hpp>
20 #include <boost/spirit/include/phoenix_fusion.hpp>
21 #include <boost/spirit/include/phoenix_object.hpp>
22 #include <boost/spirit/include/phoenix_operator.hpp>
23 #include <boost/spirit/include/phoenix_stl.hpp>
24 
25 #include <boost/lexical_cast.hpp>
26 #include <boost/fusion/include/adapt_struct.hpp>
27 #include <boost/fusion/include/std_pair.hpp>
28 #include <boost/config/warning_disable.hpp>
29 #include <boost/spirit/include/qi.hpp>
30 #include <boost/spirit/include/qi_numeric.hpp>
31 #include <boost/spirit/include/classic_position_iterator.hpp>
32 #include <boost/spirit/include/phoenix_core.hpp>
33 #include <boost/spirit/include/phoenix_function.hpp>
34 #include <boost/spirit/include/phoenix_fusion.hpp>
35 #include <boost/spirit/include/phoenix_object.hpp>
36 #include <boost/spirit/include/phoenix_operator.hpp>
37 #include <boost/spirit/include/phoenix_stl.hpp>
38 #include <boost/spirit/include/support_multi_pass.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/variant/apply_visitor.hpp>
41 #include <boost/variant/recursive_variant.hpp>
42 
43 #include <stan/gm/ast.hpp>
48 
49 
50 namespace stan {
51 
52  namespace gm {
53 
55  template <typename T1, typename T2>
56  struct result { typedef bool type; };
57 
58  bool operator()(const expression& expr,
59  std::ostream& error_msgs) const {
60  if (expr.expression_type().is_ill_formed()) {
61  error_msgs << "expression is ill formed" << std::endl;
62  return false;
63  }
64  return true;
65  }
66  };
67  boost::phoenix::function<validate_expr_type3> validate_expr_type3_f;
68 
69  // FIXME: cut and paste from term grammar, having trouble w. includes
70  struct set_fun_type3 {
71  template <typename T1, typename T2>
72  struct result { typedef fun type; };
73 
75  std::ostream& error_msgs) const {
76  std::vector<expr_type> arg_types;
77  for (size_t i = 0; i < fun.args_.size(); ++i)
78  arg_types.push_back(fun.args_[i].expression_type());
80  arg_types,
81  error_msgs);
82  return fun;
83  }
84  };
85  boost::phoenix::function<set_fun_type3> set_fun_type3_f;
86 
87  struct addition_expr3 {
88  template <typename T1, typename T2, typename T3>
89  struct result { typedef expression type; };
90 
92  const expression& expr2,
93  std::ostream& error_msgs) const {
94  if (expr1.expression_type().is_primitive()
95  && expr2.expression_type().is_primitive()) {
96  return expr1 += expr2;
97  }
98  std::vector<expression> args;
99  args.push_back(expr1);
100  args.push_back(expr2);
101  set_fun_type3 sft;
102  fun f("add",args);
103  sft(f,error_msgs);
104  return expression(f);
105  return expr1 += expr2;
106  }
107  };
108  boost::phoenix::function<addition_expr3> addition3_f;
109 
110 
112  template <typename T1, typename T2, typename T3>
113  struct result { typedef expression type; };
114 
116  const expression& expr2,
117  std::ostream& error_msgs) const {
118  if (expr1.expression_type().is_primitive()
119  && expr2.expression_type().is_primitive()) {
120  return expr1 -= expr2;
121  }
122  std::vector<expression> args;
123  args.push_back(expr1);
124  args.push_back(expr2);
125  set_fun_type3 sft;
126  fun f("subtract",args);
127  sft(f,error_msgs);
128  return expression(f);
129  }
130  };
131  boost::phoenix::function<subtraction_expr3> subtraction3_f;
132 
133 
134 
135  template <typename Iterator>
137  std::stringstream& error_msgs,
139  : expression07_grammar::base_type(expression07_r),
140  var_map_(var_map),
141  error_msgs_(error_msgs),
142  term_g(var_map,error_msgs,eg)
143  {
144  using boost::spirit::qi::_1;
145  using boost::spirit::qi::char_;
146  using boost::spirit::qi::double_;
147  using boost::spirit::qi::eps;
148  using boost::spirit::qi::int_;
149  using boost::spirit::qi::lexeme;
150  using boost::spirit::qi::lit;
151  using boost::spirit::qi::_pass;
152  using boost::spirit::qi::_val;
153  using boost::spirit::qi::labels::_r1;
154 
155  expression07_r.name("expression, precedence 7, binary +, -");
157  = term_g(_r1)
158  [_val = _1]
159  > *( ( lit('+')
160  > term_g(_r1) // expression07_r
161  [_val = addition3_f(_val,_1,boost::phoenix::ref(error_msgs))] )
162  |
163  ( lit('-')
164  > term_g(_r1) // expression07_r
165  [_val = subtraction3_f(_val,_1,boost::phoenix::ref(error_msgs))] )
166  )
167  > eps[_pass = validate_expr_type3_f(_val,boost::phoenix::ref(error_msgs_))]
168  ;
169 
170 
171  }
172  }
173 }
174 
175 #endif
std::string name_
Definition: ast.hpp:351
expr_type type_
Definition: ast.hpp:353
static function_signatures & instance()
Definition: ast_def.cpp:143
expression operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
std::vector< expression > args_
Definition: ast.hpp:352
boost::phoenix::function< validate_expr_type3 > validate_expr_type3_f
boost::phoenix::function< addition_expr3 > addition3_f
expression07_grammar(variable_map &var_map, std::stringstream &error_msgs, expression_grammar< Iterator > &eg)
boost::phoenix::function< set_fun_type3 > set_fun_type3_f
fun operator()(fun &fun, std::ostream &error_msgs) const
boost::phoenix::function< subtraction_expr3 > subtraction3_f
bool is_ill_formed() const
Definition: ast_def.cpp:105
expression operator()(expression &expr1, const expression &expr2, std::ostream &error_msgs) const
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > expression07_r
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs)
Definition: ast_def.cpp:329
bool operator()(const expression &expr, std::ostream &error_msgs) const
expr_type expression_type() const
Definition: ast_def.cpp:570
bool is_primitive() const
Definition: ast_def.cpp:93

     [ Stan Home Page ] © 2011–2014, Stan Development Team.