NEURON
index_remover.cpp
Go to the documentation of this file.
1 #include "index_remover.hpp"
2 
3 #include "utils/logger.hpp"
5 
6 namespace nmodl {
7 namespace visitor {
8 
9 IndexRemover::IndexRemover(std::string index, int value)
10  : index(std::move(index))
11  , value(value) {}
12 
13 /// if expression we are visiting is `Name` then return new `Integer` node
14 std::shared_ptr<ast::Expression> IndexRemover::replace_for_name(
15  const std::shared_ptr<ast::Expression>& node) const {
16  if (node->is_name()) {
17  auto name = std::dynamic_pointer_cast<ast::Name>(node);
18  if (name->get_node_name() == index) {
19  return std::make_shared<ast::Integer>(value, nullptr);
20  }
21  }
22  return node;
23 }
24 
26  node.visit_children(*this);
27  if (under_indexed_name) {
28  /// first recursively replaces children
29  /// replace lhs & rhs if they have matching index variable
30  auto lhs = replace_for_name(node.get_lhs());
31  auto rhs = replace_for_name(node.get_rhs());
32  node.set_lhs(std::move(lhs));
33  node.set_rhs(std::move(rhs));
34  }
35 }
36 
38  under_indexed_name = true;
39  node.visit_children(*this);
40  /// once all children are replaced, do the same for index
41  auto length = replace_for_name(node.get_length());
42  node.set_length(std::move(length));
43  under_indexed_name = false;
44 }
45 
46 } // namespace visitor
47 } // namespace nmodl
Represents binary expression in the NMODL.
Represents specific element of an array variable.
void visit_indexed_name(ast::IndexedName &node) override
visit node of type ast::IndexedName
std::string index
index variable name
IndexRemover(std::string index, int value)
int value
integer value of index variable
std::shared_ptr< ast::Expression > replace_for_name(const std::shared_ptr< ast::Expression > &node) const
if expression we are visiting is Name then return new Integer node
bool under_indexed_name
true if we are visiting index variable
void visit_binary_expression(ast::BinaryExpression &node) override
visit node of type ast::BinaryExpression
#define rhs
Definition: lineq.h:6
const char * name
Definition: init.cpp:16
void move(Item *q1, Item *q2, Item *q3)
Definition: list.cpp:200
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static Node * node(Object *)
Definition: netcvode.cpp:291
short index
Definition: cabvars.h:11
static uint32_t value
Definition: scoprand.cpp:25
Utility functions for visitors implementation.