Skip to content

Commit

Permalink
Names changed for creation and evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsingh-24 committed Mar 26, 2023
1 parent 0903c9d commit 745b39e
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5798,59 +5798,64 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return res;
}

void string_create_partition(std::string &s_var, ASR::expr_t* arg_seperator,
const Location &loc, ASR::ttype_t *arg_seperator_type) {
/*
Invoked when Seperator argument is provided as a constant string
*/
ASR::StringConstant_t* seperator_constant = ASR::down_cast<ASR::StringConstant_t>(arg_seperator);
std::string seperator = seperator_constant->m_s;
void string_compile_time_evaluation(std::string &s_var, ASR::expr_t* arg_seperator,
const Location &loc, ASR::ttype_t *arg_seperator_type, std::string attr_name) {

if(seperator.size() == 0) {
throw SemanticError("empty separator", arg_seperator->base.loc);
}

/*
using KMP algorithm to find seperator inside string
res_tuple: stores the resulting 3-tuple expression --->
(if seperator exist) tuple: (left of seperator, seperator, right of seperator)
(if seperator does not exist) tuple: (string, "", "")
res_tuple_type: stores the type of each expression present in resulting 3-tuple
*/
int seperator_pos = KMP_string_match(s_var, seperator);
if(attr_name == "partition") {
/*
Invoked when Seperator argument is provided as a constant string
*/
ASR::StringConstant_t* seperator_constant = ASR::down_cast<ASR::StringConstant_t>(arg_seperator);
std::string seperator = seperator_constant->m_s;

Vec<ASR::expr_t *> res_tuple;
Vec<ASR::ttype_t *> res_tuple_type;
res_tuple.reserve(al, 3);
res_tuple_type.reserve(al, 3);
if(seperator.size() == 0) {
throw SemanticError("empty separator", arg_seperator->base.loc);
}

std :: string first_res, second_res, third_res;
/*
using KMP algorithm to find seperator inside string
res_tuple: stores the resulting 3-tuple expression --->
(if seperator exist) tuple: (left of seperator, seperator, right of seperator)
(if seperator does not exist) tuple: (string, "", "")
res_tuple_type: stores the type of each expression present in resulting 3-tuple
*/
int seperator_pos = KMP_string_match(s_var, seperator);

if(seperator_pos == -1) {
/* seperator does not exist */
first_res = s_var;
second_res = "";
third_res = "";
} else {
first_res = s_var.substr(0, seperator_pos);
second_res = seperator;
third_res = s_var.substr(seperator_pos + seperator.size());
}
Vec<ASR::expr_t *> res_tuple;
Vec<ASR::ttype_t *> res_tuple_type;
res_tuple.reserve(al, 3);
res_tuple_type.reserve(al, 3);

std :: string first_res, second_res, third_res;

if(seperator_pos == -1) {
/* seperator does not exist */
first_res = s_var;
second_res = "";
third_res = "";
} else {
first_res = s_var.substr(0, seperator_pos);
second_res = seperator;
third_res = s_var.substr(seperator_pos + seperator.size());
}

res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, first_res), arg_seperator_type)));
res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, second_res), arg_seperator_type)));
res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, third_res), arg_seperator_type)));
res_tuple_type.push_back(al, arg_seperator_type);
res_tuple_type.push_back(al,arg_seperator_type);
res_tuple_type.push_back(al,arg_seperator_type);
res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, first_res), arg_seperator_type)));
res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, second_res), arg_seperator_type)));
res_tuple.push_back(al, ASRUtils::EXPR(ASR::make_StringConstant_t(al, loc, s2c(al, third_res), arg_seperator_type)));
res_tuple_type.push_back(al, arg_seperator_type);
res_tuple_type.push_back(al,arg_seperator_type);
res_tuple_type.push_back(al,arg_seperator_type);

ASR::ttype_t *tuple_type = ASRUtils::TYPE(ASR::make_Tuple_t(al, loc, res_tuple_type.p, res_tuple_type.n));
tmp = ASR::make_TupleConstant_t(al, loc, res_tuple.p, res_tuple.size(), tuple_type);
ASR::ttype_t *tuple_type = ASRUtils::TYPE(ASR::make_Tuple_t(al, loc, res_tuple_type.p, res_tuple_type.n));
tmp = ASR::make_TupleConstant_t(al, loc, res_tuple.p, res_tuple.size(), tuple_type);

return;


return;
}
}

void string_compile_time_evaluation(const Location &loc, std::string attr_name, std::string &s_var,
void string_create_partition(const Location &loc, std::string attr_name, std::string &s_var,
ASR::expr_t *arg_seperator) {

if(attr_name == "partition") {
Expand Down Expand Up @@ -6108,12 +6113,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {

if (ASRUtils::expr_value(arg_seperator) != nullptr) {

string_create_partition(s_var, arg_seperator, loc, arg_seperator_type);
string_compile_time_evaluation(s_var, arg_seperator, loc, arg_seperator_type, attr_name);

} else {

string_compile_time_evaluation(loc, attr_name, s_var, arg_seperator);
string_create_partition(loc, attr_name, s_var, arg_seperator);

}

return;
Expand Down

0 comments on commit 745b39e

Please sign in to comment.