@@ -3395,6 +3395,57 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3395
3395
}
3396
3396
}
3397
3397
3398
+ void set_module_symbol (std::string &mod_sym, std::vector<std::string> &paths) {
3399
+ if (mod_sym != " ltypes" ) {
3400
+ // Check for the import path and mod_sym not in runtime library
3401
+ if (import_path != " " &&
3402
+ !path_exists (paths[0 ] + ' /' + mod_sym + " .py" )) {
3403
+ paths = {import_path};
3404
+ if (parent_dir != " " ) paths[0 ] += ' /' + parent_dir;
3405
+ if (path_exists (paths[0 ])) {
3406
+ // Check for the nested modules with "."
3407
+ // Example: from x.y import z
3408
+ if (mod_sym.find (" ." ) != std::string::npos) {
3409
+ mod_sym.replace (mod_sym.find (" ." ), 1 , " /" );
3410
+ if (is_directory (paths[0 ] + " /" + mod_sym)) {
3411
+ // Directory i.e., x/y/__init__.py
3412
+ paths[0 ] += ' /' + mod_sym;
3413
+ mod_sym = " __init__" ;
3414
+ } else {
3415
+ // File i.e., x/y.py
3416
+ paths[0 ] += ' /' + mod_sym.substr (0 ,
3417
+ mod_sym.find_last_of (' /' ));
3418
+ mod_sym = mod_sym.substr (mod_sym.find_last_of (' /' ) + 1 ,
3419
+ mod_sym.size () - 1 );
3420
+ }
3421
+ } else if (is_directory (paths[0 ] + " /" + mod_sym)) {
3422
+ // Directory i.e., x/__init__.py
3423
+ paths[0 ] += ' /' + mod_sym;
3424
+ mod_sym = " __init__" ;
3425
+ }
3426
+ }
3427
+ } else if (mod_sym.find (" ." ) != std::string::npos) {
3428
+ // Check for the nested modules with "."
3429
+ // Example: from x.y import z
3430
+ mod_sym.replace (mod_sym.find (" ." ), 1 , " /" );
3431
+ if (is_directory (paths[1 ] + " /" + mod_sym)) {
3432
+ if (parent_dir != " " ) paths[1 ] += " /" ;
3433
+ paths[1 ] += mod_sym;
3434
+ mod_sym = " __init__" ;
3435
+ }
3436
+ } else if (parent_dir != " "
3437
+ && is_directory (paths[1 ] + ' /' + mod_sym)) {
3438
+ // Directory i.e., parent_dir/x/__init__.py
3439
+ paths[1 ] += ' /' + mod_sym;
3440
+ mod_sym = " __init__" ;
3441
+ } else if (is_directory (mod_sym)) {
3442
+ // Directory i.e., x/__init__.py
3443
+ paths.push_back (mod_sym);
3444
+ mod_sym = " __init__" ;
3445
+ }
3446
+ }
3447
+ }
3448
+
3398
3449
void visit_ImportFrom (const AST::ImportFrom_t &x) {
3399
3450
if (!x.m_module ) {
3400
3451
throw SemanticError (" Not implemented: The import statement must currently specify the module name" , x.base .base .loc );
@@ -3415,32 +3466,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3415
3466
st = st->parent ;
3416
3467
}
3417
3468
bool ltypes, enum_py;
3418
- if (msym != " ltypes" ) {
3419
- if (import_path != " " &&
3420
- !path_exits (paths[0 ] + ' /' + msym + " .py" )) {
3421
- paths = {import_path};
3422
- if (parent_dir != " " ) paths[0 ] += ' /' + parent_dir;
3423
- if (is_directory (paths[0 ])) {
3424
- if (msym.find (" ." ) != std::string::npos) {
3425
- msym.replace (msym.find (" ." ), 1 , " /" );
3426
- }
3427
- paths[0 ] += ' /' + msym;
3428
- msym = " __init__" ;
3429
- }
3430
- } else if (msym.find (" ." ) != std::string::npos) {
3431
- msym.replace (msym.find (" ." ), 1 , " /" );
3432
- if (parent_dir != " " ) paths[1 ] += " /" ;
3433
- paths[1 ] += msym;
3434
- msym = " __init__" ;
3435
- } else if (is_directory (msym)) {
3436
- paths = {rl_path, msym};
3437
- msym = " __init__" ;
3438
- } else if (paths[1 ] != " "
3439
- && is_directory (paths[1 ] + ' /' + msym)) {
3440
- paths[1 ] += ' /' + msym;
3441
- msym = " __init__" ;
3442
- }
3443
- }
3469
+ set_module_symbol (msym, paths);
3444
3470
t = (ASR::symbol_t *)(load_module (al, st,
3445
3471
msym, x.base .base .loc , false , paths, ltypes, enum_py,
3446
3472
[&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); },
@@ -3486,32 +3512,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3486
3512
}
3487
3513
for (auto &mod_sym : mods) {
3488
3514
bool ltypes, enum_py;
3489
- if (mod_sym != " ltypes" ) {
3490
- if (import_path != " " &&
3491
- !path_exits (paths[0 ] + ' /' + mod_sym + " .py" )) {
3492
- paths = {import_path};
3493
- if (parent_dir != " " ) paths[0 ] += ' /' + parent_dir;
3494
- if (is_directory (paths[0 ])) {
3495
- if (mod_sym.find (" ." ) != std::string::npos) {
3496
- mod_sym.replace (mod_sym.find (" ." ), 1 , " /" );
3497
- }
3498
- paths[0 ] += ' /' + mod_sym;
3499
- mod_sym = " __init__" ;
3500
- }
3501
- } else if (mod_sym.find (" ." ) != std::string::npos) {
3502
- mod_sym.replace (mod_sym.find (" ." ), 1 , " /" );
3503
- if (parent_dir != " " ) paths[1 ] += " /" ;
3504
- paths[1 ] += mod_sym;
3505
- mod_sym = " __init__" ;
3506
- } else if (is_directory (mod_sym)) {
3507
- paths = {rl_path, mod_sym};
3508
- mod_sym = " __init__" ;
3509
- } else if (paths[1 ] != " "
3510
- && is_directory (paths[1 ] + ' /' + mod_sym)) {
3511
- paths[1 ] += ' /' + mod_sym;
3512
- mod_sym = " __init__" ;
3513
- }
3514
- }
3515
+ set_module_symbol (mod_sym, paths);
3515
3516
t = (ASR::symbol_t *)(load_module (al, st,
3516
3517
mod_sym, x.base .base .loc , false , paths, ltypes, enum_py,
3517
3518
[&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); },
0 commit comments