Skip to content

Commit 53a16af

Browse files
Add str.strip() to compile time
1 parent 581f272 commit 53a16af

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4406,6 +4406,23 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
44064406
1, 1, nullptr, nullptr , 0));
44074407
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
44084408
return;
4409+
} else if (std::string(at->m_attr) == std::string("strip")) {
4410+
if(args.size() != 0) {
4411+
throw SemanticError("str.strip() takes no arguments",
4412+
x.base.base.loc);
4413+
}
4414+
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
4415+
std::string res = n->m_value;
4416+
int l = 0 ,r = res.size() - 1;
4417+
while (l < res.size() && r >= 0 && (res[l] == ' ' || res[r] == ' ')) {
4418+
l += res[l] == ' ';
4419+
r -= res[r] == ' ';
4420+
}
4421+
res = std::string(res.begin() + l, res.begin() + r + 1);
4422+
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
4423+
1, 1, nullptr, nullptr , 0));
4424+
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
4425+
return;
44094426
} else {
44104427
throw SemanticError("'str' object has no attribute '" + std::string(at->m_attr) + "'",
44114428
x.base.base.loc);

src/lpython/semantics/python_comptime_eval.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct PythonIntrinsicProcedures {
6666
{"_lpython_str_lower", {m_builtin, &eval__lpython_str_lower}},
6767
{"_lpython_str_rstrip", {m_builtin, &eval__lpython_str_rstrip}},
6868
{"_lpython_str_lstrip", {m_builtin, &eval__lpython_str_lstrip}},
69+
{"_lpython_str_strip", {m_builtin, &eval__lpython_str_strip}}
6970
};
7071
}
7172

@@ -724,6 +725,25 @@ struct PythonIntrinsicProcedures {
724725
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
725726
}
726727

728+
static ASR::expr_t *eval__lpython_str_strip(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
729+
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
730+
if (args.size() != 0) {
731+
throw SemanticError("str.strip() takes no arguments", loc);
732+
}
733+
ASR::expr_t *arg = args[0];
734+
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
735+
std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
736+
int l = 0 ,r = res.size() - 1;
737+
while (l < res.size() && r >= 0 && (res[l] == ' ' || res[r] == ' ')) {
738+
l += res[l] == ' ';
739+
r -= res[r] == ' ';
740+
}
741+
res = std::string(res.begin() + l, res.begin() + r + 1);
742+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
743+
1, 1, nullptr, nullptr, 0));
744+
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
745+
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
746+
}
727747
}; // ComptimeEval
728748

729749
} // namespace LFortran

0 commit comments

Comments
 (0)