diff --git a/src/bin/lpython.cpp b/src/bin/lpython.cpp index a9db92cc38..38e113f1df 100644 --- a/src/bin/lpython.cpp +++ b/src/bin/lpython.cpp @@ -164,9 +164,7 @@ int emit_asr(const std::string &infile, diagnostics.diagnostics.clear(); LFortran::Result - r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); std::cerr << diagnostics.render(input, lm, compiler_options); if (!r.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -208,9 +206,7 @@ int emit_cpp(const std::string &infile, diagnostics.diagnostics.clear(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); std::cerr << diagnostics.render(input, lm, compiler_options); if (!r1.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -250,9 +246,7 @@ int emit_c(const std::string &infile, diagnostics.diagnostics.clear(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); std::cerr << diagnostics.render(input, lm, compiler_options); if (!r1.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -292,9 +286,7 @@ int emit_wat(const std::string &infile, diagnostics.diagnostics.clear(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); std::cerr << diagnostics.render(input, lm, compiler_options); if (!r1.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -337,9 +329,7 @@ int get_symbols (const std::string &infile, if (r1.ok) { LFortran::LPython::AST::ast_t* ast = r1.result; LFortran::Result - x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, - infile, compiler_options.import_path); + x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); if (!x.ok) { std::cout << "{}\n"; return 0; @@ -431,9 +421,7 @@ int get_errors (const std::string &infile, if (r1.ok) { LFortran::LPython::AST::ast_t* ast = r1.result; LFortran::Result - r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, - infile, compiler_options.import_path); + r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); } std::vector diag_lists; LFortran::LPython::error_highlight h; @@ -544,9 +532,7 @@ int emit_llvm(const std::string &infile, LFortran::LPython::AST::ast_t* ast = r.result; diagnostics.diagnostics.clear(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); std::cerr << diagnostics.render(input, lm, compiler_options); if (!r1.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -602,10 +588,9 @@ int compile_python_to_object_file( diagnostics.diagnostics.clear(); auto ast_to_asr_start = std::chrono::high_resolution_clock::now(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, - !(arg_c && compiler_options.disable_main), - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, + !(arg_c && compiler_options.disable_main), infile); + auto ast_to_asr_end = std::chrono::high_resolution_clock::now(); times.push_back(std::make_pair("AST to ASR", std::chrono::duration(ast_to_asr_end - ast_to_asr_start).count())); std::cerr << diagnostics.render(input, lm, compiler_options); @@ -687,9 +672,7 @@ int compile_to_binary_wasm( diagnostics.diagnostics.clear(); auto ast_to_asr_start = std::chrono::high_resolution_clock::now(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, - compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); auto ast_to_asr_end = std::chrono::high_resolution_clock::now(); times.push_back(std::make_pair("AST to ASR", std::chrono::duration(ast_to_asr_end - ast_to_asr_start).count())); std::cerr << diagnostics.render(input, lm, compiler_options); @@ -754,8 +737,7 @@ int compile_to_binary_x86( diagnostics.diagnostics.clear(); auto ast_to_asr_start = std::chrono::high_resolution_clock::now(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); auto ast_to_asr_end = std::chrono::high_resolution_clock::now(); times.push_back(std::make_pair("AST to ASR", std::chrono::duration(ast_to_asr_end - ast_to_asr_start).count())); std::cerr << diagnostics.render(input, lm, compiler_options); @@ -820,8 +802,7 @@ int compile_to_binary_wasm_to_x86( diagnostics.diagnostics.clear(); auto ast_to_asr_start = std::chrono::high_resolution_clock::now(); LFortran::Result - r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path); + r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile); auto ast_to_asr_end = std::chrono::high_resolution_clock::now(); times.push_back(std::make_pair("AST to ASR", std::chrono::duration(ast_to_asr_end - ast_to_asr_start).count())); std::cerr << diagnostics.render(input, lm, compiler_options); @@ -1086,9 +1067,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) { if (ast.ok) { auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result; LFortran::Result - asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, "input", - compiler_options.import_path); + asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input"); out = diagnostics.render(input, lm, compiler_options); if (asr.ok) { out += LFortran::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent, @@ -1106,9 +1085,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) { if (ast.ok) { auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result; LFortran::Result - asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, "input", - compiler_options.import_path); + asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input"); out = diagnostics.render(input, lm, compiler_options); if (asr.ok) { LFortran::Result> @@ -1135,9 +1112,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) { if (ast.ok) { auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result; LFortran::Result - asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, "input", - compiler_options.import_path); + asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input"); out = diagnostics.render(input, lm, compiler_options); if (asr.ok) { auto res = LFortran::asr_to_cpp(al, *asr.result, diagnostics, @@ -1175,9 +1150,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) { if (ast.ok) { auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result; LFortran::Result - asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true, - compiler_options.disable_main, compiler_options.symtab_only, "input", - compiler_options.import_path); + asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input"); out = diagnostics.render(input, lm, compiler_options); if (asr.ok) { LFortran::Result> diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1d66ad850c..11df25767d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -253,15 +253,17 @@ ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al, // Convert the module from AST to ASR LFortran::LocationManager lm; lm.in_filename = infile; + LFortran::CompilerOptions compiler_options; + compiler_options.disable_main = true; + compiler_options.symtab_only = false; Result r2 = python_ast_to_asr(al, *ast, - diagnostics, false, true, false, infile, ""); + diagnostics, compiler_options, false, infile); // TODO: Uncomment once a check is added for ensuring // that module.py file hasn't changed between // builds. // save_pyc_files(*r2.result, infile + "c"); std::string input; read_file(infile, input); - CompilerOptions compiler_options; std::cerr << diagnostics.render(input, lm, compiler_options); if (!r2.ok) { LFORTRAN_ASSERT(diagnostics.has_error()) @@ -5679,9 +5681,8 @@ std::string get_parent_dir(const std::string &path) { } Result python_ast_to_asr(Allocator &al, - AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module, - bool disable_main, bool symtab_only, std::string file_path, - std::string import_path) + AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options, + bool main_module, std::string file_path) { std::map ast_overload; std::string parent_dir = get_parent_dir(file_path); @@ -5689,7 +5690,7 @@ Result python_ast_to_asr(Allocator &al, ASR::asr_t *unit; auto res = symbol_table_visitor(al, *ast_m, diagnostics, main_module, - ast_overload, parent_dir, import_path); + ast_overload, parent_dir, compiler_options.import_path); if (res.ok) { unit = res.result; } else { @@ -5698,7 +5699,7 @@ Result python_ast_to_asr(Allocator &al, ASR::TranslationUnit_t *tu = ASR::down_cast2(unit); LFORTRAN_ASSERT(asr_verify(*tu)); - if (!symtab_only) { + if (!compiler_options.symtab_only) { auto res2 = body_visitor(al, *ast_m, diagnostics, unit, main_module, ast_overload); if (res2.ok) { @@ -5712,7 +5713,7 @@ Result python_ast_to_asr(Allocator &al, if (main_module) { // If it is a main module, turn it into a program // Note: we can modify this behavior for interactive mode later - if (disable_main) { + if (compiler_options.disable_main) { if (tu->n_items > 0) { diagnostics.add(diag::Diagnostic( "The script is invoked as the main module and it has code to execute,\n" diff --git a/src/lpython/semantics/python_ast_to_asr.h b/src/lpython/semantics/python_ast_to_asr.h index 729311c698..8666f04abd 100644 --- a/src/lpython/semantics/python_ast_to_asr.h +++ b/src/lpython/semantics/python_ast_to_asr.h @@ -9,9 +9,8 @@ namespace LFortran::LPython { std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false); std::string pickle_tree_python(AST::ast_t &ast, bool colors=true); Result python_ast_to_asr(Allocator &al, - LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, - bool main_module, bool disable_main, bool symtab_only, std::string file_path, - std::string import_path); + LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options, + bool main_module, std::string file_path); int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u, std::string infile);