From e8dba58eb42117846d65a837a5742ba7a5e58be5 Mon Sep 17 00:00:00 2001 From: m15o Date: Sun, 13 Nov 2022 16:40:19 +0100 Subject: [PATCH] adding jmp --- src/const.h | 1 + src/interpret.c | 12 +++++++++--- src/interpret.h | 2 +- src/parse.c | 3 +++ src/runtime.c | 11 +++++++++++ src/runtime.h | 1 + 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/const.h b/src/const.h index 896613c..31935a6 100644 --- a/src/const.h +++ b/src/const.h @@ -34,6 +34,7 @@ typedef enum opcodes { COM, ALO, HER, + JMP, LEN } opcode; diff --git a/src/interpret.c b/src/interpret.c index de36bf6..5c3dfd7 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -7,7 +7,7 @@ errc exec(Ctx *ctx, opcode op); -errc address_interpreter(Ctx *ctx, int n) { +errc address_interpreter(Ctx *ctx, int ptr) { opcode op; errc e; int *m_ptr = &ctx->m_ptr; @@ -15,7 +15,7 @@ errc address_interpreter(Ctx *ctx, int n) { if ((e = stack_push(&ctx->rstack, *m_ptr)) != SUCCESS) return e; - *m_ptr = ctx->dict.entries[n].ptr; + *m_ptr = ptr; while ((op = ctx->dict.mem.data[(*m_ptr)++]) != EXT) { if ((e = exec(ctx, op)) != SUCCESS) @@ -28,6 +28,8 @@ errc address_interpreter(Ctx *ctx, int n) { /* exec: attempt to execute opcode */ errc exec(Ctx *ctx, opcode op) { int n; + errc e; + cell adr; switch (op) { case PSH: @@ -70,9 +72,13 @@ errc exec(Ctx *ctx, opcode op) { return run_alo(ctx); case HER: return run_her(ctx); + case JMP: + if ((e = stack_pop(&ctx->pstack, &adr)) != SUCCESS) + return e; + return address_interpreter(ctx, adr); default: if ((n = op - LEN) >= 0 && n <= ctx->dict.top) { - return address_interpreter(ctx, n); + return address_interpreter(ctx, ctx->dict.entries[n].ptr); } break; } diff --git a/src/interpret.h b/src/interpret.h index 20c4ec1..ad83d4b 100644 --- a/src/interpret.h +++ b/src/interpret.h @@ -4,6 +4,6 @@ #include "ctx.h" errc interpret(Ctx *ctx); -errc address_interpreter(Ctx *ctx, int n); +errc address_interpreter(Ctx *ctx, int ptr); #endif diff --git a/src/parse.c b/src/parse.c index b313ac2..7fac923 100644 --- a/src/parse.c +++ b/src/parse.c @@ -73,6 +73,9 @@ errc parse_op(Ctx *ctx, opcode *op, char *token) { return SUCCESS; } else if (strcmp(token, "here") == 0) { *op = HER; + return SUCCESS; + } else if (strcmp(token, "jmp") == 0) { + *op = JMP; return SUCCESS; } diff --git a/src/runtime.c b/src/runtime.c index 55574d3..3ca98f0 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -1,4 +1,5 @@ #include "runtime.h" +/* #include "interpret.h" */ #include "const.h" #include "parse.h" #include "screen.h" @@ -359,3 +360,13 @@ errc run_alo(Ctx *ctx) { /* ( n -- ) */ m->top += n; return SUCCESS; } + +/* errc run_jmp(Ctx *ctx) { /\* ( adr -- ) *\/ */ +/* cell adr; */ +/* errc e; */ + +/* if ((e = stack_pop(&ctx->pstack, &adr)) != SUCCESS) */ +/* return e; */ + +/* return SUCCESS; */ +/* } */ diff --git a/src/runtime.h b/src/runtime.h index c951bf5..409f272 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -12,6 +12,7 @@ errc run_lod(Ctx *ctx); /* ( addr -- val ) */ errc run_sto(Ctx *ctx); /* ( n addr -- ) */ errc run_emt(Ctx *ctx); /* ( c -- ) */ errc run_clr(Ctx *ctx); /* ( r g b -- ) */ +/* errc run_jmp(Ctx *ctx); /\* ( adr -- ) *\/ */ /* stack manipulation */ errc run_swp(Ctx *ctx); /* ( n1 n2 -- n2 n1 ) */ -- 2.38.5