Skip to content

Commit 8aadf8c

Browse files
authored
Merge pull request #2729 from Vipul-Cariappa/cli-update
Improved CLI experience for REPL
2 parents e04c568 + c0adf03 commit 8aadf8c

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

src/bin/lpython.cpp

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,37 @@ int emit_llvm(const std::string &infile,
798798
return 0;
799799
}
800800

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+
801832
int interactive_python_repl(
802833
LCompilers::PassManager& pass_manager,
803834
CompilerOptions &compiler_options,
@@ -811,30 +842,30 @@ int interactive_python_repl(
811842
std::vector<std::pair<std::string, double>> times;
812843
LCompilers::PythonCompiler::EvalResult r;
813844

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+
814859
std::string code_string;
815-
std::cout << ">>> ";
816860
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;
819866
return 0;
820867
}
821868

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-
838869
{
839870
cell_count++;
840871
LCompilers::LocationManager::FileLocations fl;
@@ -856,8 +887,6 @@ int interactive_python_repl(
856887
LCOMPILERS_ASSERT(diagnostics.has_error())
857888
std::cerr << diagnostics.render(lm, compiler_options);
858889
diagnostics.clear();
859-
code_string = "";
860-
std::cout << ">>> ";
861890
continue;
862891
}
863892

@@ -872,9 +901,6 @@ int interactive_python_repl(
872901
get_local_info(d);
873902
std::cerr << stacktrace2str(d, LCompilers::stacktrace_depth);
874903
std::cerr << e.name() + ": " << e.msg() << std::endl;
875-
876-
code_string = "";
877-
std::cout << ">>> ";
878904
continue;
879905
}
880906

@@ -984,9 +1010,6 @@ int interactive_python_repl(
9841010
}
9851011
default : throw LCompilers::LCompilersException("Return type not supported");
9861012
}
987-
988-
code_string = "";
989-
std::cout << ">>> ";
9901013
}
9911014
return 0;
9921015
}

0 commit comments

Comments
 (0)