diff options
author | dan <[email protected]> | 2023-05-26 17:52:17 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-26 17:52:17 -0400 |
commit | 70c848876dc6a4ec8d326eab4fcf129ca30d4c74 (patch) | |
tree | 7562a068080f2d6ed4f92eede33fcd150cdd18d4 | |
parent | 55d3248ea3e7f08d8f637602128b44556846d51c (diff) | |
download | forth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.tar.gz forth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.tar.bz2 forth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.zip |
refactor: make defineop static, remove from interface
-rw-r--r-- | optable.c | 99 | ||||
-rw-r--r-- | optable.h | 10 |
2 files changed, 54 insertions, 55 deletions
@@ -17,7 +17,7 @@ static void popout(stack *s); static void peekout(stack *s); static void ifdirective(stack *s, int len, char* line, int* i); -static void defineopdirective(stack *s, int len, char* line, int* i); +static void defineop(stack *s, int len, char* line, int* i); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wincompatible-function-pointer-types" @@ -37,52 +37,11 @@ static wordop optable[OPTABLE_MAX_SIZE] = { {"over", builtin, {over}}, {"rot", builtin, {rot}}, {"if", directive, {ifdirective}}, - {":", directive, {defineopdirective}}, + {":", directive, {defineop}}, }; static int optablelen = 15; #pragma clang diagnostic pop -int defineop(int starti, char *input) { - // name by which the function will be called - char *opcode = malloc(sizeof(char) * WORD_LEN_LIMIT); - int opcodei = 0; - - // code to be evaluated when function is called - char *funcscript = malloc(sizeof(char) * DEFINED_FUNC_MAX_LENGTH); - int funcscripti = 0; - - // skip ' ' and ':' - while (input[starti] == ' ' || input[starti] == ':') { - starti++; - } - - // get name - while (input[starti] != ' ' && opcodei < WORD_LEN_LIMIT) { - opcode[opcodei++] = input[starti++]; - } - opcode[opcodei] = '\0'; - - // get code - while (input[starti] != ';' && funcscripti < DEFINED_FUNC_MAX_LENGTH) { - funcscript[funcscripti++] = input[starti++]; - } - funcscript[funcscripti] = '\0'; - - // optable bounds check - if (optablelen >= OPTABLE_MAX_SIZE) { - // Error - fprintf(stderr, "Error: optable reached max size, failed to create new user defined operation"); - exit(1); - } - // add op to end of table, and increment size - optable[optablelen].word = opcode; - optable[optablelen].optype = script; - optable[optablelen].script = funcscript; - optable[optablelen].scriptlen = funcscripti; - optablelen++; - return starti; -} - wordop* getop(char *word) { for (int i = 0; i < optablelen; i++) { if (!strcmp(optable[i].word, word)) { @@ -190,6 +149,56 @@ static void ifdirective(stack *s, int len, char* line, int* i) { } } -static void defineopdirective(stack *s, int len, char* line, int* i) { - *i = defineop(*i, line); + +/** + * defineop reads a function identifier, followed by the commands to run when the function + * is called, stopping when a semicolon is reached. + * Reading is done from the input string. + * + * returns new position of input index + * + */ +static void defineop(stack* s_IGNORED, int len_IGNORED, char *input, int* starti) { + // value easier to deal with (than pointer) + int i = *starti; + // name by which the function will be called + char *opcode = malloc(sizeof(char) * WORD_LEN_LIMIT); + int opcodei = 0; + + // code to be evaluated when function is called + char *funcscript = malloc(sizeof(char) * DEFINED_FUNC_MAX_LENGTH); + int funcscripti = 0; + + // skip ' ' and ':' + while (input[i] == ' ' || input[i] == ':') { + i++; + } + + // get name + while (input[i] != ' ' && opcodei < WORD_LEN_LIMIT) { + opcode[opcodei++] = input[i++]; + } + opcode[opcodei] = '\0'; + + // get code + while (input[i] != ';' && funcscripti < DEFINED_FUNC_MAX_LENGTH) { + funcscript[funcscripti++] = input[i++]; + } + funcscript[funcscripti] = '\0'; + + // optable bounds check + if (optablelen >= OPTABLE_MAX_SIZE) { + // Error + fprintf(stderr, "Error: optable reached max size, failed to create new user defined operation"); + exit(1); + } + // add op to end of table, and increment size + optable[optablelen].word = opcode; + optable[optablelen].optype = script; + optable[optablelen].script = funcscript; + optable[optablelen].scriptlen = funcscripti; + optablelen++; + + // move read position forwards + *starti = i; } @@ -32,16 +32,6 @@ typedef struct { } wordop; /** - * defineop reads a function identifier, followed by the commands to run when the function - * is called, stopping when a semicolon is reached. - * Reading is done from the input string. - * - * returns new position of input index - * - */ -int defineop(int starti, char *input); - -/** * getop returns the first wordop in the optable which is called by the word given as a parameter * if none exist, returns 0 */ |