junrushao1994 commented on code in PR #12048:
URL: https://github.com/apache/tvm/pull/12048#discussion_r922704681


##########
src/script/printer/python_doc_printer.cc:
##########
@@ -57,6 +82,179 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc) 
{
   }
 }
 
+void PythonDocPrinter::PrintTypedDoc(const IdDoc& doc) { output_ << doc->name; 
}
+
+void PythonDocPrinter::PrintTypedDoc(const AttrAccessDoc& doc) {
+  PrintDoc(doc->value);
+  output_ << "." << doc->attr;
+}
+
+void PythonDocPrinter::PrintTypedDoc(const IndexDoc& doc) {
+  PrintDoc(doc->value);
+  if (doc->indices.size() == 0) {
+    output_ << "[()]";
+  } else {
+    output_ << "[";
+    PrintJoinedDocs(doc->indices, ", ");
+    output_ << "]";
+  }
+}
+
+const std::string OperatorToString(OperationDocNode::Kind operation_kind) {
+  static const std::vector<std::string> OP_STR_TABLE = []() {
+    using OpKind = OperationDocNode::Kind;
+    std::map<OpKind, std::string> raw_table = {
+        {OpKind::kUSub, "-"},       //
+        {OpKind::kInvert, "~"},     //
+        {OpKind::kAdd, "+"},        //
+        {OpKind::kSub, "-"},        //
+        {OpKind::kMult, "*"},       //
+        {OpKind::kDiv, "/"},        //
+        {OpKind::kFloorDiv, "//"},  //
+        {OpKind::kMod, "%"},        //
+        {OpKind::kPow, "**"},       //
+        {OpKind::kLShift, "<<"},    //
+        {OpKind::kRShift, ">>"},    //
+        {OpKind::kBitAnd, "&"},     //
+        {OpKind::kBitOr, "|"},      //
+        {OpKind::kBitXor, "^"},     //
+        {OpKind::kLt, "<"},         //
+        {OpKind::kLtE, "<="},       //
+        {OpKind::kEq, "=="},        //
+        {OpKind::kNotEq, "!="},     //
+        {OpKind::kGt, ">"},         //
+        {OpKind::kGtE, ">="},       //
+    };
+
+    std::vector<std::string> table;
+    table.resize(static_cast<int>(OperationDocNode::Kind::kSpecialEnd) + 1);
+
+    for (const auto& kv : raw_table) {
+      table[static_cast<int>(kv.first)] = kv.second;
+    }
+
+    return table;
+  }();
+
+  auto op_index = static_cast<int>(operation_kind);
+  ICHECK_LT(op_index, OP_STR_TABLE.size());
+  const std::string str = OP_STR_TABLE[op_index];
+  if (str.empty()) {
+    LOG(FATAL) << "OperationDocNode::Kind " << static_cast<int>(operation_kind)
+               << " cannot be converted to operator token in Python directly.";
+    throw;
+  }
+  return str;
+}
+
+void PythonDocPrinter::PrintTypedDoc(const OperationDoc& doc) {
+  using OpKind = OperationDocNode::Kind;
+  if (doc->kind < OpKind::kUnaryEnd) {
+    // Unary Operators
+    ICHECK_EQ(doc->operands.size(), 1);
+    output_ << OperatorToString(doc->kind);
+    PrintDoc(doc->operands[0]);
+  } else if (doc->kind < OpKind::kBinaryEnd) {
+    // Binary Operator
+    ICHECK_EQ(doc->operands.size(), 2);
+    PrintDoc(doc->operands[0]);
+    output_ << " " << OperatorToString(doc->kind) << " ";
+    PrintDoc(doc->operands[1]);
+  } else if (doc->kind == OpKind::kIfThenElse) {
+    CHECK_EQ(doc->operands.size(), 3)
+        << "ValueError: IfThenElse requires 3 operands, but got " << 
doc->operands.size();
+    PrintDoc(doc->operands[1]);
+    output_ << " if ";
+    PrintDoc(doc->operands[0]);
+    output_ << " else ";
+    PrintDoc(doc->operands[2]);
+  } else {
+    LOG(FATAL) << "Unknown OperationDocNode::Kind " << 
static_cast<int>(doc->kind);
+    throw;
+  }
+}
+
+void PythonDocPrinter::PrintTypedDoc(const CallDoc& doc) {
+  PrintDoc(doc->callee);
+
+  output_ << "(";
+
+  // Print positional args
+  bool is_first = true;
+  for (const ExprDoc& arg : doc->args) {
+    if (is_first) {
+      is_first = false;
+    } else {
+      output_ << ", ";
+    }
+    PrintDoc(arg);
+  }
+
+  // Print keyword args
+  for (size_t i = 0; i < doc->kwargs_keys.size(); i++) {
+    if (is_first) {
+      is_first = false;
+    } else {
+      output_ << ", ";
+    }
+    const String& keyword = doc->kwargs_keys[i];
+    output_ << keyword;
+    output_ << "=";
+    PrintDoc(doc->kwargs_values[i]);
+  }
+
+  output_ << ")";
+}
+
+void PythonDocPrinter::PrintTypedDoc(const LambdaDoc& doc) {
+  output_ << "lambda ";
+  PrintJoinedDocs(doc->args, ", ");
+  output_ << ": ";
+  PrintDoc(doc->body);
+}
+
+void PythonDocPrinter::PrintTypedDoc(const ListDoc& doc) {
+  output_ << "[";
+  PrintJoinedDocs(doc->elements, ", ");
+  output_ << "]";
+}
+
+void PythonDocPrinter::PrintTypedDoc(const TupleDoc& doc) {
+  output_ << "(";
+  if (doc->elements.size() == 1) {
+    PrintDoc(doc->elements[0]);
+    output_ << ",";
+  } else {
+    PrintJoinedDocs(doc->elements, ", ");
+  }
+  output_ << ")";
+}
+
+void PythonDocPrinter::PrintTypedDoc(const DictDoc& doc) {
+  output_ << "{";
+  size_t idx = 0;

Review Comment:
   ditto. add an `ICHECK_EQ` to ensure `|values| == |keys|`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to