mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 17:27:18 +00:00
Get expected behaviour; cleanup after confirming behaviour is desired
This commit is contained in:
parent
a99906c6f0
commit
164ed0b325
@ -33,6 +33,12 @@
|
|||||||
extern const unsigned char *janet_gen_boot;
|
extern const unsigned char *janet_gen_boot;
|
||||||
extern int32_t janet_gen_boot_size;
|
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) {
|
int main(int argc, const char **argv) {
|
||||||
|
|
||||||
/* Init janet */
|
/* Init janet */
|
||||||
@ -53,6 +59,8 @@ int main(int argc, const char **argv) {
|
|||||||
|
|
||||||
env = janet_core_env(NULL);
|
env = janet_core_env(NULL);
|
||||||
|
|
||||||
|
janet_def(env, "set-repl-within-form", janet_wrap_cfunction(janet_set_repl_within_form), "");
|
||||||
|
|
||||||
/* Create args tuple */
|
/* Create args tuple */
|
||||||
JanetArray *args = janet_array(argc);
|
JanetArray *args = janet_array(argc);
|
||||||
for (int i = 0; i < argc; i++)
|
for (int i = 0; i < argc; i++)
|
||||||
|
@ -1978,7 +1978,7 @@
|
|||||||
(var going true)
|
(var going true)
|
||||||
|
|
||||||
# The parser object
|
# The parser object
|
||||||
(def p (parser/new))
|
(var p (parser/new))
|
||||||
|
|
||||||
# Evaluate 1 source form in a protected manner
|
# Evaluate 1 source form in a protected manner
|
||||||
(defn eval1 [source]
|
(defn eval1 [source]
|
||||||
@ -2017,19 +2017,30 @@
|
|||||||
(while going
|
(while going
|
||||||
(if (env :exit) (break))
|
(if (env :exit) (break))
|
||||||
(buffer/clear buf)
|
(buffer/clear buf)
|
||||||
(chunks buf p)
|
(if (nil? (chunks buf p))
|
||||||
(var pindex 0)
|
(do
|
||||||
(var pstatus nil)
|
# Nil chunks represents a cancelled form in the REPL, so reset.
|
||||||
(def len (length buf))
|
(set-repl-within-form false)
|
||||||
(when (= len 0)
|
(set p (parser/new))
|
||||||
(parser/eof p)
|
(buffer/clear buf))
|
||||||
(set going false))
|
(do
|
||||||
(while (> len pindex)
|
(var pindex 0)
|
||||||
(+= pindex (parser/consume p buf pindex))
|
(var pstatus nil)
|
||||||
(while (parser/has-more p)
|
(def len (length buf))
|
||||||
(eval1 (parser/produce p)))
|
(when (= len 0)
|
||||||
(when (= (parser/status p) :error)
|
(parser/eof p)
|
||||||
(parse-err p where))))
|
(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
|
# Check final parser state
|
||||||
(while (parser/has-more p)
|
(while (parser/has-more p)
|
||||||
(eval1 (parser/produce p)))
|
(eval1 (parser/produce p)))
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <janet.h>
|
#include <janet.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
@ -39,6 +40,10 @@ void janet_line_deinit();
|
|||||||
|
|
||||||
void janet_line_get(const char *p, JanetBuffer *buffer);
|
void janet_line_get(const char *p, JanetBuffer *buffer);
|
||||||
Janet janet_line_getter(int32_t argc, Janet *argv);
|
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
|
* 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;
|
gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL;
|
||||||
janet_line_get(str, buf);
|
janet_line_get(str, buf);
|
||||||
gbl_complete_env = NULL;
|
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) {
|
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 */
|
/* Windows */
|
||||||
#ifdef JANET_WINDOWS
|
#ifdef JANET_WINDOWS
|
||||||
|
|
||||||
@ -746,8 +767,15 @@ static int line() {
|
|||||||
kleft();
|
kleft();
|
||||||
break;
|
break;
|
||||||
case 3: /* ctrl-c */
|
case 3: /* ctrl-c */
|
||||||
errno = EAGAIN;
|
if (
|
||||||
gbl_sigint_flag = 1;
|
(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();
|
clearlines();
|
||||||
return -1;
|
return -1;
|
||||||
case 4: /* ctrl-d, eof */
|
case 4: /* ctrl-d, eof */
|
||||||
@ -947,12 +975,16 @@ void janet_line_get(const char *p, JanetBuffer *buffer) {
|
|||||||
}
|
}
|
||||||
if (line()) {
|
if (line()) {
|
||||||
norawmode();
|
norawmode();
|
||||||
if (gbl_sigint_flag) {
|
if (gbl_cancel_current_repl_form) {
|
||||||
raise(SIGINT);
|
|
||||||
} else {
|
|
||||||
fputc('\n', out);
|
fputc('\n', out);
|
||||||
|
} else {
|
||||||
|
if (gbl_sigint_flag) {
|
||||||
|
raise(SIGINT);
|
||||||
|
} else {
|
||||||
|
fputc('\n', out);
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
fflush(stdin);
|
fflush(stdin);
|
||||||
norawmode();
|
norawmode();
|
||||||
@ -991,6 +1023,7 @@ int main(int argc, char **argv) {
|
|||||||
/* Replace original getline with new line getter */
|
/* Replace original getline with new line getter */
|
||||||
JanetTable *replacements = janet_table(0);
|
JanetTable *replacements = janet_table(0);
|
||||||
janet_table_put(replacements, janet_csymbolv("getline"), janet_wrap_cfunction(janet_line_getter));
|
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();
|
janet_line_init();
|
||||||
|
|
||||||
/* Get core env */
|
/* Get core env */
|
||||||
|
Loading…
Reference in New Issue
Block a user