abCalc/abCalcOpAdd.c

68 lines
1.4 KiB
C
Raw Normal View History

2013-07-24 21:15:11 +00:00
/*
abCalcOpAdd.c
By: Jeremy Rand
*/
#include <stdio.h>
#include "abCalcOpAdd.h"
2013-07-24 23:01:04 +00:00
2013-07-24 21:15:11 +00:00
#include "abCalcOp.h"
2013-07-24 23:01:04 +00:00
#include "abCalcError.h"
#include "abCalcExpr.h"
#include "abCalcExpReal.h"
#include "abCalcExprInt.h"
2013-07-24 23:01:04 +00:00
#include "abCalcStack.h"
2013-07-24 21:15:11 +00:00
#define OP_NAME "+"
static void addExecute(void);
void abCalcOpAddInit(void)
{
abCalcOpRegister(OP_NAME, addExecute);
}
void addExecute(void)
{
2013-07-24 23:01:04 +00:00
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
} else if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
return;
}
if ((expr1Real) && (expr2Real)) {
abCalcExprRealSet(&result, expr2->u.real + expr1->u.real);
2013-07-24 23:01:04 +00:00
} else {
if (expr1Real) {
abCalcExprIntSet(&result, expr2->u.integer + (abCalcIntType)expr1->u.real);
2013-07-24 23:01:04 +00:00
} else if (expr2Real) {
abCalcExprIntSet(&result, (abCalcIntType)expr2->u.real + expr1->u.integer);
2013-07-24 23:01:04 +00:00
} else {
abCalcExprIntSet(&result, expr2->u.integer + expr1->u.integer);
2013-07-24 23:01:04 +00:00
}
}
abCalcStackExprPop(NULL);
abCalcStackExprPop(NULL);
abCalcStackExprPush(&result);
2013-07-24 21:15:11 +00:00
}