//
//  EXPElement.m
//  XMLTest
//
//  Created by Ashley on 11/04/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "EXPElement.h"
#import "EXPParser.h"
#import "EXPSymbolTable.h"
#import "EXPError.h"
//#import "EXPAssemblerConstants.h"
#import "EXPVirtualMachine.h"

int nextIdentifier = 0;

int _numElementsInited = 0;
int _numElementsDealloced = 0;

// @class EXPParameterElement;

@implementation EXPElement

- (id) init
{
	self = [super init];
	
	_identifier = nextIdentifier++;
	
	_sheet = nil;
	_address = 0;
	_isLocal = NO;
	_isPrivate = NO;
	
//	printf("Initialising element %d\n", _identifier);
	_numElementsInited++;

	return self;
}

+ (int) storageSize
{
	return 0;
}

- (void) setModel:(id)model
{
	_model = model;
}

- (id) model
{
	return _model;
}

- (void) setSheet:(id)sheet
{
	_sheet = sheet;
}

- (id) sheet
{
	return _sheet;
}

- (void) setAddress:(unsigned int)address
{
//	printf("Setting address for %s %s to %04x\n", [[[self class] description] UTF8String], [[self name] UTF8String], address);
	_address = address;
}

- (unsigned int) address
{
	return _address;
}

- (void) setGradientAddress:(int)address
{
}

- (int) gradientAddress
{
	return 0xffff;
}

- (void) setInitLineNumber:(int)initLineNumber
{
	_initLineNumber = initLineNumber;
}

- (int) initLineNumber
{
	return _initLineNumber;
}

- (void) setExecutionAddress:(int)executionAddress
{
	_executionAddress = executionAddress;
}

- (int) executionAddress
{
	return _executionAddress;
}

- (void) setInitialisationAddress:(int)initialisationAddress
{
	_initialisationAddress = initialisationAddress;
}

- (int) initialisationAddress
{
	return _initialisationAddress;
}

- (unsigned int) length
{
	return 1;
}

- (NSString *)name
{
	return @"";
}

- (void) setIsLocal:(BOOL)isLocal
{
	_isLocal = isLocal;
}

- (BOOL) isLocal
{
	return _isLocal;
}

- (void) setIsPrivate:(BOOL)isPrivate
{
	_isPrivate = isPrivate;
}

- (BOOL) isPrivate
{
	return _isPrivate;
}

- (int) maxPrimes
{
	return 0;
}

- (BOOL) isArray
{
	return NO;
}

- (void) handleError:(NSError *)err
{
//	[[[self sheet] model] handleError:err];
}

- (NSString *)elementType
{
	return @"none";
}

- (EXPExpressionType) expressionType
{
	return invalidType;
}

/* - (BOOL) compile:(EXPVirtualMachine *)machine withArguments:(NSArray *)arguments dimensions:(NSArray *)dimensions error:(EXPError *)err
		 opcode:(unsigned int)opcode mode:(unsigned int)mode 
		 address:(unsigned int)address
{
	BOOL success = YES;
	
	if ((arguments==nil) || ([arguments count]==0)) {
		[machine putOpcode:opcode mode:mode];
		[machine putUInt:address];
	} else {
		[err addError:[NSString stringWithFormat:@"Element %s has argument", [self name]] atLine:-999999];
	}

	return success;
} */

- (void) clear
{
//	[self setExpression:nil];
//	[self setExpressionList:nil];
//	[self setInputs:nil];
//	[self setOutputs:nil];
}

- (void) dump
{
	printf("%s address = %08x retain count = %d\n", [[self description] UTF8String], [self address], [self retainCount]);
//	[[self expression] dump];
//	[[self expressionList] dump];
	
/*	NSArray *inputs = [self inputs];
	int i;
	for(i=0; i<[inputs count]; i++) {
		[[inputs objectAtIndex:i] dump];
	}
	
	NSArray *outputs = [self outputs];
	for(i=0; i<[outputs count]; i++) {
		[[outputs objectAtIndex:i] dump];
	} */
}

- (NSString *)description
{
//	return [NSString stringWithFormat:@"EXPElement '%@'", [self name]];
	return @"EXPElement";
}

/*- (id) retain
{
	printf("Retaining element %s retainCount = %d\n", [[self name] UTF8String], [self retainCount]);
	return [super retain];
}

- (void) release
{
	printf("Releasing element %s retainCount = %d\n", [[self name] UTF8String], [self retainCount]);
	[super release];
} */

+ (void) reportElementCount
{
	printf("Elements inited =    %d\n", _numElementsInited);
	printf("Elements dealloced = %d\n", _numElementsDealloced);
}

- (void) dealloc
{
//	printf("EXPElement - (void) dealloc '%s'\n", [[self name] UTF8String]);
	_numElementsDealloced++;
	[super dealloc];
}

@end