abCalc/abCMain.c

78 lines
1.6 KiB
C
Raw Normal View History

2013-07-24 17:01:38 +00:00
/*
abCMain.c
2013-07-24 17:01:38 +00:00
By: Jeremy Rand
*/
#include <stdio.h>
2013-07-24 17:01:38 +00:00
#include <string.h>
#include <stdlib.h>
2013-07-24 17:01:38 +00:00
#include "abCalc.h"
#include "abCStack.h"
#include "abCError.h"
2013-07-24 17:01:38 +00:00
#include "expr/abCExpr.h"
#include "ops/abCOp.h"
2013-07-24 17:01:38 +00:00
char gBuffer[AB_CALC_EXPR_STRING_MAX];
abCalcExpr gExpr;
int main(void)
{
2013-07-24 17:01:38 +00:00
int timeToQuit = 0;
int depth;
int item;
int len;
2013-07-24 21:15:11 +00:00
abCalcOp *op;
char *errorString;
2013-07-24 17:01:38 +00:00
abCalcInit();
while (!timeToQuit) {
2013-07-24 23:01:04 +00:00
printf("\n---------------------------\nStack:\n");
2013-07-24 17:01:38 +00:00
depth = abCalcStackNumItems();
if (depth == 0) {
printf(" Empty!\n");
} else {
for(item = depth - 1; item >= 0; item--) {
abCalcFormatExpr(abCalcStackExprAt(item), gBuffer);
printf(" %3d: %s\n", item + 1, gBuffer);
}
}
2013-07-24 21:15:11 +00:00
errorString = abCalcGetError();
if (errorString != NULL) {
2013-07-24 23:01:04 +00:00
printf("\n === %s ===\n", errorString);
2013-07-24 21:15:11 +00:00
abCalcClearError();
}
2013-07-24 23:01:04 +00:00
printf("> ");
2013-07-24 17:01:38 +00:00
if (fgets(gBuffer, sizeof(gBuffer), stdin) != NULL) {
len = strlen(gBuffer);
if ((gBuffer[len - 1] == '\r') ||
(gBuffer[len - 1] == '\n')) {
gBuffer[len - 1] = '\0';
}
2013-07-24 21:15:11 +00:00
op = abCalcOpLookup(gBuffer);
if (op != NULL) {
op->execute();
} else if (abCalcParseExpr(&gExpr, gBuffer) != NULL) {
abCalcStackExprPush(&gExpr);
} else {
abCalcRaiseError(abCalcSyntaxError, NULL);
2013-07-24 17:01:38 +00:00
}
2013-07-24 23:01:04 +00:00
} else {
timeToQuit = 1;
2013-07-24 17:01:38 +00:00
}
}
exit(0);
}