Skip to content

Commit 0ad7de6

Browse files
Add an option -I to get import source path
1 parent 622b084 commit 0ad7de6

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

src/bin/lpython.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ int emit_asr(const std::string &infile,
164164
diagnostics.diagnostics.clear();
165165
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
166166
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
167-
compiler_options.disable_main, compiler_options.symtab_only, infile);
167+
compiler_options.disable_main, compiler_options.symtab_only, infile,
168+
compiler_options.import_path);
168169
std::cerr << diagnostics.render(input, lm, compiler_options);
169170
if (!r.ok) {
170171
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -207,7 +208,8 @@ int emit_cpp(const std::string &infile,
207208
diagnostics.diagnostics.clear();
208209
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
209210
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
210-
compiler_options.disable_main, compiler_options.symtab_only, infile);
211+
compiler_options.disable_main, compiler_options.symtab_only, infile,
212+
compiler_options.import_path);
211213
std::cerr << diagnostics.render(input, lm, compiler_options);
212214
if (!r1.ok) {
213215
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -248,7 +250,8 @@ int emit_c(const std::string &infile,
248250
diagnostics.diagnostics.clear();
249251
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
250252
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
251-
compiler_options.disable_main, compiler_options.symtab_only, infile);
253+
compiler_options.disable_main, compiler_options.symtab_only, infile,
254+
compiler_options.import_path);
252255
std::cerr << diagnostics.render(input, lm, compiler_options);
253256
if (!r1.ok) {
254257
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -289,7 +292,8 @@ int emit_wat(const std::string &infile,
289292
diagnostics.diagnostics.clear();
290293
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
291294
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
292-
compiler_options.disable_main, compiler_options.symtab_only, infile);
295+
compiler_options.disable_main, compiler_options.symtab_only, infile,
296+
compiler_options.import_path);
293297
std::cerr << diagnostics.render(input, lm, compiler_options);
294298
if (!r1.ok) {
295299
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -333,7 +337,8 @@ int get_symbols (const std::string &infile,
333337
LFortran::LPython::AST::ast_t* ast = r1.result;
334338
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
335339
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
336-
compiler_options.disable_main, compiler_options.symtab_only, infile);
340+
compiler_options.disable_main, compiler_options.symtab_only,
341+
infile, compiler_options.import_path);
337342
if (!x.ok) {
338343
std::cout << "{}\n";
339344
return 0;
@@ -426,7 +431,8 @@ int get_errors (const std::string &infile,
426431
LFortran::LPython::AST::ast_t* ast = r1.result;
427432
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
428433
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
429-
compiler_options.disable_main, compiler_options.symtab_only, infile);
434+
compiler_options.disable_main, compiler_options.symtab_only,
435+
infile, compiler_options.import_path);
430436
}
431437
std::vector<LFortran::LPython::error_highlight> diag_lists;
432438
LFortran::LPython::error_highlight h;
@@ -538,7 +544,8 @@ int emit_llvm(const std::string &infile,
538544
diagnostics.diagnostics.clear();
539545
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
540546
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
541-
compiler_options.disable_main, compiler_options.symtab_only, infile);
547+
compiler_options.disable_main, compiler_options.symtab_only, infile,
548+
compiler_options.import_path);
542549
std::cerr << diagnostics.render(input, lm, compiler_options);
543550
if (!r1.ok) {
544551
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -596,7 +603,8 @@ int compile_python_to_object_file(
596603
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
597604
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics,
598605
!(arg_c && compiler_options.disable_main),
599-
compiler_options.disable_main, compiler_options.symtab_only, infile);
606+
compiler_options.disable_main, compiler_options.symtab_only, infile,
607+
compiler_options.import_path);
600608
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
601609
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
602610
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -679,7 +687,8 @@ int compile_to_binary_wasm(
679687
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
680688
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
681689
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
682-
compiler_options.disable_main, compiler_options.symtab_only, infile);
690+
compiler_options.disable_main, compiler_options.symtab_only, infile,
691+
compiler_options.import_path);
683692
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
684693
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
685694
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -927,7 +936,8 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
927936
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
928937
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
929938
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
930-
compiler_options.disable_main, compiler_options.symtab_only, "input");
939+
compiler_options.disable_main, compiler_options.symtab_only, "input",
940+
compiler_options.import_path);
931941
out = diagnostics.render(input, lm, compiler_options);
932942
if (asr.ok) {
933943
out += LFortran::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
@@ -946,7 +956,8 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
946956
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
947957
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
948958
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
949-
compiler_options.disable_main, compiler_options.symtab_only, "input");
959+
compiler_options.disable_main, compiler_options.symtab_only, "input",
960+
compiler_options.import_path);
950961
out = diagnostics.render(input, lm, compiler_options);
951962
if (asr.ok) {
952963
LFortran::Result<LFortran::Vec<uint8_t>>
@@ -974,7 +985,8 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
974985
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
975986
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
976987
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
977-
compiler_options.disable_main, compiler_options.symtab_only, "input");
988+
compiler_options.disable_main, compiler_options.symtab_only, "input",
989+
compiler_options.import_path);
978990
out = diagnostics.render(input, lm, compiler_options);
979991
if (asr.ok) {
980992
auto res = LFortran::asr_to_cpp(al, *asr.result, diagnostics,
@@ -1013,7 +1025,8 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
10131025
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
10141026
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
10151027
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
1016-
compiler_options.disable_main, compiler_options.symtab_only, "input");
1028+
compiler_options.disable_main, compiler_options.symtab_only, "input",
1029+
compiler_options.import_path);
10171030
out = diagnostics.render(input, lm, compiler_options);
10181031
if (asr.ok) {
10191032
LFortran::Result<LFortran::Vec<uint8_t>>
@@ -1114,7 +1127,8 @@ int main(int argc, char *argv[])
11141127
// app.add_flag("-E", arg_E, "Preprocess only; do not compile, assemble or link");
11151128
// app.add_option("-l", arg_l, "Link library option");
11161129
// app.add_option("-L", arg_L, "Library path option");
1117-
// app.add_option("-I", arg_I, "Include path")->allow_extra_args(false);
1130+
app.add_option("-I", compiler_options.import_path, "Specify the path"
1131+
"to look for the module")->allow_extra_args(false);
11181132
// app.add_option("-J", arg_J, "Where to save mod files");
11191133
// app.add_flag("-g", arg_g, "Compile with debugging information");
11201134
// app.add_option("-D", compiler_options.c_preprocessor_defines, "Define <macro>=<value> (or 1 if <value> omitted)")->allow_extra_args(false);

src/libasr/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct CompilerOptions {
3737
bool implicit_typing = false;
3838
bool implicit_interface = false;
3939
std::string target = "";
40+
std::string import_path = "";
4041
Platform platform;
4142

4243
CompilerOptions () : platform{get_platform()} {};

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al,
246246
LFortran::LocationManager lm;
247247
lm.in_filename = infile;
248248
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
249-
diagnostics, false, true, false, infile);
249+
diagnostics, false, true, false, infile, "");
250250
// TODO: Uncomment once a check is added for ensuring
251251
// that module.py file hasn't changed between
252252
// builds.
@@ -324,7 +324,8 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
324324
return nullptr;
325325
}
326326
if (!found) {
327-
err("Could not find the module '" + infile0 + "'", loc);
327+
err("Could not find the module '" + infile0 + "'. If an import path "
328+
"is available, please use the `-I` option to specify it", loc);
328329
}
329330
if (ltypes) return nullptr;
330331

@@ -467,6 +468,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
467468
IntrinsicNodeHandler intrinsic_node_handler;
468469
std::map<int, ASR::symbol_t*> &ast_overload;
469470
std::string parent_dir;
471+
std::string import_path;
470472
Vec<ASR::stmt_t*> *current_body;
471473
ASR::ttype_t* ann_assign_target_type;
472474

@@ -477,9 +479,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
477479

478480
CommonVisitor(Allocator &al, SymbolTable *symbol_table,
479481
diag::Diagnostics &diagnostics, bool main_module,
480-
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir)
482+
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir,
483+
std::string import_path)
481484
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
482-
ast_overload{ast_overload}, parent_dir{parent_dir},
485+
ast_overload{ast_overload}, parent_dir{parent_dir}, import_path{import_path},
483486
current_body{nullptr}, ann_assign_target_type{nullptr} {
484487
current_module_dependencies.reserve(al, 4);
485488
}
@@ -2865,8 +2868,10 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
28652868

28662869
SymbolTableVisitor(Allocator &al, SymbolTable *symbol_table,
28672870
diag::Diagnostics &diagnostics, bool main_module,
2868-
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir)
2869-
: CommonVisitor(al, symbol_table, diagnostics, main_module, ast_overload, parent_dir), is_derived_type{false} {}
2871+
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir,
2872+
std::string import_path)
2873+
: CommonVisitor(al, symbol_table, diagnostics, main_module, ast_overload,
2874+
parent_dir, import_path), is_derived_type{false} {}
28702875

28712876

28722877
ASR::symbol_t* resolve_symbol(const Location &loc, const std::string &sub_name) {
@@ -3298,9 +3303,11 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
32983303

32993304
Result<ASR::asr_t*> symbol_table_visitor(Allocator &al, const AST::Module_t &ast,
33003305
diag::Diagnostics &diagnostics, bool main_module,
3301-
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir)
3306+
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir,
3307+
std::string import_path)
33023308
{
3303-
SymbolTableVisitor v(al, nullptr, diagnostics, main_module, ast_overload, parent_dir);
3309+
SymbolTableVisitor v(al, nullptr, diagnostics, main_module, ast_overload,
3310+
parent_dir, import_path);
33043311
try {
33053312
v.visit_Module(ast);
33063313
} catch (const SemanticError &e) {
@@ -3326,8 +3333,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
33263333

33273334
BodyVisitor(Allocator &al, ASR::asr_t *unit, diag::Diagnostics &diagnostics,
33283335
bool main_module, std::map<int, ASR::symbol_t*> &ast_overload)
3329-
: CommonVisitor(al, nullptr, diagnostics, main_module, ast_overload, ""), asr{unit},
3330-
gotoids{0}
3336+
: CommonVisitor(al, nullptr, diagnostics, main_module, ast_overload, "", ""),
3337+
asr{unit}, gotoids{0}
33313338
{}
33323339

33333340
// Transforms statements to a list of ASR statements
@@ -5441,15 +5448,16 @@ std::string get_parent_dir(const std::string &path) {
54415448

54425449
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
54435450
AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module,
5444-
bool disable_main, bool symtab_only, std::string file_path)
5451+
bool disable_main, bool symtab_only, std::string file_path,
5452+
std::string import_path)
54455453
{
54465454
std::map<int, ASR::symbol_t*> ast_overload;
54475455
std::string parent_dir = get_parent_dir(file_path);
54485456
AST::Module_t *ast_m = AST::down_cast2<AST::Module_t>(&ast);
54495457

54505458
ASR::asr_t *unit;
54515459
auto res = symbol_table_visitor(al, *ast_m, diagnostics, main_module,
5452-
ast_overload, parent_dir);
5460+
ast_overload, parent_dir, import_path);
54535461
if (res.ok) {
54545462
unit = res.result;
54555463
} else {

src/lpython/semantics/python_ast_to_asr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace LFortran::LPython {
1010
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
1111
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
1212
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
13-
bool main_module, bool disable_main, bool symtab_only, std::string file_path);
13+
bool main_module, bool disable_main, bool symtab_only, std::string file_path,
14+
std::string import_path);
1415

1516
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
1617
std::string infile);

0 commit comments

Comments
 (0)