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

#import "EXPParameterElement.h"
#import "EXPAssemblerConstants.h"

@implementation EXPParameterElement

- (id) init
{
	self = [super init];
	_value = 0.0;
	
	return self;
}

/* - (void) setElementFromXMLElement:(NSXMLElement *)element
{
} */

- (void) setValue:(double)value
{
	_value = value;
}

- (double) value
{
	double answer = 0.0;
	EXPExpression *exp = [self expression];
	answer = [exp result];
	return answer;
}

- (BOOL) setElementFromParser:(EXPParser *)parser symbolTable:(EXPSymbolTable *)symbolTable inputs:(id)inputs
{
	BOOL success = [super setElementFromParser:parser symbolTable:symbolTable inputs:inputs];
	
	if ([parser testSymbol:@"label"]) {
		if (![parser testSymbol:@"="]) {
			success = NO;
		}

		[parser readSymbol];
		if (![parser isStringLiteral]) {
			return NO;
		}

		NSString *label = [[NSString alloc] initWithString:[parser symbol]];
		[self setLabel:label];
		[label release];

	}

	return success;

}
			
- (BOOL) compile:(EXPStackMachine *)machine error:(NSError **)err opcode:(unsigned int)opcode
{
	BOOL success = [super compile:machine error:err opcode:(opcode + PMODE)];
	return success;
}

- (BOOL) compile:(EXPStackMachine *)machine error:(NSError **)err withArguments:(NSArray *)arguments
{
	if ([arguments count]>0) {
		return NO;
	}
	BOOL success = [self compile:machine error:err opcode:LDAP];
	return success;
}

/* - (BOOL) isParameter
{
	return YES;
} */

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

- (BOOL) compileInitialisation:(EXPStackMachine *)machine error:(NSError **)err
{
	BOOL success = [super compileInitialisation:machine error:err opcode:STRP];
	return success;
}

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

- (id)copyWithZone:(NSZone *)zone
{
	id doppel = [super copyWithZone:zone];
	[doppel setValue:[self value]];
	
	return doppel;
}

@end
