Skip to content

Commit

Permalink
Add getters for IncrementDecrementExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
IEncinas10 committed Jul 14, 2023
1 parent 0fc3a5e commit 1c7bb01
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
37 changes: 37 additions & 0 deletions verilog/CST/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,41 @@ const verible::TokenInfo *ReferenceIsSimpleIdentifier(
return ReferenceBaseIsSimple(base_node);
}

const verible::SyntaxTreeLeaf *GetIncrementDecrementOperator(
const verible::Symbol &expr) {
if (expr.Kind() != SymbolKind::kNode) return nullptr;

const SyntaxTreeNode &node = verible::SymbolCastToNode(expr);

if (!node.MatchesTag(NodeEnum::kIncrementDecrementExpression)) return nullptr;

// Structure changes depending on the type of IncrementDecrement
bool is_post = node.children().front().get()->Kind() == SymbolKind::kNode;

if (is_post) {
return verible::GetSubtreeAsLeaf(
expr, NodeEnum::kIncrementDecrementExpression, 1);
}
return verible::GetSubtreeAsLeaf(expr,
NodeEnum::kIncrementDecrementExpression, 0);
}

const verible::SyntaxTreeNode *GetIncrementDecrementOperand(
const verible::Symbol &expr) {
if (expr.Kind() != SymbolKind::kNode) return nullptr;

const SyntaxTreeNode &node = verible::SymbolCastToNode(expr);

if (!node.MatchesTag(NodeEnum::kIncrementDecrementExpression)) return nullptr;

// Structure changes depending on the type of IncrementDecrement
bool is_post = node.children().front().get()->Kind() == SymbolKind::kNode;
if (is_post) {
return verible::GetSubtreeAsNode(
expr, NodeEnum::kIncrementDecrementExpression, 0);
}
return verible::GetSubtreeAsNode(expr,
NodeEnum::kIncrementDecrementExpression, 1);
}

} // namespace verilog
10 changes: 10 additions & 0 deletions verilog/CST/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ std::vector<verible::TreeSearchMatch> FindAllReferenceFullExpressions(
const verible::TokenInfo *ReferenceIsSimpleIdentifier(
const verible::Symbol &reference);

// Return the operator for a kIncrementDecrementExpression
// This function would extract the leaf containing '++' from expression '++a'
const verible::SyntaxTreeLeaf *GetIncrementDecrementOperator(
const verible::Symbol &expr);

// Return the operator for a kIncrementDecrementExpression
// This function would extract the leaf containing 'a' from expression '++a'
const verible::SyntaxTreeNode *GetIncrementDecrementOperand(
const verible::Symbol &expr);

} // namespace verilog

#endif // VERIBLE_VERILOG_CST_EXPRESSION_H_
89 changes: 89 additions & 0 deletions verilog/CST/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "common/util/logging.h"
#include "gtest/gtest.h"
#include "verilog/CST/match_test_utils.h"
#include "verilog/CST/verilog_matchers.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/CST/verilog_tree_print.h"
#include "verilog/analysis/verilog_analyzer.h"
Expand Down Expand Up @@ -692,5 +693,93 @@ TEST(ReferenceIsSimpleTest, NotSimple) {
}
}

TEST(GetIncrementDecrementOperatorTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m; endmodule\n"},
{"module m;\ninitial begin end\nendmodule"},
{"module m;\nalways_comb begin\n"
"a",
{kTag, "++"},
";\nend\nendmodule"},
{"module m;\nalways_comb begin\n",
{kTag, "++"},
"a;"
"\nend\nendmodule"},
{"module m;\nalways_comb begin\n"
"somelargename",
{kTag, "++"},
";\nend\nendmodule"},
{"module m;\nalways_comb begin\n",
{kTag, "++"},
"somelargename;\n"
"end\nendmodule"},
{"module m;\nalways_comb begin\nk = a + 2;\nend\nendmodule"},
};
for (const auto &test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView &text_structure) {
const auto &root = text_structure.SyntaxTree();
const auto exprs = verible::SearchSyntaxTree(
*ABSL_DIE_IF_NULL(root), NodekIncrementDecrementExpression());

std::vector<TreeSearchMatch> operators;
for (const auto &expr : exprs) {
const auto *operator_ = GetIncrementDecrementOperator(*expr.match);
operators.push_back(
TreeSearchMatch{operator_, {/* ignored context */}});
}
return operators;
});
}
}

TEST(GetIncrementDecrementOperandTest, Various) {
constexpr int kTag = 1; // value doesn't matter
const SyntaxTreeSearchTestCase kTestCases[] = {
{""},
{"module m; endmodule\n"},
{"module m;\ninitial begin end\nendmodule"},
{"module m;\n"
"always_comb begin\n",
{kTag, "a"},
"++;\nend\nendmodule"},
{"module m;\n"
"always_comb begin\n"
"++",
{kTag, "a"},
";\nend\nendmodule"},
{"module m;\n"
"always_comb begin\n",
{kTag, "somelargename"},
"++;\nend\nendmodule"},
{"module m;\n"
"always_comb begin\n++",
{kTag, "somelargename"},
";\nend\nendmodule"},
{"module m;\n"
"always_comb begin\n"
"k = a + 2;\n"
"end\nendmodule"},
};
for (const auto &test : kTestCases) {
TestVerilogSyntaxRangeMatches(
__FUNCTION__, test, [](const TextStructureView &text_structure) {
const auto &root = text_structure.SyntaxTree();
const auto exprs = verible::SearchSyntaxTree(
*ABSL_DIE_IF_NULL(root), NodekIncrementDecrementExpression());

std::vector<TreeSearchMatch> operands;
for (const auto &expr : exprs) {
const auto *operand = GetIncrementDecrementOperand(*expr.match);
operands.push_back(
TreeSearchMatch{operand, {/* ignored context */}});
}
return operands;
});
}
}

} // namespace
} // namespace verilog

0 comments on commit 1c7bb01

Please sign in to comment.