Skip to content

Commit 343f71d

Browse files
WIP
1 parent 1a9ceb3 commit 343f71d

File tree

8 files changed

+168
-32
lines changed

8 files changed

+168
-32
lines changed

integration_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ RUN(NAME test_dict_05 LABELS cpython llvm)
192192
RUN(NAME test_for_loop LABELS cpython llvm c)
193193
RUN(NAME modules_01 LABELS cpython llvm)
194194
RUN(NAME modules_02 LABELS cpython llvm)
195+
RUN(NAME test_import_01 LABELS cpython llvm)
195196
RUN(NAME test_math LABELS cpython llvm)
196197
RUN(NAME test_numpy_01 LABELS cpython llvm c)
197198
RUN(NAME test_numpy_02 LABELS cpython llvm c)

integration_tests/test_import_01.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test_import
2-
from test_import import print_a
2+
# from test_import import print_a
33

4-
print(print_a())
4+
# print(print_a())
55
print(test_import.print_b())
66
print(test_import.print_c())

src/bin/lpython.cpp

+28-14
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

+1
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()} {};

0 commit comments

Comments
 (0)