Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Independent is now useless, only check if variable is valid #966

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/language/code_generator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ set(AST_GENERATED_SOURCES
${PROJECT_BINARY_DIR}/src/ast/if_statement.hpp
${PROJECT_BINARY_DIR}/src/ast/include.hpp
${PROJECT_BINARY_DIR}/src/ast/independent_block.hpp
${PROJECT_BINARY_DIR}/src/ast/independent_definition.hpp
${PROJECT_BINARY_DIR}/src/ast/indexed_name.hpp
${PROJECT_BINARY_DIR}/src/ast/initial_block.hpp
${PROJECT_BINARY_DIR}/src/ast/integer.hpp
Expand Down
41 changes: 4 additions & 37 deletions src/language/nmodl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,11 @@
- IndependentBlock:
nmodl: "INDEPENDENT "
members:
- definitions:
brief: "TODO"
type: IndependentDefinition
- variables:
brief: "List of variable that should be independent"
type: Name
vector: true
separator: " "
brief: "Represents a `INDEPENDENT` block in the NMODL"
description: |
`INDEPENDENT` has following form in the NMODL specification :
Expand Down Expand Up @@ -1322,40 +1323,6 @@
optional: true
prefix: {value: " "}

- IndependentDefinition:
brief: "TODO"
members:
- sweep:
brief: "TODO"
type: Boolean
optional: true
nmodl: "SWEEP "
- name:
brief: "TODO"
type: Name
- from:
brief: "TODO"
type: Number
prefix: {value: " FROM "}
- to:
brief: "TODO"
type: Number
prefix: {value: " TO "}
- with:
brief: "TODO"
type: Integer
prefix: {value: " WITH "}
- start:
brief: "TODO"
type: Number
prefix: {value: " START "}
optional: true
- unit:
brief: "TODO"
type: Unit
optional: true
prefix: {value: " "}

- AssignedDefinition:
members:
- name:
Expand Down
1 change: 0 additions & 1 deletion src/language/node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
# when translating back to nmodl, we need print each statement
# to new line. Those nodes are are used from this list.
STATEMENT_TYPES = {"Statement",
"IndependentDefinition",
"AssignedDefinition",
"ParamAssign",
"ConstantStatement",
Expand Down
4 changes: 4 additions & 0 deletions src/language/templates/visitors/nmodl_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace visitor {
/**
* \class NmodlPrintVisitor
* \brief %Visitor for printing AST back to NMODL
* \todo Note that AstNodeType::INDEPENDENT_BLOCK is now trimmed-down
* in the AST. So if we need to make provide something like
* `nmodl-format` then we should exclude this node type i.e.
* add that in the exclude_types.
*/
class NmodlPrintVisitor: public ConstVisitor {
private:
Expand Down
13 changes: 6 additions & 7 deletions src/parser/nmodl.yy
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@
%type <ast::Number*> optional_start
%type <ast::LagStatement*> lag_statement
%type <ast::ParamAssign*> parameter_assignment
%type <ast::IndependentDefinition*> independent_definition
%type <ast::Name*> independent_definition
%type <ast::AssignedDefinition*> dependent_definition
%type <ast::Block*> declare
%type <ast::ParamAssignVector> parameter_block_body
%type <ast::IndependentDefinitionVector> independent_block_body
%type <ast::NameVector> independent_block_body
%type <ast::AssignedDefinitionVector> dependent_block_body
%type <ast::WatchStatement*> watch_statement
%type <ast::BinaryOperator> watch_direction
Expand Down Expand Up @@ -655,18 +655,18 @@ double : REAL
;


/** We still parse INDEPENDENT block but we do nothing with it, except checking that the
variable is `t` later. */
independent_block : INDEPENDENT "{" independent_block_body "}"
{
$$ = new ast::IndependentBlock($3);
ModToken block_token = $1 + $4;
$$->set_token(block_token);
}
;


independent_block_body :
{
$$ = ast::IndependentDefinitionVector();
$$ = ast::NameVector();
}
| independent_block_body independent_definition
{
Expand All @@ -676,15 +676,14 @@ independent_block_body :
| independent_block_body SWEEP independent_definition
{
$1.emplace_back($3);
$3->set_sweep(std::make_shared<ast::Boolean>(1));
$$ = $1;
}
;


independent_definition : NAME_PTR FROM number TO number withby integer optional_start units
{
$$ = new ast::IndependentDefinition(NULL, $1, $3, $5, $7, $8, $9);
$$ = $1;
}
| error
{
Expand Down
15 changes: 15 additions & 0 deletions src/visitors/semantic_analysis_visitor.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "visitors/semantic_analysis_visitor.hpp"
#include "ast/function_block.hpp"
#include "ast/independent_block.hpp"
#include "ast/procedure_block.hpp"
#include "ast/program.hpp"
#include "ast/string.hpp"
#include "ast/suffix.hpp"
#include "ast/table_statement.hpp"
#include "symtab/symbol_properties.hpp"
Expand Down Expand Up @@ -101,5 +103,18 @@ void SemanticAnalysisVisitor::visit_destructor_block(const ast::DestructorBlock&
/// -->
}

void SemanticAnalysisVisitor::visit_independent_block(const ast::IndependentBlock& node) {
/// <-- This code is for check 5
for (const auto& n: node.get_variables()) {
if (n->get_value()->get_value() != "t") {
logger->warn(
"SemanticAnalysisVisitor :: '{}' cannot be used as an independent variable, only "
"'t' is allowed.",
n->get_value()->get_value());
}
}
/// -->
}

} // namespace visitor
} // namespace nmodl
4 changes: 4 additions & 0 deletions src/visitors/semantic_analysis_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* 2. Check that destructor blocks are only inside mod file that are point_process.
* 3. A TABLE statement in functions cannot have name list, and should have one in procedures.
* 4. Check if ion variables from a `USEION` statement are not declared in `CONSTANT` block.
* 5. Check if an independent variable is not 't'.
*/
#include "ast/ast.hpp"
#include "visitors/ast_visitor.hpp"
Expand Down Expand Up @@ -60,6 +61,9 @@ class SemanticAnalysisVisitor: public ConstAstVisitor {
/// Visit destructor and check that the file is of type POINT_PROCESS or ARTIFICIAL_CELL
void visit_destructor_block(const ast::DestructorBlock& node) override;

/// Visit independent block and check if one of the variable is not t
void visit_independent_block(const ast::IndependentBlock& node) override;

public:
SemanticAnalysisVisitor() = default;

Expand Down
4 changes: 4 additions & 0 deletions test/unit/utils/nmodl_constructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ std::map<std::string, NmodlTestCase> const nmodl_valid_constructs{
t FROM 0 TO 1 WITH 1 (ms)
SWEEP u FROM 0 TO 1 WITH 1 (ms)
}
)",
R"(
INDEPENDENT {
t u}
)"
}
},
Expand Down
24 changes: 24 additions & 0 deletions test/unit/visitor/semantic_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ SCENARIO("Ion variable in CONSTANT block", "[visitor][semantic_analysis]") {
}
}
}

SCENARIO("INDEPENDENT block", "[visitor][semantic_analysis]") {
GIVEN("A mod file with Independent block with only t") {
std::string nmodl_text = R"(
INDEPENDENT {
t FROM 0 TO 1 WITH 100
}
)";
THEN("Semantic analysis succeed") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
}
}
GIVEN("A mod file with Independent block with something else than t") {
std::string nmodl_text = R"(
INDEPENDENT {
t FROM 0 TO 1 WITH 100
u FROM 0 TO 1 WITH 100
}
)";
THEN("Semantic analysis fails") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
pramodk marked this conversation as resolved.
Show resolved Hide resolved
}
}
}