From 6be81ffa361fb4c60cf880b6e406a07e9e7526fb Mon Sep 17 00:00:00 2001 From: cozis Date: Tue, 2 Nov 2021 06:43:24 +0000 Subject: [PATCH] added debugger mode --- build.sh | 2 +- src/compiler/parse.c | 4 +- src/main.c | 147 +++++++++++++++++++++++++++++++++++++++++- src/runtime/o_func.c | 2 +- src/runtime/runtime.c | 18 +++++- src/runtime/runtime.h | 2 +- 6 files changed, 167 insertions(+), 8 deletions(-) diff --git a/build.sh b/build.sh index fdac6b1..87cdbe2 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ -USING_VALGRIND=1 +USING_VALGRIND=0 FLAGS="-L3p/libs/ -I3p/include/ -Wall -Wextra -g -DUSING_VALGRIND=$USING_VALGRIND -DTRACE_SOURCE_COPIES=$TRACE_SOURCE_COPIES" diff --git a/src/compiler/parse.c b/src/compiler/parse.c index 7db497d..0fe9640 100644 --- a/src/compiler/parse.c +++ b/src/compiler/parse.c @@ -1070,8 +1070,8 @@ static Node *parse_compound_statement(Context *ctx, TokenKind end) node->base.kind = NODE_COMP; node->base.next = NULL; - node->base.offset = head->offset; - node->base.length = end_offset - head->offset; + node->base.offset = head ? head->offset : 0; + node->base.length = head ? end_offset - head->offset : 0; node->head = head; } diff --git a/src/main.c b/src/main.c index e7ae31c..a499e43 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "compiler/parse.h" #include "compiler/serialize.h" #include "compiler/compile.h" @@ -28,6 +29,8 @@ static int parse_args(Options *opts, int argc, char **argv, Error *error) opts->action = RUN; opts->input_is_file = 1; + opts->debug = 0; + opts->verbose = 0; opts->input = NULL; opts->output = NULL; @@ -99,6 +102,148 @@ static int parse_args(Options *opts, int argc, char **argv, Error *error) return i; } +static _Bool debug_callback(Runtime *runtime, void *userp) +{ + assert(runtime != NULL); + assert(userp == NULL); + + #define ANSI_COLOR_RED "\x1b[31m" + #define ANSI_COLOR_GREEN "\x1b[32m" + #define ANSI_COLOR_YELLOW "\x1b[33m" + #define ANSI_COLOR_BLUE "\x1b[34m" + #define ANSI_COLOR_MAGENTA "\x1b[35m" + #define ANSI_COLOR_CYAN "\x1b[36m" + #define ANSI_COLOR_RESET "\x1b[0m" + + char buffer[256]; + int length; + + int argc; + char *argv[32]; + + do { + + fprintf(stderr, ANSI_COLOR_GREEN "> " ANSI_COLOR_RESET); + + { + length = 0; + + char c; + while((c = getc(stdin)) != '\n') + { + if(length < (int) sizeof(buffer)-1) + buffer[length] = c; + + length += 1; + } + + if(length > (int) sizeof(buffer)-1) + { + fprintf(stdout, + "Command is too long. The internal buffer can only contain %ld bytes.\n" + "Try inserting another command.\n", + sizeof(buffer)-1); + continue; + } + + buffer[length] = '\0'; + } + + // Now split the buffer into words in the (argc, argv) style. + + { + argc = 0; + + _Bool again = 0; + int curs = 0; + while(1) + { + // Skip whitespace. + while(isspace(buffer[curs])) + curs += 1; + + if(buffer[curs] == '\0') + break; + + int offset = curs; + + while(!isspace(buffer[curs]) && buffer[curs] != '\0') + curs += 1; + + int length = curs - offset; + + assert(length > 0); + + { + int max_argc = sizeof(argv) / sizeof(argv[0]); + + if(argc == max_argc) + { + fprintf(stdout, + "Command has too many words. The internal buffer can only contain %d words.\n" + "Try inserting another command.\n", + max_argc); + again = 1; + break; + } + + argv[argc++] = buffer + offset; + } + + if(buffer[curs] == '\0') + break; + + assert(isspace(buffer[curs])); + + buffer[curs] = '\0'; + + curs += 1; // Consume the space that ended the word (not overwritten by a zero byte). + } + + if(again) + continue; + } + + if(argc == 0) + continue; + + { + if(!strcmp(argv[0], "help")) + { + fprintf(stderr, + "help ...................... Show this message\n" + "step ...................... Run an instruction\n" + "quit ...................... Stop execution\n" + "continue .................. Run until a breakpoint or the end of the code is reached\n" + "breakpoint .. Add a breakpoint\n"); + } + else if(!strcmp(argv[0], "breakpoint")) + { + fprintf(stderr, "Not implemented yet.\n"); + } + else if(!strcmp(argv[0], "continue")) + { + fprintf(stderr, "Not implemented yet.\n"); + } + else if(!strcmp(argv[0], "step")) + { + break; + } + else if(!strcmp(argv[0], "quit")) + { + return 0; + } + else + { + fprintf(stdout, "Unknown command \"%s\". Type \"help\" is you need help.\n", argv[0]); + } + } + + } while(1); + + return 1; +} + int main(int argc, char **argv) { Error error; @@ -196,7 +341,7 @@ int main(int argc, char **argv) RuntimeError error; RuntimeError_Init(&error, runtime); - Object *result = run(runtime, (Error*) &error, exe, 0, NULL, 0); + Object *result = run(runtime, (Error*) &error, exe, 0, NULL, 0, NULL, opts.debug ? debug_callback : NULL); if(result == NULL || !Object_Print(result, stderr, (Error*) &error)) { diff --git a/src/runtime/o_func.c b/src/runtime/o_func.c index 3dc18b1..62dc6bf 100644 --- a/src/runtime/o_func.c +++ b/src/runtime/o_func.c @@ -30,7 +30,7 @@ static Object *call(Object *self, Object **argv, unsigned int argc, Heap *heap, assert(func->exe != NULL); assert(func->index >= 0); - return run(func->runtime, error, func->exe, func->index, argv, argc); + return run(func->runtime, error, func->exe, func->index, argv, argc, NULL, NULL); } Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, Heap *heap, Error *error) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index ae5d993..99b75de 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -560,7 +560,7 @@ static _Bool step(Runtime *runtime, Error *error) return 1; } -Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object **argv, int argc) +Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object **argv, int argc, void *userp, _Bool (*callback)(Runtime*, void*)) { assert(runtime != NULL); assert(error != NULL); @@ -598,7 +598,21 @@ Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object * goto cleanup; // Run the code. - while(step(runtime, error)); + + if(callback != NULL) + { + if(!callback(runtime, userp)) + Error_Report(error, 0, "Forced abortion"); + else + while(step(runtime, error)) + if(!callback(runtime, userp)) + { + Error_Report(error, 0, "Forced abortion"); + break; + } + } + else + while(step(runtime, error)); // This is what the function will return. Object *result = NULL; diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 5267e2c..096e778 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -16,7 +16,7 @@ Snapshot *Snapshot_New(Runtime *runtime); void Snapshot_Free(Snapshot *snapshot); void Snapshot_Print(Snapshot *snapshot, FILE *fp); -Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object **argv, int argc); +Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object **argv, int argc, void *userp, _Bool (*callback)(Runtime*, void*)); Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int offset, Heap *heap, Error *error); #endif \ No newline at end of file