~m15o/15f

71e98d31807df2beed77cbf628a2bdbb47ab5aaa — m15o 1 year, 6 months ago e8dba58
different way to handle create
6 files changed, 71 insertions(+), 21 deletions(-)

M src/const.h
M src/ctx.h
M src/interpret.c
M src/parse.c
M src/runtime.c
M src/runtime.h
M src/const.h => src/const.h +1 -0
@@ 35,6 35,7 @@ typedef enum opcodes {
	ALO,
	HER,
	JMP,
	DOS,
  LEN
} opcode;


M src/ctx.h => src/ctx.h +4 -0
@@ 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 {

M src/interpret.c => src/interpret.c +26 -6
@@ 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;
  }

M src/parse.c => src/parse.c +3 -0
@@ 77,6 77,9 @@ errc parse_op(Ctx *ctx, opcode *op, char *token) {
  } else if (strcmp(token, "jmp") == 0) {
    *op = JMP;
    return SUCCESS;
  } else if (strcmp(token, "does>") == 0) {
    *op = DOS;
    return SUCCESS;
	}

  /* check in dictionary */

M src/runtime.c => src/runtime.c +36 -14
@@ 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;
}

M src/runtime.h => src/runtime.h +1 -1
@@ 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