Commit d9f7a038 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Implemented a simple stack to handle nested for loops



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent e22a6d22
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -89,17 +89,28 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
  using namespace antlr4;

  int previous_col = lines[0].second;
  bool is_in_for_loop = false;
  int line_counter = 0;
  // Tracking the scope of for loops by their indent
  std::stack<int> for_loop_indent;
  for (const auto &line : lines) {
    // std::cout << "processing line " << line_counter << " of " << lines.size()
    //           << ": " << line.first << ", " << line.second << std::boolalpha
    //           << ", " << is_in_for_loop << "\n";

    pyxasm_visitor visitor(bufferNames);
    // Should we close a 'for' scope after this statement
    bool close_for_scope = false;
    // If the stack is not empty and this line changed column to an outside
    // scope:
    if (!for_loop_indent.empty() && line.second < for_loop_indent.top()) {
      // Pop the stack and flag to close the scope afterward
      for_loop_indent.pop();
      close_for_scope = true;
    }

    // Enter a new for loop -> push to the stack
    if (line.first.find("for ") != std::string::npos) {
      is_in_for_loop = true;
      for_loop_indent.push(line.second);
    }

    // is_in_for_loop = line.first.find("for ") != std::string::npos &&
@@ -127,17 +138,18 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
      ss << visitor.result.first;
    }

    if ((is_in_for_loop && line.second < previous_col) ||
        (is_in_for_loop && line_counter == lines.size() - 1)) {
      // we are now not in a for loop...
      is_in_for_loop = false;
    if (close_for_scope) {
      // need to close out the c++ or loop
      ss << "}\n";
    }

    previous_col = line.second;
    line_counter++;
  }

  // If there are open for scope blocks here,
  // i.e. for loops at the end of the function body.
  while (!for_loop_indent.empty()) {
    for_loop_indent.pop();
    ss << "}\n";
  }
}
}  // namespace qcor
 No newline at end of file