ソースを参照

Clearing the superfluous things in the stack on launch

main
Ludovic 'Archivist' Lagouardette 5ヶ月前
コミット
4fe7c555ad
2個のファイルの変更36行の追加61行の削除
  1. +35
    -6
      lib.c
  2. +1
    -55
      test/test06.nk

+ 35
- 6
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);

+ 1
- 55
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

読み込み中…
キャンセル
保存