generate_initialization.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GENERATOR_GENERATE_INITIALIZATION_HPP
2 #define STAN_LANG_GENERATOR_GENERATE_INITIALIZATION_HPP
3 
4 #include <stan/lang/ast.hpp>
9 #include <ostream>
10 #include <string>
11 #include <vector>
12 
13 namespace stan {
14  namespace lang {
15 
16  /**
17  * Generate varaible initialization, checking dimension sizes are
18  * all positive, gnerating to the specified stream for a variable
19  * with the specified name, type, dimension sizes, and optional
20  * matrix/vector size declarations.
21  *
22  * @param[in,out] o stream for generating
23  * @param[in] indent indentation level
24  * @param[in] var_name name of variable being initialized
25  * @param[in] base_type base type of variable
26  * @param[in] dims dimension sizes
27  * @param[in] type_arg1 optional vector/row-vector size or matrix
28  * rows
29  * @param[in] type_arg2 optional size of matrix columns
30  */
31  void generate_initialization(std::ostream& o, size_t indent,
32  const std::string& var_name,
33  const std::string& base_type,
34  const std::vector<expression>& dims,
35  const expression& type_arg1 = expression(),
36  const expression& type_arg2 = expression()) {
37  // check array dimensions
38  for (size_t i = 0; i < dims.size(); ++i)
39  generate_validate_positive(var_name, dims[i], indent, o);
40  // check vector, row_vector, and matrix rows
41  if (!is_nil(type_arg1))
42  generate_validate_positive(var_name, type_arg1, indent, o);
43  // check matrix cols
44  if (!is_nil(type_arg2))
45  generate_validate_positive(var_name, type_arg2, indent, o);
46 
47  // initialize variable
48  generate_indent(indent, o);
49  o << var_name << " = ";
50  generate_type(base_type, dims, dims.size(), o);
51  generate_initializer(o, base_type, dims, type_arg1, type_arg2);
52  }
53 
54  }
55 }
56 #endif
void generate_validate_positive(const std::string &var_name, const expression &expr, int indents, std::ostream &o)
void generate_indent(size_t indent, std::ostream &o)
void generate_initialization(std::ostream &o, size_t indent, const std::string &var_name, const std::string &base_type, const std::vector< expression > &dims, const expression &type_arg1=expression(), const expression &type_arg2=expression())
bool is_nil(const expression &e)
Definition: is_nil_def.hpp:10
double lang
Definition: runWimpSim.h:113
std::string indent(std::size_t const i)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void generate_initializer(std::ostream &o, const std::string &base_type, const std::vector< expression > &dims, const expression &type_arg1=expression(), const expression &type_arg2=expression())
void generate_type(const std::string &base_type, const std::vector< expression > &, size_t end, std::ostream &o)