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

Handle python assignment expression more robustly



We don't want to add 'auto' in a reassignment case.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 896d7a11
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,

  int previous_col = lines[0].second;
  int line_counter = 0;
  std::vector<std::string> local_vars;
  // Tracking the Python scopes by the indent of code blocks
  std::stack<int> scope_block_indent;
  for (const auto &line : lines) {
@@ -107,7 +108,7 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
    //           << ": " << line.first << ", " << line.second << std::boolalpha
    //           << ", " << !scope_block_indent.empty() << "\n";

    pyxasm_visitor visitor(bufferNames);
    pyxasm_visitor visitor(bufferNames, local_vars);
    // Should we close a 'for'/'if' scope after this statement
    // If > 0, indicate the number of for blocks to be closed.
    int nb_closing_scopes = 0;
@@ -194,6 +195,10 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
    }
    previous_col = line.second;
    line_counter++;
    if (!visitor.new_var.empty()) {
      // A new local variable was declared, add to the tracking list.
      local_vars.emplace_back(visitor.new_var);
    }
  }
  // If there are open scope blocks here,
  // e.g. for loops at the end of the function body.
+18 −5
Original line number Diff line number Diff line
@@ -19,12 +19,17 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
  std::shared_ptr<xacc::IRProvider> provider;
  // List of buffers in the *context* of this XASM visitor
  std::vector<std::string> bufferNames;
  // List of *declared* variables
  std::vector<std::string> declared_var_names;

public:
  pyxasm_visitor(const std::vector<std::string> &buffers = {})
      : provider(xacc::getIRProvider("quantum")), bufferNames(buffers) {}
  pyxasm_visitor(const std::vector<std::string> &buffers = {},
                 const std::vector<std::string> &local_var_names = {})
      : provider(xacc::getIRProvider("quantum")), bufferNames(buffers),
        declared_var_names(local_var_names) {}
  pyxasm_result_type result;

  // New var declared (auto type) after visiting this node.
  std::string new_var;
  bool in_for_loop = false;

  antlrcpp::Any visitAtom_expr(
@@ -230,7 +235,15 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
      const std::string lhs = ctx->testlist_star_expr(0)->getText();
      const std::string rhs = replacePythonConstants(
          replaceMeasureAssignment(ctx->testlist_star_expr(1)->getText()));
      
      if (xacc::container::contains(declared_var_names, lhs)) {
        ss << lhs << " = " << rhs << "; \n";
      } else {
        // New variable: need to add *auto*
        ss << "auto " << lhs << " = " << rhs << "; \n";
        new_var = lhs;
      }
      
      result.first = ss.str();
      if (rhs.find("**") != std::string::npos) {
        // keep processing