Begin implementation of expressions and stack

This commit is contained in:
Jeremy Rand 2013-07-24 09:30:44 -05:00
parent 1e6ae9335e
commit cca7b583ea
5 changed files with 158 additions and 3 deletions

View File

@ -3,6 +3,9 @@ NAME=abCalc
all: $(NAME)
abCalcExpr.o: abCalcExpr.h
abCalcStack.o: abCalcExpr.h abCalcStack.h
$(NAME): $(OBJS)
cc -o $(NAME) $(OBJS)

View File

@ -1 +1,61 @@
/*
abCalcExpr.c
By: Jeremy Rand
*/
#include <stdlib.h>
#include "abCalcExpr.h"
static abCalcExprCallbacks *gCallbacks[abCalcExprTypeMax];
#define AB_CALC_EXPR_TYPE_INVALID(type) (((type) < abCalcExprTypeMin) || ((type) >= abCalcExprTypeMax))
void abCalcExprInit(void)
{
abCalcExprType type;
for (type = abCalcExprTypeMin; type < abCalcExprTypeMax; type++) {
gCallbacks[type] = NULL;
}
}
void abCalcRegisterExprType(abCalcExprType type, abCalcExprCallbacks *callbacks)
{
if (AB_CALC_EXPR_TYPE_INVALID(type))
return;
gCallbacks[type] = callbacks;
}
abCalcExpr *abCalcParseExpr(abCalcExpr *expr, char *buffer)
{
abCalcExpr *result = NULL;
abCalcExprType type;
for (type = abCalcExprTypeMin; type < abCalcExprTypeMax; type++) {
if (gCallbacks[type] != NULL) {
result = (gCallbacks[type]->parseExpr)(expr, buffer);
if (result != NULL)
return result;
}
}
return result;
}
char *abCalcFormatExpr(abCalcExpr *expr, char *buffer)
{
if (AB_CALC_EXPR_TYPE_INVALID(expr->type))
return NULL;
if (gCallbacks[expr->type] == NULL)
return NULL;
return (gCallbacks[expr->type]->formatExpr(expr, buffer));
}

View File

@ -5,8 +5,11 @@
typedef enum abCalcExprType {
abCalcExprTypeReal,
abCalcExprTypeInt
abCalcExprTypeMin = 0,
abCalcExprTypeReal = 0,
abCalcExprTypeInt,
abCalcExprTypeMax
} abCalcExprType;
@ -24,5 +27,13 @@ typedef struct abCalcExpr {
typedef struct abCalcExprCallbacks {
abCalcExpr * (*parseExpression)(abCalcExpr *expr, char *buffer);
abCalcExpr * (*parseExpr)(abCalcExpr *expr, char *buffer);
char * (*formatExpr)(abCalcExpr *expr, char *buffer);
} abCalcExprCallbacks;
void abCalcExprInit(void);
void abCalcRegisterExprType(abCalcExprType type, abCalcExprCallbacks *callbacks);
abCalcExpr *abCalcParseExpr(abCalcExpr *expr, char *buffer);
char *abCalcFormatExpr(abCalcExpr *expr, char *buffer);

View File

@ -0,0 +1,66 @@
/*
abCalcStack.c
By: Jeremy Rand
*/
#include <stdlib.h>
#include <string.h>
#include "abCalcStack.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) &&
(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(abCalcExpr *expr, int depth)
{
abCalcExpr *result = NULL;
if ((depth < gStackNumItems) &&
(expr != NULL)) {
result = &(gStack[gStackNumItems - 1 - depth]);
}
return result;
}

15
abCalcStack.h Normal file
View File

@ -0,0 +1,15 @@
/*
abCalcStack.h
By: Jeremy Rand
*/
#include "abCalcExpr.h"
void abCalcStackInit(void);
abCalcExpr *abCalcStackExprPush(abCalcExpr *expr);
abCalcExpr *abCalcStackExprPop(abCalcExpr *expr);
abCalcExpr *abCalcStackExprAt(abCalcExpr *expr, int depth);