Skip to content

Commit 1a5ef6f

Browse files
Fix compile time evaluation for capitalize
1 parent fe2ef52 commit 1a5ef6f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/lpython/semantics/python_comptime_eval.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -655,21 +655,17 @@ struct PythonIntrinsicProcedures {
655655

656656
static ASR::expr_t *eval__lpython_str_capitalize(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
657657
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
658-
if (args.size() != 1) {
659-
throw SemanticError("_lpython_str_capitalize() takes exactly one arguments (" +
660-
std::to_string(args.size()) + " given)", loc);
661-
}
662-
ASR::expr_t *arg1 = args[0];
663-
ASR::ttype_t *arg1_type = ASRUtils::expr_type(arg1);
664-
if (ASRUtils::is_character(*arg1_type)) {
665-
std::string val = ASR::down_cast<ASR::StringConstant_t>(arg1)->m_s;
666-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
667-
1, 1, nullptr, nullptr, 0));
668-
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
669-
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, val), res_type));
670-
} else {
671-
throw SemanticError("Only string from arguments.", loc);
672-
}
658+
if (args.size() != 0) {
659+
throw SemanticError("str.capitalize() takes no arguments", loc);
660+
}
661+
ASR::expr_t *arg = args[0];
662+
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
663+
std::string val = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
664+
val = std::toupper(val[0]);
665+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
666+
1, 1, nullptr, nullptr, 0));
667+
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
668+
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, val), res_type));
673669
}
674670

675671
static ASR::expr_t *eval__lpython_str_lower(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {

0 commit comments

Comments
 (0)