//
//  EXPSymbolReference.m
//  XMLTest
//
//  Created by Ashley on 12/05/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "EXPSymbolReference.h"
#import "EXPElement.h"

@implementation EXPSymbolReference

- (id) init
{
	self = [super init];	
	_element = nil;
	_arguments = [[NSMutableArray alloc] init];

	return self;
}

/*- (id) retain
{
	printf("EXPSymbolReference - retaining reference to %s\n", [[_element name] UTF8String]);
	return [super retain];
}

- (void) release
{
	printf("EXPSymbolReference - releasing reference to %s\n", [[_element name] UTF8String]);
	[super release];
} */

- (void) setElement:(id)element
{
	[element retain];
	[_element release];
	_element = element;
}

- (id) element
{
	return _element;
}

- (void) setArguments:(id)arguments
{
	[arguments retain];
	[_arguments release];
	_arguments = arguments;
}

- (id) arguments
{
	return _arguments;
}

- (void) addArgument:(id)argument
{
	[[self arguments] addObject:argument];
}

/*- (void) setIsSimple:(BOOL)isSimple
{
	_isSimple = isSimple;
} */

- (BOOL) isSimple
{
	EXPElement *element = [self element];
	NSString *elementType = [element elementType];
	NSArray *arguments = [self arguments];
	BOOL simple = ([elementType isEqualToString:@"variable"] && ([arguments count]==0)) || ([elementType isEqualToString:@"parameter"]) ||
				  ([elementType isEqualToString:@"auxiliary"]);
	return simple;
}

- (BOOL) compile:(EXPStackMachine *)machine error:(NSError **)err opcode:(unsigned int)opcode
{
//	printf("EXPSymbolReference compile:error:opcode %s %04x\n", [[[self element] name] UTF8String], opcode);
	return [[self element] compile:machine error:err opcode:opcode];
}

- (BOOL) compile:(EXPStackMachine *)machine error:(NSError **)err
{
//	printf("EXPSymbolReference compile:error %s\n", [[[self element] name] UTF8String]);
	EXPElement *element = [self element];
	NSArray *arguments = [self arguments];
	BOOL success = [element compile:machine error:err withArguments:arguments];
	return success;
}

/*- (void) clear
{
	[self setArguments:nil];
	[self setElement:nil];
	[super clear];
} */

- (NSString *) description
{
	return [[self element] name];
}

- (void) dealloc
{
	[_element release];
	[_arguments release];
	[super dealloc];
}

@end
