From f0b0efe498937562598b2d9467e986550d862327 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 26 May 2023 20:49:00 -0400 Subject: fix: c forth "then" keyword is discarded properly (in both "if" cases) --- optable.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'optable.c') diff --git a/optable.c b/optable.c index e5652d7..b8df8ec 100644 --- a/optable.c +++ b/optable.c @@ -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; } -- cgit v1.2.3