@@ -794,22 +794,24 @@ int emit_llvm(const std::string &infile,
794794}
795795
796796int interactive_python_repl (
797- LCompilers::PassManager& pass_manager,
797+ // LCompilers::PassManager& pass_manager,
798798 CompilerOptions &compiler_options,
799- bool time_report)
799+ bool /* time_report*/ )
800800{
801801 Allocator al (4 *1024 );
802-
802+ compiler_options.interactive = true ;
803+ LCompilers::PythonCompiler fe (compiler_options);
803804 LCompilers::diag::Diagnostics diagnostics;
804- LCompilers::LocationManager lm;
805+ LCompilers::LocationManager lm;
805806 std::vector<std::pair<std::string, double >> times;
806- std::string infile;
807-
807+ LCompilers::PythonCompiler::EvalResult r;
808+ LCompilers::PassManager pass_manager;
809+ pass_manager.use_default_passes ();
810+
808811 std::string code_string;
809812 std::cout << " >>> " ;
810813 size_t cell_count = 0 ;
811814 for (std::string input; std::getline (std::cin, input);) {
812-
813815 if (input == " exit" || input == " quit" ) // exit condition
814816 return 0 ;
815817
@@ -833,76 +835,100 @@ int interactive_python_repl(
833835 {
834836 cell_count++;
835837 LCompilers::LocationManager::FileLocations fl;
836- infile = " stdin" + std::to_string (cell_count);
837- fl.in_filename = infile;
838+ fl.in_filename = " input" ;
839+ std::ofstream out (" input" );
840+ out << code_string;
838841 lm.files .push_back (fl);
839842 lm.init_simple (code_string);
840843 lm.file_ends .push_back (code_string.size ());
841844 }
842845
843- /* parse and run */
844- // parsing string to AST
845- auto parsing_start = std::chrono::high_resolution_clock::now ();
846- // ???: change `parse_python_file` to `parse` or `parse_string`
847- LCompilers::Result<LCompilers::LPython::AST::Module_t*> r = LCompilers::LPython::parse (al, code_string, 0 , diagnostics);
848- auto parsing_end = std::chrono::high_resolution_clock::now ();
849- times.push_back (std::make_pair (" Parsing" , std::chrono::duration<double , std::milli>(parsing_end - parsing_start).count ()));
850- std::cerr << diagnostics.render (lm, compiler_options);
851- if (!r.ok ) {
852- LCOMPILERS_ASSERT (diagnostics.has_error ())
853- print_time_report (times, time_report);
854- return 1 ;
846+ try {
847+ auto evaluation_start_time = std::chrono::high_resolution_clock::now ();
848+ LCompilers::Result<LCompilers::PythonCompiler::EvalResult>
849+ res = fe.evaluate (code_string, false , lm, pass_manager, diagnostics);
850+ std::cerr << diagnostics.render (lm, compiler_options);
851+ if (res.ok ) {
852+ r = res.result ;
853+ } else {
854+ LCOMPILERS_ASSERT (diagnostics.has_error ())
855+ code_string = " " ;
856+ std::cout << " >>> " ;
857+ continue ;
858+ }
859+
860+ auto evaluation_end_time = std::chrono::high_resolution_clock::now ();
861+ times.push_back (std::make_pair (" evalution " + std::to_string (cell_count), std::chrono::duration
862+ <double , std::milli>(evaluation_start_time - evaluation_end_time).count ()));
863+
864+ } catch (const LCompilers::LCompilersException &e) {
865+ std::cerr << " Internal Compiler Error: Unhandled exception" << std::endl;
866+ std::vector<LCompilers::StacktraceItem> d = e.stacktrace_addresses ();
867+ get_local_addresses (d);
868+ get_local_info (d);
869+ std::cerr << stacktrace2str (d, LCompilers::stacktrace_depth);
870+ std::cerr << e.name () + " : " << e.msg () << std::endl;
871+
872+ code_string = " " ;
873+ std::cout << " >>> " ;
874+ continue ;
855875 }
856876
857- // AST -> ASR
858- LCompilers::LPython::AST::ast_t * ast = (LCompilers::LPython::AST::ast_t *)r.result ;
859- diagnostics.diagnostics .clear ();
860- auto ast_to_asr_start = std::chrono::high_resolution_clock::now ();
861- LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
862- r1 = LCompilers::LPython::python_ast_to_asr (al, lm, nullptr , *ast, diagnostics, compiler_options,
863- !(false && compiler_options.po .disable_main ), " __main__" , infile);
864-
865- auto ast_to_asr_end = std::chrono::high_resolution_clock::now ();
866- times.push_back (std::make_pair (" AST to ASR" , std::chrono::duration<double , std::milli>(ast_to_asr_end - ast_to_asr_start).count ()));
867- std::cerr << diagnostics.render (lm, compiler_options);
868- if (!r1.ok ) {
869- LCOMPILERS_ASSERT (diagnostics.has_error ())
870- print_time_report (times, time_report);
871- return 2 ;
872- }
873- LCompilers::ASR::TranslationUnit_t* asr = r1.result ;
874- diagnostics.diagnostics .clear ();
875-
876- LCompilers::PythonCompiler fe (compiler_options);
877- LCompilers::LLVMEvaluator e (compiler_options.target );
878- std::unique_ptr<LCompilers::LLVMModule> m;
879- auto asr_to_llvm_start = std::chrono::high_resolution_clock::now ();
880- LCompilers::Result<std::unique_ptr<LCompilers::LLVMModule>>
881- res = fe.get_llvm3 (*asr, pass_manager, diagnostics, infile);
882- auto asr_to_llvm_end = std::chrono::high_resolution_clock::now ();
883- times.push_back (std::make_pair (" ASR to LLVM" , std::chrono::duration<double , std::milli>(asr_to_llvm_end - asr_to_llvm_start).count ()));
884-
885- std::cerr << diagnostics.render (lm, compiler_options);
886- if (!res.ok ) {
887- LCOMPILERS_ASSERT (diagnostics.has_error ())
888- print_time_report (times, time_report);
889- return 3 ;
877+ switch (r.type ) {
878+ case (LCompilers::PythonCompiler::EvalResult::integer4) : {
879+ // if (verbose) std::cout << "Return type: integer" << std::endl;
880+ // if (verbose) section("Result:");
881+ std::cout << r.i32 << std::endl;
882+ break ;
883+ }
884+ case (LCompilers::PythonCompiler::EvalResult::integer8) : {
885+ // if (verbose) std::cout << "Return type: integer(8)" << std::endl;
886+ // if (verbose) section("Result:");
887+ std::cout << r.i64 << std::endl;
888+ break ;
889+ }
890+ case (LCompilers::PythonCompiler::EvalResult::real4) : {
891+ // if (verbose) std::cout << "Return type: real" << std::endl;
892+ // if (verbose) section("Result:");
893+ std::cout << std::setprecision (8 ) << r.f32 << std::endl;
894+ break ;
895+ }
896+ case (LCompilers::PythonCompiler::EvalResult::real8) : {
897+ // if (verbose) std::cout << "Return type: real(8)" << std::endl;
898+ // if (verbose) section("Result:");
899+ std::cout << std::setprecision (17 ) << r.f64 << std::endl;
900+ break ;
901+ }
902+ case (LCompilers::PythonCompiler::EvalResult::complex4) : {
903+ // if (verbose) std::cout << "Return type: complex" << std::endl;
904+ // if (verbose) section("Result:");
905+ std::cout << std::setprecision (8 ) << " (" << r.c32 .re << " , " << r.c32 .im << " )" << std::endl;
906+ break ;
907+ }
908+ case (LCompilers::PythonCompiler::EvalResult::complex8) : {
909+ // if (verbose) std::cout << "Return type: complex(8)" << std::endl;
910+ // if (verbose) section("Result:");
911+ std::cout << std::setprecision (17 ) << " (" << r.c64 .re << " , " << r.c64 .im << " )" << std::endl;
912+ break ;
913+ }
914+ case (LCompilers::PythonCompiler::EvalResult::statement) : {
915+ // if (verbose) {
916+ // std::cout << "Return type: none" << std::endl;
917+ // section("Result:");
918+ // std::cout << "(statement)" << std::endl;
919+ // }
920+ break ;
921+ }
922+ case (LCompilers::PythonCompiler::EvalResult::none) : {
923+ // if (verbose) {
924+ // std::cout << "Return type: none" << std::endl;
925+ // section("Result:");
926+ // std::cout << "(nothing to execute)" << std::endl;
927+ // }
928+ break ;
929+ }
930+ default : throw LCompilers::LCompilersException (" Return type not supported" );
890931 }
891- m = std::move (res.result );
892-
893- bool call_init = false ;
894- bool call_stmts = false ;
895- if (m->get_return_type (" __module___main_____main__global_init" ) == " void" )
896- call_init = true ;
897- if (m->get_return_type (" __module___main_____main__global_stmts" ) == " void" )
898- call_stmts = true ;
899-
900- e.add_module (std::move (m));
901- if (call_init)
902- e.voidfn (" __module___main_____main__global_init" );
903- if (call_stmts)
904- e.voidfn (" __module___main_____main__global_stmts" );
905- /* end parse and run */
906932
907933 code_string = " " ;
908934 std::cout << " >>> " ;
@@ -1955,7 +1981,7 @@ int main(int argc, char *argv[])
19551981 compiler_options.po .disable_main = true ;
19561982 compiler_options.emit_debug_line_column = false ;
19571983 compiler_options.generate_object_code = false ;
1958- return interactive_python_repl (lpython_pass_manager, compiler_options, time_report);
1984+ return interactive_python_repl (compiler_options, time_report);
19591985 }
19601986
19611987 // TODO: for now we ignore the other filenames, only handle
0 commit comments