From 8dee74c3d0d69c60fbb5bef6274c1760ed78ce56 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 16 Apr 2021 21:09:48 +0500 Subject: [PATCH] Improved error handling --- run.c | 74 +++++++++++++++++++++++++++++++++++++++++---------------- stack.c | 41 +++++++++++++++++++++++++------- stack.h | 8 +++---- 3 files changed, 89 insertions(+), 34 deletions(-) diff --git a/run.c b/run.c index de3a9e6..8aab562 100644 --- a/run.c +++ b/run.c @@ -17,56 +17,88 @@ int run(command_t **buff, stack_t *stack) int exec(command_t cmd, stack_t *stack) { + int *res; + switch (cmd.code) { + case NONE: + break; case PUSH: - stack_push(stack, cmd.args[0]); - break; + { + stack_push(stack, cmd.args[0], res); + + if (res != NULL) + return *res; + } + break; case POP: - stack_pop(stack); - break; + { + stack_pop(stack, res); + + if (res != NULL) + return *res; + } + break; case ADD: { - int b = stack_pop(stack); - int a = stack_pop(stack); + int b = stack_pop(stack, res); + int a = stack_pop(stack, res); + if (res != NULL) + return *res; - stack_push(stack, a + b); + stack_push(stack, a + b, res); + if (res != NULL) + return *res; } break; case SUB: { - int b = stack_pop(stack); - int a = stack_pop(stack); + int b = stack_pop(stack, res); + int a = stack_pop(stack, res); + if (res != NULL) + return *res; - stack_push(stack, a - b); + stack_push(stack, a - b, res); + if (res != NULL) + return *res; } break; case MUL: { - int b = stack_pop(stack); - int a = stack_pop(stack); + int b = stack_pop(stack, res); + int a = stack_pop(stack, res); + if (res != NULL) + return *res; - stack_push(stack, a * b); + stack_push(stack, a * b, res); + if (res != NULL) + return *res; } break; case DIV: { - int b = stack_pop(stack); - int a = stack_pop(stack); + int b = stack_pop(stack, res); + int a = stack_pop(stack, res); + if (res != NULL) + return *res; - stack_push(stack, a / b); + stack_push(stack, a / b, res); + if (res != NULL) + return *res; } break; case MOD: { - int b = stack_pop(stack); - int a = stack_pop(stack); + int b = stack_pop(stack, res); + int a = stack_pop(stack, res); + if (res != NULL) + return *res; - stack_push(stack, a % b); + stack_push(stack, a % b, res); + if (res != NULL) + return *res; } break; - case NONE: - break; default: return -1; } diff --git a/stack.c b/stack.c index 1fb05fa..f67d285 100644 --- a/stack.c +++ b/stack.c @@ -1,14 +1,22 @@ #include "./stack.h" -stack_t *stack_new(int size) +stack_t *stack_new(int size, int *result) { + result = NULL; + stack_t *stack = (stack_t *)malloc(sizeof(stack_t)); if (stack == NULL) - return -1; + { + *result = -1; + return *result; + } stack->mem = (int *)malloc(sizeof(int) * size); if (stack->mem == NULL) - return -1; + { + *result = -1; + return *result; + } stack->size = size; stack->cursor = -1; @@ -16,10 +24,15 @@ stack_t *stack_new(int size) return stack; } -int stack_push(stack_t *stack, int value) +int stack_push(stack_t *stack, int value, int *result) { + result = NULL; + if (stack->cursor > stack->size - 1) - return 1; + { + *result = 1; + return *result; + } stack->mem[stack->cursor + 1] = value; stack->cursor += 1; @@ -27,10 +40,15 @@ int stack_push(stack_t *stack, int value) return 0; } -int stack_pop(stack_t *stack) +int stack_pop(stack_t *stack, int *result) { + result = NULL; + if (stack->cursor == -1) - return 1; + { + *result = 1; + return *result; + } int value = stack->mem[stack->cursor]; stack->cursor -= 1; @@ -38,10 +56,15 @@ int stack_pop(stack_t *stack) return value; } -int stack_get(stack_t *stack, int pos) +int stack_get(stack_t *stack, int pos, int *result) { + result = NULL; + if (pos < 0 || pos >= stack->size) - return 1; + { + *result = 1; + return *result; + } return stack->mem[pos]; } \ No newline at end of file diff --git a/stack.h b/stack.h index 408c658..e964460 100644 --- a/stack.h +++ b/stack.h @@ -10,9 +10,9 @@ typedef struct stack int cursor; } stack_t; -stack_t *stack_new(int size); -int stack_push(stack_t *stack, int value); -int stack_pop(stack_t *stack); -int stack_get(stack_t *stack, int pos); +stack_t *stack_new(int size, int *result); +int stack_push(stack_t *stack, int value, int *result); +int stack_pop(stack_t *stack, int *result); +int stack_get(stack_t *stack, int pos, int *result); #endif \ No newline at end of file