Improved error handling
This commit is contained in:
parent
f40633f4f1
commit
8dee74c3d0
74
run.c
74
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;
|
||||
}
|
||||
|
41
stack.c
41
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];
|
||||
}
|
8
stack.h
8
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
|
Loading…
x
Reference in New Issue
Block a user