~m15o/15f

ef1ed5446312af9597ce6dba792678a78388d07c — m15o 4 months ago 9158ca4 master
immediate
7 files changed, 54 insertions(+), 18 deletions(-)

M src/const.h
M src/ctx.h
M src/interpret.c
M src/interpret.h
M src/parse.c
M src/runtime.c
M src/runtime.h
M src/const.h => src/const.h +2 -0
@@ 36,6 36,8 @@ typedef enum opcodes {
	HER,
	JMP,
	DOS,
	IMM,
	CMP,
  LEN
} opcode;


M src/ctx.h => src/ctx.h +1 -0
@@ 15,6 15,7 @@ typedef struct entry {
  int ptr;
	int rt;
	enum types type;
	int immediate;
} Entry;

typedef struct memory {

M src/interpret.c => src/interpret.c +7 -2
@@ 78,6 78,10 @@ errc exec(Ctx *ctx, opcode op) {
    return run_her(ctx);
  case DOS:
    return run_dos(ctx);
  case IMM:
    return run_imm(ctx);
  case CMP:
    return run_cmp(ctx);		
  case JMP:
    if ((e = stack_pop(&ctx->pstack, &adr)) != SUCCESS)
      return e;


@@ 95,10 99,11 @@ errc exec(Ctx *ctx, opcode op) {
        /* Open address interpreter of rt if != ptr */
        if (e->ptr != e->rt) {
          return address_interpreter(ctx, e->rt);
				}
        }
        return SUCCESS;
      } else
      } else {
        return address_interpreter(ctx, ctx->dict.entries[n].ptr);
			}
    }
    break;
  }

M src/interpret.h => src/interpret.h +1 -0
@@ 5,5 5,6 @@

errc interpret(Ctx *ctx);
errc address_interpreter(Ctx *ctx, int ptr);
errc exec(Ctx *ctx, opcode op);

#endif

M src/parse.c => src/parse.c +7 -1
@@ 80,7 80,13 @@ errc parse_op(Ctx *ctx, opcode *op, char *token) {
  } else if (strcmp(token, "does>") == 0) {
    *op = DOS;
    return SUCCESS;
	}
  } else if (strcmp(token, "immediate") == 0) {
    *op = IMM;
    return SUCCESS;
  } else if (strcmp(token, "compile") == 0) {
    *op = CMP;
    return SUCCESS;
  }

  /* check in dictionary */
  if ((n = dict_find(&ctx->dict, token)) != -1) {

M src/runtime.c => src/runtime.c +34 -15
@@ 1,5 1,6 @@
#include "runtime.h"
#include "const.h"
#include "interpret.h"
#include "parse.h"
#include "screen.h"
#include "token.h"


@@ 45,10 46,10 @@ errc run_cre(Ctx *ctx) { /* ( -- ) */
  if (token_next(token) == EOF)
    return ERR_WORD_NOT_FOUND;

  printf("CREATE %s\n", token);
  strncpy(d->entries[d->top].name, token, TOKEN_LEN);
  d->entries[d->top].type = CREATE;
  d->entries[d->top].ptr = d->mem.top;
  d->entries[d->top].immediate = 0;
  d->entries[d->top++].rt = d->mem.top;

  return SUCCESS;


@@ 57,9 58,21 @@ errc run_cre(Ctx *ctx) { /* ( -- ) */
/* def_op: compile an opcode */
errc def_op(Ctx *ctx, char *token) {
  opcode op;
  int n;
  errc e;
  Memory *m = &ctx->dict.mem;

  /* attempt to execute if immediate word */

  /* check in dictionary */
  if ((n = dict_find(&ctx->dict, token)) != -1) {
    if (ctx->dict.entries[n].immediate) {
      if ((e = parse_op(ctx, &op, token)) != SUCCESS)
        return e;
      return exec(ctx, op);
    }
  }

  if ((e = parse_op(ctx, &op, token)) != SUCCESS)
    return e;



@@ 91,6 104,7 @@ errc run_def(Ctx *ctx) { /* ( -- ) */
  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].immediate = 0;
  d->entries[d->top++].rt = d->mem.top;

  while (token_next(token) != EOF && strcmp(token, ";") != 0) {


@@ 372,23 386,28 @@ errc run_alo(Ctx *ctx) { /* ( n -- ) */
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);
  return SUCCESS;
}

  /* Dict *d = &ctx->dict; */
  /* Memory *m = &ctx->dict.mem; */
  /* cell prev_ptr; */
  /* errc e; */
errc run_imm(Ctx *ctx) { /* ( -- ) */
  Dict *d = &ctx->dict;
  d->entries[d->top - 1].immediate = 1;
  return SUCCESS;
}

  /* prev_ptr = d->entries[d->top - 1].ptr; */
  /* d->entries[d->top - 1].ptr = m->top; */
errc run_cmp(Ctx *ctx) { /* ( -- ) */
  int n;
  cell op;
  Memory *m = &ctx->dict.mem;

  /* if ((e = mem_write_num(m, prev_ptr)) != SUCCESS) */
  /*   return e; */
  /* get next memory cell */
  op = m->data[ctx->m_ptr];

  /* if ((e = mem_write_num(m, ctx->m_ptr)) != SUCCESS) */
  /*   return e; */
  /* make sure it's a word */
  /* if ((n = op - LEN) >= 0 && n <= ctx->dict.top) { */

  /* return mem_write(m, JMP); */
  return SUCCESS;
	mem_write(m, op);

	ctx->m_ptr++;
	return SUCCESS;
}

M src/runtime.h => src/runtime.h +2 -0
@@ 27,5 27,7 @@ errc run_ext(Ctx *ctx); /* ( -- ) */
errc run_cre(Ctx *ctx); /* ( -- ) */
errc run_alo(Ctx *ctx); /* ( n -- ) */
errc run_dos(Ctx *ctx); /* ( -- ) */
errc run_imm(Ctx *ctx); /* ( -- ) */
errc run_cmp(Ctx *ctx); /* ( -- ) */

#endif