Skip to content

Commit 3d5ec07

Browse files
committed
wip
1 parent ab14558 commit 3d5ec07

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed

integration_tests/str_to_list_cast.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,20 @@ def f():
1010
assert len(x) == 0
1111
x = list("L")
1212
assert len(x) == 1 and x[0] == 'L'
13+
x = list(s)
14+
assert len(x) == len(s)
15+
for i in range(len(x)):
16+
assert x[i] == s[i]
17+
s = "agowietg348203wk.smg.afejwp398273wd.a,23to0MEG.F,"
18+
x = list(s)
19+
assert len(x) == len(s)
20+
for i in range(len(x)):
21+
assert x[i] == s[i]
22+
s += str(i)
23+
x = list(s)
24+
assert len(x) == len(s)
25+
for i in range(len(x)):
26+
assert x[i] == s[i]
27+
1328

1429
f()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
821821
AST::keyword_t* kwargs=nullptr, size_t n_kwargs=0) {
822822
if (intrinsic_node_handler.is_present(call_name)) {
823823
return intrinsic_node_handler.get_intrinsic_node(call_name, al, loc,
824-
args, ann_assign_target_type);
824+
args, ann_assign_target_type, current_scope);
825825
}
826826
ASR::symbol_t *s_generic = nullptr, *stemp = s;
827827
// handling ExternalSymbol
@@ -4698,7 +4698,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
46984698
return ;
46994699
} else if (intrinsic_node_handler.is_present(call_name)) {
47004700
tmp = intrinsic_node_handler.get_intrinsic_node(call_name, al,
4701-
x.base.base.loc, args, ann_assign_target_type);
4701+
x.base.base.loc, args, ann_assign_target_type,
4702+
current_scope);
47024703
return;
47034704
} else {
47044705
// The function was not found and it is not intrinsic

src/lpython/semantics/python_intrinsic_eval.h

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace LFortran {
1212
struct IntrinsicNodeHandler {
1313

1414
typedef ASR::asr_t* (*intrinsic_eval_callback)(Allocator &, Vec<ASR::call_arg_t>,
15-
const Location &, ASR::ttype_t *);
15+
const Location &, ASR::ttype_t *, SymbolTable *);
1616

1717
std::map<std::string, intrinsic_eval_callback> intrinsic_map;
1818

@@ -36,19 +36,20 @@ struct IntrinsicNodeHandler {
3636

3737
ASR::asr_t* get_intrinsic_node(std::string call_name,
3838
Allocator &al, const Location &loc, Vec<ASR::call_arg_t> args,
39-
ASR::ttype_t *ann_assign_target_type) {
39+
ASR::ttype_t *ann_assign_target_type, SymbolTable *scope) {
4040
auto search = intrinsic_map.find(call_name);
4141
if (search != intrinsic_map.end()) {
4242
intrinsic_eval_callback cb = search->second;
43-
return cb(al, args, loc, ann_assign_target_type);
43+
return cb(al, args, loc, ann_assign_target_type, scope);
4444
} else {
4545
throw SemanticError(call_name + " is not implemented yet",
4646
loc);
4747
}
4848
}
4949

5050
static ASR::asr_t* handle_intrinsic_int(Allocator &al, Vec<ASR::call_arg_t> args,
51-
const Location &loc, ASR::ttype_t * /*ann_assign_target_type*/) {
51+
const Location &loc, ASR::ttype_t * /*ann_assign_target_type*/,
52+
SymbolTable * /*scope*/) {
5253
ASR::expr_t *arg = nullptr, *value = nullptr;
5354
ASR::ttype_t *type = nullptr;
5455
if (args.size() > 1) {
@@ -129,7 +130,8 @@ struct IntrinsicNodeHandler {
129130

130131

131132
static ASR::asr_t* handle_intrinsic_float(Allocator &al, Vec<ASR::call_arg_t> args,
132-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
133+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
134+
SymbolTable * /*scope*/) {
133135
ASR::expr_t *arg = nullptr, *value = nullptr;
134136
ASR::ttype_t *type = nullptr;
135137
if (args.size() > 1) {
@@ -184,7 +186,8 @@ struct IntrinsicNodeHandler {
184186
}
185187

186188
static ASR::asr_t* handle_intrinsic_bool(Allocator &al, Vec<ASR::call_arg_t> args,
187-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
189+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
190+
SymbolTable * /*scope*/) {
188191
if (args.size() > 1) {
189192
throw SemanticError("Either 0 or 1 argument is expected in 'bool()'",
190193
loc);
@@ -255,7 +258,8 @@ struct IntrinsicNodeHandler {
255258

256259

257260
static ASR::asr_t* handle_intrinsic_str(Allocator &al, Vec<ASR::call_arg_t> args,
258-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
261+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
262+
SymbolTable * /*scope*/) {
259263
if (args.size() > 1) {
260264
throw SemanticError("Either 0 or 1 argument is expected in 'str()'",
261265
loc);
@@ -316,7 +320,8 @@ struct IntrinsicNodeHandler {
316320
}
317321

318322
static ASR::asr_t* handle_intrinsic_len(Allocator &al, Vec<ASR::call_arg_t> args,
319-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
323+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
324+
SymbolTable * /*scope*/) {
320325
if (args.size() != 1) {
321326
throw SemanticError("len() takes exactly one argument (" +
322327
std::to_string(args.size()) + " given)", loc);
@@ -372,7 +377,8 @@ struct IntrinsicNodeHandler {
372377
}
373378

374379
static ASR::asr_t* handle_reshape(Allocator &al, Vec<ASR::call_arg_t> args,
375-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
380+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
381+
SymbolTable * /*scope*/) {
376382
if( args.size() != 2 ) {
377383
throw SemanticError("reshape accepts only 2 arguments, got " +
378384
std::to_string(args.size()) + " arguments instead.",
@@ -398,7 +404,8 @@ struct IntrinsicNodeHandler {
398404
}
399405

400406
static ASR::asr_t* handle_intrinsic_ord(Allocator &al, Vec<ASR::call_arg_t> args,
401-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
407+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
408+
SymbolTable * /*scope*/) {
402409
if (args.size() != 1) {
403410
throw SemanticError("ord() takes exactly one argument (" +
404411
std::to_string(args.size()) + " given)", loc);
@@ -425,7 +432,8 @@ struct IntrinsicNodeHandler {
425432
}
426433

427434
static ASR::asr_t* handle_intrinsic_chr(Allocator &al, Vec<ASR::call_arg_t> args,
428-
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/) {
435+
const Location &loc, ASR::ttype_t */*ann_assign_target_type*/,
436+
SymbolTable * /*scope*/) {
429437
if (args.size() != 1) {
430438
throw SemanticError("chr() takes exactly one argument (" +
431439
std::to_string(args.size()) + " given)", loc);
@@ -455,7 +463,8 @@ struct IntrinsicNodeHandler {
455463
}
456464

457465
static ASR::asr_t* handle_intrinsic_list(Allocator &al, Vec<ASR::call_arg_t> args,
458-
const Location &loc, ASR::ttype_t *ann_assign_target_type) {
466+
const Location &loc, ASR::ttype_t *ann_assign_target_type,
467+
SymbolTable *current_scope) {
459468
if (args.size() > 1) {
460469
throw SemanticError("list() takes 0 or 1 argument (" +
461470
std::to_string(args.size()) + " given)", loc);
@@ -498,9 +507,51 @@ struct IntrinsicNodeHandler {
498507
return ASR::make_ListConstant_t(al, loc, list.p,
499508
list.size(), list_type);
500509
}
501-
return ASR::make_Cast_t(
502-
al, loc, arg, ASR::cast_kindType::CharacterToList,
503-
list_type, nullptr);
510+
auto int_type = ASR::make_Integer_t(al, loc, 4, nullptr, 0);
511+
std::string explicit_iter_name = current_scope->get_unique_name("__explicit_iterator");
512+
auto *explicit_iter_variable = ASR::make_Variable_t(al, loc,
513+
current_scope, s2c(al, explicit_iter_name), ASR::intentType::Local,
514+
nullptr, nullptr, ASR::storage_typeType::Default,
515+
ASRUtils::TYPE(int_type), ASR::abiType::Source,
516+
ASR::accessType::Public, ASR::presenceType::Required, false
517+
);
518+
519+
std::string list_name = current_scope->get_unique_name("_str_cast_to_list");
520+
auto *list_variable = ASR::make_Variable_t(al, loc,
521+
current_scope, s2c(al, list_name), ASR::intentType::Local,
522+
nullptr, nullptr, ASR::storage_typeType::Default,
523+
list_type, ASR::abiType::Source,
524+
ASR::accessType::Public, ASR::presenceType::Required, false
525+
);
526+
527+
current_scope->add_symbol(explicit_iter_name,
528+
ASR::down_cast<ASR::symbol_t>(explicit_iter_variable));
529+
current_scope->add_symbol(list_name,
530+
ASR::down_cast<ASR::symbol_t>(list_variable));
531+
532+
ASR::do_loop_head_t head;
533+
auto explicit_iter_var = ASR::make_Var_t(al, loc, current_scope->get_symbol("__explicit_iterator"));
534+
auto list_var_tmp = ASR::make_Var_t(al, loc, current_scope->get_symbol("_str_cast_to_list"));
535+
head.m_v = ASRUtils::EXPR(explicit_iter_var);
536+
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
537+
al, loc, 1, ASRUtils::TYPE(int_type)));
538+
auto index_plus_one = ASR::make_IntegerBinOp_t(al, loc, ASRUtils::EXPR(explicit_iter_var),
539+
ASR::binopType::Add, constant_one, ASRUtils::TYPE(int_type), nullptr);
540+
ASR::expr_t *loop_src_var_element = ASRUtils::EXPR(ASR::make_StringItem_t(
541+
al, loc, arg,
542+
ASRUtils::EXPR(index_plus_one), ASRUtils::TYPE(int_type), nullptr));
543+
head.m_start = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, loc,
544+
0, ASRUtils::TYPE(int_type)));
545+
head.m_end = ASRUtils::EXPR(ASR::make_StringLen_t(al, loc,
546+
arg, ASRUtils::TYPE(int_type), nullptr));
547+
head.m_increment = constant_one;
548+
head.loc = head.m_v->base.loc;
549+
Vec<ASR::stmt_t*> body;
550+
body.reserve(al, 1);
551+
body.push_back(al, ASRUtils::STMT(make_ListAppend_t(al,
552+
loc, ASRUtils::EXPR(list_var_tmp), loop_src_var_element)));
553+
return ASR::make_DoLoop_t(al, loc, head,
554+
body.p, body.size());
504555
} else {
505556
throw SemanticError("'" + ASRUtils::type_to_str_python(type) +
506557
"' object conversion to List is not implemented ",

0 commit comments

Comments
 (0)