User Guide
double.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 ///
9 /// THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
10 ///
11 
12 #pragma once
13 
14 /**
15  * \dir
16  * \brief Auto generated AST Implementations
17  *
18  * \file
19  * \brief Auto generated AST classes declaration
20  */
21 
22 #include "ast/ast_decl.hpp"
23 #include "ast/number.hpp"
24 
25 namespace nmodl::ast {
26 
27 /**
28  * \addtogroup ast_class
29  * \ingroup ast
30  * \{
31  */
32 
33 /**
34  * \brief Represents a double variable
35  *
36  * %Double precision float value in the mod file is represented by ast::Double.
37  * For example, float literals like `0.1` in the mod file are parsed as double
38  * and stored using ast::Double::value :
39  *
40  * \code{.mod}
41  * PROCEDURE foo() {
42  * LOCAL x
43  * x = 0.1 + tau
44  * }
45  * \endcode
46  *
47  * Note that the variables are not classified ast integer or float in the AST. The
48  * decision about variable types is done by code generation backends.
49  *
50  * \sa ast::Integer ast::Float
51  *
52 */
53 class Double : public Number {
54  private:
55  /// Value of double
56  std::string value;
57  /// token with location information
58  std::shared_ptr<ModToken> token;
59 
60  public:
61  /// \name Ctor & dtor
62  /// \{
63  explicit Double(const std::string& value);
64  Double(const Double& obj);
65  Double() = default;
66  virtual ~Double() = default;
67  /// \}
68 
69  /**
70  * \brief Check if the ast node is an instance of ast::Double
71  * \return true as object is of type ast::Double
72  */
73  bool is_double () const noexcept override {
74  return true;
75  }
76 
77  /**
78  * \brief Return a copy of the current node
79  *
80  * Recursively make a new copy/clone of the current node including
81  * all members and return a pointer to the node. This is used for
82  * passes like nmodl::visitor::InlineVisitor where nodes are cloned in the
83  * ast.
84  *
85  * \return pointer to the clone/copy of the current node
86  */
87  // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
88  Double* clone() const override {
89  return new Double(*this);
90  }
91  // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
92 
93  /// \name Getters
94  /// \{
95 
96  /**
97  * \brief Return type (ast::AstNodeType) of ast node
98  *
99  * Every node in the ast has a type defined in ast::AstNodeType and this
100  * function is used to retrieve the same.
101  *
102  * \return ast node type i.e. ast::AstNodeType::DOUBLE
103  *
104  * \sa Ast::get_node_type_name
105  */
106  AstNodeType get_node_type() const noexcept override {
107  return AstNodeType::DOUBLE;
108  }
109 
110  /**
111  * \brief Return type (ast::AstNodeType) of ast node as std::string
112  *
113  * Every node in the ast has a type defined in ast::AstNodeType.
114  * This type name can be returned as a std::string for printing
115  * node to text/json form.
116  *
117  * \return name of the node type as a string i.e. "Double"
118  *
119  * \sa Ast::get_node_name
120  */
121  std::string get_node_type_name() const noexcept override {
122  return "Double";
123  }
124 
125 
126  /**
127  * \brief Get std::shared_ptr from `this` pointer of the current ast node
128  */
129  std::shared_ptr<Ast> get_shared_ptr() override {
130  return std::static_pointer_cast<Double>(shared_from_this());
131  }
132 
133  /**
134  * \brief Get std::shared_ptr from `this` pointer of the current ast node
135  */
136  std::shared_ptr<const Ast> get_shared_ptr() const override {
137  return std::static_pointer_cast<const Double>(shared_from_this());
138  }
139 
140  /**
141  * \brief Return associated token for the current ast node
142  *
143  * Not all ast nodes have token information. For example, nmodl::visitor::NeuronSolveVisitor
144  * can insert new nodes in the ast as a solution of ODEs. In this case, we return
145  * nullptr to store in the nmodl::symtab::SymbolTable.
146  *
147  * \return pointer to token if exist otherwise nullptr
148  */
149  const ModToken* get_token() const noexcept override {
150  return token.get();
151  }
152 
153 
154 
155  /**
156  * \brief Getter for member variable \ref Double.value
157  */
158  const std::string& get_value() const noexcept {
159  return value;
160  }
161  /// \}
162 
163  /// \name Setters
164  /// \{
165  /**
166  * \brief Set token for the current ast node
167  */
168  void set_token(const ModToken& tok) { token = std::make_shared<ModToken>(tok); }
169  /**
170  * \brief Set new value to the current ast node
171  * \sa Double::eval
172  */
173  void set(std::string _value) {
174  value = _value;
175  }
176 
177  /**
178  * \brief Setter for member variable \ref Double.value
179  */
180  void set_value(std::string value);
181 
182  /// \}
183 
184  /// \name Visitor
185  /// \{
186  /**
187  * \brief visit children i.e. member variables of current node using provided visitor
188  *
189  * Different nodes in the AST have different members (i.e. children). This method
190  * recursively visits children using provided visitor.
191  *
192  * \param v Concrete visitor that will be used to recursively visit children
193  *
194  * \sa Ast::visit_children for example.
195  */
196  void visit_children(visitor::Visitor& v) override;
197 
198  /**
199  * \brief visit children i.e. member variables of current node using provided visitor
200  *
201  * Different nodes in the AST have different members (i.e. children). This method
202  * recursively visits children using provided visitor.
203  *
204  * \param v Concrete constant visitor that will be used to recursively visit children
205  *
206  * \sa Ast::visit_children for example.
207  */
208  void visit_children(visitor::ConstVisitor& v) const override;
209 
210  /**
211  * \brief accept (or visit) the current AST node using provided visitor
212  *
213  * Instead of visiting children of AST node, like Ast::visit_children,
214  * accept allows to visit the current node itself using provided concrete
215  * visitor.
216  *
217  * \param v Concrete visitor that will be used to recursively visit node
218  *
219  * \sa Ast::accept for example.
220  */
221  void accept(visitor::Visitor& v) override;
222 
223  /**
224  * \copydoc accept(visitor::Visitor&)
225  */
226  void accept(visitor::ConstVisitor& v) const override;
227  /// \}
228 
229  /**
230  * \brief Negate the value of current ast node
231  *
232  * Parser parse `-x` in two parts : `x` and then `-`. Once second token
233  * `-` is encountered, the corresponding value of ast node needs to be
234  * multiplied by `-1` for ast::Number node types.
235  */
236  void negate() override {
237  value = value.insert (0, 1, '-');
238  }
239 
240  /**
241  * \brief Return value of the current ast node as double
242  */
243  double to_double() override {
244  return std::stod(value);
245  }
246  /**
247  * \brief Return value of the ast node
248  *
249  * Base data type nodes like ast::Inetegr, ast::Double can be evaluated
250  * to their literal values. This method is used to access underlying
251  * literal value.
252  *
253  * \sa Double::set
254  */
255  std::string eval() const {
256  return value;
257  }
258  private:
259  /**
260  * \brief Set this object as parent for all the children
261  *
262  * This should be called in every object (with children) constructor
263  * to set parents. Since it is called only in the constructors it
264  * should not be virtual to avoid ambiguities (issue #295).
265  */
266  void set_parent_in_children();
267 };
268 
269 /** \} */ // end of ast_class
270 
271 
272 
273 } // namespace nmodl::ast
nmodl::visitor::ConstVisitor
Abstract base class for all constant visitors implementation.
Definition: visitor.hpp:298
nmodl::ast::Double::token
std::shared_ptr< ModToken > token
token with location information
Definition: double.hpp:58
nmodl::ast::Double::is_double
bool is_double() const noexcept override
Check if the ast node is an instance of ast::Double.
Definition: double.hpp:73
nmodl::ast::Double::eval
std::string eval() const
Return value of the ast node.
Definition: double.hpp:255
nmodl::ast::Double::accept
void accept(visitor::Visitor &v) override
accept (or visit) the current AST node using provided visitor
Definition: ast.cpp:681
nmodl::ast::Double
Represents a double variable.
Definition: double.hpp:53
nmodl::ast::Double::~Double
virtual ~Double()=default
ast_decl.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::ast::Double::get_node_type_name
std::string get_node_type_name() const noexcept override
Return type (ast::AstNodeType) of ast node as std::string.
Definition: double.hpp:121
nmodl::ast::Double::get_token
const ModToken * get_token() const noexcept override
Return associated token for the current ast node.
Definition: double.hpp:149
nmodl::ast::Double::to_double
double to_double() override
Return value of the current ast node as double.
Definition: double.hpp:243
nmodl::ast::Double::get_shared_ptr
std::shared_ptr< Ast > get_shared_ptr() override
Get std::shared_ptr from this pointer of the current ast node.
Definition: double.hpp:129
nmodl::ast::Double::get_value
const std::string & get_value() const noexcept
Getter for member variable Double::value.
Definition: double.hpp:158
number.hpp
Auto generated AST classes declaration.
nmodl::ast
Abstract Syntax Tree (AST) related implementations.
Definition: ast_common.hpp:29
nmodl::ast::Double::set_value
void set_value(std::string value)
Setter for member variable Double::value.
Definition: ast.cpp:718
nmodl::ast::Double::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:675
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:164
nmodl::ast::AstNodeType::DOUBLE
@ DOUBLE
type of ast::Double
nmodl::visitor::Visitor
Abstract base class for all visitors implementation.
Definition: visitor.hpp:39
nmodl::ast::Double::set
void set(std::string _value)
Set new value to the current ast node.
Definition: double.hpp:173
nmodl::ast::Double::get_shared_ptr
std::shared_ptr< const Ast > get_shared_ptr() const override
Get std::shared_ptr from this pointer of the current ast node.
Definition: double.hpp:136
nmodl::ast::Double::negate
void negate() override
Negate the value of current ast node.
Definition: double.hpp:236
nmodl::ast::Double::get_node_type
AstNodeType get_node_type() const noexcept override
Return type (ast::AstNodeType) of ast node.
Definition: double.hpp:106
nmodl::ast::Double::clone
Double * clone() const override
Return a copy of the current node.
Definition: double.hpp:88
nmodl::ast::Double::value
std::string value
Value of double.
Definition: double.hpp:56
nmodl::ast::Double::set_parent_in_children
void set_parent_in_children()
Set this object as parent for all the children.
Definition: ast.cpp:711
nmodl::ast::Number
Base class for all numbers.
Definition: number.hpp:39
nmodl::ast::Double::Double
Double()=default
nmodl::ast::Double::set_token
void set_token(const ModToken &tok)
Set token for the current ast node.
Definition: double.hpp:168
nmodl::ModToken
Represent token returned by scanner.
Definition: modtoken.hpp:50