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

Semantic: check there is at most one DERIVATIVE block #1099

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .github/workflows/nmodl-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}

- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install Python3 dependencies
working-directory: ${{runner.workspace}}/nmodl
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
.decode()
)
__version__ = v[: v.rfind("-")].replace("-", ".") if "-" in v else v
print("VERSION INFO FROM GIT:: ", v, __version__)
# allow to override version during development/testing
if "NMODL_WHEEL_VERSION" in os.environ:
__version__ = os.environ['NMODL_WHEEL_VERSION']
Expand Down Expand Up @@ -111,7 +112,7 @@ def run(self, *args, **kwargs):
docs=Docs, doctest=get_sphinx_command, buildhtml=get_sphinx_command,
),
zip_safe=False,
# myst_parser 2.0.0 want sphinx >= 6 but it leads to incompatibily with sphinxcontrib-applehelp and
# myst_parser 2.0.0 want sphinx >= 6 but it leads to incompatibily with sphinxcontrib-applehelp and
# sphinxcontrib-htmlhelp that want PEP-517 support instead of setup.py (name clash with '-' and '.')
# So we pin low versions of packages
setup_requires=[
Expand Down
31 changes: 11 additions & 20 deletions src/visitors/semantic_analysis_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ bool SemanticAnalysisVisitor::check(const ast::Program& node) {
return check_fail;
}

void SemanticAnalysisVisitor::visit_program(const ast::Program& node) {
/// <-- This code is for check 8
const auto& derivative_block_nodes = collect_nodes(node, {ast::AstNodeType::DERIVATIVE_BLOCK});
if (derivative_block_nodes.size() > 1) {
logger->critical("It is not supported to have several DERIVATIVE blocks");
check_fail = true;
}
/// -->
node.visit_children(*this);
}

void SemanticAnalysisVisitor::visit_procedure_block(const ast::ProcedureBlock& node) {
/// <-- This code is for check 1
in_procedure = true;
Expand Down Expand Up @@ -171,25 +182,5 @@ void SemanticAnalysisVisitor::visit_mutex_unlock(const ast::MutexUnlock& /* node
/// -->
}

void SemanticAnalysisVisitor::visit_breakpoint_block(const ast::BreakpointBlock& node) {
/// <-- This code is for check 8
solve_block_found_in_this_breakpoint_block = false;
node.visit_children(*this);
solve_block_found_in_this_breakpoint_block = false;
/// -->
}

void SemanticAnalysisVisitor::visit_solve_block(const ast::SolveBlock& /* node */) {
/// <-- This code is for check 8
if (solve_block_found_in_this_breakpoint_block) {
logger->critical(
"It is not allowed to have several solve blocks in the same breakpoint block");
check_fail = true;
} else {
solve_block_found_in_this_breakpoint_block = true;
}
/// -->
}

} // namespace visitor
} // namespace nmodl
12 changes: 4 additions & 8 deletions src/visitors/semantic_analysis_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* 5. Check if an independent variable is not 't'.
* 6. Check that mutex are not badly use
* 7. Check than function table got at least one argument.
* 8. Check that at most one solve block is present per breakpoint block.
* 8. Check that at most one derivative block is present.
*/
#include "ast/ast.hpp"
#include "visitors/ast_visitor.hpp"
Expand All @@ -55,8 +55,9 @@ class SemanticAnalysisVisitor: public ConstAstVisitor {
bool is_point_process = false;
/// true if we are inside a mutex locked part
bool in_mutex = false;
/// true if we already found a solve block
bool solve_block_found_in_this_breakpoint_block = false;

/// Check number of DERIVATIVE blocks
void visit_program(const ast::Program& node) override;

/// Store if we are in a procedure and if the arity of this is 1
void visit_procedure_block(const ast::ProcedureBlock& node) override;
Expand Down Expand Up @@ -85,11 +86,6 @@ class SemanticAnalysisVisitor: public ConstAstVisitor {
/// Look if MUTEXUNLOCK is outside a locked block
void visit_mutex_unlock(const ast::MutexUnlock& node) override;

void visit_breakpoint_block(const ast::BreakpointBlock& node) override;

/// Check how many solve block we got
void visit_solve_block(const ast::SolveBlock& node) override;

public:
SemanticAnalysisVisitor(bool accel_backend = false)
: accel_backend(accel_backend) {}
Expand Down
31 changes: 10 additions & 21 deletions test/unit/visitor/semantic_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,27 @@ SCENARIO("FUNCTION_TABLE block", "[visitor][semantic_analysis]") {
}


SCENARIO("At most one solve block per breakpoint block", "[visitor][semantic_analysis]") {
GIVEN("A breakpoint block with only one solve block") {
SCENARIO("At most one DERIVATIVE block", "[visitor][semantic_analysis]") {
GIVEN("Only one DERIVATIVE block") {
std::string nmodl_text = R"(
BREAKPOINT {
SOLVE dX METHOD cnexp
DERIVATIVE states {
m' = m/mTau
}
)";
THEN("Semantic analysis should success") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
}
}
GIVEN("2 breakpoints block with one solve block each") {
GIVEN("2 DERIVATIVE blocks") {
std::string nmodl_text = R"(
PROCEDURE foo() {
SOLVE dX METHOD cnexp
DERIVATIVE states1 {
m' = m/mTau
}
BREAKPOINT {
SOLVE dY METHOD cnexp
DERIVATIVE states2 {
h' = h/hTau
}
)";
THEN("Semantic analysis should success") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
}
}
GIVEN("A breakpoint block with two solve blocks") {
std::string nmodl_text = R"(
BREAKPOINT {
SOLVE dX METHOD cnexp
SOLVE dY METHOD cnexp
}
)";
THEN("Semantic analysis should fail") {
THEN("Semantic analysis should failed") {
REQUIRE(run_semantic_analysis_visitor(nmodl_text));
}
}
Expand Down
Loading