Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
expression_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__PARSER__EXPRESSION_GRAMMAR_DEF__HPP
2 #define STAN__GM__PARSER__EXPRESSION_GRAMMAR_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 
54 
55  // FIXME: cut and paste from term grammar, having trouble w. includes
56  struct set_fun_type2 {
57  template <typename T1, typename T2>
58  struct result { typedef fun type; };
59 
61  std::ostream& error_msgs) const {
62  std::vector<expr_type> arg_types;
63  for (size_t i = 0; i < fun.args_.size(); ++i)
64  arg_types.push_back(fun.args_[i].expression_type());
66  arg_types,
67  error_msgs);
68  return fun;
69  }
70  };
71  boost::phoenix::function<set_fun_type2> set_fun_type2_f;
72 
73  struct binary_op_expr {
74  template <typename T1, typename T2, typename T3, typename T4, typename T5>
75  struct result { typedef expression type; };
76 
78  const expression& expr2,
79  const std::string& op,
80  const std::string& fun_name,
81  std::ostream& error_msgs) const {
82  if (!expr1.expression_type().is_primitive()
83  || !expr2.expression_type().is_primitive()) {
84  error_msgs << "binary infix operator "
85  << op
86  << " with functional interpretation "
87  << fun_name
88  << " requires arguments or primitive type (int or real)"
89  << ", found left type=" << expr1.expression_type()
90  << ", right arg type=" << expr2.expression_type()
91  << "; ";
92  }
93  std::vector<expression> args;
94  args.push_back(expr1);
95  args.push_back(expr2);
96  set_fun_type2 sft;
97  fun f(fun_name,args);
98  sft(f,error_msgs);
99  return expression(f);
100  }
101  };
102  boost::phoenix::function<binary_op_expr> binary_op_f;
103 
104 
105  template <typename Iterator>
107  std::stringstream& error_msgs)
108  : expression_grammar::base_type(expression_r),
109  var_map_(var_map),
110  error_msgs_(error_msgs),
111  expression07_g(var_map,error_msgs,*this)
112  {
113  using boost::spirit::qi::_1;
114  using boost::spirit::qi::char_;
115  using boost::spirit::qi::double_;
116  using boost::spirit::qi::eps;
117  using boost::spirit::qi::int_;
118  using boost::spirit::qi::lexeme;
119  using boost::spirit::qi::lit;
120  using boost::spirit::qi::_pass;
121  using boost::spirit::qi::_val;
122  using boost::spirit::qi::labels::_r1;
123 
124  // _r1 : var_origin
125 
126  expression_r.name("expression (top level, precedence 15, binary ||");
128  = expression14_r(_r1) [_val = _1]
129  > *( lit("||")
130  > expression14_r(_r1) [_val = binary_op_f(_val,_1,"||","logical_or",
131  boost::phoenix::ref(error_msgs))]
132  );
133 
134  expression14_r.name("expression, precedence 14, binary &&");
136  = expression10_r(_r1) [_val = _1]
137  > *( lit("&&")
138  > expression10_r(_r1) [_val = binary_op_f(_val,_1,"&&","logical_and",
139  boost::phoenix::ref(error_msgs))]
140  );
141 
142  expression10_r.name("expression, precedence 10, binary ==, !=");
144  = expression09_r(_r1) [_val = _1]
145  > *( ( lit("==")
146  > expression09_r(_r1) [_val = binary_op_f(_val,_1,"==","logical_eq",
147  boost::phoenix::ref(error_msgs))] )
148  |
149  ( lit("!=")
150  > expression09_r(_r1) [_val = binary_op_f(_val,_1,"!=","logical_neq",
151  boost::phoenix::ref(error_msgs))] )
152  );
153 
154  expression09_r.name("expression, precedence 9, binary <, <=, >, >=");
156  = expression07_g(_r1) [_val = _1]
157  > *( ( lit("<=")
158  > expression07_g(_r1) [_val = binary_op_f(_val,_1,"<","logical_lte",
159  boost::phoenix::ref(error_msgs))] )
160  |
161  ( lit("<")
162  > expression07_g(_r1) [_val = binary_op_f(_val,_1,"<=","logical_lt",
163  boost::phoenix::ref(error_msgs))] )
164  |
165  ( lit(">=")
166  > expression07_g(_r1) [_val = binary_op_f(_val,_1,">","logical_gte",
167  boost::phoenix::ref(error_msgs))] )
168  |
169  ( lit(">")
170  > expression07_g(_r1) [_val = binary_op_f(_val,_1,">=","logical_gt",
171  boost::phoenix::ref(error_msgs))] )
172  );
173 
174  }
175  }
176 }
177 
178 #endif
boost::phoenix::function< set_fun_type2 > set_fun_type2_f
std::string name_
Definition: ast.hpp:351
expr_type type_
Definition: ast.hpp:353
static function_signatures & instance()
Definition: ast_def.cpp:143
boost::phoenix::function< binary_op_expr > binary_op_f
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > expression09_r
std::vector< expression > args_
Definition: ast.hpp:352
expression07_grammar< Iterator > expression07_g
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > expression14_r
fun operator()(fun &fun, std::ostream &error_msgs) const
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > expression_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > expression10_r
expression operator()(expression &expr1, const expression &expr2, const std::string &op, const std::string &fun_name, std::ostream &error_msgs) const
expression_grammar(variable_map &var_map, std::stringstream &error_msgs)
expr_type get_result_type(const std::string &name, const std::vector< expr_type > &args, std::ostream &error_msgs)
Definition: ast_def.cpp:329
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.