diff options
author | dan <[email protected]> | 2023-06-02 23:02:50 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-06-02 23:02:50 -0400 |
commit | f130b76fabb440b02c418a537074ec80489a9480 (patch) | |
tree | 4f462a847c24e62111e880fa330dcebab4bde3dc | |
parent | b6f1237fb2dd391c399b6c6d294a80d969b9dbfa (diff) | |
download | forth-f130b76fabb440b02c418a537074ec80489a9480.tar.gz forth-f130b76fabb440b02c418a537074ec80489a9480.tar.bz2 forth-f130b76fabb440b02c418a537074ec80489a9480.zip |
refactor: delete unused outputbuffer and outputline, add prefix to optype enum
-rw-r--r-- | forthmachine.c | 4 | ||||
-rw-r--r-- | forthmachine.h | 8 | ||||
-rw-r--r-- | forthmachine_optable.c | 57 | ||||
-rw-r--r-- | stack.h | 1 |
4 files changed, 32 insertions, 38 deletions
diff --git a/forthmachine.c b/forthmachine.c index 4532f78..ba67013 100644 --- a/forthmachine.c +++ b/forthmachine.c @@ -16,10 +16,10 @@ forthmachine* forthmachine_new() { static void op_exec(wordop* op, forthmachine* fm) { switch (op->optype) { - case builtin: + case optype_builtin: op->op(fm); break; - case compiled: + case optype_compiled: for (int j = 0; j < op->oplistlen; j++) { switch (op->oplist[j].type) { case compileditem_literal: diff --git a/forthmachine.h b/forthmachine.h index 28f3932..de59fc5 100644 --- a/forthmachine.h +++ b/forthmachine.h @@ -13,13 +13,9 @@ typedef struct optable optable; typedef struct wordop wordop; typedef struct forthmachine forthmachine; - typedef void (*stackop)(forthmachine* fm); typedef void (*directiveop)(forthmachine* fm, int len, char* line, int* i); -extern char* outputbuffer; -extern int outputline; - typedef enum { compileditem_stackop = 0, compileditem_literal = 1, @@ -38,8 +34,8 @@ typedef struct { } compileditem; typedef enum { - builtin = 0, - compiled = 1, + optype_builtin = 0, + optype_compiled = 1, } optype; struct wordop { diff --git a/forthmachine_optable.c b/forthmachine_optable.c index aa379bb..08241ed 100644 --- a/forthmachine_optable.c +++ b/forthmachine_optable.c @@ -31,30 +31,30 @@ static void roll(forthmachine* fm); static void clearstack(forthmachine* fm); const static wordop inittable[] = { - {".", builtin, {popout}}, - {"peek", builtin, {peekout}}, - {"+", builtin, {add}}, - {"-", builtin, {sub}}, - {"*", builtin, {mult}}, - {"/", builtin, {s_div}}, - {"negate", builtin, {negate}}, - {"abs", builtin, {s_abs}}, - {"mod", builtin, {mod}}, - {"max", builtin, {max}}, - {"min", builtin, {min}}, - {"dup", builtin, {dup}}, - {"not", builtin, {not}}, - {"=", builtin, {eq}}, - {"swap", builtin, {swap}}, - {"drop", builtin, {drop}}, - {"over", builtin, {over}}, - {"rot", builtin, {rot}}, - {"pick", builtin, {pick}}, - {"roll", builtin, {roll}}, - {"then", builtin, {donothing}}, - {"depth", builtin, {depth}}, - {".s", builtin, {printall}}, - {"clearstack", builtin, {clearstack}}, + {".", optype_builtin, {popout}}, + {"peek", optype_builtin, {peekout}}, + {"+", optype_builtin, {add}}, + {"-", optype_builtin, {sub}}, + {"*", optype_builtin, {mult}}, + {"/", optype_builtin, {s_div}}, + {"negate", optype_builtin, {negate}}, + {"abs", optype_builtin, {s_abs}}, + {"mod", optype_builtin, {mod}}, + {"max", optype_builtin, {max}}, + {"min", optype_builtin, {min}}, + {"dup", optype_builtin, {dup}}, + {"not", optype_builtin, {not}}, + {"=", optype_builtin, {eq}}, + {"swap", optype_builtin, {swap}}, + {"drop", optype_builtin, {drop}}, + {"over", optype_builtin, {over}}, + {"rot", optype_builtin, {rot}}, + {"pick", optype_builtin, {pick}}, + {"roll", optype_builtin, {roll}}, + {"then", optype_builtin, {donothing}}, + {"depth", optype_builtin, {depth}}, + {".s", optype_builtin, {printall}}, + {"clearstack", optype_builtin, {clearstack}}, }; @@ -76,7 +76,7 @@ static compileditem* optable_compilewords(optable* ot, int len, char** script) { void optable_addop(optable* ot, char* name, int len, char** words) { ot->optable[ot->len].word = malloc(sizeof(name)); strcpy(ot->optable[ot->len].word, name); - ot->optable[ot->len].optype = compiled; + ot->optable[ot->len].optype = optype_compiled; ot->optable[ot->len].oplistlen = len; compileditem* oplist = optable_compilewords(ot, len, words); ot->optable[ot->len].oplist = oplist; @@ -353,12 +353,11 @@ void optable_defineop(optable* optable, char *input, int* starti) { stack_free(ifcounter); wordop* existingop = optable_getop(optable, opcode); - if (existingop) { - if (existingop->optype == compiled && existingop->oplist) { + if (existingop->optype == optype_compiled && existingop->oplist) { free(existingop->oplist); } - existingop->optype = compiled; + existingop->optype = optype_compiled; existingop->oplist = oplist; existingop->oplistlen = opsi; } else { @@ -370,7 +369,7 @@ void optable_defineop(optable* optable, char *input, int* starti) { } // add op to end of table, and increment size optable->optable[optable->len].word = opcode; - optable->optable[optable->len].optype = compiled; + optable->optable[optable->len].optype = optype_compiled; optable->optable[optable->len].oplist = oplist; optable->optable[optable->len].oplistlen = opsi; optable->len++; @@ -12,7 +12,6 @@ typedef struct { stackitem* start; } stack; - stack* stack_new(); void stack_free(stack* s); |