diff options
author | dan <[email protected]> | 2023-05-26 15:08:31 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-26 15:08:31 -0400 |
commit | bf2482154d14333b0eb2641423f3406f08c06973 (patch) | |
tree | 098ef5dd4e242d9f635e40baae06ee907367d83d | |
parent | 2b7ea0857da80e1353ce0a72239093f108fdc1e9 (diff) | |
download | forth-bf2482154d14333b0eb2641423f3406f08c06973.tar.gz forth-bf2482154d14333b0eb2641423f3406f08c06973.tar.bz2 forth-bf2482154d14333b0eb2641423f3406f08c06973.zip |
refactor: switch case to pick optype; isnumber tidy up;
-rw-r--r-- | forth.c | 39 |
1 files changed, 20 insertions, 19 deletions
@@ -12,16 +12,17 @@ #include "optable.h" #endif -//https://stackoverflow.com/a/58585995 -char isnumber(char *text) { - int j; - j = strlen(text); - while(j--) { - if(text[j] >= '0' && text[j] <= '9') - continue; - return 0; +bool isnumber(char *text) { + for(int j = strlen(text); j > 0; j--) { + if(text[j] < '0' && text[j] > '9') { + return false; + } } - return 1; + return true; +} + +bool notdelim(char c) { + return c != ' ' && c != '\n' && c != '\t' && c != '\0'; } void eval(stack* s, int len, char* line); @@ -29,22 +30,22 @@ void eval(stack* s, int len, char* line); void exec(stack *s, char *word, int len, char* line, int* i) { wordop* op = getop(word); if (op) { - if (op->optype == script) { - eval(s, op->scriptlen, op->script); - } else if (op->optype == builtin) { - op->op(s); - } else if (op->optype == directive) { - op->directive(s, len, line, i); + switch (op->optype) { + case script: + eval(s, op->scriptlen, op->script); + break; + case builtin: + op->op(s); + break; + case directive: + op->directive(s, len, line, i); + break; } } else if (isnumber(word)) { push(s, atoi(word)); } } -bool notdelim(char c) { - return c != ' ' && c != '\n' && c != '\t' && c != '\0'; -} - void eval(stack* s, int len, char* line) { char word[WORD_LEN_LIMIT]; int wordi = 0; |