// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include "paddle/ap/include/axpr/constants.h" #include "paddle/ap/include/axpr/method_class.h" namespace ap::axpr { template struct BuiltinStringBinaryHelper { static Result Call(const std::string& str, const T& rhs) { return adt::errors::TypeError{ std::string() + "unsupported operand types for " + ArithmeticOp::Name() + ": 'str' and '" + TypeImpl{}.Name() + "'"}; } }; template struct BuiltinStringBinaryHelper { static Result Call(const std::string& lhs, const std::string& rhs) { return ArithmeticAdd::Call(lhs, rhs); } }; template struct BuiltinStringBinaryHelper { static Result Call(const std::string& lhs, const int64_t& rhs_val) { return lhs + std::to_string(rhs_val); } }; template struct BuiltinStringBinaryHelper { static Result Call(const std::string& lhs, const bool& rhs_val) { return lhs + (rhs_val ? "True" : "False"); } }; #define SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(cls_name) \ template \ struct BuiltinStringBinaryHelper { \ static Result Call(const std::string& lhs, const std::string& rhs) { \ return cls_name::Call(lhs, rhs); \ } \ }; SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticEQ); SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticNE); SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticGT); SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticGE); SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticLT); SPECIALIZE_BuiltinStringBinaryHelper_string_cmp(ArithmeticLE); #undef SPECIALIZE_BuiltinStringBinaryHelper_string template struct BuiltinStringBinaryHelper { static Result Call(const std::string& lhs, int64_t size) { size = (size > 0 ? size : 0); std::ostringstream ss; for (int i = 0; i < size; ++i) { ss << lhs; } return ss.str(); } }; template struct BuiltinStringBinaryHelper { static Result Call(const std::string& lhs, bool size) { std::ostringstream ss; for (int i = 0; i < static_cast(size); ++i) { ss << lhs; } return ss.str(); } }; template Result BuiltinStringBinary(const std::string& str, const Val& rhs_val) { return rhs_val.Match( [&](const BuiltinClassInstance& impl) -> Result { return adt::errors::TypeError{ std::string() + "unsupported operand types for " + ArithmeticOp::Name() + ": 'str' and '" + impl.type.class_attrs()->class_name + "'"}; }, [&](const ClassInstance& impl) -> Result { return adt::errors::TypeError{std::string() + "unsupported operand types for " + ArithmeticOp::Name() + ": 'str' and '" + impl->type.class_attrs->class_name + "'"}; }, [&](const auto& rhs) -> Result { using T = std::decay_t; return BuiltinStringBinaryHelper::Call(str, rhs); }); } } // namespace ap::axpr