Skip to content

Commit 71fb600

Browse files
Add str.capitalize() to ast->asr
1 parent 8499cee commit 71fb600

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,23 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
42434243
}
42444244
ASR::expr_t *se = ASR::down_cast<ASR::expr_t>(
42454245
ASR::make_Var_t(al, x.base.base.loc, st));
4246+
if (ASR::is_a<ASR::Character_t>(*(ASRUtils::expr_type(se)))) {
4247+
if (std::string(at->m_attr) == std::string("capitalize")) {
4248+
if(args.size() != 0) {
4249+
throw SemanticError("str.capitalize() takes no arguments",
4250+
x.base.base.loc);
4251+
}
4252+
ASR::symbol_t *fn_div = resolve_intrinsic_function(x.base.base.loc, "_lpython_str_capitalize");
4253+
Vec<ASR::call_arg_t> args;
4254+
args.reserve(al, 1);
4255+
ASR::call_arg_t arg;
4256+
arg.loc = x.base.base.loc;
4257+
arg.m_value = se;
4258+
args.push_back(al, arg);
4259+
tmp = make_call_helper(al, fn_div, current_scope, args, "_lpython_str_capitalize", x.base.base.loc);
4260+
return;
4261+
}
4262+
}
42464263
handle_attribute(se, at->m_attr, x.base.base.loc, eles);
42474264
return;
42484265
}
@@ -4276,6 +4293,23 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
42764293
throw SemanticError("'int' object has no attribute '" + std::string(at->m_attr) + "'",
42774294
x.base.base.loc);
42784295
}
4296+
} else if (AST::is_a<AST::ConstantStr_t>(*at->m_value)) {
4297+
if (std::string(at->m_attr) == std::string("capitalize")) {
4298+
if(args.size() != 0) {
4299+
throw SemanticError("str.capitalize() takes no arguments",
4300+
x.base.base.loc);
4301+
}
4302+
AST::ConstantStr_t *n = AST::down_cast<AST::ConstantStr_t>(at->m_value);
4303+
std::string res = n->m_value;
4304+
res[0] = toupper(res[0]);
4305+
ASR::ttype_t *str_type = ASRUtils::TYPE(ASR::make_Character_t(al, x.base.base.loc,
4306+
1, 1, nullptr, nullptr , 0));
4307+
tmp = ASR::make_StringConstant_t(al, x.base.base.loc, s2c(al, res), str_type);
4308+
return;
4309+
} else {
4310+
throw SemanticError("'str' object has no attribute '" + std::string(at->m_attr) + "'",
4311+
x.base.base.loc);
4312+
}
42794313
} else {
42804314
throw SemanticError("Only Name type and constant integers supported in Call",
42814315
x.base.base.loc);

src/lpython/semantics/python_attribute_eval.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <libasr/string_utils.h>
77
#include <lpython/utils.h>
88
#include <lpython/semantics/semantic_exception.h>
9-
109
namespace LFortran {
1110

1211
struct AttributeHandler {
@@ -331,6 +330,7 @@ struct AttributeHandler {
331330

332331
}; // AttributeHandler
333332

333+
334334
} // namespace LFortran
335335

336336
#endif /* LPYTHON_ATTRIBUTE_EVAL_H */

src/lpython/semantics/python_comptime_eval.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ struct PythonIntrinsicProcedures {
6161
{"_lpython_floordiv", {m_builtin, &eval__lpython_floordiv}},
6262
{"_mod", {m_builtin, &eval__mod}},
6363
{"max" , {m_builtin , &eval_max}},
64-
{"min" , {m_builtin , &eval_min}}
64+
{"min" , {m_builtin , &eval_min}},
65+
{"_lpython_str_capitalize", {m_builtin, &eval__lpython_str_capitalize}}
6566
};
6667
}
6768

@@ -592,8 +593,8 @@ struct PythonIntrinsicProcedures {
592593
ASR::expr_t *first_element = args[0];
593594
ASR::ttype_t *first_element_type = ASRUtils::expr_type(first_element);
594595
semantic_error_flag &= ASRUtils::is_integer(*first_element_type)
595-
|| ASRUtils::is_real(*first_element_type)
596-
|| ASRUtils::is_character(*first_element_type);
596+
|| ASRUtils::is_real(*first_element_type)
597+
|| ASRUtils::is_character(*first_element_type);
597598
int32_t smallest_ind = 0;
598599
if (semantic_error_flag) {
599600
if (ASRUtils::is_integer(*first_element_type)) {
@@ -650,6 +651,24 @@ struct PythonIntrinsicProcedures {
650651

651652
}
652653

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

654673

655674
}; // ComptimeEval

0 commit comments

Comments
 (0)