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