Skip to content

Commit fd38017

Browse files
committed
Provide __main__ as default module_name
1 parent a14d2c8 commit fd38017

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/bin/lpython.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int emit_asr(const std::string &infile,
231231
diagnostics.diagnostics.clear();
232232
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
233233
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics,
234-
compiler_options, true, "", infile);
234+
compiler_options, true, "__main__", infile);
235235
std::cerr << diagnostics.render(lm, compiler_options);
236236
if (!r.ok) {
237237
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -291,7 +291,7 @@ int emit_cpp(const std::string &infile,
291291

292292
diagnostics.diagnostics.clear();
293293
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
294-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
294+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
295295
std::cerr << diagnostics.render(lm, compiler_options);
296296
if (!r1.ok) {
297297
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -336,7 +336,7 @@ int emit_c(const std::string &infile,
336336

337337
diagnostics.diagnostics.clear();
338338
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
339-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
339+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
340340
std::cerr << diagnostics.render(lm, compiler_options);
341341
if (!r1.ok) {
342342
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -393,7 +393,7 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
393393

394394
diagnostics.diagnostics.clear();
395395
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
396-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
396+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
397397
std::cerr << diagnostics.render(lm, compiler_options);
398398
if (!r1.ok) {
399399
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -453,7 +453,7 @@ int emit_wat(const std::string &infile,
453453

454454
diagnostics.diagnostics.clear();
455455
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
456-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
456+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
457457
std::cerr << diagnostics.render(lm, compiler_options);
458458
if (!r1.ok) {
459459
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -502,7 +502,7 @@ int get_symbols (const std::string &infile,
502502
if (r1.ok) {
503503
LCompilers::LPython::AST::ast_t* ast = r1.result;
504504
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
505-
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
505+
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
506506
if (!x.ok) {
507507
std::cout << "{}\n";
508508
return 0;
@@ -603,7 +603,7 @@ int get_errors (const std::string &infile,
603603
if (r1.ok) {
604604
LCompilers::LPython::AST::ast_t* ast = r1.result;
605605
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
606-
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
606+
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
607607
}
608608
std::vector<LCompilers::error_highlight> diag_lists;
609609
LCompilers::error_highlight h;
@@ -723,7 +723,7 @@ int emit_llvm(const std::string &infile,
723723
LCompilers::LPython::AST::ast_t* ast = r.result;
724724
diagnostics.diagnostics.clear();
725725
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
726-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
726+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
727727
std::cerr << diagnostics.render(lm, compiler_options);
728728
if (!r1.ok) {
729729
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -799,7 +799,7 @@ int compile_python_to_object_file(
799799
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
800800
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
801801
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
802-
!(arg_c && compiler_options.disable_main), "", infile);
802+
!(arg_c && compiler_options.disable_main), "__main__", infile);
803803

804804
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
805805
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
@@ -916,7 +916,7 @@ int compile_to_binary_wasm(
916916
diagnostics.diagnostics.clear();
917917
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
918918
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
919-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
919+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
920920
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
921921
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
922922
std::cerr << diagnostics.render(lm, compiler_options);
@@ -989,7 +989,7 @@ int compile_to_binary_x86(
989989
diagnostics.diagnostics.clear();
990990
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
991991
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
992-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
992+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
993993
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
994994
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
995995
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1063,7 +1063,7 @@ int compile_to_binary_wasm_to_x86(
10631063
diagnostics.diagnostics.clear();
10641064
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
10651065
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1066-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
1066+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
10671067
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
10681068
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
10691069
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1359,7 +1359,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
13591359
if (ast.ok) {
13601360
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13611361
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1362-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1362+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
13631363
out = diagnostics.render(lm, compiler_options);
13641364
if (asr.ok) {
13651365
out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
@@ -1377,7 +1377,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
13771377
if (ast.ok) {
13781378
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13791379
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1380-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1380+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
13811381
out = diagnostics.render(lm, compiler_options);
13821382
if (asr.ok) {
13831383
LCompilers::Result<LCompilers::Vec<uint8_t>>
@@ -1404,7 +1404,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
14041404
if (ast.ok) {
14051405
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
14061406
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1407-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1407+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
14081408
out = diagnostics.render(lm, compiler_options);
14091409
if (asr.ok) {
14101410
auto res = LCompilers::asr_to_cpp(al, *asr.result, diagnostics,
@@ -1442,7 +1442,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
14421442
if (ast.ok) {
14431443
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
14441444
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1445-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
1445+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "__main__", "input");
14461446
out = diagnostics.render(lm, compiler_options);
14471447
if (asr.ok) {
14481448
LCompilers::Result<LCompilers::Vec<uint8_t>>

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,12 +3201,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
32013201

32023202
void add_name(const Location &loc) {
32033203
std::string var_name = "__name__";
3204-
std::string var_value;
3205-
if (main_module) {
3206-
var_value = "__main__";
3207-
} else {
3208-
var_value = module_name;
3209-
}
3204+
std::string var_value = module_name;
32103205
size_t s_size = var_value.size();
32113206
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
32123207
1, s_size, nullptr));

0 commit comments

Comments
 (0)