Skip to content

Commit a33fa28

Browse files
Vipul-Cariappaubaidsk
authored andcommitted
removed Interactive_t related stuff by merging required changes into SymbolTable.visit_Module
1 parent da5feda commit a33fa28

File tree

6 files changed

+42
-288
lines changed

6 files changed

+42
-288
lines changed

src/lpython/parser/parser.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,6 @@ Result<LPython::AST::Module_t*> parse(Allocator &al, const std::string &s,
4242
p.result.p, p.result.size(), p.type_ignore.p, p.type_ignore.size());
4343
}
4444

45-
Result<LPython::AST::ast_t*> parse_to_ast(Allocator &al, const std::string &s,
46-
uint32_t prev_loc, diag::Diagnostics &diagnostics)
47-
{
48-
Parser p(al, diagnostics);
49-
try {
50-
p.parse(s, prev_loc);
51-
} catch (const parser_local::TokenizerError &e) {
52-
Error error;
53-
diagnostics.diagnostics.push_back(e.d);
54-
return error;
55-
} catch (const parser_local::ParserError &e) {
56-
Error error;
57-
diagnostics.diagnostics.push_back(e.d);
58-
return error;
59-
}
60-
61-
Location l;
62-
if (p.result.size() == 0) {
63-
l.first=0;
64-
l.last=0;
65-
} else {
66-
l.first=p.result[0]->base.loc.first;
67-
l.last=p.result[p.result.size()-1]->base.loc.last;
68-
}
69-
return LPython::AST::make_Interactive_t(al, l, p.result.p, p.result.size());
70-
}
71-
7245
void Parser::parse(const std::string &input, uint32_t prev_loc)
7346
{
7447
inp = input;

src/lpython/parser/parser.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ Result<LPython::AST::Module_t*> parse(Allocator &al,
3636
const std::string &s, uint32_t prev_loc,
3737
diag::Diagnostics &diagnostics);
3838

39-
Result<LPython::AST::ast_t*> parse_to_ast(Allocator &al,
40-
const std::string &s, uint32_t prev_loc,
41-
diag::Diagnostics &diagnostics);
42-
4339
Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
4440
const std::string &runtime_library_dir,
4541
const std::string &infile,

src/lpython/python_evaluator.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PythonCompiler::PythonCompiler(CompilerOptions compiler_options)
3131
al{1024*1024},
3232
#ifdef HAVE_LFORTRAN_LLVM
3333
e{std::make_unique<LLVMEvaluator>()},
34-
eval_count{0},
34+
eval_count{1},
3535
#endif
3636
compiler_options{compiler_options},
3737
symbol_table{nullptr}
@@ -70,7 +70,7 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
7070
}
7171

7272
// 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);
7474
ASR::TranslationUnit_t* asr;
7575
if (res2.ok) {
7676
asr = res2.result;
@@ -84,6 +84,11 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
8484
}
8585

8686
// 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+
8792
Result<std::unique_ptr<LLVMModule>> res3 = get_llvm3(*asr,
8893
pass_manager, diagnostics, lm.files.back().in_filename);
8994
std::unique_ptr<LCompilers::LLVMModule> m;
@@ -98,32 +103,19 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
98103
result.llvm_ir = m->str();
99104
}
100105

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;
110109
}
111110

112111
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);
118114
}
119115

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);
127119
}
128120

129121
eval_count++;
@@ -139,8 +131,8 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
139131
// Src -> AST
140132
const std::string *code=&code_orig;
141133
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);
144136
if (res.ok) {
145137
return (LCompilers::LPython::AST::ast_t*)res.result;
146138
} else {
@@ -151,15 +143,15 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
151143

152144
Result<ASR::TranslationUnit_t*> PythonCompiler::get_asr3(
153145
LCompilers::LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
154-
LocationManager &lm)
146+
LocationManager &lm, bool is_interactive)
155147
{
156148
ASR::TranslationUnit_t* asr;
157149
// AST -> ASR
158150
if (symbol_table) {
159151
symbol_table->mark_all_variables_external(al);
160152
}
161153
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);
163155
if (res.ok) {
164156
asr = res.result;
165157
} else {
@@ -200,7 +192,7 @@ Result<std::unique_ptr<LLVMModule>> PythonCompiler::get_llvm3(
200192
Result<std::unique_ptr<LCompilers::LLVMModule>> res
201193
= asr_to_llvm(asr, diagnostics,
202194
e->get_context(), al, lpm, compiler_options,
203-
"", infile); // ??? What about run function
195+
run_fn, infile);
204196
if (res.ok) {
205197
m = std::move(res.result);
206198
} else {

src/lpython/python_evaluator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class PythonCompiler
6161

6262
Result<ASR::TranslationUnit_t*> get_asr3(
6363
LCompilers::LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
64-
LocationManager &lm);
64+
LocationManager &lm, bool is_interactive=false);
6565

6666
Result<std::unique_ptr<LLVMModule>> get_llvm3(ASR::TranslationUnit_t &asr,
6767
LCompilers::PassManager& lpm, diag::Diagnostics &diagnostics,

0 commit comments

Comments
 (0)