diff options
author | dan <[email protected]> | 2023-05-26 20:49:00 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-26 20:49:00 -0400 |
commit | f0b0efe498937562598b2d9467e986550d862327 (patch) | |
tree | 212f8427015143d2e4084dfb3481922926b5fa14 | |
parent | 109e4c38a4a3d0573be3d0a0a425a5bfcf313dba (diff) | |
download | forth-f0b0efe498937562598b2d9467e986550d862327.tar.gz forth-f0b0efe498937562598b2d9467e986550d862327.tar.bz2 forth-f0b0efe498937562598b2d9467e986550d862327.zip |
fix: c forth "then" keyword is discarded properly (in both "if" cases)
-rw-r--r-- | optable.c | 23 |
1 files changed, 15 insertions, 8 deletions
@@ -15,6 +15,7 @@ static void sub(stack *s); static void dup(stack *s); static void popout(stack *s); static void peekout(stack *s); +static void donothing(stack *s); static void ifdirective(stack *s, int len, char* line, int* i); static void defineop(stack *s, int len, char* line, int* i); @@ -36,10 +37,11 @@ static wordop optable[OPTABLE_MAX_SIZE] = { {"drop", builtin, {drop}}, {"over", builtin, {over}}, {"rot", builtin, {rot}}, + {"then", builtin, {donothing}}, {"if", directive, {ifdirective}}, {":", directive, {defineop}}, }; -static int optablelen = 15; +static int optablelen = 16; #pragma clang diagnostic pop wordop* getop(char *word) { @@ -132,21 +134,26 @@ static void peekout(stack *s) { printf("%d\n", x); } +static void donothing(stack *s) { + // Do nothing at all (i.e. discard token) +} /* Directives */ -static void ifdirective(stack *s, int len, char* line, int* i) { +static void ifdirective(stack *s, int len, char* line, int* starti) { + int i = *starti; stackitem predicate = pop(s); if (!predicate) { - while (len > *i && !( - line[*i-3] == 't' && - line[*i-2] == 'h' && - line[*i-1] == 'e' && - line[*i] == 'n' + while (len > i && i >= 4 && !( + line[i-4] == 't' && + line[i-3] == 'h' && + line[i-2] == 'e' && + line[i-1] == 'n' )) { - (*i)++; + i++; } } + *starti = i; } |