add U.R, MOD

master
Ondřej Hruška 3 years ago
parent 651fe65ae2
commit 883cc7faf1
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 19
      src/fh_builtins_arith.c
  2. 2
      src/fh_builtins_control.c
  3. 11
      src/fh_builtins_system.c
  4. 13
      src/fh_builtins_text.c
  5. 6
      src/fh_runtime.c
  6. 3
      src/main.c

@ -331,6 +331,24 @@ static enum fh_error w_slash_mod(struct fh_thread_s *fh, const struct fh_word_s
return FH_OK;
}
static enum fh_error w_mod(struct fh_thread_s *fh, const struct fh_word_s *w)
{
(void) w;
enum fh_error rv;
uint32_t a = 0, b = 0;
TRY(ds_pop(fh, &b));
TRY(ds_pop(fh, &a));
if (b == 0) {
return FH_ERR_DIV_BY_ZERO;
}
uint32_t rem = a % b;
TRY(ds_push(fh, rem));
return FH_OK;
}
const struct name_and_handler fh_builtins_arith[] = {
/* Arithmetics */
{"base", wp_const, 0, MAGICADDR_BASE},
@ -349,6 +367,7 @@ const struct name_and_handler fh_builtins_arith[] = {
{"/", w_slash, 0, 0},
{"abs", w_abs, 0, 0},
{"/mod", w_slash_mod, 0, 0},
{"mod", w_mod, 0, 0},
{"invert", w_invert, 0, 0},
{"negate", w_negate, 0, 0},
{"0<", w_zero_less, 0, 0},

@ -225,7 +225,7 @@ const struct name_and_handler fh_builtins_control[] = {
{"?do", wp_do, 1, 1},
{"loop", wp_loop, 1, 0},
{"leave", w_leave, 1, 0},
{"loop+", wp_loop, 1, 1},
{"+loop", wp_loop, 1, 1},
{"begin", w_begin, 1, 0},
{"while", w_while, 1, 0},
{"repeat", w_repeat, 1, 0},

@ -41,11 +41,22 @@ static enum fh_error w_bye(struct fh_thread_s *fh, const struct fh_word_s *w)
return FH_OK;
}
static enum fh_error w_debug(struct fh_thread_s *fh, const struct fh_word_s *w)
{
(void) w;
enum fh_error rv;
uint32_t val;
TRY(ds_pop(fh, &val));
fh_globals.verbose = val;
return FH_OK;
}
const struct name_and_handler fh_builtins_system[] = {
{"depth", w_depth, 0, 0},
{"unused", w_unused, 0, 0},
{"reset", w_reset, 1, 0},
{"bye", w_bye, 0, 0},
{"debug", w_debug, 0, 0},
{ /* end marker */ }
};

@ -64,6 +64,18 @@ static enum fh_error w_dot(struct fh_thread_s *fh, const struct fh_word_s *w)
return FH_OK;
}
static enum fh_error w_u_r(struct fh_thread_s *fh, const struct fh_word_s *w)
{
(void) w;
enum fh_error rv;
uint32_t a = 0, n = 0;
TRY(ds_pop(fh, &n));
TRY(ds_pop(fh, &a));
FHPRINT("%*d", n, a);
return FH_OK;
}
static enum fh_error w_type(struct fh_thread_s *fh, const struct fh_word_s *w)
{
(void) w;
@ -191,6 +203,7 @@ const struct name_and_handler fh_builtins_text[] = {
{"cr", wp_putc, 0, '\n'},
{"space", wp_putc, 0, ' '},
{"bl", wp_const, 0, ' '},
{"u.r", w_u_r, 0, 0},
{"??", w_debug_dump, 0, 0},
{"emit", w_emit, 0, 0},
{"see", w_see, 0, 0},

@ -329,10 +329,14 @@ enum fh_error w_user_word(struct fh_thread_s *fh, const struct fh_word_s *w0)
// R: index,limit
TRY(rs_peek(fh, &limit));
LOG("+LOOP, i=%d, step %d, limit %d", fh->loop_i, val, limit);
index0 = fh->loop_i;
fh->loop_i += val;
if (index0 < limit == fh->loop_i < limit) { // boundary not crossed, continue
LOG("after add: %d", fh->loop_i);
if (((int32_t)index0 < (int32_t)limit) == ((int32_t)fh->loop_i < (int32_t)limit)) { // boundary not crossed, continue
fh->execptr = instr->data; // go to beginning
} else {
// end of loop

@ -78,6 +78,9 @@ int main(int argc, char *argv[])
}
/* reset state */
fh_setstate(&fh, FH_STATE_INTERPRET, FH_SUBSTATE_NONE);
// reset stack pointers
fh.data_stack_top = 0;
fh.return_stack_top = 0;
}
FHPRINT("%s", prompt);

Loading…
Cancel
Save