From 71e98d31807df2beed77cbf628a2bdbb47ab5aaa Mon Sep 17 00:00:00 2001 From: m15o Date: Mon, 14 Nov 2022 07:53:57 +0100 Subject: [PATCH] different way to handle create --- src/const.h | 1 + src/ctx.h | 4 ++++ src/interpret.c | 32 +++++++++++++++++++++++++------ src/parse.c | 3 +++ src/runtime.c | 50 +++++++++++++++++++++++++++++++++++-------------- src/runtime.h | 2 +- 6 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/const.h b/src/const.h index 31935a6..9ffb639 100644 --- a/src/const.h +++ b/src/const.h @@ -35,6 +35,7 @@ typedef enum opcodes { ALO, HER, JMP, + DOS, LEN } opcode; diff --git a/src/ctx.h b/src/ctx.h index ddcc913..6f41386 100644 --- a/src/ctx.h +++ b/src/ctx.h @@ -3,6 +3,8 @@ #include "const.h" +enum types { WORD, CREATE }; + typedef struct stack { cell data[STACK_LEN]; int top; @@ -11,6 +13,8 @@ typedef struct stack { typedef struct entry { char name[TOKEN_LEN]; int ptr; + int rt; + enum types type; } Entry; typedef struct memory { diff --git a/src/interpret.c b/src/interpret.c index 5c3dfd7..1712b57 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -20,6 +20,10 @@ errc address_interpreter(Ctx *ctx, int ptr) { while ((op = ctx->dict.mem.data[(*m_ptr)++]) != EXT) { if ((e = exec(ctx, op)) != SUCCESS) return e; + if (op == DOS) { + op = EXT; + break; + } } return exec(ctx, op); @@ -28,8 +32,8 @@ errc address_interpreter(Ctx *ctx, int ptr) { /* exec: attempt to execute opcode */ errc exec(Ctx *ctx, opcode op) { int n; - errc e; - cell adr; + errc e; + cell adr; switch (op) { case PSH: @@ -72,13 +76,29 @@ errc exec(Ctx *ctx, opcode op) { return run_alo(ctx); case HER: return run_her(ctx); + case DOS: + return run_dos(ctx); case JMP: - if ((e = stack_pop(&ctx->pstack, &adr)) != SUCCESS) - return e; - return address_interpreter(ctx, adr); + 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, ctx->dict.entries[n].ptr); + if (ctx->dict.entries[n].type == CREATE) { + Entry *e; + + e = &ctx->dict.entries[n]; + + /* Push ptr of entry on stack */ + stack_push(&ctx->pstack, e->ptr); /* todo check errors */ + + /* Open address interpreter of rt if != ptr */ + if (e->ptr != e->rt) { + return address_interpreter(ctx, e->rt); + } + return SUCCESS; + } else + return address_interpreter(ctx, ctx->dict.entries[n].ptr); } break; } diff --git a/src/parse.c b/src/parse.c index 7fac923..f29eb80 100644 --- a/src/parse.c +++ b/src/parse.c @@ -76,6 +76,9 @@ errc parse_op(Ctx *ctx, opcode *op, char *token) { return SUCCESS; } else if (strcmp(token, "jmp") == 0) { *op = JMP; + return SUCCESS; + } else if (strcmp(token, "does>") == 0) { + *op = DOS; return SUCCESS; } diff --git a/src/runtime.c b/src/runtime.c index 3ca98f0..711caab 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -1,5 +1,4 @@ #include "runtime.h" -/* #include "interpret.h" */ #include "const.h" #include "parse.h" #include "screen.h" @@ -46,9 +45,11 @@ errc run_cre(Ctx *ctx) { /* ( -- ) */ if (token_next(token) == EOF) return ERR_WORD_NOT_FOUND; - printf(": %s\n", token); + printf("CREATE %s\n", token); strncpy(d->entries[d->top].name, token, TOKEN_LEN); - d->entries[d->top++].ptr = d->mem.top; + d->entries[d->top].type = CREATE; + d->entries[d->top].ptr = d->mem.top; + d->entries[d->top++].rt = d->mem.top; return SUCCESS; } @@ -80,10 +81,17 @@ errc def_num(Ctx *ctx, char *token) { /* run_def: compile a definition */ errc run_def(Ctx *ctx) { /* ( -- ) */ char token[TOKEN_LEN]; + Dict *d = &ctx->dict; errc e; - if ((e = run_cre(ctx)) != SUCCESS) - return e; + if (token_next(token) == EOF) + return ERR_WORD_NOT_FOUND; + + printf(": %s\n", token); + strncpy(d->entries[d->top].name, token, TOKEN_LEN); + d->entries[d->top].type = WORD; + d->entries[d->top].ptr = d->mem.top; + d->entries[d->top++].rt = d->mem.top; while (token_next(token) != EOF && strcmp(token, ";") != 0) { if ((e = def_op(ctx, token) != SUCCESS)) @@ -98,7 +106,6 @@ errc run_def(Ctx *ctx) { /* ( -- ) */ /* run_push: push following cell to pstack */ errc run_psh(Ctx *ctx) { /* ( -- n ) */ Memory *m = &ctx->dict.mem; - cell n; return stack_push(&ctx->pstack, m->data[ctx->m_ptr++]); } @@ -339,7 +346,8 @@ errc run_com(Ctx *ctx) { /* ( n -- ) */ if ((e = stack_pop(&ctx->pstack, &n)) != SUCCESS) return e; - return mem_write_num(m, n);; + return mem_write(m, n); + ; } /* run_her: 'here' push mem_top to pstack */ @@ -361,12 +369,26 @@ errc run_alo(Ctx *ctx) { /* ( n -- ) */ return SUCCESS; } -/* errc run_jmp(Ctx *ctx) { /\* ( adr -- ) *\/ */ -/* cell adr; */ -/* errc e; */ +errc run_dos(Ctx *ctx) { /* ( -- ) */ + Dict *d = &ctx->dict; + d->entries[d->top - 1].rt = ctx->m_ptr; + printf("Update rt of %s to %d\n", d->entries[d->top - 1].name, + d->entries[d->top - 1].rt); + + /* Dict *d = &ctx->dict; */ + /* Memory *m = &ctx->dict.mem; */ + /* cell prev_ptr; */ + /* errc e; */ + + /* prev_ptr = d->entries[d->top - 1].ptr; */ + /* d->entries[d->top - 1].ptr = m->top; */ -/* if ((e = stack_pop(&ctx->pstack, &adr)) != SUCCESS) */ -/* return e; */ + /* if ((e = mem_write_num(m, prev_ptr)) != SUCCESS) */ + /* return e; */ -/* return SUCCESS; */ -/* } */ + /* if ((e = mem_write_num(m, ctx->m_ptr)) != SUCCESS) */ + /* return e; */ + + /* return mem_write(m, JMP); */ + return SUCCESS; +} diff --git a/src/runtime.h b/src/runtime.h index 409f272..f291374 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -12,7 +12,6 @@ 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 ) */ @@ -27,5 +26,6 @@ errc run_com(Ctx *ctx); /* ( n -- ) */ errc run_ext(Ctx *ctx); /* ( -- ) */ errc run_cre(Ctx *ctx); /* ( -- ) */ errc run_alo(Ctx *ctx); /* ( n -- ) */ +errc run_dos(Ctx *ctx); /* ( -- ) */ #endif -- 2.38.4