mirror of
https://github.com/jeremysrand/abCalc.git
synced 2024-11-27 05:49:44 +00:00
Begin implementation of expressions and stack
This commit is contained in:
parent
1e6ae9335e
commit
cca7b583ea
3
Makefile
3
Makefile
@ -3,6 +3,9 @@ NAME=abCalc
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
abCalcExpr.o: abCalcExpr.h
|
||||
abCalcStack.o: abCalcExpr.h abCalcStack.h
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
cc -o $(NAME) $(OBJS)
|
||||
|
||||
|
60
abCalcExpr.c
60
abCalcExpr.c
@ -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));
|
||||
}
|
||||
|
17
abCalcExpr.h
17
abCalcExpr.h
@ -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);
|
||||
|
@ -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
15
abCalcStack.h
Normal 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);
|
Loading…
Reference in New Issue
Block a user