Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[asr->python] add string visitors #2588

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/libasr/codegen/asr_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,58 @@ class ASRToLpythonVisitor : public ASR::BaseVisitor<ASRToLpythonVisitor>
s += "len(" + s + ")";
}

void visit_StringItem(const ASR::StringItem_t &x) {
std::string r = "";
visit_expr(*x.m_arg);
r += s;
r += "[";
visit_expr(*x.m_idx);
r += s;
r += "]";
s = r;
}

void visit_StringSection(const ASR::StringSection_t &x) {
std::string r = "";
visit_expr(*x.m_arg);
r += s;
if (x.m_start) {
r += "[";
visit_expr(*x.m_start);
r += s;
}
if (x.m_end) {
r += ":";
visit_expr(*x.m_end);
r += s;
}
if (x.m_step) {
r += ":";
visit_expr(*x.m_step);
r += s;
r += "]";
} else {
r += "]";
}
Comment on lines +591 to +608
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (x.m_start) {
r += "[";
visit_expr(*x.m_start);
r += s;
}
if (x.m_end) {
r += ":";
visit_expr(*x.m_end);
r += s;
}
if (x.m_step) {
r += ":";
visit_expr(*x.m_step);
r += s;
r += "]";
} else {
r += "]";
}
r += "[";
if (x.m_start) {
visit_expr(*x.m_start);
r += s;
}
r += ":";
if (x.m_end) {
visit_expr(*x.m_end);
r += s;
}
if (x.m_step) {
r += ":";
visit_expr(*x.m_step);
r += s;
}
r += "]";

I suggested this changes because of the following cases:

s: str = "12345"
print(s[:])
print(s[3:])
print(s[3:4:])

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave comment for proper understanding of code.

s = r;
}

void visit_StringFormat(const ASR::StringFormat_t &x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does LPython represents StringFormat in the ASR? (If we don't support, let's implement this later? )
I think we don't properly support f-strings yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I added these because there is an entry for StringFormat. Here:

| StringFormat(expr fmt, expr* args, string_format_kind kind, ttype type, expr? value)

Do you think we should keep it? Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have an entry in ASR.asdl, but LPython doesn't utilise it yet.
So, I think let's remove it for now. And handle it correctly once LPython supports f-strings.

std::string r = "";
visit_expr(*x.m_fmt);
r += s;
r += ".format(";
for (size_t i = 0; i < x.n_args; i++) {
visit_expr(*x.m_args[i]);
r += s;
if (i < x.n_args - 1) {
r += ", ";
}
}
r += ")";
s = r;
}

void visit_IfExp(const ASR::IfExp_t &x) {
std::string r;
visit_expr(*x.m_body);
Expand Down
Loading