1 #ifdef SIMPLE_REPL_ENABLE_RLINE
2 #include "vendor/rline.h"
3 static int enableRline = 1;
6 #define PROMPT_MAIN ">>> "
7 #define PROMPT_BLOCK " > "
9 static int runSimpleRepl(
void) {
11 SetConsoleOutputCP(65001);
16 size_t lineCapacity = 8;
18 char ** lines = KRK_ALLOCATE(
char *, lineCapacity);
21 char * allData = NULL;
24 #ifdef SIMPLE_REPL_ENABLE_RLINE
25 rline_exp_set_prompts(PROMPT_MAIN,
"", 4, 0);
27 rline_exp_set_syntax(
"krk");
28 rline_exp_set_tab_complete_func(NULL);
33 #ifdef SIMPLE_REPL_ENABLE_RLINE
37 rline_exp_set_prompts(PROMPT_BLOCK,
"", 4, 0);
39 rline_preload = malloc(blockWidth + 1);
40 for (
int i = 0; i < blockWidth; ++i) {
41 rline_preload[i] =
' ';
43 rline_preload[blockWidth] =
'\0';
50 fprintf(stdout,
"%s", inBlock ? PROMPT_BLOCK : PROMPT_MAIN);
54 #ifdef SIMPLE_REPL_ENABLE_RLINE
57 if (rline(buf, 4096) == 0) {
64 char * out = fgets(buf, 4096, stdin);
65 if (!out || !strlen(buf)) {
66 fprintf(stdout,
"^D\n");
71 #ifdef SIMPLE_REPL_ENABLE_RLINE
75 if (buf[strlen(buf)-1] !=
'\n') {
80 if (lineCapacity < lineCount + 1) {
82 size_t old = lineCapacity;
83 lineCapacity = KRK_GROW_CAPACITY(old);
84 lines = KRK_GROW_ARRAY(
char *,lines,old,lineCapacity);
88 lines[i] = strdup(buf);
90 size_t lineLength = strlen(lines[i]);
91 totalData += lineLength;
95 for (
size_t j = 0; j < lineLength; ++j) {
96 if (lines[i][j] !=
' ' && lines[i][j] !=
'\n') {
107 if (lineLength > 1 && lines[i][lineLength-2] ==
':') {
109 blockWidth = countSpaces + 4;
111 }
else if (lineLength > 1 && lines[i][lineLength-2] ==
'\\') {
114 }
else if (inBlock && lineLength != 1) {
117 totalData -= lineLength;
121 blockWidth = countSpaces;
123 }
else if (lineLength > 1 && lines[i][countSpaces] ==
'@') {
125 blockWidth = countSpaces;
130 if (isSpaces && !i) valid = 0;
138 allData = malloc(totalData + 1);
142 for (
size_t i = 0; i < lineCount; ++i) {
143 if (valid) strcat(allData, lines[i]);
144 #ifdef SIMPLE_REPL_ENABLE_RLINE
146 rline_history_insert(strdup(lines[i]));
152 KRK_FREE_ARRAY(
char *, lines, lineCapacity);
155 if (!IS_NONE(result)) {
158 const char * formatStr =
" \033[1;90m=> %s\033[0m\n";
162 }
else if (type->
_tostr) {
166 if (!IS_STRING(result)) {
167 fprintf(stdout,
" \033[1;91m=> Unable to produce representation for value.\033[0m\n");
169 fprintf(stdout, formatStr, AS_CSTRING(result));
KrkObj * _tostr
__str__ Called to produce a string from an instance
KrkObj * _reprer
__repr__ Called to create a reproducible string representation of an instance
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value to an attribute table.
Stack reference or primative value.
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
void krk_resetStack(void)
Reset the current thread's stack state to the top level.
#define vm
Convenience macro for namespacing.
KrkValue krk_interpret(const char *src, const char *fromFile)
Compile and execute a source code input.
void krk_push(KrkValue value)
Push a stack value.
KrkValue krk_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.