@@ -798,6 +798,37 @@ int emit_llvm(const std::string &infile,
798
798
return 0 ;
799
799
}
800
800
801
+ bool determine_completeness (std::string command)
802
+ {
803
+ auto get_last_line = [](std::string input) {
804
+ if (input.length () == 1 ) {
805
+ return input;
806
+ }
807
+ size_t position = input.length () - 2 ;
808
+ while ((!(input[position] == ' \n ' || input[position] == ' \r ' )) && (position > 0 )) {
809
+ position--;
810
+ }
811
+ if (input[position] == ' \n ' || input[position] == ' \r ' ) {
812
+ position += 1 ;
813
+ }
814
+ return input.substr (position);
815
+ };
816
+
817
+ std::string last_line = get_last_line (command);
818
+ if ((last_line.rfind (" def" , 0 ) == 0 ) ||
819
+ (last_line.rfind (" for" , 0 ) == 0 ) ||
820
+ (last_line.rfind (" if" , 0 ) == 0 ) ||
821
+ (last_line.rfind (" else" , 0 ) == 0 ) ||
822
+ (last_line.rfind (" elif" , 0 ) == 0 ) ||
823
+ (last_line.rfind (" class" , 0 ) == 0 ) ||
824
+ (last_line.rfind (' @' , 0 ) == 0 ) ||
825
+ (last_line.rfind (' ' , 0 ) == 0 ) ||
826
+ (last_line.rfind (' \t ' , 0 ) == 0 )) {
827
+ return false ;
828
+ }
829
+ return true ;
830
+ }
831
+
801
832
int interactive_python_repl (
802
833
LCompilers::PassManager& pass_manager,
803
834
CompilerOptions &compiler_options,
@@ -811,30 +842,30 @@ int interactive_python_repl(
811
842
std::vector<std::pair<std::string, double >> times;
812
843
LCompilers::PythonCompiler::EvalResult r;
813
844
845
+ Terminal term (true , false );
846
+ std::cout << " Interactive LPython. Experimental prototype, not ready for end users." << std::endl;
847
+ std::string version = LFORTRAN_VERSION;
848
+ std::cout << " LPython version: " << version << std::endl;
849
+ std::cout << " * Use Ctrl-D to exit" << std::endl;
850
+ std::cout << " * Use Enter to submit" << std::endl;
851
+ std::cout << " * Use Alt-Enter or Ctrl-N to make a new line" << std::endl;
852
+ std::cout << " - Editing (Keys: Left, Right, Home, End, Backspace, Delete)" << std::endl;
853
+ std::cout << " - History (Keys: Up, Down)" << std::endl;
854
+
855
+ std::vector<std::string> history;
856
+
857
+ std::function<bool (std::string)> iscomplete = determine_completeness;
858
+
814
859
std::string code_string;
815
- std::cout << " >>> " ;
816
860
size_t cell_count = 0 ;
817
- for (std::string input; std::getline (std::cin, input);) {
818
- if (input == " exit" || input == " quit" ) {
861
+ while (true ) {
862
+ std::string code_string = prompt0 (term, " >>> " , history, iscomplete);
863
+ if (code_string.size () == 1 && code_string[0 ] == CTRL_KEY (' d' )) {
864
+ std::cout << std::endl;
865
+ std::cout << " Exiting." << std::endl;
819
866
return 0 ;
820
867
}
821
868
822
- if ((input.rfind (" def" , 0 ) == 0 ) ||
823
- (input.rfind (" for" , 0 ) == 0 ) ||
824
- (input.rfind (" if" , 0 ) == 0 ) ||
825
- (input.rfind (" else" , 0 ) == 0 ) ||
826
- (input.rfind (" elif" , 0 ) == 0 ) ||
827
- (input.rfind (" class" , 0 ) == 0 ) ||
828
- (input.rfind (' @' , 0 ) == 0 ) ||
829
- (input.rfind (' ' , 0 ) == 0 ) ||
830
- (input.rfind (' \t ' , 0 ) == 0 )) {
831
- // start of a block
832
- code_string += input + " \n " ;
833
- std::cout << " ... " ;
834
- continue ;
835
- }
836
- code_string += input + " \n " ;
837
-
838
869
{
839
870
cell_count++;
840
871
LCompilers::LocationManager::FileLocations fl;
@@ -856,8 +887,6 @@ int interactive_python_repl(
856
887
LCOMPILERS_ASSERT (diagnostics.has_error ())
857
888
std::cerr << diagnostics.render (lm, compiler_options);
858
889
diagnostics.clear ();
859
- code_string = " " ;
860
- std::cout << " >>> " ;
861
890
continue ;
862
891
}
863
892
@@ -872,9 +901,6 @@ int interactive_python_repl(
872
901
get_local_info (d);
873
902
std::cerr << stacktrace2str (d, LCompilers::stacktrace_depth);
874
903
std::cerr << e.name () + " : " << e.msg () << std::endl;
875
-
876
- code_string = " " ;
877
- std::cout << " >>> " ;
878
904
continue ;
879
905
}
880
906
@@ -984,9 +1010,6 @@ int interactive_python_repl(
984
1010
}
985
1011
default : throw LCompilers::LCompilersException (" Return type not supported" );
986
1012
}
987
-
988
- code_string = " " ;
989
- std::cout << " >>> " ;
990
1013
}
991
1014
return 0 ;
992
1015
}
0 commit comments