2013-07-24 23:59:18 +00:00
|
|
|
/*
|
2013-07-25 14:42:02 +00:00
|
|
|
abCOpDiv.c
|
2013-07-24 23:59:18 +00:00
|
|
|
By: Jeremy Rand
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-07-25 14:42:02 +00:00
|
|
|
#include "abCError.h"
|
|
|
|
#include "abCStack.h"
|
2013-07-24 23:59:18 +00:00
|
|
|
|
2013-07-25 15:21:21 +00:00
|
|
|
#include "expr/abCExpr.h"
|
|
|
|
#include "expr/abCExpReal.h"
|
|
|
|
#include "expr/abCExprInt.h"
|
|
|
|
|
|
|
|
#include "ops/abCOp.h"
|
|
|
|
#include "ops/abCOpDiv.h"
|
|
|
|
|
2013-07-24 23:59:18 +00:00
|
|
|
|
2013-07-27 02:27:29 +00:00
|
|
|
#define DIV_NAME "/"
|
2013-07-24 23:59:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void divExecute(void);
|
|
|
|
|
|
|
|
|
|
|
|
void abCalcOpDivInit(void)
|
|
|
|
{
|
2013-07-27 02:27:29 +00:00
|
|
|
abCalcOpRegister(DIV_NAME, divExecute);
|
2013-07-24 23:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void divExecute(void)
|
|
|
|
{
|
|
|
|
abCalcExpr result;
|
|
|
|
char expr1Real = 0;
|
|
|
|
char expr2Real = 0;
|
2013-07-27 02:27:29 +00:00
|
|
|
AB_CALC_OP_TWO_ARGS(DIV_NAME);
|
2013-07-24 23:59:18 +00:00
|
|
|
|
|
|
|
if (expr1->type == abCalcExprTypeReal) {
|
|
|
|
expr1Real = 1;
|
|
|
|
if (expr1->u.real == 0.0) {
|
2013-07-27 02:27:29 +00:00
|
|
|
abCalcRaiseError(abCalcInfiniteResultError, DIV_NAME);
|
2013-07-24 23:59:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (expr1->type == abCalcExprTypeInt) {
|
|
|
|
if (expr1->u.integer == 0l) {
|
2013-07-27 02:27:29 +00:00
|
|
|
abCalcRaiseError(abCalcInfiniteResultError, DIV_NAME);
|
2013-07-24 23:59:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2013-07-27 02:27:29 +00:00
|
|
|
abCalcRaiseError(abCalcBadArgTypeError, DIV_NAME);
|
2013-07-24 23:59:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr2->type == abCalcExprTypeReal) {
|
|
|
|
expr2Real = 1;
|
|
|
|
} else if (expr2->type != abCalcExprTypeInt) {
|
2013-07-27 02:27:29 +00:00
|
|
|
abCalcRaiseError(abCalcBadArgTypeError, DIV_NAME);
|
2013-07-24 23:59:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((expr1Real) && (expr2Real)) {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprRealSet(&result, expr2->u.real / expr1->u.real);
|
2013-07-24 23:59:18 +00:00
|
|
|
} else {
|
|
|
|
if (expr1Real) {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprIntSet(&result, expr2->u.integer / (abCalcIntType)expr1->u.real);
|
2013-07-24 23:59:18 +00:00
|
|
|
} else if (expr2Real) {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprIntSet(&result, (abCalcIntType)expr2->u.real / expr1->u.integer);
|
2013-07-24 23:59:18 +00:00
|
|
|
} else {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprIntSet(&result, expr2->u.integer / expr1->u.integer);
|
2013-07-24 23:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abCalcStackExprPop(NULL);
|
|
|
|
abCalcStackExprPop(NULL);
|
|
|
|
abCalcStackExprPush(&result);
|
|
|
|
}
|