@@ -4808,6 +4808,119 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4808
4808
cptr, ASR::down_cast<ASR::expr_t >(pp), nullptr );
4809
4809
}
4810
4810
4811
+ void handle_string_attributes (ASR::expr_t *s_var,
4812
+ Vec<ASR::call_arg_t > &args, std::string attr_name, const Location &loc) {
4813
+ std::string fn_call_name = " " ;
4814
+ Vec<ASR::call_arg_t > fn_args;
4815
+ fn_args.reserve (al, 1 );
4816
+ if (attr_name == " capitalize" ) {
4817
+ if (args.size () != 0 ) {
4818
+ throw SemanticError (" str.capitalize() takes no arguments" ,
4819
+ loc);
4820
+ }
4821
+ fn_call_name = " _lpython_str_capitalize" ;
4822
+ ASR::call_arg_t arg;
4823
+ arg.loc = loc;
4824
+ arg.m_value = s_var;
4825
+ fn_args.push_back (al, arg);
4826
+ } else if (attr_name == " lower" ) {
4827
+ if (args.size () != 0 ) {
4828
+ throw SemanticError (" str.lower() takes no arguments" ,
4829
+ loc);
4830
+ }
4831
+ fn_call_name = " _lpython_str_lower" ;
4832
+ ASR::call_arg_t arg;
4833
+ arg.loc = loc;
4834
+ arg.m_value = s_var;
4835
+ fn_args.push_back (al, arg);
4836
+ } else if (attr_name == " find" ) {
4837
+ if (args.size () != 1 ) {
4838
+ throw SemanticError (" str.find() takes one argument" ,
4839
+ loc);
4840
+ }
4841
+ ASR::expr_t *arg_sub = args[0 ].m_value ;
4842
+ ASR::ttype_t *arg_sub_type = ASRUtils::expr_type (arg_sub);
4843
+ if (!ASRUtils::is_character (*arg_sub_type)) {
4844
+ throw SemanticError (" str.find() takes one argument of type: str" ,
4845
+ loc);
4846
+ }
4847
+ fn_call_name = " _lpython_str_find" ;
4848
+ ASR::call_arg_t str;
4849
+ str.loc = loc;
4850
+ str.m_value = s_var;
4851
+ ASR::call_arg_t sub;
4852
+ sub.loc = loc;
4853
+ sub.m_value = args[0 ].m_value ;
4854
+ fn_args.push_back (al, str);
4855
+ fn_args.push_back (al, sub);
4856
+ } else if (attr_name == " rstrip" ) {
4857
+ if (args.size () != 0 ) {
4858
+ throw SemanticError (" str.rstrip() takes no arguments" ,
4859
+ loc);
4860
+ }
4861
+ fn_call_name = " _lpython_str_rstrip" ;
4862
+ ASR::call_arg_t arg;
4863
+ arg.loc = loc;
4864
+ arg.m_value = s_var;
4865
+ fn_args.push_back (al, arg);
4866
+ } else if (attr_name == " lstrip" ) {
4867
+ if (args.size () != 0 ) {
4868
+ throw SemanticError (" str.lstrip() takes no arguments" ,
4869
+ loc);
4870
+ }
4871
+ fn_call_name = " _lpython_str_lstrip" ;
4872
+ ASR::call_arg_t arg;
4873
+ arg.loc = loc;
4874
+ arg.m_value = s_var;
4875
+ fn_args.push_back (al, arg);
4876
+ } else if (attr_name == " strip" ) {
4877
+ if (args.size () != 0 ) {
4878
+ throw SemanticError (" str.strip() takes no arguments" ,
4879
+ loc);
4880
+ }
4881
+ fn_call_name = " _lpython_str_strip" ;
4882
+ ASR::call_arg_t arg;
4883
+ arg.loc = loc;
4884
+ arg.m_value = s_var;
4885
+ fn_args.push_back (al, arg);
4886
+ } else if (attr_name == " swapcase" ) {
4887
+ if (args.size () != 0 ) {
4888
+ throw SemanticError (" str.swapcase() takes no arguments" ,
4889
+ loc);
4890
+ }
4891
+ fn_call_name = " _lpython_str_swapcase" ;
4892
+ ASR::call_arg_t arg;
4893
+ arg.loc = loc;
4894
+ arg.m_value = s_var;
4895
+ fn_args.push_back (al, arg);
4896
+ } else if (attr_name == " startswith" ) {
4897
+ if (args.size () != 1 ) {
4898
+ throw SemanticError (" str.startwith() takes one argument" ,
4899
+ loc);
4900
+ }
4901
+ ASR::expr_t *arg_sub = args[0 ].m_value ;
4902
+ ASR::ttype_t *arg_sub_type = ASRUtils::expr_type (arg_sub);
4903
+ if (!ASRUtils::is_character (*arg_sub_type)) {
4904
+ throw SemanticError (" str.startwith() takes one argument of type: str" ,
4905
+ loc);
4906
+ }
4907
+ fn_call_name = " _lpython_str_startswith" ;
4908
+ ASR::call_arg_t str;
4909
+ str.loc = loc;
4910
+ str.m_value = s_var;
4911
+ ASR::call_arg_t sub;
4912
+ sub.loc = loc;
4913
+ sub.m_value = args[0 ].m_value ;
4914
+ fn_args.push_back (al, str);
4915
+ fn_args.push_back (al, sub);
4916
+ } else {
4917
+ throw SemanticError (" String method not implemented: " + attr_name,
4918
+ loc);
4919
+ }
4920
+ ASR::symbol_t *fn_call = resolve_intrinsic_function (loc, fn_call_name);
4921
+ tmp = make_call_helper (al, fn_call, current_scope, fn_args, fn_call_name, loc);
4922
+ }
4923
+
4811
4924
void visit_Call (const AST::Call_t &x) {
4812
4925
std::string call_name;
4813
4926
Vec<ASR::call_arg_t > args;
@@ -4845,139 +4958,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4845
4958
ASR::expr_t *se = ASR::down_cast<ASR::expr_t >(
4846
4959
ASR::make_Var_t (al, x.base .base .loc , st));
4847
4960
if (ASR::is_a<ASR::Character_t>(*(ASRUtils::expr_type (se)))) {
4848
- if (std::string (at->m_attr ) == std::string (" capitalize" )) {
4849
- if (args.size () != 0 ) {
4850
- throw SemanticError (" str.capitalize() takes no arguments" ,
4851
- x.base .base .loc );
4852
- }
4853
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_capitalize" );
4854
- Vec<ASR::call_arg_t > args;
4855
- args.reserve (al, 1 );
4856
- ASR::call_arg_t arg;
4857
- arg.loc = x.base .base .loc ;
4858
- arg.m_value = se;
4859
- args.push_back (al, arg);
4860
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_capitalize" , x.base .base .loc );
4861
- return ;
4862
- } else if (std::string (at->m_attr ) == std::string (" lower" )) {
4863
- if (args.size () != 0 ) {
4864
- throw SemanticError (" str.lower() takes no arguments" ,
4865
- x.base .base .loc );
4866
- }
4867
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_lower" );
4868
- Vec<ASR::call_arg_t > args;
4869
- args.reserve (al, 1 );
4870
- ASR::call_arg_t arg;
4871
- arg.loc = x.base .base .loc ;
4872
- arg.m_value = se;
4873
- args.push_back (al, arg);
4874
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_lower" , x.base .base .loc );
4875
- return ;
4876
- } else if (std::string (at->m_attr ) == std::string (" find" )) {
4877
- if (args.size () != 1 ) {
4878
- throw SemanticError (" str.find() takes one argument" ,
4879
- x.base .base .loc );
4880
- }
4881
- ASR::expr_t *arg_sub = args[0 ].m_value ;
4882
- ASR::ttype_t *arg_sub_type = ASRUtils::expr_type (arg_sub);
4883
- if (!ASRUtils::is_character (*arg_sub_type)) {
4884
- throw SemanticError (" str.find() takes one argument of type: str" ,
4885
- x.base .base .loc );
4886
- }
4887
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_find" );
4888
- Vec<ASR::call_arg_t > function_args;
4889
- function_args.reserve (al, 1 );
4890
- ASR::call_arg_t str;
4891
- str.loc = x.base .base .loc ;
4892
- str.m_value = se;
4893
- ASR::call_arg_t sub;
4894
- sub.loc = x.base .base .loc ;
4895
- sub.m_value = args[0 ].m_value ;
4896
- function_args.push_back (al, str);
4897
- function_args.push_back (al, sub);
4898
- tmp = make_call_helper (al, fn_div, current_scope, function_args, " _lpython_str_find" , x.base .base .loc );
4899
- return ;
4900
- } else if (std::string (at->m_attr ) == std::string (" rstrip" )) {
4901
- if (args.size () != 0 ) {
4902
- throw SemanticError (" str.srtrip() takes no arguments" ,
4903
- x.base .base .loc );
4904
- }
4905
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_rstrip" );
4906
- Vec<ASR::call_arg_t > args;
4907
- args.reserve (al, 1 );
4908
- ASR::call_arg_t arg;
4909
- arg.loc = x.base .base .loc ;
4910
- arg.m_value = se;
4911
- args.push_back (al, arg);
4912
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_rstrip" , x.base .base .loc );
4913
- return ;
4914
- } else if (std::string (at->m_attr ) == std::string (" lstrip" )) {
4915
- if (args.size () != 0 ) {
4916
- throw SemanticError (" str.lrtrip() takes no arguments" ,
4917
- x.base .base .loc );
4918
- }
4919
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_lstrip" );
4920
- Vec<ASR::call_arg_t > args;
4921
- args.reserve (al, 1 );
4922
- ASR::call_arg_t arg;
4923
- arg.loc = x.base .base .loc ;
4924
- arg.m_value = se;
4925
- args.push_back (al, arg);
4926
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_lstrip" , x.base .base .loc );
4927
- return ;
4928
- } else if (std::string (at->m_attr ) == std::string (" strip" )) {
4929
- if (args.size () != 0 ) {
4930
- throw SemanticError (" str.srtrip() takes no arguments" ,
4931
- x.base .base .loc );
4932
- }
4933
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_strip" );
4934
- Vec<ASR::call_arg_t > args;
4935
- args.reserve (al, 1 );
4936
- ASR::call_arg_t arg;
4937
- arg.loc = x.base .base .loc ;
4938
- arg.m_value = se;
4939
- args.push_back (al, arg);
4940
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_strip" , x.base .base .loc );
4941
- return ;
4942
- } else if (std::string (at->m_attr ) == std::string (" swapcase" )) {
4943
- if (args.size () != 0 ) {
4944
- throw SemanticError (" str.swapcase() takes no arguments" ,
4945
- x.base .base .loc );
4946
- }
4947
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_swapcase" );
4948
- Vec<ASR::call_arg_t > args;
4949
- args.reserve (al, 1 );
4950
- ASR::call_arg_t arg;
4951
- arg.loc = x.base .base .loc ;
4952
- arg.m_value = se;
4953
- args.push_back (al, arg);
4954
- tmp = make_call_helper (al, fn_div, current_scope, args, " _lpython_str_swapcase" , x.base .base .loc );
4955
- return ;
4956
- } else if (std::string (at->m_attr ) == std::string (" startswith" )) {
4957
- if (args.size () != 1 ) {
4958
- throw SemanticError (" str.startwith() takes one argument" ,
4959
- x.base .base .loc );
4960
- }
4961
- ASR::expr_t *arg_sub = args[0 ].m_value ;
4962
- ASR::ttype_t *arg_sub_type = ASRUtils::expr_type (arg_sub);
4963
- if (!ASRUtils::is_character (*arg_sub_type)) {
4964
- throw SemanticError (" str.startwith() takes one argument of type: str" ,
4965
- x.base .base .loc );
4966
- }
4967
- ASR::symbol_t *fn_div = resolve_intrinsic_function (x.base .base .loc , " _lpython_str_startswith" );
4968
- Vec<ASR::call_arg_t > function_args;
4969
- function_args.reserve (al, 1 );
4970
- ASR::call_arg_t str;
4971
- str.loc = x.base .base .loc ;
4972
- str.m_value = se;
4973
- ASR::call_arg_t sub;
4974
- sub.loc = x.base .base .loc ;
4975
- sub.m_value = args[0 ].m_value ;
4976
- function_args.push_back (al, str);
4977
- function_args.push_back (al, sub);
4978
- tmp = make_call_helper (al, fn_div, current_scope, function_args, " _lpython_str_startswith" , x.base .base .loc );
4979
- return ;
4980
- }
4961
+ handle_string_attributes (se, args, at->m_attr , x.base .base .loc );
4962
+ return ;
4981
4963
}
4982
4964
handle_attribute (se, at->m_attr , x.base .base .loc , eles);
4983
4965
return ;
0 commit comments