aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--forthmachine.c2
-rw-r--r--forthmachine.h3
-rw-r--r--forthmachine_optable.c9
-rw-r--r--stack.c20
4 files changed, 16 insertions, 18 deletions
diff --git a/forthmachine.c b/forthmachine.c
index a421630..6cdefb5 100644
--- a/forthmachine.c
+++ b/forthmachine.c
@@ -8,7 +8,7 @@
forthmachine* forthmachine_new(errorhandler errorhandler) {
forthmachine* fm = (forthmachine*)malloc(sizeof(forthmachine));
- fm->ot = optable_new();
+ fm->ot = optable_new(errorhandler);
fm->s = stack_new(errorhandler);
fm->outputbuffer = (char*)malloc(sizeof(char) * MAX_OUTPUT_BUFFER_SIZE);
fm->errorhandler = errorhandler;
diff --git a/forthmachine.h b/forthmachine.h
index a3a4522..4a55952 100644
--- a/forthmachine.h
+++ b/forthmachine.h
@@ -54,6 +54,7 @@ struct wordop {
struct optable {
int len;
wordop* optable;
+ errorhandler errorhandler;
};
/**
@@ -64,7 +65,7 @@ wordop* optable_getop(optable* optable, char *word);
void optable_defineop(optable* optable, char *input, int* starti);
-optable* optable_new();
+optable* optable_new(errorhandler errorhandler);
struct forthmachine {
optable* ot;
diff --git a/forthmachine_optable.c b/forthmachine_optable.c
index 8164d5c..cd4b46b 100644
--- a/forthmachine_optable.c
+++ b/forthmachine_optable.c
@@ -1,3 +1,4 @@
+#include "errorhandler.h"
#include "forthmachine.h"
#include <stdbool.h>
#include <stdio.h>
@@ -83,12 +84,13 @@ void optable_addop(optable* ot, char* name, int len, char** words) {
ot->len++;
}
-optable* optable_new() {
+optable* optable_new(errorhandler errorhandler) {
optable* ot = malloc(sizeof(optable));
ot->optable = malloc(sizeof(wordop) * OPTABLE_MAX_SIZE);
int inittablesize = sizeof(inittable);
ot->len = inittablesize / sizeof(*inittable);
memcpy(ot->optable, inittable, inittablesize);
+ ot->errorhandler = errorhandler;
char* defs[] = {
": nip swap drop ;",
@@ -363,9 +365,8 @@ void optable_defineop(optable* optable, char *input, int* starti) {
} else {
// optable->optable bounds check
if (optable->len >= OPTABLE_MAX_SIZE) {
- // Error
- fprintf(stderr, "Error: optable->optable reached max size, failed to create new user defined operation");
- exit(1);
+ optable->errorhandler("Error: optable->optable reached max size, failed to create new user defined operation");
+ return;
}
// add op to end of table, and increment size
optable->optable[optable->len].word = opcode;
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);
}
}