//
//  EXPUnaryOp.m
//  Expression
//
//  Created by Ashley on 28/02/2006.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "EXPExpression.h"
#import "EXPUnaryOp.h"
#import "EXPAssemblerConstants.h"

static int _numEXPUnaryOps = 0;

@implementation EXPUnaryOp

+ (int) numEXPUnaryOps
{
	return _numEXPUnaryOps;
}

- (id) initWithLeft:(id) left
{
	[super init];
	[self setLeft:left];
	_numEXPUnaryOps++;
	return self;
}

- (void) setLeft:(id) left
{
	[left retain];
	[_left release];
	_left = left;
}

- (id) left
{
	return _left;
}

- (NSString *)description
{
	return [NSString stringWithFormat:@"-(%@)", [[self left] description]];
}

- (BOOL) compile:(EXPStackMachine *)machine error:(NSError **)err
{
	BOOL success =[[self left] compile:machine error:err];
	[machine putUInt:NEGA];
	return success;
}

- (double) result
{
	return -[_left result];
}

- (void) dump
{
	[super dump];
	[[self left] dump];
}

- (void) dealloc
{
//	printf("EXPUnaryOp - (void)dealloc\n");
	[_left release];
	_numEXPUnaryOps--;
	[super dealloc];
}

@end

@implementation EXPUnaryNot

- (NSString *)description
{
	return [NSString stringWithFormat:@"!(%@)", [[self left] description]];
}

- (double) result
{
	return ![[self left] result];
}

@end

@implementation EXPUnaryPostIncrement

- (NSString *)description
{
	return [NSString stringWithFormat:@"%@++", [[self left] description]];
}

/*- (double) result
{
	EXPExpression *left = [self left];
	double x = [left result:symbolTable];
	if ([left respondsToSelector:@selector(setValue:withSymbolTable:)]) {
		[left setValue:(x + 1) withSymbolTable:symbolTable];
	}
	return x; 
}*/

@end

@implementation EXPUnaryPreIncrement

- (NSString *)description
{
	return [NSString stringWithFormat:@"++%@", [[self left] description]];
}

/*- (double) result: (id)symbolTable
{
	EXPExpression *left = [self left];
	double x = [left result:symbolTable] + 1;
	if ([left respondsToSelector:@selector(setValue:withSymbolTable:)]) {
		[left setValue:x withSymbolTable:symbolTable];
	}
	return x;
} */

@end
