simple-arithmetic-compiler/runtime/OperandStack.c

29 lines
540 B
C

#include "OperandStack.h"
int OStackSize(OperandStack* stack) {
return stack->top;
}
int OStackPush(OperandStack* stack, int operand) {
if (OStackSize(stack) >= TYPE_STACK_SIZE) {
// Stack overflow
return -1;
}
stack->operands[stack->top] = operand;
stack->top++;
return 0;
}
int OStackPop(OperandStack* stack, int* result) {
if (OStackSize(stack) == 0) {
// Stack underflow
return -1;
}
*result = stack->operands[stack->top - 1];
stack->top--;
return 0;
}