@@ -172,9 +172,9 @@ int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
172
172
// * First the current directory (this is incorrect, we need to do it relative to the current file)
173
173
// * Then the LPython runtime directory
174
174
LFortran::Result<std::string> get_full_path (const std::string &filename,
175
- const std::string &runtime_library_dir, bool <ypes) {
175
+ const std::string &runtime_library_dir, std::string& input,
176
+ bool <ypes) {
176
177
ltypes = false ;
177
- std::string input;
178
178
bool status = read_file (filename, input);
179
179
if (status) {
180
180
return filename;
@@ -194,8 +194,8 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
194
194
} else {
195
195
return LFortran::Error ();
196
196
}
197
- } else if (filename == " numpy.py" ) {
198
- filename_intrinsic = runtime_library_dir + " /lpython_intrinsic_numpy.py " ;
197
+ } else if (startswith ( filename, " numpy.py" ) ) {
198
+ filename_intrinsic = runtime_library_dir + " /lpython_intrinsic_ " + filename ;
199
199
status = read_file (filename_intrinsic, input);
200
200
if (status) {
201
201
return filename_intrinsic;
@@ -210,9 +210,10 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
210
210
}
211
211
212
212
bool set_module_path (std::string infile0, std::vector<std::string> &rl_path,
213
- std::string& infile, std::string& path_used, bool & ltypes) {
213
+ std::string& infile, std::string& path_used, std::string& input,
214
+ bool & ltypes) {
214
215
for (auto path: rl_path) {
215
- Result<std::string> rinfile = get_full_path (infile0, path, ltypes);
216
+ Result<std::string> rinfile = get_full_path (infile0, path, input, ltypes);
216
217
if (rinfile.ok ) {
217
218
infile = rinfile.result ;
218
219
path_used = path;
@@ -222,6 +223,37 @@ bool set_module_path(std::string infile0, std::vector<std::string> &rl_path,
222
223
return false ;
223
224
}
224
225
226
+ ASR::TranslationUnit_t* compile_module_till_asr (Allocator& al,
227
+ std::vector<std::string> &rl_path, std::string infile,
228
+ const Location &loc,
229
+ const std::function<void (const std::string &, const Location &)> err) {
230
+ // TODO: diagnostic should be an argument to this function
231
+ diag::Diagnostics diagnostics;
232
+ Result<AST::ast_t *> r = parse_python_file (al, rl_path[0 ], infile,
233
+ diagnostics, false );
234
+ if (!r.ok ) {
235
+ err (" The file '" + infile + " ' failed to parse" , loc);
236
+ }
237
+ LFortran::LPython::AST::ast_t * ast = r.result ;
238
+
239
+ // Convert the module from AST to ASR
240
+ LFortran::LocationManager lm;
241
+ lm.in_filename = infile;
242
+ Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr (al, *ast,
243
+ diagnostics, false , true , false , infile);
244
+ save_pyc_files (*r2.result , infile + " c" );
245
+ std::string input;
246
+ read_file (infile, input);
247
+ CompilerOptions compiler_options;
248
+ std::cerr << diagnostics.render (input, lm, compiler_options);
249
+ if (!r2.ok ) {
250
+ LFORTRAN_ASSERT (diagnostics.has_error ())
251
+ return nullptr ; // Error
252
+ }
253
+
254
+ return r2.result ;
255
+ }
256
+
225
257
ASR::Module_t* load_module (Allocator &al, SymbolTable *symtab,
226
258
const std::string &module_name,
227
259
const Location &loc, bool intrinsic,
@@ -247,43 +279,31 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
247
279
std::string infile0 = module_name + " .py" ;
248
280
std::string infile0c = infile0 + " c" ;
249
281
std::string path_used = " " , infile;
282
+ bool compile_module = true ;
283
+ ASR::TranslationUnit_t* mod1 = nullptr ;
284
+ std::string input;
250
285
bool found = set_module_path (infile0c, rl_path, infile,
251
- path_used, ltypes);
286
+ path_used, input, ltypes);
287
+ std::cout<<" infile0c: " <<infile0c<<" " <<found<<std::endl;
252
288
if ( !found ) {
289
+ input.clear ();
253
290
found = set_module_path (infile0, rl_path, infile,
254
- path_used, ltypes);
291
+ path_used, input, ltypes);
255
292
} else {
256
- load_pycfile ();
293
+ std::cout<<" loading_pyc_file: " <<std::endl;
294
+ mod1 = load_pycfile (al, input, false );
295
+ std::cout<<" loaded_pyc_file: " <<std::endl;
296
+ compile_module = false ;
257
297
}
258
298
259
299
if (!found) {
260
300
err (" Could not find the module '" + infile0 + " '" , loc);
261
301
}
262
302
if (ltypes) return nullptr ;
263
303
264
- // TODO: diagnostic should be an argument to this function
265
- diag::Diagnostics diagnostics;
266
- Result<AST::ast_t *> r = parse_python_file (al, rl_path[0 ], infile,
267
- diagnostics, false );
268
- if (!r.ok ) {
269
- err (" The file '" + infile + " ' failed to parse" , loc);
270
- }
271
- LFortran::LPython::AST::ast_t * ast = r.result ;
272
-
273
- // Convert the module from AST to ASR
274
- LFortran::LocationManager lm;
275
- lm.in_filename = infile;
276
- Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr (al, *ast,
277
- diagnostics, false , true , false , infile);
278
- std::string input;
279
- read_file (infile, input);
280
- CompilerOptions compiler_options;
281
- std::cerr << diagnostics.render (input, lm, compiler_options);
282
- if (!r2.ok ) {
283
- LFORTRAN_ASSERT (diagnostics.has_error ())
284
- return nullptr ; // Error
304
+ if ( compile_module ) {
305
+ mod1 = compile_module_till_asr (al, rl_path, infile, loc, err);
285
306
}
286
- ASR::TranslationUnit_t* mod1 = r2.result ;
287
307
288
308
// insert into `symtab`
289
309
std::vector<std::pair<std::string, ASR::Module_t*>> children_modules;
0 commit comments