add U.R, MOD
This commit is contained in:
@@ -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},
|
||||
|
||||
+16
-16
@@ -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 */ }
|
||||
};
|
||||
|
||||
@@ -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},
|
||||
|
||||
+5
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user