@@ -12,7 +12,7 @@ namespace LFortran {
12
12
struct IntrinsicNodeHandler {
13
13
14
14
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 * );
16
16
17
17
std::map<std::string, intrinsic_eval_callback> intrinsic_map;
18
18
@@ -36,19 +36,20 @@ struct IntrinsicNodeHandler {
36
36
37
37
ASR::asr_t * get_intrinsic_node (std::string call_name,
38
38
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 ) {
40
40
auto search = intrinsic_map.find (call_name);
41
41
if (search != intrinsic_map.end ()) {
42
42
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 );
44
44
} else {
45
45
throw SemanticError (call_name + " is not implemented yet" ,
46
46
loc);
47
47
}
48
48
}
49
49
50
50
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*/ ) {
52
53
ASR::expr_t *arg = nullptr , *value = nullptr ;
53
54
ASR::ttype_t *type = nullptr ;
54
55
if (args.size () > 1 ) {
@@ -129,7 +130,8 @@ struct IntrinsicNodeHandler {
129
130
130
131
131
132
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*/ ) {
133
135
ASR::expr_t *arg = nullptr , *value = nullptr ;
134
136
ASR::ttype_t *type = nullptr ;
135
137
if (args.size () > 1 ) {
@@ -184,7 +186,8 @@ struct IntrinsicNodeHandler {
184
186
}
185
187
186
188
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*/ ) {
188
191
if (args.size () > 1 ) {
189
192
throw SemanticError (" Either 0 or 1 argument is expected in 'bool()'" ,
190
193
loc);
@@ -255,7 +258,8 @@ struct IntrinsicNodeHandler {
255
258
256
259
257
260
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*/ ) {
259
263
if (args.size () > 1 ) {
260
264
throw SemanticError (" Either 0 or 1 argument is expected in 'str()'" ,
261
265
loc);
@@ -316,7 +320,8 @@ struct IntrinsicNodeHandler {
316
320
}
317
321
318
322
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*/ ) {
320
325
if (args.size () != 1 ) {
321
326
throw SemanticError (" len() takes exactly one argument (" +
322
327
std::to_string (args.size ()) + " given)" , loc);
@@ -372,7 +377,8 @@ struct IntrinsicNodeHandler {
372
377
}
373
378
374
379
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*/ ) {
376
382
if ( args.size () != 2 ) {
377
383
throw SemanticError (" reshape accepts only 2 arguments, got " +
378
384
std::to_string (args.size ()) + " arguments instead." ,
@@ -398,7 +404,8 @@ struct IntrinsicNodeHandler {
398
404
}
399
405
400
406
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*/ ) {
402
409
if (args.size () != 1 ) {
403
410
throw SemanticError (" ord() takes exactly one argument (" +
404
411
std::to_string (args.size ()) + " given)" , loc);
@@ -425,7 +432,8 @@ struct IntrinsicNodeHandler {
425
432
}
426
433
427
434
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*/ ) {
429
437
if (args.size () != 1 ) {
430
438
throw SemanticError (" chr() takes exactly one argument (" +
431
439
std::to_string (args.size ()) + " given)" , loc);
@@ -455,7 +463,8 @@ struct IntrinsicNodeHandler {
455
463
}
456
464
457
465
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) {
459
468
if (args.size () > 1 ) {
460
469
throw SemanticError (" list() takes 0 or 1 argument (" +
461
470
std::to_string (args.size ()) + " given)" , loc);
@@ -498,9 +507,51 @@ struct IntrinsicNodeHandler {
498
507
return ASR::make_ListConstant_t (al, loc, list.p ,
499
508
list.size (), list_type);
500
509
}
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 ());
504
555
} else {
505
556
throw SemanticError (" '" + ASRUtils::type_to_str_python (type) +
506
557
" ' object conversion to List is not implemented " ,
0 commit comments