Skip to content

Commit b3eb3fe

Browse files
Add str.lstrip() to compile time
1 parent 7e73ddb commit b3eb3fe

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4376,6 +4376,22 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
43764376
1, 1, nullptr, nullptr , 0));
43774377
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
43784378
return;
4379+
} else if (std::string(at->m_attr) == std::string("lstrip")) {
4380+
if(args.size() != 0) {
4381+
throw SemanticError("str.lstrip() takes no arguments",
4382+
x.base.base.loc);
4383+
}
4384+
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
4385+
std::string res = n->m_value;
4386+
int ind = 0;
4387+
while (ind < res.size() && res[ind] == ' ') {
4388+
ind++;
4389+
}
4390+
res = std::string(res.begin() + ind, res.end());
4391+
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
4392+
1, 1, nullptr, nullptr , 0));
4393+
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
4394+
return;
43794395
} else {
43804396
throw SemanticError("'str' object has no attribute '" + std::string(at->m_attr) + "'",
43814397
x.base.base.loc);

src/lpython/semantics/python_comptime_eval.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct PythonIntrinsicProcedures {
6565
{"_lpython_str_capitalize", {m_builtin, &eval__lpython_str_capitalize}},
6666
{"_lpython_str_lower", {m_builtin, &eval__lpython_str_lower}},
6767
{"_lpython_str_rstrip", {m_builtin, &eval__lpython_str_rstrip}},
68+
{"_lpython_str_lstrip", {m_builtin, &eval__lpython_str_lstrip}},
6869
};
6970
}
7071

@@ -706,6 +707,22 @@ struct PythonIntrinsicProcedures {
706707
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
707708
}
708709

710+
static ASR::expr_t *eval__lpython_str_lstrip(Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
711+
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
712+
if (args.size() != 0) {
713+
throw SemanticError("str.lstrip() takes no arguments", loc);
714+
}
715+
ASR::expr_t *arg = args[0];
716+
ASR::ttype_t *arg_type = ASRUtils::expr_type(arg);
717+
std::string res = ASR::down_cast<ASR::StringConstant_t>(arg)->m_s;
718+
int ind = 0;
719+
while (ind < res.size() && res[ind++] == ' ');
720+
res = std::string(res.begin() + ind, res.end());
721+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
722+
1, 1, nullptr, nullptr, 0));
723+
ASR::ttype_t *res_type = ASRUtils::TYPE(ASR::make_StringConstant_t(al, loc, s2c(al, ""), type));
724+
return ASR::down_cast<ASR::expr_t>(ASR::make_StringConstant_t(al, loc, s2c(al, res), res_type));
725+
}
709726

710727
}; // ComptimeEval
711728

0 commit comments

Comments
 (0)