/* abCalcStack.c By: Jeremy Rand */ #include #include #include "abCalcStack.h" #include "abCalcError.h" #define AB_CALC_STACK_DEPTH 128 static abCalcExpr gStack[AB_CALC_STACK_DEPTH]; static int gStackNumItems = 0; void abCalcStackInit(void) { } abCalcExpr *abCalcStackExprPush(abCalcExpr *expr) { abCalcExpr *result = NULL; if (gStackNumItems >= AB_CALC_STACK_DEPTH) { abCalcRaiseError(abCalcStackFullError, NULL); return NULL; } if ((gStackNumItems < AB_CALC_STACK_DEPTH) && (expr != NULL)) { result = &(gStack[gStackNumItems]); memcpy(result, expr, sizeof(*expr)); gStackNumItems++; } return result; } abCalcExpr *abCalcStackExprPop(abCalcExpr *expr) { abCalcExpr *result = NULL; if ((gStackNumItems > 0) && (expr != NULL)) { gStackNumItems--; result = expr; memcpy(expr, &(gStack[gStackNumItems]), sizeof(*expr)); } return result; } int abCalcStackNumItems(void) { return gStackNumItems; } abCalcExpr *abCalcStackExprAt(int depth) { abCalcExpr *result = NULL; if (depth < gStackNumItems) { result = &(gStack[gStackNumItems - 1 - depth]); } return result; }