#pragma once #include #include #include /* Replace '16' by desired byte size on both following lines. */ #define SLOTH_BITS 16 typedef uint16_t SlothByte; typedef XXH32_hash_t SlothHash; typedef struct SlothDict SlothDict; typedef const char* SlothError; typedef struct Sloth Sloth; typedef struct SlothInstr SlothInstr; struct SlothDict { SlothHash hash; int c_func; int immediate; SlothError (*func)(Sloth *ctx); SlothInstr *instrs; SlothDict *next; }; #define SLOTH_DICT_C(word, func) \ (SlothDict){XXH32(word, strlen(word), 0), 1, 0, func, NULL, NULL} #define SLOTH_DICT_I(word, func) \ (SlothDict){XXH32(word, strlen(word), 0), 1, 1, func, NULL, NULL} struct Sloth { SlothByte *stack; size_t stack_size; size_t stack_capacity; SlothByte mem[1 << SLOTH_BITS]; SlothDict *dict; int compile; SlothDict comp; }; struct SlothInstr { int litteral; union { SlothByte byte; SlothHash hash; }; SlothInstr *next; }; SlothError sloth_init(Sloth *ctx); void sloth_deinit(Sloth *ctx); SlothError sloth_dict_append(Sloth *ctx, SlothDict dict); SlothError sloth_str_to_instr(Sloth *ctx, const char *s, SlothInstr *instr); SlothError sloth_exec_instr(Sloth *ctx, const SlothInstr *instr); SlothError sloth_exec(Sloth *ctx, const char *s); SlothError sloth_exec_line(Sloth *ctx, char *s); SlothError sloth_pop(Sloth *ctx, SlothByte *v); SlothError sloth_push(Sloth *ctx, SlothByte v); SlothError sloth_add(Sloth *ctx); SlothError sloth_write(Sloth *ctx); SlothError sloth_store(Sloth *ctx); SlothError sloth_retrieve(Sloth *ctx); SlothError sloth_compare(Sloth *ctx); SlothError sloth_neg(Sloth *ctx); SlothError sloth_compile_begin(Sloth *ctx); SlothError sloth_compile_end(Sloth *ctx); SlothError sloth_inspect_stack(Sloth *ctx);