Skip to content

Commit ac1e51f

Browse files
Add str.swapcase() to compile time
1 parent bcdfad8 commit ac1e51f

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4421,6 +4421,25 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
44214421
1, 1, nullptr, nullptr , 0));
44224422
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
44234423
return;
4424+
} else if (std::string(at->m_attr) == std::string("swapcase")) {
4425+
if(args.size() != 0) {
4426+
throw SemanticError("str.swapcase() takes no arguments",
4427+
x.base.base.loc);
4428+
}
4429+
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
4430+
std::string res = n->m_value;
4431+
for (size_t i = 0; i < res.size(); i++) {
4432+
char &cur = res[i];
4433+
if(cur >= 'a' && cur <= 'z') {
4434+
cur = cur -'a' + 'A';
4435+
} else if(cur >= 'A' && cur <= 'Z') {
4436+
cur = cur - 'A' + 'a';
4437+
}
4438+
}
4439+
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
4440+
1, 1, nullptr, nullptr , 0));
4441+
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
4442+
return;
44244443
} else if (std::string(at->m_attr) == std::string("startswith")) {
44254444
if (args.size() != 1) {
44264445
throw SemanticError("str.startswith() takes one arguments",

src/lpython/semantics/python_comptime_eval.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct PythonIntrinsicProcedures {
6868
{"_lpython_str_rstrip", {m_builtin, &eval__lpython_str_rstrip}},
6969
{"_lpython_str_lstrip", {m_builtin, &eval__lpython_str_lstrip}},
7070
{"_lpython_str_strip", {m_builtin, &eval__lpython_str_strip}},
71+
{"_lpython_str_swapcase", {m_builtin, &eval__lpython_str_swapcase}},
7172
{"_lpython_str_startswith", {m_builtin, &eval__lpython_str_startswith}}
7273
};
7374
}
@@ -742,7 +743,16 @@ struct PythonIntrinsicProcedures {
742743
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
743744
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
744745
}
745-
746+
747+
static ASR::expr_t *eval__lpython_str_swapcase(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
748+
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
749+
std::string res = "";
750+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
751+
1, 1, nullptr, nullptr, 0));
752+
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
753+
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
754+
}
755+
746756
static ASR::expr_t *eval_lpython_str_find(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &/*args*/) {
747757
// compile time action implemented on ast->asr
748758
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));

0 commit comments

Comments
 (0)