aboutsummaryrefslogtreecommitdiffstats
path: root/stack.c
diff options
context:
space:
mode:
authordan <[email protected]>2023-06-03 14:47:52 -0400
committerdan <[email protected]>2023-06-03 14:47:52 -0400
commit30553db7c86a70e43f956d385d317a1dfad9b2d0 (patch)
tree2493d74d5684d6ce82825397707d10a5ddd4a456 /stack.c
parent48c02f4fccd13cf329594988a19a4edd1fce4774 (diff)
downloadforth-30553db7c86a70e43f956d385d317a1dfad9b2d0.tar.gz
forth-30553db7c86a70e43f956d385d317a1dfad9b2d0.tar.bz2
forth-30553db7c86a70e43f956d385d317a1dfad9b2d0.zip
feat: optable errors handled with error handlers, not just by printing
Diffstat (limited to 'stack.c')
-rw-r--r--stack.c20
1 files changed, 8 insertions, 12 deletions
diff --git a/stack.c b/stack.c
index 3fadaf4..575e38a 100644
--- a/stack.c
+++ b/stack.c
@@ -23,10 +23,6 @@ void stack_free(stack* s) {
#define ERROR_STACK_OVERFLOW "Error: Stack Overflow"
#define ERROR_OUTPUT_BUFFER_OVERFLOW "Error: Output would overflow buffer if printed"
-static void stack_handleerror(stack* s, char* errormessage) {
- s->errorhandler(errormessage);
-}
-
stackitem stack_pop(stack* s) {
if (s->size > 0) {
s->size = s->size - 1;
@@ -34,7 +30,7 @@ stackitem stack_pop(stack* s) {
return si;
} else {
// tried to pop empty stack
- stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP);
+ s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP);
return 0;
}
}
@@ -44,7 +40,7 @@ stackitem stack_peek(stack* s) {
return s->start[s->size - 1];
} else {
// tried to pop empty stack
- stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP);
+ s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP);
return 0;
}
}
@@ -52,7 +48,7 @@ stackitem stack_peek(stack* s) {
void stack_push(stack *s, stackitem si) {
// fprintf(stderr, "pushing %d", si);
if (s->size >= s->maxsize) {
- stack_handleerror(s, ERROR_STACK_OVERFLOW);
+ s->errorhandler(ERROR_STACK_OVERFLOW);
} else {
s->start[s->size] = si;
s->size = s->size + 1;
@@ -69,18 +65,18 @@ int stack_depth(stack* s) {
void stack_tostringappend(stack* s, int sbmaxlen, char* sb) {
int i = strlen(sb);
if (i >= sbmaxlen) {
- stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW);
+ s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW);
}
sprintf(&(sb[i]), "<%d>", s->size);
i = strlen(sb);
if (i >= sbmaxlen) {
- stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW);
+ s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW);
}
for (int j = 0; j < s->size; j++) {
sprintf(&(sb[i]), " %d", s->start[j]);
i = strlen(sb);
if (i >= sbmaxlen) {
- stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW);
+ s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW);
}
}
sprintf(&(sb[i]), "\n");
@@ -98,7 +94,7 @@ void stack_roll(stack* s) {
s->start[i] = newtop;
} else {
// tried to pop empty stack
- stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP);
+ s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP);
}
}
@@ -109,7 +105,7 @@ void stack_pick(stack *s) {
stack_push(s, s->start[maxindex - posfromtop]);
} else {
// tried to pop empty stack
- stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP);
+ s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP);
}
}