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

Work on while loop



Add `while` token and handler for the while Python statement

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 19ee8d86
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -79,10 +79,12 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
      line += for_stmt;
    }

    // If statement:
    // If statement or while statement:
    // Add a space b/w tokens.
    // Note: Python has an "elif" token, which doesn't have a C++ equiv.
    if (Toks[i].is(clang::tok::TokenKind::kw_if) ||
        PP.getSpelling(Toks[i]) == "elif") {
        PP.getSpelling(Toks[i]) == "elif" ||
        Toks[i].is(clang::tok::TokenKind::kw_while)) {
      line += " ";
      i += 1;
      line += PP.getSpelling(Toks[i]);
@@ -171,6 +173,11 @@ void PyXasmTokenCollector::collect(clang::Preprocessor &PP,
      // Remove the first two characters ("el")
      // hence this line will be parsed as an idependent C++ if block:
      lineText.erase(0, 2);
    } else if (line.first.rfind("while ", 0) == 0) {
      // rewrite to 
      // while (condition) {}
      // Just capture the indent level to close the scope properly
      scope_block_indent.push(line.second);
    }
    // is_in_for_loop = line.first.find("for ") != std::string::npos &&
    // line.second >= previous_col;
+8 −0
Original line number Diff line number Diff line
@@ -501,6 +501,14 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
    return visitChildren(ctx);
  }

  virtual antlrcpp::Any
  visitWhile_stmt(pyxasmParser::While_stmtContext *ctx) override {
    std::stringstream ss;
    ss << "while (" << ctx->test()->getText() << ") {\n";
    result.first = ss.str();
    return 0;
  }

 private:
  // Replaces common Python constants, e.g. 'math.pi' or 'numpy.pi'.
  // Note: the library names have been resolved to their original names.