1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Add preliminary debugger to default repl.

Also upddate colors, and fix formatting.
This commit is contained in:
Calvin Rose 2019-05-02 17:11:30 -04:00
parent 03e3ecb0a1
commit 1cfc7b3b0d
5 changed files with 167 additions and 56 deletions

View File

@ -1724,15 +1724,33 @@
get a chunk of source code that should return nil for end of file.
The second parameter is a function that is called when a signal is
caught."
[&opt chunks onsignal]
(def newenv (make-env))
[&opt chunks onsignal env]
(def level (+ (dyn :debug-level 0) 1))
(default env (make-env))
(default onsignal (fn [f x]
(case (fiber/status f)
:dead (do
(pp x)
(put newenv '_ @{:value x}))
(debug/stacktrace f x))))
(run-context {:env newenv
(put env '_ @{:value x}))
(let [nextenv (make-env env)]
(put nextenv '_fiber @{:value f})
(put nextenv '_signal @{:value x})
(setdyn :debug-level level)
(debug/stacktrace f x)
(print ```
entering debugger - Ctrl-D to exit
_fiber is bound to the suspended fiber
_signal is the error or signal value
```)
(repl (fn [buf p]
(def status (parser/state p))
(def c (parser/where p))
(def prompt (string "debug[" level "]:" c ":" status "> "))
(getline prompt buf))
onsignal nextenv)))))
(run-context {:env env
:chunks chunks
:on-status onsignal
:source "repl"}))

View File

@ -83,7 +83,8 @@ static const JanetAbstractType it_u64_type = {
int64_t janet_unwrap_s64(Janet x) {
switch (janet_type(x)) {
default: break;
default:
break;
case JANET_NUMBER : {
double dbl = janet_unwrap_number(x);
if (fabs(dbl) <= MAX_INT_IN_DBL)
@ -111,7 +112,8 @@ int64_t janet_unwrap_s64(Janet x) {
uint64_t janet_unwrap_u64(Janet x) {
switch (janet_type(x)) {
default: break;
default:
break;
case JANET_NUMBER : {
double dbl = janet_unwrap_number(x);
if ((dbl >= 0) && (dbl <= MAX_INT_IN_DBL))

View File

@ -314,23 +314,24 @@ static void print_newline(struct pretty *S, int just_a_space) {
}
/* Color coding for types */
static const char janet_cycle_color[] = "\x1B[36m";
static const char *janet_pretty_colors[] = {
"\x1B[32m",
"\x1B[36m",
"\x1B[36m",
NULL,
"\x1B[36m",
"\x1B[35m",
"\x1B[34m",
"\x1B[33m",
NULL,
NULL,
NULL,
NULL,
"\x1B[36m",
"\x1B[36m",
"\x1B[36m",
"\x1B[36m"
"\x1B[35m",
NULL,
NULL,
NULL,
NULL
"\x1B[36m",
"\x1B[36m",
"\x1B[36m",
"\x1B[36m"
};
#define JANET_PRETTY_DICT_ONELINE 4
@ -348,9 +349,15 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) {
default: {
Janet seenid = janet_table_get(&S->seen, x);
if (janet_checktype(seenid, JANET_NUMBER)) {
if (S->flags & JANET_PRETTY_COLOR) {
janet_buffer_push_cstring(S->buffer, janet_cycle_color);
}
janet_buffer_push_cstring(S->buffer, "<cycle ");
integer_to_string_b(S->buffer, janet_unwrap_integer(seenid));
janet_buffer_push_u8(S->buffer, '>');
if (S->flags & JANET_PRETTY_COLOR) {
janet_buffer_push_cstring(S->buffer, "\x1B[0m");
}
return;
} else {
janet_table_put(&S->seen, x, janet_wrap_integer(S->seen.count));

View File

@ -27,54 +27,132 @@
/* Macro fills */
JanetType (janet_type)(Janet x) { return janet_type(x); }
int (janet_checktype)(Janet x, JanetType type) { return janet_checktype(x, type); }
int (janet_checktypes)(Janet x, int typeflags) { return janet_checktypes(x, typeflags); }
int (janet_truthy)(Janet x) { return janet_truthy(x); }
JanetType(janet_type)(Janet x) {
return janet_type(x);
}
int (janet_checktype)(Janet x, JanetType type) {
return janet_checktype(x, type);
}
int (janet_checktypes)(Janet x, int typeflags) {
return janet_checktypes(x, typeflags);
}
int (janet_truthy)(Janet x) {
return janet_truthy(x);
}
const JanetKV *(janet_unwrap_struct)(Janet x) { return janet_unwrap_struct(x); }
const Janet *(janet_unwrap_tuple)(Janet x) { return janet_unwrap_tuple(x); }
JanetFiber *(janet_unwrap_fiber)(Janet x) { return janet_unwrap_fiber(x); }
JanetArray *(janet_unwrap_array)(Janet x) { return janet_unwrap_array(x); }
JanetTable *(janet_unwrap_table)(Janet x) { return janet_unwrap_table(x); }
JanetBuffer *(janet_unwrap_buffer)(Janet x) { return janet_unwrap_buffer(x); }
const uint8_t *(janet_unwrap_string)(Janet x) { return janet_unwrap_string(x); }
const uint8_t *(janet_unwrap_symbol)(Janet x) { return janet_unwrap_symbol(x); }
const uint8_t *(janet_unwrap_keyword)(Janet x) { return janet_unwrap_keyword(x); }
void *(janet_unwrap_abstract)(Janet x) { return janet_unwrap_abstract(x); }
void *(janet_unwrap_pointer)(Janet x) { return janet_unwrap_pointer(x); }
JanetFunction *(janet_unwrap_function)(Janet x) { return janet_unwrap_function(x); }
JanetCFunction (janet_unwrap_cfunction)(Janet x) { return janet_unwrap_cfunction(x); }
int (janet_unwrap_boolean)(Janet x) { return janet_unwrap_boolean(x); }
int32_t (janet_unwrap_integer)(Janet x) { return janet_unwrap_integer(x); }
const JanetKV *(janet_unwrap_struct)(Janet x) {
return janet_unwrap_struct(x);
}
const Janet *(janet_unwrap_tuple)(Janet x) {
return janet_unwrap_tuple(x);
}
JanetFiber *(janet_unwrap_fiber)(Janet x) {
return janet_unwrap_fiber(x);
}
JanetArray *(janet_unwrap_array)(Janet x) {
return janet_unwrap_array(x);
}
JanetTable *(janet_unwrap_table)(Janet x) {
return janet_unwrap_table(x);
}
JanetBuffer *(janet_unwrap_buffer)(Janet x) {
return janet_unwrap_buffer(x);
}
const uint8_t *(janet_unwrap_string)(Janet x) {
return janet_unwrap_string(x);
}
const uint8_t *(janet_unwrap_symbol)(Janet x) {
return janet_unwrap_symbol(x);
}
const uint8_t *(janet_unwrap_keyword)(Janet x) {
return janet_unwrap_keyword(x);
}
void *(janet_unwrap_abstract)(Janet x) {
return janet_unwrap_abstract(x);
}
void *(janet_unwrap_pointer)(Janet x) {
return janet_unwrap_pointer(x);
}
JanetFunction *(janet_unwrap_function)(Janet x) {
return janet_unwrap_function(x);
}
JanetCFunction(janet_unwrap_cfunction)(Janet x) {
return janet_unwrap_cfunction(x);
}
int (janet_unwrap_boolean)(Janet x) {
return janet_unwrap_boolean(x);
}
int32_t (janet_unwrap_integer)(Janet x) {
return janet_unwrap_integer(x);
}
#if defined(JANET_NANBOX_32) || defined(JANET_NANBOX_64)
Janet (janet_wrap_nil)(void) { return janet_wrap_nil(); }
Janet (janet_wrap_true)(void) { return janet_wrap_true(); }
Janet (janet_wrap_false)(void) { return janet_wrap_false(); }
Janet (janet_wrap_boolean)(int x) { return janet_wrap_boolean(x); }
Janet (janet_wrap_string)(const uint8_t *x) { return janet_wrap_string(x); }
Janet (janet_wrap_symbol)(const uint8_t *x) { return janet_wrap_symbol(x); }
Janet (janet_wrap_keyword)(const uint8_t *x) { return janet_wrap_keyword(x); }
Janet (janet_wrap_array)(JanetArray *x) { return janet_wrap_array(x); }
Janet (janet_wrap_tuple)(const Janet *x) { return janet_wrap_tuple(x); }
Janet (janet_wrap_struct)(const JanetKV *x) { return janet_wrap_struct(x); }
Janet (janet_wrap_fiber)(JanetFiber *x) { return janet_wrap_fiber(x); }
Janet (janet_wrap_buffer)(JanetBuffer *x) { return janet_wrap_buffer(x); }
Janet (janet_wrap_function)(JanetFunction *x) { return janet_wrap_function(x); }
Janet (janet_wrap_cfunction)(JanetCFunction x) { return janet_wrap_cfunction(x); }
Janet (janet_wrap_table)(JanetTable *x) { return janet_wrap_table(x); }
Janet (janet_wrap_abstract)(void *x) { return janet_wrap_abstract(x); }
Janet (janet_wrap_pointer)(void *x) { return janet_wrap_pointer(x); }
Janet (janet_wrap_integer)(int32_t x) { return janet_wrap_integer(x); }
Janet(janet_wrap_nil)(void) {
return janet_wrap_nil();
}
Janet(janet_wrap_true)(void) {
return janet_wrap_true();
}
Janet(janet_wrap_false)(void) {
return janet_wrap_false();
}
Janet(janet_wrap_boolean)(int x) {
return janet_wrap_boolean(x);
}
Janet(janet_wrap_string)(const uint8_t *x) {
return janet_wrap_string(x);
}
Janet(janet_wrap_symbol)(const uint8_t *x) {
return janet_wrap_symbol(x);
}
Janet(janet_wrap_keyword)(const uint8_t *x) {
return janet_wrap_keyword(x);
}
Janet(janet_wrap_array)(JanetArray *x) {
return janet_wrap_array(x);
}
Janet(janet_wrap_tuple)(const Janet *x) {
return janet_wrap_tuple(x);
}
Janet(janet_wrap_struct)(const JanetKV *x) {
return janet_wrap_struct(x);
}
Janet(janet_wrap_fiber)(JanetFiber *x) {
return janet_wrap_fiber(x);
}
Janet(janet_wrap_buffer)(JanetBuffer *x) {
return janet_wrap_buffer(x);
}
Janet(janet_wrap_function)(JanetFunction *x) {
return janet_wrap_function(x);
}
Janet(janet_wrap_cfunction)(JanetCFunction x) {
return janet_wrap_cfunction(x);
}
Janet(janet_wrap_table)(JanetTable *x) {
return janet_wrap_table(x);
}
Janet(janet_wrap_abstract)(void *x) {
return janet_wrap_abstract(x);
}
Janet(janet_wrap_pointer)(void *x) {
return janet_wrap_pointer(x);
}
Janet(janet_wrap_integer)(int32_t x) {
return janet_wrap_integer(x);
}
#endif
#ifndef JANET_NANBOX_32
double (janet_unwrap_number)(Janet x) { return janet_unwrap_number(x); }
double (janet_unwrap_number)(Janet x) {
return janet_unwrap_number(x);
}
#endif
#ifdef JANET_NANBOX_64
Janet (janet_wrap_number)(double x) { return janet_wrap_number(x); }
Janet(janet_wrap_number)(double x) {
return janet_wrap_number(x);
}
#endif
/*****/

View File

@ -94,6 +94,7 @@ static int cols = 80;
static char *history[JANET_HISTORY_MAX];
static int history_count = 0;
static int historyi = 0;
static int sigint_flag = 0;
static struct termios termios_start;
/* Unsupported terminal list from linenoise */
@ -333,6 +334,7 @@ static int line() {
return 0;
case 3: /* ctrl-c */
errno = EAGAIN;
sigint_flag = 1;
return -1;
case 127: /* backspace */
case 8: /* ctrl-h */
@ -458,7 +460,11 @@ void janet_line_get(const char *p, JanetBuffer *buffer) {
}
if (line()) {
norawmode();
fputc('\n', stdout);
if (sigint_flag) {
raise(SIGINT);
} else {
fputc('\n', stdout);
}
return;
}
norawmode();