@@ -46,8 +46,12 @@ LFortran::Result<LFortran::Python::AST::ast_t*> parse_python_file(Allocator &al,
46
46
return ast;
47
47
}
48
48
49
+ // Does a CPython style lookup for a module:
50
+ // * First the current directory (this is incorrect, we need to do it relative to the current file)
51
+ // * Then the LPython runtime directory
49
52
LFortran::Result<std::string> get_full_path (const std::string &filename,
50
- const std::string &runtime_library_dir) {
53
+ const std::string &runtime_library_dir, bool <ypes) {
54
+ ltypes = false ;
51
55
std::string input;
52
56
bool status = read_file (filename, input);
53
57
if (status) {
@@ -58,7 +62,19 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
58
62
if (status) {
59
63
return filename_intrinsic;
60
64
} else {
61
- return LFortran::Error ();
65
+ // If this is `ltypes`, do a special lookup
66
+ if (filename == " ltypes.py" ) {
67
+ std::string filename_intrinsic = runtime_library_dir + " /ltypes/" + filename;
68
+ bool status = read_file (filename_intrinsic, input);
69
+ if (status) {
70
+ ltypes = true ;
71
+ return filename_intrinsic;
72
+ } else {
73
+ return LFortran::Error ();
74
+ }
75
+ } else {
76
+ return LFortran::Error ();
77
+ }
62
78
}
63
79
}
64
80
}
@@ -67,7 +83,9 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
67
83
const std::string &module_name,
68
84
const Location &loc, bool /* intrinsic*/ ,
69
85
const std::string &rl_path,
86
+ bool <ypes,
70
87
const std::function<void (const std::string &, const Location &)> err) {
88
+ ltypes = false ;
71
89
LFORTRAN_ASSERT (symtab);
72
90
if (symtab->scope .find (module_name) != symtab->scope .end ()) {
73
91
ASR::symbol_t *m = symtab->scope [module_name];
@@ -81,10 +99,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
81
99
82
100
// Parse the module `module_name`.py to AST
83
101
std::string infile0 = module_name + " .py" ;
84
- Result<std::string> rinfile = get_full_path (infile0, rl_path);
102
+ Result<std::string> rinfile = get_full_path (infile0, rl_path, ltypes );
85
103
if (!rinfile.ok ) {
86
104
err (" Could not find the module '" + infile0 + " '" , loc);
87
105
}
106
+ if (ltypes) return nullptr ;
88
107
std::string infile = rinfile.result ;
89
108
Result<AST::ast_t *> r = parse_python_file (al, rl_path, infile);
90
109
if (!r.ok ) {
@@ -95,6 +114,7 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
95
114
// Convert the module from AST to ASR
96
115
LFortran::LocationManager lm;
97
116
lm.in_filename = infile;
117
+ // TODO: diagnostic should be an argument to this function
98
118
diag::Diagnostics diagnostics;
99
119
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr (al, *ast, diagnostics, false );
100
120
std::string input;
@@ -441,10 +461,22 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
441
461
if (!main_module) {
442
462
st = st->parent ;
443
463
}
464
+ bool ltypes;
444
465
t = (ASR::symbol_t *)(load_module (al, st,
445
- msym, x.base .base .loc , false , rl_path,
466
+ msym, x.base .base .loc , false , rl_path, ltypes,
446
467
[&](const std::string &msg, const Location &loc) { throw SemanticError (msg, loc); }
447
468
));
469
+ if (ltypes) {
470
+ // TODO: For now we skip ltypes import completely. Later on we should note what symbols
471
+ // got imported from it, and give an error message if an annotation is used without
472
+ // importing it.
473
+ tmp = nullptr ;
474
+ return ;
475
+ }
476
+ if (!t) {
477
+ throw SemanticError (" The module '" + msym + " ' cannot be loaded" ,
478
+ x.base .base .loc );
479
+ }
448
480
}
449
481
450
482
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
0 commit comments