@@ -31,7 +31,7 @@ PythonCompiler::PythonCompiler(CompilerOptions compiler_options)
31
31
al{1024 *1024 },
32
32
#ifdef HAVE_LFORTRAN_LLVM
33
33
e{std::make_unique<LLVMEvaluator>()},
34
- eval_count{0 },
34
+ eval_count{1 },
35
35
#endif
36
36
compiler_options{compiler_options},
37
37
symbol_table{nullptr }
@@ -70,7 +70,7 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
70
70
}
71
71
72
72
// AST -> ASR
73
- Result<ASR::TranslationUnit_t*> res2 = get_asr3 (*ast, diagnostics, lm);
73
+ Result<ASR::TranslationUnit_t*> res2 = get_asr3 (*ast, diagnostics, lm, true );
74
74
ASR::TranslationUnit_t* asr;
75
75
if (res2.ok ) {
76
76
asr = res2.result ;
@@ -84,6 +84,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
84
84
}
85
85
86
86
// ASR -> LLVM
87
+ std::string module_prefix = " __module___main___" ;
88
+ std::string module_name = " __main__" ;
89
+ std::string sym_name = module_name + " global_stmts_" + std::to_string (eval_count) + " __" ;
90
+ run_fn = module_prefix + sym_name;
91
+
87
92
Result<std::unique_ptr<LLVMModule>> res3 = get_llvm3 (*asr,
88
93
pass_manager, diagnostics, lm.files .back ().in_filename );
89
94
std::unique_ptr<LCompilers::LLVMModule> m;
@@ -98,32 +103,19 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
98
103
result.llvm_ir = m->str ();
99
104
}
100
105
101
- bool call_init = false ;
102
- bool call_stmts = false ;
103
- std::string init_fn = " __module___main_____main____lpython_interactive_init_" + std::to_string (eval_count) + " __" ;
104
- std::string stmts_fn = " __module___main_____main____lpython_interactive_stmts_" + std::to_string (eval_count) + " __" ;
105
- if (m->get_return_type (init_fn) != " none" ) {
106
- call_init = true ;
107
- }
108
- if (m->get_return_type (stmts_fn) != " none" ) {
109
- call_stmts = true ;
106
+ bool call_run_fn = false ;
107
+ if (m->get_return_type (run_fn) != " none" ) {
108
+ call_run_fn = true ;
110
109
}
111
110
112
111
e->add_module (std::move (m));
113
- if (call_init) {
114
- e->voidfn (init_fn);
115
- }
116
- if (call_stmts) {
117
- e->voidfn (stmts_fn);
112
+ if (call_run_fn) {
113
+ e->voidfn (run_fn);
118
114
}
119
115
120
- if (call_init) {
121
- ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol (" __main__" ))->m_symtab
122
- ->erase_symbol (" __main____lpython_interactive_init_" + std::to_string (eval_count) + " __" );
123
- }
124
- if (call_stmts) {
125
- ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol (" __main__" ))->m_symtab
126
- ->erase_symbol (" __main____lpython_interactive_stmts_" + std::to_string (eval_count) + " __" );
116
+ if (call_run_fn) {
117
+ ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol (module_name))->m_symtab
118
+ ->erase_symbol (sym_name);
127
119
}
128
120
129
121
eval_count++;
@@ -139,8 +131,8 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
139
131
// Src -> AST
140
132
const std::string *code=&code_orig;
141
133
std::string tmp;
142
- Result<LCompilers::LPython::AST::ast_t *>
143
- res = LCompilers::LPython::parse_to_ast (al, *code, 0 , diagnostics);
134
+ Result<LCompilers::LPython::AST::Module_t *>
135
+ res = LCompilers::LPython::parse (al, *code, 0 , diagnostics);
144
136
if (res.ok ) {
145
137
return (LCompilers::LPython::AST::ast_t *)res.result ;
146
138
} else {
@@ -151,15 +143,15 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
151
143
152
144
Result<ASR::TranslationUnit_t*> PythonCompiler::get_asr3 (
153
145
LCompilers::LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
154
- LocationManager &lm)
146
+ LocationManager &lm, bool is_interactive )
155
147
{
156
148
ASR::TranslationUnit_t* asr;
157
149
// AST -> ASR
158
150
if (symbol_table) {
159
151
symbol_table->mark_all_variables_external (al);
160
152
}
161
153
auto res = LCompilers::LPython::python_ast_to_asr (al, lm, symbol_table, ast, diagnostics,
162
- compiler_options, true , " __main__" , " " , false , true );
154
+ compiler_options, true , " __main__" , " " , false , is_interactive ? eval_count : 0 );
163
155
if (res.ok ) {
164
156
asr = res.result ;
165
157
} else {
@@ -200,7 +192,7 @@ Result<std::unique_ptr<LLVMModule>> PythonCompiler::get_llvm3(
200
192
Result<std::unique_ptr<LCompilers::LLVMModule>> res
201
193
= asr_to_llvm (asr, diagnostics,
202
194
e->get_context (), al, lpm, compiler_options,
203
- " " , infile); // ??? What about run function
195
+ run_fn , infile);
204
196
if (res.ok ) {
205
197
m = std::move (res.result );
206
198
} else {
0 commit comments