From f2d0d82eb312824c79bf0b1fb223acc0df95e1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 21 Nov 2021 16:27:48 +0100 Subject: [PATCH] implement BUFFER: --- src/fh_builtins_mem.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/fh_builtins_mem.c b/src/fh_builtins_mem.c index 1bad0fb..7863472 100644 --- a/src/fh_builtins_mem.c +++ b/src/fh_builtins_mem.c @@ -96,6 +96,46 @@ static enum fh_error w_allot(struct fh_thread_s *fh, const struct fh_word_s *w) return FH_OK; } +static enum fh_error rt_read_buffer_addr(struct fh_thread_s *fh, const struct fh_word_s *w) +{ + enum fh_error rv; + TRY(ds_push(fh, w->param)); + return FH_OK; +} + +static enum fh_error w_buffer_colon(struct fh_thread_s *fh, const struct fh_word_s *w) +{ + (void) w; + enum fh_error rv; + ENSURE_STATE(FH_STATE_INTERPRET); + + uint32_t count = 0; + TRY(ds_pop(fh, &count)); + TRY(fh_heap_reserve(fh, count, NULL)); + + char *wordname = NULL; + size_t namelen = 0; + fh_input_consume_spaces(fh); + TRY(fh_input_read_word(fh, &wordname, &namelen)); + LOG("Buffer name: %.*s", namelen, wordname); + + uint32_t ptr; + TRY(fh_heap_reserve(fh, DICTWORD_SIZE + count, &ptr)); + + struct fh_word_s *new_word = fh_word_at(fh, ptr); + if (!new_word) return FH_ERR_INTERNAL; + new_word->previous = fh->dict_last; + new_word->param = ptr + DICTWORD_SIZE; + new_word->handler = rt_read_buffer_addr; + strncpy(new_word->name, wordname, namelen); + new_word->name[namelen] = 0; + new_word->flags = WORDFLAG_WORD; + + fh->dict_last = ptr; + + return FH_OK; +} + static enum fh_error w_move(struct fh_thread_s *fh, const struct fh_word_s *w) { (void) w; @@ -218,6 +258,7 @@ const struct name_and_handler fh_builtins_mem[] = { {"2@", w_two_fetch, 0, 0}, {"aligned", w_aligned, 0, 0}, {"allot", w_allot, 0, 0}, + {"buffer:", w_buffer_colon, 0, 0}, {"align", w_align, 0, 0}, {",", w_comma, 0, 0}, {"c,", w_c_comma, 0, 0},