Skip to content

Commit

Permalink
Merge pull request lcompilers#2354 from Agent-Hellboy/add_join_in_str
Browse files Browse the repository at this point in the history
Add join method in str
  • Loading branch information
certik committed Oct 3, 2023
2 parents d78559b + a4183b3 commit 98e53f4
Show file tree
Hide file tree
Showing 66 changed files with 1,300 additions and 1,261 deletions.
16 changes: 16 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def test_str_repeat():
assert 3*a*3 == "XyzXyzXyzXyzXyzXyzXyzXyzXyz"
assert a*-1 == ""

def test_str_join():
a: str
a = ","
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a,b"

def test_str_join2():
a: str
a = "**"
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a**b"


def test_constant_str_subscript():
assert "abc"[2] == "c"
assert "abc"[:2] == "ab"
Expand All @@ -55,6 +70,7 @@ def check():
test_str_index()
test_str_slice()
test_str_repeat()
test_str_join()
test_constant_str_subscript()

check()
16 changes: 15 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <lpython/parser/parser.h>
#include <libasr/serialization.h>


namespace LCompilers::LPython {

namespace CastingUtil {
Expand Down Expand Up @@ -1251,7 +1252,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs,
args, rt_subs, func, loc);
}

if (ASRUtils::get_FunctionType(func)->m_is_restriction) {
rt_vec.push_back(s);
} else if (ASRUtils::is_generic_function(s)) {
Expand Down Expand Up @@ -6664,6 +6664,20 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
arg.loc = loc;
arg.m_value = s_var;
fn_args.push_back(al, arg);
} else if (attr_name == "join") {
if (args.size() != 1) {
throw SemanticError("str.join() takes one argument",
loc);
}
fn_call_name = "_lpython_str_join";
ASR::call_arg_t str_var;
str_var.loc = loc;
str_var.m_value = s_var;
ASR::call_arg_t passed_int;
passed_int.loc = loc;
passed_int.m_value = args[0].m_value;
fn_args.push_back(al, str_var);
fn_args.push_back(al, passed_int);
} else if (attr_name == "find") {
if (args.size() != 1) {
throw SemanticError("str.find() takes one argument",
Expand Down
1 change: 1 addition & 0 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct PythonIntrinsicProcedures {
{"_lpython_str_count", {m_builtin, &not_implemented}},
{"_lpython_str_lower", {m_builtin, &not_implemented}},
{"_lpython_str_upper", {m_builtin, &not_implemented}},
{"_lpython_str_join", {m_builtin, &not_implemented}},
{"_lpython_str_find", {m_builtin, &not_implemented}},
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,14 @@ def _lpython_str_upper(x: str) -> str:
res += i
return res

@overload
def _lpython_str_join(s:str, lis:list[str]) -> str:
if len(lis) == 0: return ""
res:str = lis[0]
i:i32
for i in range(1, len(lis)):
res += s + lis[i]
return res

@overload
def _lpython_str_find(s: str, sub: str) -> i32:
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "960bc68922ceaabf957cbdc2cfc76c1f4a4758856072d6be3ae9ed18",
"stdout_hash": "f1338c6f6e5f0d9e55addc9bd28498dde023b5870395fd4e097788d2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 98e53f4

Please sign in to comment.