Improved error handling

This commit is contained in:
Dmitriy Shishkov 2021-04-16 21:09:48 +05:00
parent f40633f4f1
commit 8dee74c3d0
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
3 changed files with 89 additions and 34 deletions

74
run.c
View File

@ -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;
}

41
stack.c
View File

@ -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];
}

View File

@ -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