Skip to content

Commit

Permalink
feat: add local variable collector.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Aug 14, 2024
1 parent 8b4f627 commit 6eca80f
Show file tree
Hide file tree
Showing 10 changed files with 822 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ add_library(yugawara
yugawara/analyzer/details/rewrite_scan.cpp
yugawara/analyzer/details/classify_expression.cpp
yugawara/analyzer/details/inline_variables.cpp
yugawara/analyzer/details/collect_local_variables.cpp

# analyzer misc.
yugawara/analyzer/details/detect_join_endpoint_style.cpp
Expand Down
10 changes: 10 additions & 0 deletions src/yugawara/analyzer/details/classify_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class engine {
if (!saw_not_small_) {
results.insert(expression_class::small);
}
if (saw_variable_declaration_) {
results.insert(expression_class::variable_declaration);
}
if (saw_function_call_) {
results.insert(expression_class::function_call);
}
}
return results;
}
Expand Down Expand Up @@ -101,6 +107,7 @@ class engine {
}
dispatch(expression.body());
saw_not_trivial_ = true;
saw_variable_declaration_ = true;
}

void operator()(::takatori::scalar::function_call const& expression) {
Expand All @@ -110,13 +117,16 @@ class engine {
saw_not_constant_ = true;
saw_not_trivial_ = true;
saw_not_small_ = true;
saw_function_call_ = true;
}

private:
bool saw_unknown_ { false };
bool saw_not_constant_ { false };
bool saw_not_trivial_ { false };
bool saw_not_small_ { false };
bool saw_variable_declaration_ { false };
bool saw_function_call_ { false };
};

} // namespace
Expand Down
16 changes: 14 additions & 2 deletions src/yugawara/analyzer/details/classify_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ enum class expression_class : std::size_t {
* @brief a simple expression.
*/
small,

/**
* @brief includes local variable declarations.
*/
variable_declaration,

/**
* @brief includes local variable declarations.
*/
function_call,
};

/**
Expand All @@ -43,7 +53,7 @@ enum class expression_class : std::size_t {
using expression_class_set = ::takatori::util::enum_set<
expression_class,
expression_class::unknown,
expression_class::small>;
expression_class::function_call>;

/**
* @brief returns string representation of the value.
Expand All @@ -58,6 +68,8 @@ constexpr std::string_view to_string_view(expression_class value) noexcept {
case kind::constant: return "constant"sv;
case kind::trivial: return "trivial"sv;
case kind::small: return "small"sv;
case kind::variable_declaration: return "variable_declaration"sv;
case kind::function_call: return "function_call"sv;
}
std::abort();
}
Expand All @@ -75,7 +87,7 @@ inline std::ostream& operator<<(std::ostream& out, expression_class value) {
/**
* @brief classifies the given expression.
* @param expression the target expression
* @return
* @return the corresponded expression classes
*/
[[nodiscard]] expression_class_set classify_expression(::takatori::scalar::expression const& expression);

Expand Down
Loading

0 comments on commit 6eca80f

Please sign in to comment.