Support rline in the simple-repl and update it to be more like the real one
This commit is contained in:
parent
0df346fb9b
commit
879a26f3cf
|
@ -7,8 +7,8 @@
|
||||||
#include "simple-repl.h"
|
#include "simple-repl.h"
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
/* Disable automatic traceback printing, default modules */
|
/* Disable default modules */
|
||||||
krk_initVM(KRK_GLOBAL_CLEAN_OUTPUT|KRK_GLOBAL_NO_DEFAULT_MODULES);
|
krk_initVM(KRK_GLOBAL_NO_DEFAULT_MODULES);
|
||||||
|
|
||||||
/* Set up our module context. */
|
/* Set up our module context. */
|
||||||
krk_startModule("__main__");
|
krk_startModule("__main__");
|
||||||
|
@ -29,7 +29,6 @@ int main(int argc, char * argv[]) {
|
||||||
fprintf(stdout, " => %s\n", AS_CSTRING(result));
|
fprintf(stdout, " => %s\n", AS_CSTRING(result));
|
||||||
}
|
}
|
||||||
} else if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
|
} else if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
|
||||||
krk_dumpTraceback();
|
|
||||||
retval = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,7 +1,16 @@
|
||||||
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
|
#include "vendor/rline.h"
|
||||||
|
static int enableRline = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
#define PROMPT_MAIN ">>> "
|
#define PROMPT_MAIN ">>> "
|
||||||
#define PROMPT_BLOCK " > "
|
#define PROMPT_BLOCK " > "
|
||||||
|
|
||||||
static int runSimpleRepl(void) {
|
static int runSimpleRepl(void) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
int exitRepl = 0;
|
int exitRepl = 0;
|
||||||
while (!exitRepl) {
|
while (!exitRepl) {
|
||||||
size_t lineCapacity = 8;
|
size_t lineCapacity = 8;
|
||||||
|
@ -12,20 +21,57 @@ static int runSimpleRepl(void) {
|
||||||
char * allData = NULL;
|
char * allData = NULL;
|
||||||
int inBlock = 0;
|
int inBlock = 0;
|
||||||
int blockWidth = 0;
|
int blockWidth = 0;
|
||||||
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
|
rline_exp_set_prompts(PROMPT_MAIN, "", 4, 0);
|
||||||
|
rline_exit_string="";
|
||||||
|
rline_exp_set_syntax("krk");
|
||||||
|
rline_exp_set_tab_complete_func(NULL);
|
||||||
|
#endif
|
||||||
while (1) {
|
while (1) {
|
||||||
/* This would be a nice place for line editing */
|
/* This would be a nice place for line editing */
|
||||||
char buf[4096] = {0};
|
char buf[4096] = {0};
|
||||||
fprintf(stdout, "%s", inBlock ? PROMPT_BLOCK : PROMPT_MAIN);
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
fflush(stdout);
|
if (inBlock) {
|
||||||
|
/* When entering multiple lines, the additional lines
|
||||||
char * out = fgets(buf, 4096, stdin);
|
* will show a single > (and keep the left side aligned) */
|
||||||
if (!out || !strlen(buf)) {
|
rline_exp_set_prompts(PROMPT_BLOCK, "", 4, 0);
|
||||||
fprintf(stdout, "^D\n");
|
/* Also add indentation as necessary */
|
||||||
valid = 0;
|
rline_preload = malloc(blockWidth + 1);
|
||||||
exitRepl = 1;
|
for (int i = 0; i < blockWidth; ++i) {
|
||||||
break;
|
rline_preload[i] = ' ';
|
||||||
|
}
|
||||||
|
rline_preload[blockWidth] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!enableRline) {
|
||||||
|
#else
|
||||||
|
if (1) {
|
||||||
|
#endif
|
||||||
|
fprintf(stdout, "%s", inBlock ? PROMPT_BLOCK : PROMPT_MAIN);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
|
rline_scroll = 0;
|
||||||
|
if (enableRline) {
|
||||||
|
if (rline(buf, 4096) == 0) {
|
||||||
|
valid = 0;
|
||||||
|
exitRepl = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#endif
|
||||||
|
char * out = fgets(buf, 4096, stdin);
|
||||||
|
if (!out || !strlen(buf)) {
|
||||||
|
fprintf(stdout, "^D\n");
|
||||||
|
valid = 0;
|
||||||
|
exitRepl = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (buf[strlen(buf)-1] != '\n') {
|
if (buf[strlen(buf)-1] != '\n') {
|
||||||
valid = 0;
|
valid = 0;
|
||||||
break;
|
break;
|
||||||
|
@ -95,14 +141,21 @@ static int runSimpleRepl(void) {
|
||||||
|
|
||||||
for (size_t i = 0; i < lineCount; ++i) {
|
for (size_t i = 0; i < lineCount; ++i) {
|
||||||
if (valid) strcat(allData, lines[i]);
|
if (valid) strcat(allData, lines[i]);
|
||||||
|
#ifdef SIMPLE_REPL_ENABLE_RLINE
|
||||||
|
if (enableRline) {
|
||||||
|
rline_history_insert(strdup(lines[i]));
|
||||||
|
rline_scroll = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
free(lines[i]);
|
free(lines[i]);
|
||||||
}
|
}
|
||||||
FREE_ARRAY(char *, lines, lineCapacity);
|
FREE_ARRAY(char *, lines, lineCapacity);
|
||||||
if (valid) {
|
if (valid) {
|
||||||
KrkValue result = krk_interpret(allData, "<stdin>");
|
KrkValue result = krk_interpret(allData, "<stdin>");
|
||||||
if (!IS_NONE(result)) {
|
if (!IS_NONE(result)) {
|
||||||
|
krk_attachNamedValue(&vm.builtins->fields, "_", result);
|
||||||
KrkClass * type = krk_getType(result);
|
KrkClass * type = krk_getType(result);
|
||||||
const char * formatStr = " \033[1;30m=> %s\033[0m\n";
|
const char * formatStr = " \033[1;90m=> %s\033[0m\n";
|
||||||
if (type->_reprer) {
|
if (type->_reprer) {
|
||||||
krk_push(result);
|
krk_push(result);
|
||||||
result = krk_callDirect(type->_reprer, 1);
|
result = krk_callDirect(type->_reprer, 1);
|
||||||
|
@ -111,12 +164,10 @@ static int runSimpleRepl(void) {
|
||||||
result = krk_callDirect(type->_tostr, 1);
|
result = krk_callDirect(type->_tostr, 1);
|
||||||
}
|
}
|
||||||
if (!IS_STRING(result)) {
|
if (!IS_STRING(result)) {
|
||||||
fprintf(stdout, " \033[1;31m=> Unable to produce representation for value.\033[0m\n");
|
fprintf(stdout, " \033[1;91m=> Unable to produce representation for value.\033[0m\n");
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, formatStr, AS_CSTRING(result));
|
fprintf(stdout, formatStr, AS_CSTRING(result));
|
||||||
}
|
}
|
||||||
} else if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
|
|
||||||
krk_dumpTraceback();
|
|
||||||
}
|
}
|
||||||
krk_resetStack();
|
krk_resetStack();
|
||||||
free(allData);
|
free(allData);
|
||||||
|
|
Loading…
Reference in New Issue