diff --git a/src/fh_builtins_arith.c b/src/fh_builtins_arith.c index df8b43e..4391b90 100644 --- a/src/fh_builtins_arith.c +++ b/src/fh_builtins_arith.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}, diff --git a/src/fh_builtins_control.c b/src/fh_builtins_control.c index db70574..93392f5 100644 --- a/src/fh_builtins_control.c +++ b/src/fh_builtins_control.c @@ -215,22 +215,22 @@ static enum fh_error wp_ij(struct fh_thread_s *fh, const struct fh_word_s *w) } const struct name_and_handler fh_builtins_control[] = { - {"i", wp_ij, 0, 0}, - {"j", wp_ij, 0, 1}, - {"if", w_if, 1, 0}, - {"else", w_else, 1, 0}, - {"then", w_then, 1, 0}, - {"recurse", w_recurse, 1, 0}, - {"do", wp_do, 1, 0}, - {"?do", wp_do, 1, 1}, - {"loop", wp_loop, 1, 0}, - {"leave", w_leave, 1, 0}, - {"loop+", wp_loop, 1, 1}, - {"begin", w_begin, 1, 0}, - {"while", w_while, 1, 0}, - {"repeat", w_repeat, 1, 0}, - {"again", w_again, 1, 0}, - {"until", w_until, 1, 0}, + {"i", wp_ij, 0, 0}, + {"j", wp_ij, 0, 1}, + {"if", w_if, 1, 0}, + {"else", w_else, 1, 0}, + {"then", w_then, 1, 0}, + {"recurse", w_recurse, 1, 0}, + {"do", wp_do, 1, 0}, + {"?do", wp_do, 1, 1}, + {"loop", wp_loop, 1, 0}, + {"leave", w_leave, 1, 0}, + {"+loop", wp_loop, 1, 1}, + {"begin", w_begin, 1, 0}, + {"while", w_while, 1, 0}, + {"repeat", w_repeat, 1, 0}, + {"again", w_again, 1, 0}, + {"until", w_until, 1, 0}, { /* end marker */ } }; diff --git a/src/fh_builtins_system.c b/src/fh_builtins_system.c index 6e966ea..c11ae0c 100644 --- a/src/fh_builtins_system.c +++ b/src/fh_builtins_system.c @@ -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 */ } }; diff --git a/src/fh_builtins_text.c b/src/fh_builtins_text.c index d62ffca..440258d 100644 --- a/src/fh_builtins_text.c +++ b/src/fh_builtins_text.c @@ -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}, diff --git a/src/fh_runtime.c b/src/fh_runtime.c index 164f890..4248abf 100644 --- a/src/fh_runtime.c +++ b/src/fh_runtime.c @@ -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 diff --git a/src/main.c b/src/main.c index d411b72..8bf2d23 100644 --- a/src/main.c +++ b/src/main.c @@ -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);