|
|
@ -68,7 +68,8 @@ static const char *errornames[] = { |
|
|
|
[FH_ERR_UNKNOWN_WORD] = "UNKNOWN_WORD", |
|
|
|
[FH_ERR_UNKNOWN_WORD] = "UNKNOWN_WORD", |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const char *fherr_name(enum fh_error e) { |
|
|
|
const char *fherr_name(enum fh_error e) |
|
|
|
|
|
|
|
{ |
|
|
|
if (e >= FH_ERR_MAX) { |
|
|
|
if (e >= FH_ERR_MAX) { |
|
|
|
return "Unknown"; |
|
|
|
return "Unknown"; |
|
|
|
} |
|
|
|
} |
|
|
@ -112,6 +113,13 @@ enum fh_state { |
|
|
|
FH_STATE_INTERPRET = 0, |
|
|
|
FH_STATE_INTERPRET = 0, |
|
|
|
FH_STATE_COMPILE, |
|
|
|
FH_STATE_COMPILE, |
|
|
|
FH_STATE_SHUTDOWN, |
|
|
|
FH_STATE_SHUTDOWN, |
|
|
|
|
|
|
|
FH_STATE_MAX, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *statenames[] = { |
|
|
|
|
|
|
|
[FH_STATE_INTERPRET] = "INTERPRET", |
|
|
|
|
|
|
|
[FH_STATE_COMPILE] = "COMPILE", |
|
|
|
|
|
|
|
[FH_STATE_SHUTDOWN] = "SHUTDOWN", |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
enum fh_substate { |
|
|
|
enum fh_substate { |
|
|
@ -121,6 +129,16 @@ enum fh_substate { |
|
|
|
FH_SUBSTATE_DOTQUOTE, |
|
|
|
FH_SUBSTATE_DOTQUOTE, |
|
|
|
FH_SUBSTATE_PARENCOMMENT, |
|
|
|
FH_SUBSTATE_PARENCOMMENT, |
|
|
|
FH_SUBSTATE_LINECOMMENT, |
|
|
|
FH_SUBSTATE_LINECOMMENT, |
|
|
|
|
|
|
|
FH_SUBSTATE_MAX, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *substatenames[] = { |
|
|
|
|
|
|
|
[FH_SUBSTATE_NONE] = "NONE", |
|
|
|
|
|
|
|
[FH_SUBSTATE_COLONNAME] = "COLONNAME", |
|
|
|
|
|
|
|
[FH_SUBSTATE_SQUOTE] = "SQUOTE", |
|
|
|
|
|
|
|
[FH_SUBSTATE_DOTQUOTE] = "DOTQUOTE", |
|
|
|
|
|
|
|
[FH_SUBSTATE_PARENCOMMENT] = "PARENCOMMENT", |
|
|
|
|
|
|
|
[FH_SUBSTATE_LINECOMMENT] = "LINECOMMENT", |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct fh_thread_s { |
|
|
|
struct fh_thread_s { |
|
|
@ -246,6 +264,26 @@ static inline enum fh_error cs_push(struct fh_thread_s *fh, uint32_t in) |
|
|
|
|
|
|
|
|
|
|
|
//endregion Push & Pop
|
|
|
|
//endregion Push & Pop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void showstate(const struct fh_thread_s *fh) { |
|
|
|
|
|
|
|
if(fh->substate==0) { |
|
|
|
|
|
|
|
LOG("state = %s", statenames[fh->state]); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
LOG("state = %s.%s", statenames[fh->state], substatenames[fh->substate]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void fh_setstate(struct fh_thread_s *fh, enum fh_state state, enum fh_substate substate) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
fh->state = state; |
|
|
|
|
|
|
|
fh->substate = substate; |
|
|
|
|
|
|
|
showstate(fh); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void fh_setsubstate(struct fh_thread_s *fh, enum fh_substate substate) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
fh->substate = substate; |
|
|
|
|
|
|
|
showstate(fh); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error fh_allot( |
|
|
|
enum fh_error fh_allot( |
|
|
|
struct fh_thread_s *fh, |
|
|
|
struct fh_thread_s *fh, |
|
|
@ -406,9 +444,7 @@ enum fh_error w_colon(struct fh_thread_s *fh) |
|
|
|
return FH_ERR_INVALID_STATE; |
|
|
|
return FH_ERR_INVALID_STATE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LOG("state=COMPILE"); |
|
|
|
fh_setstate(fh, FH_STATE_COMPILE, FH_SUBSTATE_COLONNAME); |
|
|
|
fh->state = FH_STATE_COMPILE; |
|
|
|
|
|
|
|
fh->substate = FH_SUBSTATE_COLONNAME; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fh->dict_top >= DICT_SIZE) { |
|
|
|
if (fh->dict_top >= DICT_SIZE) { |
|
|
|
return FH_ERR_DICT_FULL; |
|
|
|
return FH_ERR_DICT_FULL; |
|
|
@ -434,8 +470,7 @@ enum fh_error w_semicolon(struct fh_thread_s *fh) |
|
|
|
memcpy(&fh->compile[addr], &instr, sizeof(struct fh_instruction_s)); |
|
|
|
memcpy(&fh->compile[addr], &instr, sizeof(struct fh_instruction_s)); |
|
|
|
|
|
|
|
|
|
|
|
/* Return to interpret state */ |
|
|
|
/* Return to interpret state */ |
|
|
|
LOG("state=INTERPRET"); |
|
|
|
fh_setstate(fh, FH_STATE_INTERPRET, 0); |
|
|
|
fh->state = FH_STATE_INTERPRET; |
|
|
|
|
|
|
|
fh->dict[fh->dict_top].end = fh->compile_top; /* one past the end cell */ |
|
|
|
fh->dict[fh->dict_top].end = fh->compile_top; /* one past the end cell */ |
|
|
|
fh->dict_top++; |
|
|
|
fh->dict_top++; |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
@ -476,32 +511,32 @@ enum fh_error w_space(struct fh_thread_s *fh) |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error w_s_quote(struct fh_thread_s *fh) |
|
|
|
enum fh_error w_s_quote(struct fh_thread_s *fh) |
|
|
|
{ |
|
|
|
{ |
|
|
|
fh->substate = FH_SUBSTATE_SQUOTE; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_SQUOTE); |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error w_dot_quote(struct fh_thread_s *fh) |
|
|
|
enum fh_error w_dot_quote(struct fh_thread_s *fh) |
|
|
|
{ |
|
|
|
{ |
|
|
|
fh->substate = FH_SUBSTATE_DOTQUOTE; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_DOTQUOTE); |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error w_backslash(struct fh_thread_s *fh) |
|
|
|
enum fh_error w_backslash(struct fh_thread_s *fh) |
|
|
|
{ |
|
|
|
{ |
|
|
|
fh->substate = FH_SUBSTATE_LINECOMMENT; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_LINECOMMENT); |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error w_paren(struct fh_thread_s *fh) |
|
|
|
enum fh_error w_paren(struct fh_thread_s *fh) |
|
|
|
{ |
|
|
|
{ |
|
|
|
fh->substate = FH_SUBSTATE_PARENCOMMENT; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_PARENCOMMENT); |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
enum fh_error w_bye(struct fh_thread_s *fh) |
|
|
|
enum fh_error w_bye(struct fh_thread_s *fh) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LOG("state=SHUTDOWN"); |
|
|
|
LOG("state=SHUTDOWN"); |
|
|
|
fh->state = FH_STATE_SHUTDOWN; |
|
|
|
fh_setstate(fh, FH_STATE_SHUTDOWN, 0); |
|
|
|
return FH_OK; |
|
|
|
return FH_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -723,7 +758,7 @@ enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf) |
|
|
|
/* new word's name is found */ |
|
|
|
/* new word's name is found */ |
|
|
|
LOG("New word name = \"%.*s\"", (int) length, rp); |
|
|
|
LOG("New word name = \"%.*s\"", (int) length, rp); |
|
|
|
strncpy(fh->dict[fh->dict_top].name, rp, length); |
|
|
|
strncpy(fh->dict[fh->dict_top].name, rp, length); |
|
|
|
fh->substate = FH_SUBSTATE_NONE; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_NONE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (end) { |
|
|
|
if (end) { |
|
|
@ -740,7 +775,7 @@ enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf) |
|
|
|
length = end - rp - 1; |
|
|
|
length = end - rp - 1; |
|
|
|
LOG("Quoted string: \"%.*s\"", (int) length, rp); |
|
|
|
LOG("Quoted string: \"%.*s\"", (int) length, rp); |
|
|
|
TRY(fh_handle_quoted_string(fh, rp, length)); |
|
|
|
TRY(fh_handle_quoted_string(fh, rp, length)); |
|
|
|
fh->substate = FH_SUBSTATE_NONE; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_NONE); |
|
|
|
rp = end + 1; |
|
|
|
rp = end + 1; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
/* no end. this is weird. */ |
|
|
|
/* no end. this is weird. */ |
|
|
@ -753,7 +788,7 @@ enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf) |
|
|
|
end = strchr(rp, ')'); |
|
|
|
end = strchr(rp, ')'); |
|
|
|
if (end) { |
|
|
|
if (end) { |
|
|
|
LOG("Discard inline comment"); |
|
|
|
LOG("Discard inline comment"); |
|
|
|
fh->substate = FH_SUBSTATE_NONE; |
|
|
|
fh_setsubstate(fh, FH_SUBSTATE_NONE); |
|
|
|
rp = end + 1; |
|
|
|
rp = end + 1; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
/* no end, discard all */ |
|
|
|
/* no end, discard all */ |
|
|
|