NEURON
local_to_assigned.cpp
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 #include <catch2/catch_test_macros.hpp>
9 
10 #include "ast/program.hpp"
11 #include "parser/nmodl_driver.hpp"
17 
18 using namespace nmodl;
19 using namespace visitor;
20 using namespace test_utils;
21 
24 
25 //=============================================================================
26 // GlobalToRange visitor tests
27 //=============================================================================
28 
29 std::shared_ptr<ast::Program> run_local_to_assigned_visitor(const std::string& text) {
30  std::map<std::string, std::string> rval;
32  auto ast = driver.parse_string(text);
33 
35  PerfVisitor().visit_program(*ast);
38  return ast;
39 }
40 
41 SCENARIO("LOCAL to ASSIGNED variable transformer", "[visitor][localtoassigned]") {
42  GIVEN("mod file with LOCAL variables that are written") {
43  std::string input_nmodl = R"(
44  NEURON {
45  SUFFIX test
46  }
47 
48  LOCAL x, y, z
49 
50  INITIAL {
51  x = 1
52  }
53 
54  BREAKPOINT {
55  z = 2
56  }
57  )";
58 
59  auto ast = run_local_to_assigned_visitor(input_nmodl);
60  auto symtab = ast->get_symbol_table();
61 
62  THEN("LOCAL variables that are written are turned to ASSIGNED") {
63  /// check for all ASSIGNED variables : old ones + newly converted ones
64  auto vars = symtab->get_variables_with_properties(NmodlType::assigned_definition);
65  REQUIRE(vars.size() == 2);
66 
67  /// x and z should be converted from LOCAL to ASSIGNED
68  auto x = symtab->lookup("x");
69  REQUIRE(x != nullptr);
70  REQUIRE(x->has_any_property(NmodlType::assigned_definition) == true);
71  REQUIRE(x->has_any_property(NmodlType::local_var) == false);
72 
73  auto z = symtab->lookup("z");
74  REQUIRE(z != nullptr);
75  REQUIRE(z->has_any_property(NmodlType::assigned_definition) == true);
76  REQUIRE(z->has_any_property(NmodlType::local_var) == false);
77  }
78 
79  THEN("LOCAL variables that are read only remain LOCAL") {
80  auto vars = symtab->get_variables_with_properties(NmodlType::local_var);
81  REQUIRE(vars.size() == 1);
82  REQUIRE(vars[0]->get_name() == "y");
83  }
84  }
85 }
Class that binds all pieces together for parsing nmodl file.
Visitor to convert top level LOCAL variables to ASSIGNED variables.
void visit_program(ast::Program &node) override
Visit ast::Program node to transform top level LOCAL variables to ASSIGNED if they are written in the...
Visitor for measuring performance related information
void visit_program(const ast::Program &node) override
visit node of type ast::Program
Concrete visitor for constructing symbol table from AST.
void visit_program(ast::Program &node) override
visit node of type ast::Program
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
SCENARIO("LOCAL to ASSIGNED variable transformer", "[visitor][localtoassigned]")
std::shared_ptr< ast::Program > run_local_to_assigned_visitor(const std::string &text)
Visitor to convert top level LOCAL variables to ASSIGNED variables.
auto get_name(Tag const &tag, int field_index)
Get the nicest available name for the field_index-th instance of Tag.
NmodlType
NMODL variable properties.
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
Visitor for measuring performance related information
#define text
Definition: plot.cpp:60
Auto generated AST classes declaration.
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28