|
User Guide
|
Go to the documentation of this file.
22 #include <fmt/format.h>
33 const std::string& original_string,
37 if (vars.find(original_string) == vars.end()) {
38 return original_string;
40 std::string new_string = original_string;
45 if (singleton_random_string_class.random_string_exists(original_string)) {
46 const auto random_suffix =
"_" +
47 singleton_random_string_class.get_random_string(original_string);
48 new_string = original_string + random_suffix;
52 while (vars.find(new_string) != vars.end()) {
53 const auto random_suffix =
54 "_" + singleton_random_string_class.reset_random_string(original_string, use_num);
55 new_string = original_string + random_suffix;
62 const std::string& suffix,
63 std::map<std::string, int>& variables) {
64 auto it = variables.emplace(name, 0);
65 auto counter = it.first->second;
68 std::ostringstream oss;
69 oss << name <<
'_' << suffix <<
'_' << counter;
75 for (
const auto& statement: statements) {
76 if (statement->is_local_list_statement()) {
77 return std::static_pointer_cast<LocalListStatement>(statement);
86 if (variables ==
nullptr) {
87 auto statement = std::make_shared<LocalListStatement>(
LocalVarVector());
97 if (local_list_statement ==
nullptr) {
98 throw std::logic_error(
"no local statement");
100 auto var = std::make_shared<LocalVar>(varname);
101 local_list_statement->emplace_back_local_var(var);
128 auto nmodl_text =
"PROCEDURE dummy() { " + code_statement +
" }";
130 auto procedure = std::dynamic_pointer_cast<ProcedureBlock>(ast->get_blocks().front());
131 auto statement = std::shared_ptr<Statement>(
132 procedure->get_statement_block()->get_statements()[0]->clone());
137 const std::vector<std::string>::const_iterator& code_statements_beg,
138 const std::vector<std::string>::const_iterator& code_statements_end) {
139 std::vector<std::shared_ptr<Statement>> statements;
140 statements.reserve(code_statements_end - code_statements_beg);
141 std::transform(code_statements_beg,
143 std::back_inserter(statements),
156 const std::vector<std::string>& code_statements) {
158 std::string nmodl_text =
"PROCEDURE dummy() {\n";
159 for (
auto& statement: code_statements) {
160 nmodl_text += statement +
"\n";
164 auto procedure = std::dynamic_pointer_cast<ProcedureBlock>(ast->get_blocks().front());
165 auto statement_block = std::shared_ptr<StatementBlock>(
166 procedure->get_statement_block()->clone());
167 return statement_block;
171 std::set<std::string> vars;
174 NmodlType property = NmodlType::global_var | NmodlType::local_var | NmodlType::range_var |
175 NmodlType::param_assign | NmodlType::extern_var |
176 NmodlType::prime_name | NmodlType::assigned_definition |
177 NmodlType::read_ion_var | NmodlType::write_ion_var |
178 NmodlType::nonspecific_cur_var | NmodlType::electrode_cur_var |
179 NmodlType::constant_var | NmodlType::extern_neuron_variable |
180 NmodlType::state_var | NmodlType::factor_def;
181 for (
const auto& globalvar: symtab->get_variables_with_properties(property)) {
182 std::string var_name = globalvar->get_name();
183 if (globalvar->is_array()) {
186 vars.insert(var_name);
196 function_calls.begin(),
197 function_calls.end(),
198 [&name](
const std::shared_ptr<const ast::Ast>& f) {
199 return std::dynamic_pointer_cast<const ast::FunctionCall>(f)->get_node_name() == name;
207 const std::vector<ast::AstNodeType>& types) {
209 return visitor.
lookup(node, types);
213 const std::vector<ast::AstNodeType>& types) {
215 return visitor.
lookup(node, types);
220 return std::any_of(solve_blocks.begin(), solve_blocks.end(), [](
auto const& solve_block) {
222 const auto& method = dynamic_cast<ast::SolveBlock const&>(*solve_block).get_method();
223 return method && method->get_node_name() ==
"sparse";
227 std::string
to_nmodl(
const ast::Ast& node,
const std::set<ast::AstNodeType>& exclude_types) {
228 std::stringstream stream;
235 std::string
to_json(
const ast::Ast& node,
bool compact,
bool expand,
bool add_nmodl) {
236 std::stringstream stream;
247 const std::shared_ptr<ast::Expression>& lhs,
248 const std::shared_ptr<ast::Expression>& rhs) {
250 std::unordered_set<std::string> out;
252 if (!lhs->is_var_name()) {
256 const auto& lhs_var_name = std::dynamic_pointer_cast<ast::VarName>(lhs);
261 auto rhs_nodes = lookup_visitor.
get_nodes();
262 std::for_each(rhs_nodes.begin(),
264 [&out](
const std::shared_ptr<ast::Ast>& node) { out.emplace(to_nmodl(node)); });
275 std::string full_var_name;
276 if (node.
get_name()->is_indexed_name()) {
277 auto index_name_node = std::dynamic_pointer_cast<ast::IndexedName>(node.
get_name());
282 return full_var_name;
std::shared_ptr< Expression > get_length() const noexcept
Getter for member variable IndexedName::length.
Class that binds all pieces together for parsing nmodl file.
std::string get_indexed_name(const ast::IndexedName &node)
Given a Indexed node, return the name with index.
std::string to_nmodl(const ast::Ast &node, const std::set< ast::AstNodeType > &exclude_types)
Given AST node, return the NMODL string representation.
Base class for all identifiers.
Base class for all Abstract Syntax Tree node types.
std::vector< std::shared_ptr< Statement > > create_statements(const std::vector< std::string >::const_iterator &code_statements_beg, const std::vector< std::string >::const_iterator &code_statements_end)
Same as for create_statement but for vectors of strings.
bool calls_function(const ast::Ast &node, const std::string &name)
Checks whether block contains a call to a particular function.
std::string get_node_name() const override
Return name of the node.
std::shared_ptr< ast::LocalListStatement > get_local_list_statement(const StatementBlock &node)
Return pointer to local statement in the given block, otherwise nullptr.
encapsulates code generation backend implementations
std::string to_json(const ast::Ast &node, bool compact, bool expand, bool add_nmodl)
Given AST node, return the JSON string representation.
StatementVector::const_iterator insert_statement(StatementVector::const_iterator position, const std::shared_ptr< Statement > &n)
Insert member to statements.
Represents an integer variable.
Implement string manipulation functions.
bool is_random_construct_function(const std::string &name)
Is given name a one of the function for RANDOM construct.
std::pair< std::string, std::unordered_set< std::string > > statement_dependencies(const std::shared_ptr< ast::Expression > &lhs, const std::shared_ptr< ast::Expression > &rhs)
If lhs and rhs combined represent an assignment (we assume to have an "=" in between them) we extract...
std::vector< std::shared_ptr< LocalVar > > LocalVarVector
const StatementVector & get_statements() const noexcept
Getter for member variable StatementBlock::statements.
Utility functions for visitors implementation.
Represents specific element of an array variable.
nmodl::parser::UnitDriver driver
UseNumbersInString
Enum to wrap bool variable to select if random string should have numbers or not.
Visitor for printing AST back to NMODL
std::string to_string(const T &obj)
@ SOLVE_BLOCK
type of ast::SolveBlock
@ FUNCTION_CALL
type of ast::FunctionCall
std::string get_full_var_name(const ast::VarName &node)
Given a VarName node, return the full var name including index.
static std::unordered_map< std::string, std::string > RANDOM_FUNCTIONS_MAPPING
bool parse_string(const std::string &input)
parser Units provided as string (used for testing)
virtual void accept(visitor::Visitor &v)=0
Accept (or visit) the AST node using current visitor.
std::shared_ptr< Statement > create_statement(const std::string &code_statement)
Convert given code statement (in string format) to corresponding ast node.
std::vector< std::shared_ptr< const ast::Ast > > collect_nodes(const ast::Ast &node, const std::vector< ast::AstNodeType > &types)
traverse node recursively and collect nodes of given types
JSONVisitor & compact_json(bool flag)
JSONVisitor & expand_keys(bool flag)
void add_local_statement(StatementBlock &node)
Add empty local statement to given block if already doesn't exist.
Visitor for printing AST in JSON format
std::string get_new_name(const std::string &name, const std::string &suffix, std::map< std::string, int > &variables)
Return new name variable by appending _suffix_COUNT where COUNT is number of times the given variable...
Represents block encapsulating list of statements.
NmodlType
NMODL variable properties.
static SingletonRandomString & instance()
Function to instantiate the SingletonRandomString class.
std::shared_ptr< StatementBlock > create_statement_block(const std::vector< std::string > &code_statements)
Convert given code statement (in string format) to corresponding ast node.
LocalVar * add_local_variable(StatementBlock &node, Identifier *varname)
std::shared_ptr< Identifier > get_name() const noexcept
Getter for member variable VarName::name.
symtab::SymbolTable * get_symbol_table() const override
Return associated symbol table for the current ast node.
Represents top level AST node for whole NMODL input.
bool sparse_solver_exists(const ast::Ast &node)
std::string suffix_random_string(const std::set< std::string > &vars, const std::string &original_string, const UseNumbersInString use_num)
Return the "original_string" with a random suffix if "original_string" exists in "vars".
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
std::set< std::string > get_global_vars(const Program &node)
Return set of strings with the names of all global variables.
Auto generated AST classes declaration.
std::string get_node_name() const override
Return name of the node.
JSONVisitor & add_nmodl(bool flag)
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
@ VAR_NAME
type of ast::VarName