Skip to content

Commit a36d692

Browse files
clean up
1 parent 824d34e commit a36d692

File tree

3 files changed

+53
-102
lines changed

3 files changed

+53
-102
lines changed

src/bin/lpython.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ void print_time_report(std::vector<std::pair<std::string, double>> &times, bool
744744

745745
#ifdef HAVE_LFORTRAN_LLVM
746746

747+
void section(const std::string &s)
748+
{
749+
std::cout << color(LCompilers::style::bold) << color(LCompilers::fg::blue) << s << color(LCompilers::style::reset) << color(LCompilers::fg::reset) << std::endl;
750+
}
751+
747752
int emit_llvm(const std::string &infile,
748753
const std::string &runtime_library_dir,
749754
LCompilers::PassManager& pass_manager,
@@ -794,9 +799,9 @@ int emit_llvm(const std::string &infile,
794799
}
795800

796801
int interactive_python_repl(
797-
// LCompilers::PassManager& pass_manager,
802+
LCompilers::PassManager& pass_manager,
798803
CompilerOptions &compiler_options,
799-
bool /*time_report*/)
804+
bool verbose)
800805
{
801806
Allocator al(4*1024);
802807
compiler_options.interactive = true;
@@ -805,8 +810,6 @@ int interactive_python_repl(
805810
LCompilers::LocationManager lm;
806811
std::vector<std::pair<std::string, double>> times;
807812
LCompilers::PythonCompiler::EvalResult r;
808-
LCompilers::PassManager pass_manager;
809-
pass_manager.use_default_passes();
810813

811814
std::string code_string;
812815
std::cout << ">>> ";
@@ -831,8 +834,6 @@ int interactive_python_repl(
831834
}
832835
code_string += input + "\n";
833836

834-
// std::cout << "code block: \n" << code_string;
835-
836837
{
837838
cell_count++;
838839
LCompilers::LocationManager::FileLocations fl;
@@ -847,7 +848,7 @@ int interactive_python_repl(
847848
try {
848849
auto evaluation_start_time = std::chrono::high_resolution_clock::now();
849850
LCompilers::Result<LCompilers::PythonCompiler::EvalResult>
850-
res = fe.evaluate(code_string, false, lm, pass_manager, diagnostics);
851+
res = fe.evaluate(code_string, verbose, lm, pass_manager, diagnostics);
851852
if (res.ok) {
852853
r = res.result;
853854
} else {
@@ -876,57 +877,66 @@ int interactive_python_repl(
876877
continue;
877878
}
878879

880+
if (verbose) {
881+
section("AST:");
882+
std::cout << r.ast << std::endl;
883+
section("ASR:");
884+
std::cout << r.asr << std::endl;
885+
section("LLVM IR:");
886+
std::cout << r.llvm_ir << std::endl;
887+
}
888+
879889
switch (r.type) {
880890
case (LCompilers::PythonCompiler::EvalResult::integer4) : {
881-
// if (verbose) std::cout << "Return type: integer" << std::endl;
882-
// if (verbose) section("Result:");
891+
if (verbose) std::cout << "Return type: integer" << std::endl;
892+
if (verbose) section("Result:");
883893
std::cout << r.i32 << std::endl;
884894
break;
885895
}
886896
case (LCompilers::PythonCompiler::EvalResult::integer8) : {
887-
// if (verbose) std::cout << "Return type: integer(8)" << std::endl;
888-
// if (verbose) section("Result:");
897+
if (verbose) std::cout << "Return type: integer(8)" << std::endl;
898+
if (verbose) section("Result:");
889899
std::cout << r.i64 << std::endl;
890900
break;
891901
}
892902
case (LCompilers::PythonCompiler::EvalResult::real4) : {
893-
// if (verbose) std::cout << "Return type: real" << std::endl;
894-
// if (verbose) section("Result:");
903+
if (verbose) std::cout << "Return type: real" << std::endl;
904+
if (verbose) section("Result:");
895905
std::cout << std::setprecision(8) << r.f32 << std::endl;
896906
break;
897907
}
898908
case (LCompilers::PythonCompiler::EvalResult::real8) : {
899-
// if (verbose) std::cout << "Return type: real(8)" << std::endl;
900-
// if (verbose) section("Result:");
909+
if (verbose) std::cout << "Return type: real(8)" << std::endl;
910+
if (verbose) section("Result:");
901911
std::cout << std::setprecision(17) << r.f64 << std::endl;
902912
break;
903913
}
904914
case (LCompilers::PythonCompiler::EvalResult::complex4) : {
905-
// if (verbose) std::cout << "Return type: complex" << std::endl;
906-
// if (verbose) section("Result:");
915+
if (verbose) std::cout << "Return type: complex" << std::endl;
916+
if (verbose) section("Result:");
907917
std::cout << std::setprecision(8) << "(" << r.c32.re << ", " << r.c32.im << ")" << std::endl;
908918
break;
909919
}
910920
case (LCompilers::PythonCompiler::EvalResult::complex8) : {
911-
// if (verbose) std::cout << "Return type: complex(8)" << std::endl;
912-
// if (verbose) section("Result:");
921+
if (verbose) std::cout << "Return type: complex(8)" << std::endl;
922+
if (verbose) section("Result:");
913923
std::cout << std::setprecision(17) << "(" << r.c64.re << ", " << r.c64.im << ")" << std::endl;
914924
break;
915925
}
916926
case (LCompilers::PythonCompiler::EvalResult::statement) : {
917-
// if (verbose) {
918-
// std::cout << "Return type: none" << std::endl;
919-
// section("Result:");
920-
// std::cout << "(statement)" << std::endl;
921-
// }
927+
if (verbose) {
928+
std::cout << "Return type: none" << std::endl;
929+
section("Result:");
930+
std::cout << "(statement)" << std::endl;
931+
}
922932
break;
923933
}
924934
case (LCompilers::PythonCompiler::EvalResult::none) : {
925-
// if (verbose) {
926-
// std::cout << "Return type: none" << std::endl;
927-
// section("Result:");
928-
// std::cout << "(nothing to execute)" << std::endl;
929-
// }
935+
if (verbose) {
936+
std::cout << "Return type: none" << std::endl;
937+
section("Result:");
938+
std::cout << "(nothing to execute)" << std::endl;
939+
}
930940
break;
931941
}
932942
default : throw LCompilers::LCompilersException("Return type not supported");
@@ -1983,7 +1993,7 @@ int main(int argc, char *argv[])
19831993
compiler_options.po.disable_main = true;
19841994
compiler_options.emit_debug_line_column = false;
19851995
compiler_options.generate_object_code = false;
1986-
return interactive_python_repl(compiler_options, time_report);
1996+
return interactive_python_repl(lpython_pass_manager, compiler_options, arg_v);
19871997
}
19881998

19891999
// TODO: for now we ignore the other filenames, only handle

src/lpython/python_evaluator.cpp

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ PythonCompiler::~PythonCompiler() = default;
4343

4444
Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
4545
#ifdef HAVE_LFORTRAN_LLVM
46-
const std::string &code_orig, bool /*verbose*/, LocationManager &lm,
46+
const std::string &code_orig, bool verbose, LocationManager &lm,
4747
LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics
4848
#else
4949
const std::string &/*code_orig*/, bool /*verbose*/,
@@ -65,11 +65,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
6565
return res.error;
6666
}
6767

68-
// if (verbose) {
69-
// result.ast = LCompilers::LPython::pickle_python(*ast, true, true);
70-
// }
71-
72-
// std::cout << result.ast << std::endl;
68+
if (verbose) {
69+
result.ast = LCompilers::LPython::pickle_python(*ast, true, true);
70+
}
7371

7472
// AST -> ASR
7573
Result<ASR::TranslationUnit_t*> res2 = get_asr3(*ast, diagnostics, lm);
@@ -81,11 +79,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
8179
return res2.error;
8280
}
8381

84-
// if (verbose) {
85-
// result.asr = pickle(*asr, true, true, true);
86-
// }
87-
88-
// std::cout << result.asr << std::endl;
82+
if (verbose) {
83+
result.asr = pickle(*asr, true, true, true);
84+
}
8985

9086
// ASR -> LLVM
9187
Result<std::unique_ptr<LLVMModule>> res3 = get_llvm3(*asr,
@@ -98,11 +94,9 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
9894
return res3.error;
9995
}
10096

101-
// if (verbose) {
102-
// result.llvm_ir = m->str();
103-
// }
104-
105-
// std::cout << m->str() << std::endl;
97+
if (verbose) {
98+
result.llvm_ir = m->str();
99+
}
106100

107101
bool call_init = false;
108102
bool call_stmts = false;
@@ -123,7 +117,6 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
123117
e->voidfn(stmts_fn);
124118
}
125119

126-
// TODO: remove init_fn and stmts_fn from asr
127120
if (call_init) {
128121
ASR::down_cast<ASR::Module_t>(symbol_table->resolve_symbol("__main__"))->m_symtab
129122
->erase_symbol("__main____lpython_interactive_init_" + std::to_string(eval_count) + "__");
@@ -134,45 +127,6 @@ Result<PythonCompiler::EvalResult> PythonCompiler::evaluate(
134127
}
135128

136129
eval_count++;
137-
138-
// std::string return_type = m->get_return_type(run_fn);
139-
140-
// // LLVM -> Machine code -> Execution
141-
// e->add_module(std::move(m));
142-
// if (return_type == "integer4") {
143-
// int32_t r = e->int32fn(run_fn);
144-
// result.type = EvalResult::integer4;
145-
// result.i32 = r;
146-
// } else if (return_type == "integer8") {
147-
// int64_t r = e->int64fn(run_fn);
148-
// result.type = EvalResult::integer8;
149-
// result.i64 = r;
150-
// } else if (return_type == "real4") {
151-
// float r = e->floatfn(run_fn);
152-
// result.type = EvalResult::real4;
153-
// result.f32 = r;
154-
// } else if (return_type == "real8") {
155-
// double r = e->doublefn(run_fn);
156-
// result.type = EvalResult::real8;
157-
// result.f64 = r;
158-
// } else if (return_type == "complex4") {
159-
// std::complex<float> r = e->complex4fn(run_fn);
160-
// result.type = EvalResult::complex4;
161-
// result.c32.re = r.real();
162-
// result.c32.im = r.imag();
163-
// } else if (return_type == "complex8") {
164-
// std::complex<double> r = e->complex8fn(run_fn);
165-
// result.type = EvalResult::complex8;
166-
// result.c64.re = r.real();
167-
// result.c64.im = r.imag();
168-
// } else if (return_type == "void") {
169-
// e->voidfn(run_fn);
170-
// result.type = EvalResult::statement;
171-
// } else if (return_type == "none") {
172-
// result.type = EvalResult::none;
173-
// } else {
174-
// throw LCompilersException("PythonCompiler::evaluate(): Return type not supported");
175-
// }
176130
return result;
177131
#else
178132
throw LCompilersException("LLVM is not enabled");
@@ -187,8 +141,6 @@ Result<LCompilers::LPython::AST::ast_t*> PythonCompiler::get_ast2(
187141
std::string tmp;
188142
Result<LCompilers::LPython::AST::ast_t*>
189143
res = LCompilers::LPython::parse_to_ast(al, *code, 0, diagnostics);
190-
// Result<LCompilers::LPython::AST::Module_t*>
191-
// res = LCompilers::LPython::parse(al, *code, 0, diagnostics);
192144
if (res.ok) {
193145
return (LCompilers::LPython::AST::ast_t*)res.result;
194146
} else {
@@ -203,11 +155,7 @@ Result<ASR::TranslationUnit_t*> PythonCompiler::get_asr3(
203155
{
204156
ASR::TranslationUnit_t* asr;
205157
// AST -> ASR
206-
// Remove the old execution function if it exists
207158
if (symbol_table) {
208-
if (symbol_table->get_symbol(run_fn) != nullptr) {
209-
symbol_table->erase_symbol(run_fn);
210-
}
211159
symbol_table->mark_all_variables_external(al);
212160
}
213161
auto res = LCompilers::LPython::python_ast_to_asr(al, lm, symbol_table, ast, diagnostics,
@@ -234,9 +182,6 @@ Result<std::unique_ptr<LLVMModule>> PythonCompiler::get_llvm3(
234182
)
235183
{
236184
#ifdef HAVE_LFORTRAN_LLVM
237-
// eval_count++;
238-
// run_fn = "__lfortran_evaluate_" + std::to_string(eval_count);
239-
240185
if (compiler_options.emit_debug_info) {
241186
if (!compiler_options.emit_debug_line_column) {
242187
diagnostics.add(LCompilers::diag::Diagnostic(

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,16 +4175,11 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
41754175
} else {
41764176
ASR::Module_t* module_sym =
41774177
ASR::down_cast<ASR::Module_t>(parent_scope->resolve_symbol(module_name));
4178-
current_scope = module_sym->m_symtab;
41794178
LCOMPILERS_ASSERT(module_sym != nullptr);
4179+
current_scope = module_sym->m_symtab;
41804180
for (size_t i=0; i<x.n_body; i++) {
41814181
visit_stmt(*x.m_body[i]);
41824182
}
4183-
module_sym->m_dependencies = current_module_dependencies.p;
4184-
module_sym->n_dependencies = current_module_dependencies.size();
4185-
if (!overload_defs.empty()) {
4186-
create_GenericProcedure(x.base.base.loc);
4187-
}
41884183
}
41894184

41904185
global_scope = nullptr;
@@ -5040,7 +5035,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50405035
}
50415036

50425037
void visit_Interactive(const AST::Interactive_t &x) {
5043-
static size_t interactive_execution_count = 0;
5038+
static size_t interactive_execution_count = 0; // ???: should this be a class member variable
5039+
// interactive_execution_count should be the same as eval_count in PythonCompiler
50445040

50455041
ASR::TranslationUnit_t *unit = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
50465042
current_scope = unit->m_symtab;

0 commit comments

Comments
 (0)