abCalc/abCalcOpDiv.c

78 lines
1.7 KiB
C
Raw Normal View History

2013-07-24 23:59:18 +00:00
/*
abCalcOpDiv.c
By: Jeremy Rand
*/
#include <stdio.h>
#include "abCalcOpDiv.h"
#include "abCalcOp.h"
#include "abCalcError.h"
#include "abCalcExpr.h"
#include "abCalcStack.h"
#define OP_NAME "/"
static void divExecute(void);
void abCalcOpDivInit(void)
{
abCalcOpRegister(OP_NAME, divExecute);
}
void divExecute(void)
{
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
if (expr1->u.real == 0.0) {
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
return;
}
} else if (expr1->type == abCalcExprTypeInt) {
if (expr1->u.integer == 0l) {
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
return;
}
} else {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
return;
}
if ((expr1Real) && (expr2Real)) {
result.type = abCalcExprTypeReal;
result.u.real = expr2->u.real / expr1->u.real;
} else {
result.type = abCalcExprTypeInt;
if (expr1Real) {
result.u.integer = expr2->u.integer / (abCalcIntType)expr1->u.real;
} else if (expr2Real) {
result.u.integer = (abCalcIntType)expr2->u.real / expr1->u.integer;
} else {
result.u.integer = expr2->u.integer / expr1->u.integer;
}
}
abCalcStackExprPop(NULL);
abCalcStackExprPop(NULL);
abCalcStackExprPush(&result);
}