From 4fe7c555ad83670a930535db3075642ce6b39d67 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 28 May 2024 16:45:01 +0200 Subject: [PATCH] Clearing the superfluous things in the stack on launch --- lib.c | 41 ++++++++++++++++++++++++++++++------ test/test06.nk | 56 +------------------------------------------------- 2 files changed, 36 insertions(+), 61 deletions(-) diff --git a/lib.c b/lib.c index f2677c4..222c137 100644 --- a/lib.c +++ b/lib.c @@ -821,6 +821,7 @@ void ink_compile(struct context *pContext, const char* buffer) { frame.executing.type = INK_FUNCTION; frame.index = 0; err = ink_push_fn(pContext, frame); + pContext->routines[pContext->routine_current].top = 0; if (err < 0) { pContext->panic = 1; return; @@ -1523,12 +1524,39 @@ static void run_gc(struct context* ctx) { } static void clear_stack(struct context* ctx) { - struct ink_routine* currentRoutine; - currentRoutine = ctx->routines + ctx->routine_current; - while (currentRoutine->top >= 1) { - ink_pop(ctx); - } - return; + struct ink_routine* currentRoutine; + currentRoutine = ctx->routines + ctx->routine_current; + while (currentRoutine->top >= 1) { + ink_pop(ctx); + } + return; +} + +static void dump_stack(struct context* ctx) { + struct ink_routine* currentRoutine; + int index; + char* idx; + char* type; + char* value; + char* it; + currentRoutine = ctx->routines + ctx->routine_current; + index = currentRoutine->top; + while (index) { + --index; + idx = ink_itoa(ctx,index); + type = ink_itoa(ctx, currentRoutine->stack[index].type); + value = ink_itoa(ctx,currentRoutine->stack[index].value); + for(it = idx; *it; ++it) ctx->putchar(*it); + ctx->putchar(' ');ctx->putchar('|');ctx->putchar(' '); + for(it = type; *it; ++it) ctx->putchar(*it); + ctx->putchar(' ');ctx->putchar('|');ctx->putchar(' '); + for(it = value; *it; ++it) ctx->putchar(*it); + ctx->putchar('\n'); + ctx->free(value); + ctx->free(type); + ctx->free(idx); + } + return; } static void collect_noop(struct context* _1, void* _2) {} @@ -1885,6 +1913,7 @@ int ink_std_library(struct context* ctx) { v += ink_add_native(ctx, "dup", dupe_elem); v += ink_add_native(ctx, "drop", drop_elem); v += ink_add_native(ctx, "stack.clear", clear_stack); + v += ink_add_native(ctx, "stack.dump", dump_stack); v += ink_add_native(ctx, "pluck", pluck_elem); v += ink_add_native(ctx, "return_if", return_if); v += ink_add_native(ctx, "jump_if", jump_if); diff --git a/test/test06.nk b/test/test06.nk index 7624c4c..ccd22b9 100644 --- a/test/test06.nk +++ b/test/test06.nk @@ -1,59 +1,5 @@ # This says Hello World [ 72 101 108 108 111 32 87 111 114 108 100 10 ] - +stack.dump # This prints the array above - -array.print_utf8 - -[ 72 101 108 108 111 32 87 111 114 108 100 10 ] -# original -dup array.clone dup -# original cloned cloned -32 128 encrypt -# original cloned -string.dump -10 print_utf8 -string.dump - -# We encrypt 6 times in a row -[ 84 104 105 115 32 105 115 32 97 110 32 101 120 97 109 112 108 101 32 111 102 32 116 101 120 116 32 116 111 32 98 101 32 101 110 99 114 121 112 116 101 100 32 116 119 105 99 101 46 ] -dup array.clone -dup -33 128 encrypt -dup -57 128 encrypt -dup -91 128 encrypt -dup -33 128 encrypt -dup -57 128 encrypt -dup -91 128 encrypt - -# Print encrypted -dup -10 print_utf8 -string.dump - -# We decrypt in reverse order with a key that is the modular inverse of the original (adjacent to subtraction) -dup -128 91 - 128 encrypt -dup -128 57 - 128 encrypt -dup -128 33 - 128 encrypt -dup -128 91 - 128 encrypt -dup -128 57 - 128 encrypt -dup -128 33 - 128 encrypt - -# Print encrypted then decrypted -10 print_utf8 -string.dump -# Print never encrypted -10 print_utf8 -string.dump