File: semantic_analysis.cpp

package info (click to toggle)
nmodl 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,992 kB
  • sloc: cpp: 28,492; javascript: 9,841; yacc: 2,804; python: 1,967; lex: 1,674; xml: 181; sh: 136; ansic: 37; makefile: 18; pascal: 7
file content (167 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*************************************************************************
 * Copyright (C) 2018-2022 Blue Brain Project
 *
 * This file is part of NMODL distributed under the terms of the GNU
 * Lesser General Public License. See top-level LICENSE file for details.
 *************************************************************************/

#include <catch2/catch_test_macros.hpp>

#include "ast/program.hpp"
#include "parser/nmodl_driver.hpp"
#include "test/unit/utils/test_utils.hpp"
#include "visitors/semantic_analysis_visitor.hpp"
#include "visitors/symtab_visitor.hpp"


using namespace nmodl;
using namespace visitor;
using namespace test_utils;

using nmodl::parser::NmodlDriver;

//=============================================================================
// Procedure/Function inlining tests
//=============================================================================

bool run_semantic_analysis_visitor(const std::string& text) {
    NmodlDriver driver;
    const auto& ast = driver.parse_string(text);
    SymtabVisitor().visit_program(*ast);
    return SemanticAnalysisVisitor{}.check(*ast);
}

SCENARIO("TABLE stmt", "[visitor][semantic_analysis]") {
    GIVEN("Procedure with more than one argument") {
        std::string nmodl_text = R"(
            PROCEDURE rates_1(a, b) {
                TABLE ainf FROM 0 TO 1 WITH 1
                ainf = 1
            }
        )";
        THEN("fail") {
            REQUIRE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
    GIVEN("Procedure with exactly one argument") {
        std::string nmodl_text = R"(
            PROCEDURE rates_1(a) {
                TABLE ainf FROM 0 TO 1 WITH 1
                ainf = 1
            }
        )";
        THEN("pass") {
            REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
    GIVEN("Procedure with less than one argument") {
        std::string nmodl_text = R"(
            PROCEDURE rates_1() {
                TABLE ainf FROM 0 TO 1 WITH 1
                ainf = 1
            }
        )";
        THEN("fail") {
            REQUIRE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
}

SCENARIO("Destructor block", "[visitor][semantic_analysis]") {
    GIVEN("A point-process mod file, with a destructor") {
        std::string nmodl_text = R"(
            DESTRUCTOR { : Destructor is before
            }

            NEURON {
                POINT_PROCESS test
            }
        )";
        THEN("pass") {
            REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
    GIVEN("A artifial-cell mod file, with a destructor") {
        std::string nmodl_text = R"(
            NEURON {
                ARTIFICIAL_CELL test
            }

            DESTRUCTOR {
            }
        )";
        THEN("pass") {
            REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
    GIVEN("A non point-process mod file, with a destructor") {
        std::string nmodl_text = R"(
            NEURON {
            }

            DESTRUCTOR {
            }
        )";
        THEN("fail") {
            REQUIRE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
}

SCENARIO("Ion variable in CONSTANT block", "[visitor][semantic_analysis]") {
    GIVEN("A mod file with ion variable redeclared in a CONSTANT block") {
        std::string nmodl_text = R"(
            NEURON {
                SUFFIX cdp4Nsp
                USEION ca READ cao, cai, ica WRITE cai
            }
            CONSTANT { cao = 2  (mM) }
        )";
        THEN("Semantic analysis fails") {
            REQUIRE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
}

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));
        }
    }
}

SCENARIO("FUNCTION_TABLE block", "[visitor][semantic_analysis]") {
    GIVEN("A mod file with FUNCTION_TABLE without argument") {
        std::string nmodl_text = R"(
            FUNCTION_TABLE ttt()
        )";
        THEN("Semantic analysis should fail") {
            REQUIRE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
    GIVEN("A mod file with FUNCTION_TABLE with at least one argument") {
        std::string nmodl_text = R"(
            FUNCTION_TABLE ttt(w (mV))
        )";
        THEN("Semantic analysis should success") {
            REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
        }
    }
}