1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-24 06:03:17 +00:00

Get expected behaviour; cleanup after confirming behaviour is desired

This commit is contained in:
LouisJackman 2020-05-20 22:40:05 +01:00
parent a99906c6f0
commit 164ed0b325
No known key found for this signature in database
GPG Key ID: C83A456999EEBC34
3 changed files with 73 additions and 21 deletions

View File

@ -33,6 +33,12 @@
extern const unsigned char *janet_gen_boot;
extern int32_t janet_gen_boot_size;
static Janet janet_set_repl_within_form(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
(void) argv;
return janet_wrap_nil();
}
int main(int argc, const char **argv) {
/* Init janet */
@ -53,6 +59,8 @@ int main(int argc, const char **argv) {
env = janet_core_env(NULL);
janet_def(env, "set-repl-within-form", janet_wrap_cfunction(janet_set_repl_within_form), "");
/* Create args tuple */
JanetArray *args = janet_array(argc);
for (int i = 0; i < argc; i++)

View File

@ -1978,7 +1978,7 @@
(var going true)
# The parser object
(def p (parser/new))
(var p (parser/new))
# Evaluate 1 source form in a protected manner
(defn eval1 [source]
@ -2017,19 +2017,30 @@
(while going
(if (env :exit) (break))
(buffer/clear buf)
(chunks buf p)
(var pindex 0)
(var pstatus nil)
(def len (length buf))
(when (= len 0)
(parser/eof p)
(set going false))
(while (> len pindex)
(+= pindex (parser/consume p buf pindex))
(while (parser/has-more p)
(eval1 (parser/produce p)))
(when (= (parser/status p) :error)
(parse-err p where))))
(if (nil? (chunks buf p))
(do
# Nil chunks represents a cancelled form in the REPL, so reset.
(set-repl-within-form false)
(set p (parser/new))
(buffer/clear buf))
(do
(var pindex 0)
(var pstatus nil)
(def len (length buf))
(when (= len 0)
(parser/eof p)
(set going false))
(if (= len 1)
(set-repl-within-form false)
(set-repl-within-form true))
(while (> len pindex)
(set-repl-within-form true)
(+= pindex (parser/consume p buf pindex))
(while (parser/has-more p)
(eval1 (parser/produce p)))
(when (= (parser/status p) :error)
(parse-err p where))))))
# Check final parser state
(while (parser/has-more p)
(eval1 (parser/produce p)))

View File

@ -25,6 +25,7 @@
#endif
#include <janet.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
@ -39,6 +40,10 @@ void janet_line_deinit();
void janet_line_get(const char *p, JanetBuffer *buffer);
Janet janet_line_getter(int32_t argc, Janet *argv);
static Janet janet_set_repl_within_form(int32_t argc, Janet *argv);
static JANET_THREAD_LOCAL bool gbl_cancel_current_repl_form = false;
static JANET_THREAD_LOCAL bool gbl_repl_within_form = false;
/*
* Line Editing
@ -54,7 +59,17 @@ Janet janet_line_getter(int32_t argc, Janet *argv) {
gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL;
janet_line_get(str, buf);
gbl_complete_env = NULL;
return janet_wrap_buffer(buf);
Janet result;
if (gbl_cancel_current_repl_form) {
gbl_cancel_current_repl_form = false;
// Signal that the user bailed out of the current form
result = janet_wrap_nil();
} else {
result = janet_wrap_buffer(buf);
}
return result;
}
static void simpleline(JanetBuffer *buffer) {
@ -71,6 +86,12 @@ static void simpleline(JanetBuffer *buffer) {
}
}
static Janet janet_set_repl_within_form(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
gbl_repl_within_form = janet_unwrap_boolean(argv[0]) != 0;
return janet_wrap_nil();
}
/* Windows */
#ifdef JANET_WINDOWS
@ -746,8 +767,15 @@ static int line() {
kleft();
break;
case 3: /* ctrl-c */
errno = EAGAIN;
gbl_sigint_flag = 1;
if (
(gbl_len == 0)
&& !gbl_repl_within_form
) { /* quit on empty line */
errno = EAGAIN;
gbl_sigint_flag = 1;
} else { /* interrupt expression on non-empty line */
gbl_cancel_current_repl_form = true;
}
clearlines();
return -1;
case 4: /* ctrl-d, eof */
@ -947,12 +975,16 @@ void janet_line_get(const char *p, JanetBuffer *buffer) {
}
if (line()) {
norawmode();
if (gbl_sigint_flag) {
raise(SIGINT);
} else {
if (gbl_cancel_current_repl_form) {
fputc('\n', out);
} else {
if (gbl_sigint_flag) {
raise(SIGINT);
} else {
fputc('\n', out);
}
return;
}
return;
}
fflush(stdin);
norawmode();
@ -991,6 +1023,7 @@ int main(int argc, char **argv) {
/* Replace original getline with new line getter */
JanetTable *replacements = janet_table(0);
janet_table_put(replacements, janet_csymbolv("getline"), janet_wrap_cfunction(janet_line_getter));
janet_table_put(replacements, janet_csymbolv("set-repl-within-form"), janet_wrap_cfunction(janet_set_repl_within_form));
janet_line_init();
/* Get core env */