#import "Calc.h"

//digits are 0 through 9
#define DIVIDE 10
#define MULTIPLY 11
#define SUBTRACT 12
#define ADD 13
#define NOOP 20

@implementation Calc

- init
{
	[super init];
	[self clearAll];
	return self;
}

- (void)clearAll
{
	xRegister = 0.0;
	pendingOperation = NOOP;	
}

- (double)performOperationWith:(double)value
{
	switch (pendingOperation){
		case ADD:
			xRegister += value;
			break;
		case SUBTRACT:
			xRegister -= value;
			break;
		case MULTIPLY:
			xRegister *= value;
			break;
		case DIVIDE:
			xRegister /= value;
			break;
		default:
			xRegister = value;
			break;
	}
	pendingOperation = NOOP;
    return xRegister;
}

- (double)setOperation:(int)operator forValue:(double)value
{
	xRegister = [self performOperationWith:value];	// do any pending stuff
	pendingOperation = operator;
    return xRegister;
}

- (void)changePendingOperation:(int)operator
{
	pendingOperation = operator;
}

@end


int main(int argc, char *argv[])
{
	id calcObj;
	id conn;

	[NXPort worryAboutPortInvalidation];

    calcObj = [[Calc alloc] init];
	conn = [NXAutoreleaseConnection registerRoot:calcObj
		withName:"Calculator"];
	if (conn) {
		fprintf(stderr, "Calculator registered\n");
		[conn run];
		[calcObj free];
	} else {
		fprintf(stderr, "Couldn't register calculator - exiting.\n");
	    exit(-1);
	}

	exit(0);
}

