aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2023-05-26 17:52:17 -0400
committerdan <[email protected]>2023-05-26 17:52:17 -0400
commit70c848876dc6a4ec8d326eab4fcf129ca30d4c74 (patch)
tree7562a068080f2d6ed4f92eede33fcd150cdd18d4
parent55d3248ea3e7f08d8f637602128b44556846d51c (diff)
downloadforth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.tar.gz
forth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.tar.bz2
forth-70c848876dc6a4ec8d326eab4fcf129ca30d4c74.zip
refactor: make defineop static, remove from interface
-rw-r--r--optable.c99
-rw-r--r--optable.h10
2 files changed, 54 insertions, 55 deletions
diff --git a/optable.c b/optable.c
index 71ef581..e5652d7 100644
--- a/optable.c
+++ b/optable.c
@@ -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;
}
diff --git a/optable.h b/optable.h
index ebf9a06..b55b38e 100644
--- a/optable.h
+++ b/optable.h
@@ -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
*/