User Guide
misc.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"
18 
19 using namespace nmodl;
20 using namespace visitor;
21 using namespace test;
22 using namespace test_utils;
23 
24 using ast::AstNodeType;
26 
27 //=============================================================================
28 // Passes can run multiple times
29 //=============================================================================
30 
31 void run_visitor_passes(const std::string& text) {
33  const auto& ast = driver.parse_string(text);
34  {
35  SymtabVisitor v1;
36  InlineVisitor v2;
37  LocalizeVisitor v3;
38  CheckParentVisitor v4(true);
39  v1.visit_program(*ast);
40  v2.visit_program(*ast);
41  v3.visit_program(*ast);
42  v4.check_ast(*ast);
43  v4.check_ast(*ast);
44  v1.visit_program(*ast);
45  v1.visit_program(*ast);
46  v4.check_ast(*ast);
47  v2.visit_program(*ast);
48  v3.visit_program(*ast);
49  v2.visit_program(*ast);
50  v4.check_ast(*ast);
51  }
52 }
53 
54 
55 SCENARIO("Running visitor passes multiple times", "[visitor]") {
56  GIVEN("A mod file") {
57  std::string nmodl_text = R"(
58  NEURON {
59  RANGE tau
60  }
61 
62  DERIVATIVE states {
63  tau = 11.1
64  exp(tau)
65  }
66  )";
67 
68  THEN("Passes can run multiple times") {
69  std::string input = reindent_text(nmodl_text);
70  REQUIRE_NOTHROW(run_visitor_passes(input));
71  }
72  }
73 }
74 
75 //=============================================================================
76 // to_nmodl with excluding set of node types
77 //=============================================================================
78 
79 SCENARIO("Sympy specific AST to NMODL conversion") {
80  GIVEN("NMODL block with unit usage") {
81  static const std::string nmodl = R"(
82  BREAKPOINT {
83  Pf_NMDA = (1/1.38) * 120 (mM) * 0.6
84  VDCC = gca_bar_VDCC * 4(um2)*PI*3(1/um3)
85  gca_bar_VDCC = 0.0372 (nS/um2)
86  }
87  )";
88 
89  static const std::string expected = R"(
90  BREAKPOINT {
91  Pf_NMDA = (1/1.38)*120*0.6
92  VDCC = gca_bar_VDCC*4*PI*3
93  gca_bar_VDCC = 0.0372
94  }
95  )";
96 
97  THEN("to_nmodl can ignore all units") {
98  auto input = reindent_text(nmodl);
100  const auto& ast = driver.parse_string(input);
101  const auto& result = to_nmodl(ast, {AstNodeType::UNIT});
102  REQUIRE(result == reindent_text(expected));
103  }
104  }
105 }
test_utils.hpp
nmodl::parser::NmodlDriver
Class that binds all pieces together for parsing nmodl file.
Definition: nmodl_driver.hpp:67
localize_visitor.hpp
Visitor to transform global variable usage to local
nmodl::to_nmodl
std::string to_nmodl(const ast::Ast &node, const std::set< ast::AstNodeType > &exclude_types)
Given AST node, return the NMODL string representation.
Definition: visitor_utils.cpp:234
nmodl::test_utils::reindent_text
std::string reindent_text(const std::string &text, int indent_level)
Reindent nmodl text for text-to-text comparison.
Definition: test_utils.cpp:53
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:166
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
SCENARIO
SCENARIO("Running visitor passes multiple times", "[visitor]")
Definition: misc.cpp:55
driver
nmodl::parser::UnitDriver driver
Definition: parser.cpp:28
run_visitor_passes
void run_visitor_passes(const std::string &text)
Definition: misc.cpp:31
nmodl::parser::UnitDriver::parse_string
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
Definition: unit_driver.cpp:40
checkparent_visitor.hpp
Visitor for checking parents of ast nodes
inline_visitor.hpp
Visitor to inline local procedure and function calls
nmodl_driver.hpp
symtab_visitor.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.