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