-
Notifications
You must be signed in to change notification settings - Fork 171
Add builtin functions #2651
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
Add builtin functions #2651
Changes from all commits
c4be5c9
b8225df
8e45dd0
6c481ce
1550703
cad68e0
d87f0fa
3aa3dbe
c23d29d
8e9fdf5
f947a3d
7964ab4
1a9a70c
0c2bcab
42628ca
359bf27
b9de14e
decfe10
b2656c3
eef9923
3772abc
a95406a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -6880,6 +6880,59 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> { | |||
value.m_value = args[2].m_value; | ||||
fn_args.push_back(al, value); | ||||
} | ||||
} else if(attr_name == "center") { | ||||
if (args.size() != 1 && args.size() != 2) { | ||||
throw SemanticError("str.center() takes one or two argument", | ||||
loc); | ||||
} | ||||
ASR::expr_t *arg_value = args[0].m_value; | ||||
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value); | ||||
if (!ASRUtils::is_integer(*arg_value_type)) { | ||||
throw SemanticError("str.center() argument 1 must be integer", loc); | ||||
} | ||||
|
||||
fn_call_name = "_lpython_str_center"; | ||||
ASR::call_arg_t str; | ||||
str.loc = loc; | ||||
str.m_value = s_var; | ||||
|
||||
ASR::call_arg_t value; | ||||
value.loc = loc; | ||||
value.m_value = args[0].m_value; | ||||
fn_args.push_back(al, str); | ||||
fn_args.push_back(al, value); | ||||
|
||||
if(args.size() == 2){ | ||||
ASR::expr_t *arg_value = args[1].m_value; | ||||
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value); | ||||
if (!ASRUtils::is_character(*arg_value_type)) { | ||||
throw SemanticError("str.center() argument 2 must be str", loc); | ||||
} | ||||
value.m_value = args[1].m_value; | ||||
fn_args.push_back(al, value); | ||||
} | ||||
} else if(attr_name == "expandtabs") { | ||||
if(args.size() > 1) { | ||||
throw SemanticError("str.expandtabs() takes at most one argument.", loc); | ||||
} | ||||
fn_call_name = "_lpython_str_expandtabs"; | ||||
ASR::call_arg_t str; | ||||
str.loc = loc; | ||||
str.m_value = s_var; | ||||
fn_args.push_back(al, str); | ||||
|
||||
if(args.size() == 1){ | ||||
ASR::expr_t *arg_value = args[0].m_value; | ||||
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value); | ||||
if (!ASRUtils::is_integer(*arg_value_type)) { | ||||
throw SemanticError("str.expandtabs() argument must be integer", loc); | ||||
} | ||||
|
||||
ASR::call_arg_t value; | ||||
value.loc = loc; | ||||
value.m_value = args[0].m_value; | ||||
fn_args.push_back(al, value); | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This handles runtime implementation. We also need to support compile-time implementation inside
|
||||
} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') { | ||||
/* | ||||
String Validation Methods i.e all "is" based functions are handled here | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1085,6 +1085,65 @@ def _lpython_str_isspace(s: str) -> bool: | |
return False | ||
return True | ||
|
||
@overload | ||
def _lpython_str_center(s: str, width: i32, fillchar: str) -> str: | ||
""" | ||
Return centered in a string of length width. | ||
Padding is done using the specified fillchar (default is an ASCII space). | ||
The original string is returned if width is less than or equal to len(s). | ||
""" | ||
if(len(fillchar) != 1): | ||
raise TypeError("The fill character must be exactly one character long") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
str_len: i32 = len(s) | ||
if width <= str_len: | ||
return s | ||
width -= str_len | ||
result: str = "" | ||
left_padding: i32 = i32(width/2) + _mod(width,2) | ||
i: i32 | ||
for i in range(left_padding): | ||
result += fillchar | ||
right_padding: i32 = width - left_padding | ||
result += s | ||
for i in range(right_padding): | ||
result += fillchar | ||
Comment on lines
+1103
to
+1109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can utilize the new improvement in string repeat added a few days ago and simplify this to just: right_padding: i32 = width - left_padding
result += fillchar * left_padding
result += s
result += fillchar * right_padding #2652 needs to be addressed to achieve it. For now we may keep the current |
||
return result | ||
|
||
@overload | ||
def _lpython_str_center(s: str, width: i32) -> str: | ||
return _lpython_str_center(s, width, ' ') | ||
|
||
@overload | ||
def _lpython_str_expandtabs(s: str, tabsize: i32) -> str: | ||
""" | ||
Return a copy of the string where all tab characters are replaced | ||
by one or more spaces, depending on the current column and the given tab size. | ||
""" | ||
if len(s) == 0: | ||
return s | ||
col: i32 = 0 | ||
result: str = "" | ||
c: str | ||
for c in s: | ||
if c == '\t': | ||
if tabsize > 0: | ||
i: i32 | ||
iterations: i32 = tabsize - _mod(col,tabsize) | ||
for i in range(iterations): | ||
result += ' ' | ||
Comment on lines
+1132
to
+1133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now this is fine, but we can later refactor this to simply use |
||
col = 0 | ||
elif c == '\n' or c == '\r': | ||
result += c | ||
col = 0 | ||
else: | ||
result += c | ||
col += 1 | ||
return result | ||
|
||
@overload | ||
def _lpython_str_expandtabs(s: str) -> str: | ||
return _lpython_str_expandtabs(s, 8) | ||
|
||
def list(s: str) -> list[str]: | ||
l: list[str] = [] | ||
i: i32 | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation supports runtime strings. Please add tests for them. By runtime strings, I mean a string stored in a variable. You can have a look at how
is_upper()
is tested.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@farah-salama please update the test references also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please rebuild and update the test references again?