improvements, fixes

master
Ondřej Hruška 3 years ago
parent 6f9ffc7d20
commit 85feba4fdf
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 55
      main.c

@ -136,6 +136,12 @@ struct fh_instruction_s {
uint32_t data; uint32_t data;
}; };
static inline void instr_init(struct fh_instruction_s *instr, enum fb_instruction_kind kind, uint32_t data)
{
instr->kind = kind;
instr->data = data;
}
#define INSTR_SIZE (sizeof(struct fh_instruction_s)) #define INSTR_SIZE (sizeof(struct fh_instruction_s))
/** Bytecode word indices that are not in the dict, have special effect */ /** Bytecode word indices that are not in the dict, have special effect */
@ -492,6 +498,7 @@ enum fh_error w_user_word(struct fh_thread_s *fh)
case FH_INSTR_WORD: case FH_INSTR_WORD:
wn = instr->data; wn = instr->data;
switch (wn) { switch (wn) {
/* special case for strings stored in compile memory */
case CPLWORD_ALLOCSTR: case CPLWORD_ALLOCSTR:
case CPLWORD_TYPESTR: case CPLWORD_TYPESTR:
strl = *((uint32_t *) &fh->compile[fh->execptr]); strl = *((uint32_t *) &fh->compile[fh->execptr]);
@ -555,7 +562,6 @@ enum fh_error w_colon(struct fh_thread_s *fh)
enum fh_error w_semicolon(struct fh_thread_s *fh) enum fh_error w_semicolon(struct fh_thread_s *fh)
{ {
enum fh_error rv; enum fh_error rv;
uint32_t addr = 0;
struct fh_instruction_s instr; struct fh_instruction_s instr;
if (fh->state != FH_STATE_COMPILE) { if (fh->state != FH_STATE_COMPILE) {
@ -651,20 +657,20 @@ enum fh_error register_builtin_words(struct fh_thread_s *fh)
{"s\"", w_s_quote, 1}, {"s\"", w_s_quote, 1},
{".\"", w_dot_quote, 1}, {".\"", w_dot_quote, 1},
/* Compiler control words */ /* Compiler control words */
{"bye", w_bye}, {"bye", w_bye, 0},
/* Basic arithmetics */ /* Basic arithmetics */
{"+", w_add}, {"+", w_add, 0},
{"-", w_sub}, {"-", w_sub, 0},
{"*", w_mul}, {"*", w_mul, 0},
/* Control words */ /* Control words */
{":", w_colon}, {":", w_colon, 0},
{";", w_semicolon, 1}, {";", w_semicolon, 1},
{".", w_dot}, {".", w_dot, 0},
{"type", w_type}, {"type", w_type, 0},
{"cr", w_cr}, {"cr", w_cr, 0},
{"space", w_space}, {"space", w_space, 0},
{"\\", w_backslash}, // line comment {"\\", w_backslash, 0}, // line comment
{"(", w_paren}, // enclosed comment {"(", w_paren, 0}, // enclosed comment
{ /* end marker */ } { /* end marker */ }
}; };
@ -730,10 +736,11 @@ static enum fh_error fh_handle_quoted_string(
} else { } else {
LOG("Compile a string"); LOG("Compile a string");
/* compile */ /* compile */
instr.kind = FH_INSTR_WORD; if (fh->substate == FH_SUBSTATE_SQUOTE) {
instr.data = fh->substate == FH_SUBSTATE_SQUOTE ? instr_init(&instr, FH_INSTR_WORD, CPLWORD_ALLOCSTR);
CPLWORD_ALLOCSTR : } else {
CPLWORD_TYPESTR; instr_init(&instr, FH_INSTR_WORD, CPLWORD_TYPESTR);
}
uint32_t len32 = len; uint32_t len32 = len;
/* string is encoded as a special compiler command, the size, /* string is encoded as a special compiler command, the size,
* and then the string, all 4-byte aligned. */ * and then the string, all 4-byte aligned. */
@ -758,18 +765,17 @@ static enum fh_error fh_handle_word(
} }
/* First, try if it's a known word */ /* First, try if it's a known word */
// TODO we could use binary search if the dict was ordered
struct fh_word_s *w = &fh->dict[0]; struct fh_word_s *w = &fh->dict[0];
struct fh_instruction_s instr; struct fh_instruction_s instr;
uint32_t cnt = 0; uint32_t cnt = 0;
uint32_t addr = 0;
enum fh_error rv; enum fh_error rv;
while (w->handler) { while (w->handler) {
if (0 == strncasecmp(start, w->name, len) && w->name[len] == 0) { if (0 == strncasecmp(start, w->name, len) && w->name[len] == 0) {
// word found! // word found!
if (fh->state == FH_STATE_COMPILE && !w->immediate) { if (fh->state == FH_STATE_COMPILE && !w->immediate) {
LOG("Compile word call: %s", w->name); LOG("Compile word call: %s", w->name);
instr.kind = FH_INSTR_WORD; instr_init(&instr, FH_INSTR_WORD, cnt);
instr.data = cnt;
TRY(fh_compile_put(fh, &instr, INSTR_SIZE)); TRY(fh_compile_put(fh, &instr, INSTR_SIZE));
} else { } else {
/* interpret */ /* interpret */
@ -794,8 +800,7 @@ static enum fh_error fh_handle_word(
if (fh->state == FH_STATE_COMPILE) { if (fh->state == FH_STATE_COMPILE) {
LOG("Compile number: %ld", v); LOG("Compile number: %ld", v);
instr.kind = FH_INSTR_NUMBER; instr_init(&instr, FH_INSTR_NUMBER, (uint32_t) v);
instr.data = (uint32_t) v;
TRY(fh_compile_put(fh, &instr, INSTR_SIZE)); TRY(fh_compile_put(fh, &instr, INSTR_SIZE));
} else { } else {
/* interpret */ /* interpret */
@ -986,12 +991,8 @@ int main(int argc, char *argv[])
// Show resource usage // Show resource usage
LOG("\nResources used: DS %dW, RS %dW, CS %dW, heap %dB, program %dB, dict %dx\n", LOG("\nResources used: DS %dW, RS %dW, CS %dW, heap %dB, program %dB, dict %dx\n",
(int) fh.data_stack_hwm, (int) fh.data_stack_hwm, (int) fh.return_stack_hwm, (int) fh.control_stack_hwm,
(int) fh.return_stack_hwm, (int) fh.heap_top, (int) fh.compile_top, (int) fh.dict_top);
(int) fh.control_stack_hwm,
(int) fh.heap_top,
(int) fh.compile_top,
(int) fh.dict_top);
FHPRINT_SVC("Bye.\n"); FHPRINT_SVC("Bye.\n");
return 0; return 0;

Loading…
Cancel
Save