Fighting with crashes in the NDA and giving up for now so adding more operations

This commit is contained in:
Jeremy Rand 2013-07-26 21:27:29 -05:00
parent c5ca693973
commit 9f2f1c5d53
37 changed files with 535 additions and 103 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -12,7 +12,7 @@ OPS_OBJS=ops/abCOp.o ops/abCOpAdd.o ops/abCOpSubtr.o ops/abCOpMult.o \
ops/abCOpDiv.o ops/abCOpPower.o ops/abCOpAnd.o ops/abCOpOr.o ops/abCOpXor.o \
ops/abCOpNot.o ops/abCOpBin.o ops/abCOpOct.o ops/abCOpDec.o ops/abCOpHex.o \
ops/abCOpStws.o ops/abCOpRcws.o ops/abCOpSl.o ops/abCOpRl.o ops/abCOpSr.o \
ops/abCOpRr.o ops/abCOpAsr.o
ops/abCOpRr.o ops/abCOpAsr.o ops/abCOpInv.o ops/abCOpChs.o ops/abCOpDrop.o
SHELL_OBJS=abCMain.o $(CALC_OBJS) $(EXPR_OBJS) $(OPS_OBJS)
@ -20,7 +20,7 @@ NDA_OBJS=abCalcNDA.o $(CALC_OBJS) $(EXPR_OBJS) $(OPS_OBJS)
NDA_RES=abCalcNDA.r
abCMain.o: abCalc.h abCStack.h expr/abCExpr.h ops/abCOp.h abCError.h
abCalcNDA.o: abCalcNDA.defs abCalc.h
abCalcNDA.o: abCalcNDA.defs abCalc.h abCStack.h abCError.h expr/abCExpr.h ops/abCOp.h
abCalcNDA.r: abCalcNDA.defs
abCalc.o: abCalc.h expr/abCExpr.h abCMode.h expr/abCExpReal.h expr/abCExprInt.h \
@ -58,4 +58,7 @@ ops/abCOpRl.o: ops/abCOp.h abCMode.h abCError.h abCStack.h expr/abCExpr.h expr/a
ops/abCOpSr.o: ops/abCOp.h abCError.h abCStack.h expr/abCExpr.h expr/abCExprInt.h ops/abCOpSr.h
ops/abCOpRr.o: ops/abCOp.h abCMode.h abCError.h abCStack.h expr/abCExpr.h expr/abCExprInt.h ops/abCOpRr.h
ops/abCOpAsr.o: ops/abCOp.h abCMode.h abCError.h abCStack.h expr/abCExpr.h expr/abCExprInt.h ops/abCOpAsr.h
ops/abCOpInv.o: ops/abCOp.h abCError.h abCStack.h expr/abCExpr.h expr/abCExpReal.h ops/abCOpInv.h
ops/abCOpChs.o: ops/abCOp.h abCError.h abCStack.h expr/abCExpr.h expr/abCExpReal.h ops/abCOpChs.h
ops/abCOpDrop.o: ops/abCOp.h abCError.h abCStack.h expr/abCExpr.h ops/abCOpDrop.h

View File

@ -15,7 +15,7 @@ static char *gErrorStrings[abCalcErrorTypeMax];
static abCalcErrorType gCurrErrorType = abCalcNoError;
static char *gCurrErrorOpName = NULL;
static char gErrorBuffer[128];
char gErrorBuffer[128];
void abCalcErrorInit(void)

View File

@ -4,6 +4,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -11,10 +12,7 @@
#include "abCError.h"
#define AB_CALC_STACK_DEPTH 128
static abCalcExpr gStack[AB_CALC_STACK_DEPTH];
abCalcExpr gStack[AB_CALC_STACK_DEPTH];
static int gStackNumItems = 0;
@ -75,3 +73,21 @@ abCalcExpr *abCalcStackExprAt(int depth)
return result;
}
char *abCalcStackExprStringAt(int depth, char *buffer)
{
static char tmpBuffer[AB_CALC_EXPR_STRING_MAX];
if (buffer == NULL)
return NULL;
sprintf(buffer, "%3d: ", depth + 1);
if (depth < gStackNumItems) {
if (abCalcFormatExpr(&(gStack[gStackNumItems - 1 - depth]), tmpBuffer) != NULL) {
strcat(buffer, tmpBuffer);
}
}
return buffer;
}

View File

@ -11,6 +11,9 @@
#include "expr/abCExpr.h"
#define AB_CALC_STACK_DEPTH 128
void abCalcStackInit(void);
abCalcExpr *abCalcStackExprPush(abCalcExpr *expr);
@ -19,6 +22,8 @@ abCalcExpr *abCalcStackExprPop(abCalcExpr *expr);
abCalcExpr *abCalcStackExprAt(int depth);
char *abCalcStackExprStringAt(int depth, char *buffer);
int abCalcStackNumItems(void);

View File

@ -5,6 +5,7 @@
#pragma nda NDAOpen NDAClose NDAAction NDAInit 0 0xFFFF " abCalc\\H**"
#pragma stacksize 4096
#include <orca.h>
@ -17,23 +18,62 @@
#include <Loader.h>
#include <Control.h>
#include <Event.h>
#include <List.h>
#include <string.h>
#include <stdlib.h>
#include "abCalcNDA.defs"
#include "abCalc.h"
#include "abCStack.h"
#include "abCError.h"
#include "expr/abCExpr.h"
#include "ops/abCOp.h"
void UpdateStack(Boolean draw);
typedef struct listElement {
char *memPtr;
Byte memFlag;
} listElement;
static BOOLEAN gCalcActive = FALSE;
static GrafPortPtr gCalcWinPtr = NULL;
static unsigned int gUserId;
static unsigned int gResourceId;
listElement *gOpList = NULL;
listElement gStackList[AB_CALC_STACK_DEPTH];
abCalcExpr gNDAExpr;
void NDAClose(void)
{
int i;
int numOps;
if (gCalcActive) {
CloseWindow(gCalcWinPtr);
gCalcWinPtr = NULL;
gCalcActive = FALSE;
numOps = abCalcOpNumOps();
for (i = 0; i < numOps; i++) {
free(gOpList[i].memPtr);
}
free(gOpList);
gOpList = NULL;
for (i = 0; i < AB_CALC_STACK_DEPTH; i++) {
if (gStackList[i].memPtr != NULL) {
free(gStackList[i].memPtr);
gStackList[i].memPtr = NULL;
}
}
}
CloseResourceFile(gResourceId);
ResourceShutDown();
@ -42,10 +82,16 @@ void NDAClose(void)
void NDAInit(int code)
{
int i;
if (code) {
gCalcActive = FALSE;
gUserId = MMStartUp();
abCalcInit();
for (i = 0; i < AB_CALC_STACK_DEPTH; i++) {
gStackList[i].memPtr = NULL;
}
} else if (gCalcActive) {
NDAClose();
}
@ -69,6 +115,9 @@ GrafPortPtr NDAOpen(void)
unsigned int oldLevel;
SysPrefsRecGS prefsDCB;
unsigned int oldPrefs;
int numOps;
int i;
Handle opListCtl;
if (gCalcActive)
return NULL;
@ -98,6 +147,19 @@ GrafPortPtr NDAOpen(void)
ShowWindow(gCalcWinPtr);
SelectWindow(gCalcWinPtr);
if (gOpList == NULL) {
numOps = abCalcOpNumOps();
gOpList = malloc(sizeof(*gOpList) * numOps);
for (i = 0; i < numOps; i++) {
gOpList[i].memPtr = abCalcOpNth(i)->name;
gOpList[i].memFlag = 0;
}
}
opListCtl = (Handle)GetCtlHandleFromID(gCalcWinPtr, abCalcOpList);
NewList2(NULL, 1, (Ref)gOpList, 0, numOps, opListCtl);
UpdateStack(FALSE);
gCalcActive = TRUE;
prefsDCB.preferences = oldPrefs;
@ -112,6 +174,150 @@ GrafPortPtr NDAOpen(void)
}
void UpdateStack(BOOLEAN draw)
{
Handle stackListCtl;
int i;
int numOnStack = abCalcStackNumItems();
int numToDisplay = numOnStack;
if (numToDisplay < 4) {
numToDisplay = 4;
}
stackListCtl = (Handle)GetCtlHandleFromID(gCalcWinPtr, abCalcStackList);
for (i = numToDisplay - 1; i >= 0; i--) {
if (gStackList[i].memPtr == NULL) {
gStackList[i].memPtr = malloc(AB_CALC_EXPR_STRING_MAX + 8);
}
abCalcStackExprStringAt(i, gStackList[i].memPtr);
gStackList[i].memFlag = 0;
}
NewList2(NULL, 1, (Ref)gStackList, 0, numToDisplay, stackListCtl);
if (draw)
DrawMember2(0, stackListCtl);
}
void DoCalcStuff(char *cmd)
{
abCalcOp *op = abCalcOpLookup(cmd);
if (op != NULL) {
op->execute();
} else if (abCalcParseExpr(&gNDAExpr, cmd) != NULL) {
abCalcStackExprPush(&gNDAExpr);
} else {
abCalcRaiseError(abCalcSyntaxError, NULL);
}
UpdateStack(TRUE);
}
void HandleControl(EventRecord *event)
{
static Str255 strBuf;
CtlRecHndl entryBox = GetCtlHandleFromID(gCalcWinPtr, abCalcEntryBox);
switch (event->wmTaskData4) {
case abCalcBtn0:
break;
case abCalcBtn1:
break;
case abCalcBtn2:
break;
case abCalcBtn3:
break;
case abCalcBtn4:
break;
case abCalcBtn5:
break;
case abCalcBtn6:
break;
case abCalcBtn7:
break;
case abCalcBtn8:
break;
case abCalcBtn9:
break;
case abCalcBtnEnter:
GetLETextByID(gCalcWinPtr, abCalcEntryBox, &strBuf);
strBuf.text[strBuf.textLength] = '\0';
DoCalcStuff(strBuf.text);
strBuf.textLength = 0;
SetLETextByID(gCalcWinPtr, abCalcEntryBox, &strBuf);
break;
case abCalcBtnDot:
break;
case abCalcBtnNum:
break;
case abCalcBtnAdd:
DoCalcStuff("+");
break;
case abCalcBtnSub:
DoCalcStuff("-");
break;
case abCalcBtnMult:
DoCalcStuff("*");
break;
case abCalcBtnDiv:
DoCalcStuff("/");
break;
case abCalcBtnPow:
DoCalcStuff("^");
break;
case abCalcBtnA:
break;
case abCalcBtnB:
break;
case abCalcBtnC:
break;
case abCalcBtnD:
break;
case abCalcBtnE:
break;
case abCalcBtnF:
break;
case abCalcEntryBox:
break;
case abCalcStackList:
break;
case abCalcOpList:
break;
}
}
BOOLEAN NDAAction(EventRecord *sysEvent, int code)
{
int event;
@ -133,6 +339,7 @@ BOOLEAN NDAAction(EventRecord *sysEvent, int code)
break;
case wInControl:
HandleControl(&localEvent);
break;
}
}

View File

@ -218,10 +218,12 @@ resource rControlTemplate (abCalcBtnEnter) {
abCalcBtnEnter,
{abCalcBtnRow4, abCalcBtnCol0, abCalcBtnRow4+abCalcBtnHeight, abCalcBtnCol2+abCalcBtnWidth},
SimpleButtonControl {{
$0002,
$0003,
$3002,
0,
abCalcBtnEnterStr
abCalcBtnEnterStr,
0,
{"\n","",0,0}
}};
};
resource rPString (abCalcBtnEnterStr, noCrossBank) { "Enter" };
@ -360,14 +362,14 @@ resource rPString (abCalcBtnFStr, noCrossBank) { "F" };
resource rControlTemplate (abCalcStackList) {
abCalcStackList, /* control ID */
{abCalcBtnSpaceY, abCalcBtnSpaceX, abCalcBtnRow7-abCalcBtnSpaceX, abCalcBtnCol3+abCalcBtnWidth-23}, /* control rect */
{1, abCalcBtnSpaceX, 41, abCalcWinWidth-abCalcBtnSpaceX}, /* control rect */
ListControl {{
$0000, /* flags */
$1400, /* more flags */
0, /* refcon */
0, /* list size */
4, /* List View */
$0000, /* List Type */
$0007, /* List Type */
0, /* List Start */
10, /* ListMemHeight */
5, /* List Mem Size */
@ -378,14 +380,14 @@ resource rControlTemplate (abCalcStackList) {
resource rControlTemplate (abCalcOpList) {
abCalcOpList, /* control ID */
{abCalcBtnSpaceY, abCalcOpCol, abCalcWinHeight-abCalcBtnSpaceY, abCalcWinWidth-abCalcBtnSpaceX-23}, /* control rect */
{abCalcBtnRow7, abCalcOpCol, abCalcWinHeight-8, abCalcWinWidth-abCalcBtnSpaceX}, /* control rect */
ListControl {{
$0000, /* flags */
$1400, /* more flags */
0, /* refcon */
0, /* list size */
4, /* List View */
$0000, /* List Type */
$0007, /* List Type */
0, /* List Start */
10, /* ListMemHeight */
5, /* List Mem Size */
@ -401,7 +403,7 @@ resource rControlTemplate (abCalcEntryBox) {
$0000, /* flags */
$7000, /* more flags */
0, /* refcon */
20, /* Max size */
34, /* Max size */
0 /* text Ref */
}};
};

View File

@ -16,7 +16,7 @@ static abCalcExpr *abCalcExprRealParse(abCalcExpr *expr, char *buffer);
static char *abCalcExprRealFormat(abCalcExpr *expr, char *buffer);
static abCalcExprCallbacks gCallbacks = {
static abCalcExprCallbacks gRealCallbacks = {
abCalcExprRealParse,
abCalcExprRealFormat
};
@ -24,7 +24,7 @@ static abCalcExprCallbacks gCallbacks = {
void abCalcExprRealInit(void)
{
abCalcRegisterExprType(abCalcExprTypeReal, &gCallbacks);
abCalcRegisterExprType(abCalcExprTypeReal, &gRealCallbacks);
}
@ -104,7 +104,7 @@ char *abCalcExprRealFormat(abCalcExpr *expr, char *buffer)
{
abCalcRealType exp;
abCalcRealType value;
char format[16];
static char format[16];
int numDecDigits;
int periodPos = -1;
int zerosStart = -1;

View File

@ -17,7 +17,7 @@ static abCalcExpr *abCalcExprIntParse(abCalcExpr *expr, char *buffer);
static char *abCalcExprIntFormat(abCalcExpr *expr, char *buffer);
static abCalcExprCallbacks gCallbacks = {
static abCalcExprCallbacks gIntCallbacks = {
abCalcExprIntParse,
abCalcExprIntFormat
};
@ -25,7 +25,7 @@ static abCalcExprCallbacks gCallbacks = {
void abCalcExprIntInit(void)
{
abCalcRegisterExprType(abCalcExprTypeInt, &gCallbacks);
abCalcRegisterExprType(abCalcExprTypeInt, &gIntCallbacks);
}

View File

@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "abCError.h"
#include "abCStack.h"
@ -19,6 +20,8 @@
#include "ops/abCOpMult.h"
#include "ops/abCOpDiv.h"
#include "ops/abCOpPower.h"
#include "ops/abCOpInv.h"
#include "ops/abCOpChs.h"
#include "ops/abCOpAnd.h"
#include "ops/abCOpOr.h"
@ -41,7 +44,7 @@
#define AB_CALC_MAX_OPS 128
static abCalcOp gOps[AB_CALC_MAX_OPS];
abCalcOp gOps[AB_CALC_MAX_OPS];
static int gNumOps = 0;
@ -54,6 +57,10 @@ void abCalcOpInit(void)
abCalcOpMultInit();
abCalcOpDivInit();
abCalcOpPowerInit();
abCalcOpInvInit();
abCalcOpChsInit();
abCalcOpDropInit();
abCalcOpAndInit();
abCalcOpOrInit();
@ -105,3 +112,17 @@ abCalcOp *abCalcOpLookup(char *name)
}
abCalcOp *abCalcOpNth(int n)
{
if ((n < 0) ||
(n >= gNumOps))
return NULL;
return &gOps[n];
}
int abCalcOpNumOps(void)
{
return gNumOps;
}

View File

@ -37,5 +37,8 @@ void abCalcOpRegister(char *name, void (*execute)(void));
abCalcOp *abCalcOpLookup(char *name);
abCalcOp *abCalcOpNth(int n);
int abCalcOpNumOps(void);
#endif

View File

@ -18,7 +18,7 @@
#include "ops/abCOpAdd.h"
#define OP_NAME "+"
#define ADD_NAME "+"
static void addExecute(void);
@ -26,7 +26,7 @@ static void addExecute(void);
void abCalcOpAddInit(void)
{
abCalcOpRegister(OP_NAME, addExecute);
abCalcOpRegister(ADD_NAME, addExecute);
}
@ -35,19 +35,19 @@ void addExecute(void)
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(ADD_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
} else if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, ADD_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, ADD_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpAnd.h"
#define OP_NAME "AND"
#define AND_NAME "AND"
static void andExecute(void);
@ -24,22 +24,22 @@ static void andExecute(void);
void abCalcOpAndInit(void)
{
abCalcOpRegister(OP_NAME, andExecute);
abCalcOpRegister(AND_NAME, andExecute);
}
void andExecute(void)
{
abCalcExpr result;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(AND_NAME);
if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, AND_NAME);
return;
}
if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, AND_NAME);
return;
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpAsr.h"
#define OP_NAME "ASR"
#define ASR_NAME "ASR"
static void asrExecute(void);
@ -25,7 +25,7 @@ static void asrExecute(void);
void abCalcOpAsrInit(void)
{
abCalcOpRegister(OP_NAME, asrExecute);
abCalcOpRegister(ASR_NAME, asrExecute);
}
@ -34,10 +34,10 @@ void asrExecute(void)
abCalcExpr result;
int width;
abCalcIntType upperBit;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(ASR_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, ASR_NAME);
return;
}

View File

@ -12,7 +12,7 @@
#include "ops/abCOpBin.h"
#define OP_NAME "BIN"
#define BIN_NAME "BIN"
static void binExecute(void);
@ -20,7 +20,7 @@ static void binExecute(void);
void abCalcOpBinInit(void)
{
abCalcOpRegister(OP_NAME, binExecute);
abCalcOpRegister(BIN_NAME, binExecute);
}

46
ops/abCOpChs.c Normal file
View File

@ -0,0 +1,46 @@
/*
abCOpChs.c
By: Jeremy Rand
*/
#include <math.h>
#include <stdio.h>
#include "abCError.h"
#include "abCStack.h"
#include "expr/abCExpr.h"
#include "expr/abCExpReal.h"
#include "ops/abCOp.h"
#include "ops/abCOpChs.h"
#define CHS_NAME "CHS"
static void chsExecute(void);
void abCalcOpChsInit(void)
{
abCalcOpRegister(CHS_NAME, chsExecute);
}
void chsExecute(void)
{
abCalcExpr result;
AB_CALC_OP_ONE_ARG(CHS_NAME);
if (expr->type != abCalcExprTypeReal) {
abCalcRaiseError(abCalcBadArgTypeError, CHS_NAME);
return;
}
abCalcExprRealSet(&result, -1.0 * expr->u.real);
abCalcStackExprPop(NULL);
abCalcStackExprPush(&result);
}

14
ops/abCOpChs.h Normal file
View File

@ -0,0 +1,14 @@
/*
abCOpChs.h
By: Jeremy Rand
*/
#ifndef ABCOPCHS_H
#define ABCOPCHS_H
void abCalcOpChsInit(void);
#endif

View File

@ -12,7 +12,7 @@
#include "ops/abCOpDec.h"
#define OP_NAME "DEC"
#define DEC_NAME "DEC"
static void decExecute(void);
@ -20,7 +20,7 @@ static void decExecute(void);
void abCalcOpDecInit(void)
{
abCalcOpRegister(OP_NAME, decExecute);
abCalcOpRegister(DEC_NAME, decExecute);
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpDiv.h"
#define OP_NAME "/"
#define DIV_NAME "/"
static void divExecute(void);
@ -25,7 +25,7 @@ static void divExecute(void);
void abCalcOpDivInit(void)
{
abCalcOpRegister(OP_NAME, divExecute);
abCalcOpRegister(DIV_NAME, divExecute);
}
@ -34,28 +34,28 @@ void divExecute(void)
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(DIV_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
if (expr1->u.real == 0.0) {
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
abCalcRaiseError(abCalcInfiniteResultError, DIV_NAME);
return;
}
} else if (expr1->type == abCalcExprTypeInt) {
if (expr1->u.integer == 0l) {
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
abCalcRaiseError(abCalcInfiniteResultError, DIV_NAME);
return;
}
} else {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, DIV_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, DIV_NAME);
return;
}

36
ops/abCOpDrop.c Normal file
View File

@ -0,0 +1,36 @@
/*
abCOpDrop.c
By: Jeremy Rand
*/
#include <math.h>
#include <stdio.h>
#include "abCError.h"
#include "abCStack.h"
#include "expr/abCExpr.h"
#include "ops/abCOp.h"
#include "ops/abCOpDrop.h"
#define DROP_NAME "DROP"
static void dropExecute(void);
void abCalcOpDropInit(void)
{
abCalcOpRegister(DROP_NAME, dropExecute);
}
void dropExecute(void)
{
AB_CALC_OP_ONE_ARG(DROP_NAME);
abCalcStackExprPop(NULL);
}

14
ops/abCOpDrop.h Normal file
View File

@ -0,0 +1,14 @@
/*
abCOpDrop.h
By: Jeremy Rand
*/
#ifndef ABCOPDROP_H
#define ABCOPDROP_H
void abCalcOpDropInit(void);
#endif

View File

@ -12,7 +12,7 @@
#include "ops/abCOpHex.h"
#define OP_NAME "HEX"
#define HEX_NAME "HEX"
static void hexExecute(void);
@ -20,7 +20,7 @@ static void hexExecute(void);
void abCalcOpHexInit(void)
{
abCalcOpRegister(OP_NAME, hexExecute);
abCalcOpRegister(HEX_NAME, hexExecute);
}

51
ops/abCOpInv.c Normal file
View File

@ -0,0 +1,51 @@
/*
abCOpInv.c
By: Jeremy Rand
*/
#include <math.h>
#include <stdio.h>
#include "abCError.h"
#include "abCStack.h"
#include "expr/abCExpr.h"
#include "expr/abCExpReal.h"
#include "ops/abCOp.h"
#include "ops/abCOpInv.h"
#define INV_NAME "INV"
static void invExecute(void);
void abCalcOpInvInit(void)
{
abCalcOpRegister(INV_NAME, invExecute);
}
void invExecute(void)
{
abCalcExpr result;
AB_CALC_OP_ONE_ARG(INV_NAME);
if (expr->type != abCalcExprTypeReal) {
abCalcRaiseError(abCalcBadArgTypeError, INV_NAME);
return;
}
if (expr->u.real == 0.0) {
abCalcRaiseError(abCalcInfiniteResultError, INV_NAME);
return;
}
abCalcExprRealSet(&result, 1.0 / expr->u.real);
abCalcStackExprPop(NULL);
abCalcStackExprPush(&result);
}

14
ops/abCOpInv.h Normal file
View File

@ -0,0 +1,14 @@
/*
abCOpInv.h
By: Jeremy Rand
*/
#ifndef ABCOPINV_H
#define ABCOPINV_H
void abCalcOpInvInit(void);
#endif

View File

@ -17,7 +17,7 @@
#include "ops/abCOpMult.h"
#define OP_NAME "*"
#define MUL_NAME "*"
static void multExecute(void);
@ -25,7 +25,7 @@ static void multExecute(void);
void abCalcOpMultInit(void)
{
abCalcOpRegister(OP_NAME, multExecute);
abCalcOpRegister(MUL_NAME, multExecute);
}
@ -34,19 +34,19 @@ void multExecute(void)
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(MUL_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
} else if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, MUL_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, MUL_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpNot.h"
#define OP_NAME "NOT"
#define NOT_NAME "NOT"
static void notExecute(void);
@ -24,17 +24,17 @@ static void notExecute(void);
void abCalcOpNotInit(void)
{
abCalcOpRegister(OP_NAME, notExecute);
abCalcOpRegister(NOT_NAME, notExecute);
}
void notExecute(void)
{
abCalcExpr result;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(NOT_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, NOT_NAME);
return;
}

View File

@ -12,7 +12,7 @@
#include "ops/abCOpOct.h"
#define OP_NAME "OCT"
#define OCT_NAME "OCT"
static void octExecute(void);
@ -20,7 +20,7 @@ static void octExecute(void);
void abCalcOpOctInit(void)
{
abCalcOpRegister(OP_NAME, octExecute);
abCalcOpRegister(OCT_NAME, octExecute);
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpOr.h"
#define OP_NAME "OR"
#define OR_NAME "OR"
static void orExecute(void);
@ -24,22 +24,22 @@ static void orExecute(void);
void abCalcOpOrInit(void)
{
abCalcOpRegister(OP_NAME, orExecute);
abCalcOpRegister(OR_NAME, orExecute);
}
void orExecute(void)
{
abCalcExpr result;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(OR_NAME);
if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, OR_NAME);
return;
}
if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, OR_NAME);
return;
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpPower.h"
#define OP_NAME "^"
#define POWER_NAME "^"
static void powerExecute(void);
@ -25,7 +25,7 @@ static void powerExecute(void);
void abCalcOpPowerInit(void)
{
abCalcOpRegister(OP_NAME, powerExecute);
abCalcOpRegister(POWER_NAME, powerExecute);
}
@ -33,21 +33,21 @@ void powerExecute(void)
{
abCalcExpr result;
double integral;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(POWER_NAME);
if (expr1->type != abCalcExprTypeReal) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, POWER_NAME);
return;
}
if (expr2->type != abCalcExprTypeReal) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, POWER_NAME);
return;
}
if (expr2->u.real == 0.0) {
if (expr1->u.real < 0.0) {
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
abCalcRaiseError(abCalcInfiniteResultError, POWER_NAME);
return;
}
if (expr1->u.real == 0.0) {
@ -59,7 +59,7 @@ void powerExecute(void)
if (expr2->u.real < 0.0) {
modf(expr1->u.real, &integral);
if (expr1->u.real != integral) {
abCalcRaiseError(abCalcComplexResultError, OP_NAME);
abCalcRaiseError(abCalcComplexResultError, POWER_NAME);
return;
}
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpRcws.h"
#define OP_NAME "RCWS"
#define RCWS_NAME "RCWS"
static void rcwsExecute(void);
@ -24,7 +24,7 @@ static void rcwsExecute(void);
void abCalcOpRcwsInit(void)
{
abCalcOpRegister(OP_NAME, rcwsExecute);
abCalcOpRegister(RCWS_NAME, rcwsExecute);
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpRl.h"
#define OP_NAME "RL"
#define RL_NAME "RL"
static void rlExecute(void);
@ -25,7 +25,7 @@ static void rlExecute(void);
void abCalcOpRlInit(void)
{
abCalcOpRegister(OP_NAME, rlExecute);
abCalcOpRegister(RL_NAME, rlExecute);
}
@ -34,10 +34,10 @@ void rlExecute(void)
abCalcExpr result;
int width;
int topBit;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(RL_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, RL_NAME);
return;
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpRr.h"
#define OP_NAME "RR"
#define RR_NAME "RR"
static void rrExecute(void);
@ -25,7 +25,7 @@ static void rrExecute(void);
void abCalcOpRrInit(void)
{
abCalcOpRegister(OP_NAME, rrExecute);
abCalcOpRegister(RR_NAME, rrExecute);
}
@ -34,10 +34,10 @@ void rrExecute(void)
abCalcExpr result;
int width;
abCalcIntType bottomBit;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(RR_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, RR_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpSl.h"
#define OP_NAME "SL"
#define SL_NAME "SL"
static void slExecute(void);
@ -24,17 +24,17 @@ static void slExecute(void);
void abCalcOpSlInit(void)
{
abCalcOpRegister(OP_NAME, slExecute);
abCalcOpRegister(SL_NAME, slExecute);
}
void slExecute(void)
{
abCalcExpr result;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(SL_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, SL_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpSr.h"
#define OP_NAME "SR"
#define SR_NAME "SR"
static void srExecute(void);
@ -24,17 +24,17 @@ static void srExecute(void);
void abCalcOpSrInit(void)
{
abCalcOpRegister(OP_NAME, srExecute);
abCalcOpRegister(SR_NAME, srExecute);
}
void srExecute(void)
{
abCalcExpr result;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(SR_NAME);
if (expr->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, SR_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpStws.h"
#define OP_NAME "STWS"
#define STWS_NAME "STWS"
static void stwsExecute(void);
@ -24,24 +24,24 @@ static void stwsExecute(void);
void abCalcOpStwsInit(void)
{
abCalcOpRegister(OP_NAME, stwsExecute);
abCalcOpRegister(STWS_NAME, stwsExecute);
}
void stwsExecute(void)
{
int newWidth;
AB_CALC_OP_ONE_ARG(OP_NAME);
AB_CALC_OP_ONE_ARG(STWS_NAME);
if (expr->type != abCalcExprTypeReal) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, STWS_NAME);
return;
}
newWidth = (int)expr->u.real;
if ((newWidth < 1) ||
(newWidth > AB_CALC_EXPR_MAX_INT_WIDTH)) {
abCalcRaiseError(abCalcBadArgValueError, OP_NAME);
abCalcRaiseError(abCalcBadArgValueError, STWS_NAME);
return;
}

View File

@ -17,7 +17,7 @@
#include "ops/abCOpSubtr.h"
#define OP_NAME "-"
#define SUB_NAME "-"
static void subtrExecute(void);
@ -25,7 +25,7 @@ static void subtrExecute(void);
void abCalcOpSubtrInit(void)
{
abCalcOpRegister(OP_NAME, subtrExecute);
abCalcOpRegister(SUB_NAME, subtrExecute);
}
@ -34,19 +34,19 @@ void subtrExecute(void)
abCalcExpr result;
char expr1Real = 0;
char expr2Real = 0;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(SUB_NAME);
if (expr1->type == abCalcExprTypeReal) {
expr1Real = 1;
} else if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, SUB_NAME);
return;
}
if (expr2->type == abCalcExprTypeReal) {
expr2Real = 1;
} else if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, SUB_NAME);
return;
}

View File

@ -16,7 +16,7 @@
#include "ops/abCOpXor.h"
#define OP_NAME "XOR"
#define XOR_NAME "XOR"
static void xorExecute(void);
@ -24,22 +24,22 @@ static void xorExecute(void);
void abCalcOpXorInit(void)
{
abCalcOpRegister(OP_NAME, xorExecute);
abCalcOpRegister(XOR_NAME, xorExecute);
}
void xorExecute(void)
{
abCalcExpr result;
AB_CALC_OP_TWO_ARGS(OP_NAME);
AB_CALC_OP_TWO_ARGS(XOR_NAME);
if (expr1->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, XOR_NAME);
return;
}
if (expr2->type != abCalcExprTypeInt) {
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
abCalcRaiseError(abCalcBadArgTypeError, XOR_NAME);
return;
}