//
//  EXPFunctionElement.m
//  XMLTest
//
//  Created by Ashley on 10/05/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "EXPFunctionElement.h"
#import "EXPSymbolTable.h"
//#import "EXPAssemblerConstants.h"
//#import "EXPVirtualMachine.h"
#import "EXPError.h"

@implementation EXPFunctionElement

+ (void) registerFunction:(EXPSymbolTable *)table name:(NSString *)name variableArgs:(BOOL)vargs formalArguments:(id)formalArguments 
	returnType:(EXPExpressionType)returnType
{
	EXPFunctionElement *function = [[EXPFunctionElement alloc] init];
	[function setName:name];
	[function setIsBuiltin:YES];
	[function setVariableArgs:vargs];
	[function setExpressionType:returnType];
	[table declareSymbol:function];
	[function release];
}

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

- (EXPExpressionType) expressionType
{
	return _expressionType;
}

/*- (void) addFormalArgument:(id)formalArgument error:(id)error
{
	[[self formalArguments] addObject:formalArgument];
} */

- (void) setVariableArgs:(BOOL)variableArgs
{
	_variableArgs = variableArgs;
}

- (BOOL) variableArgs
{
	return _variableArgs;
}

- (void) setIsBuiltin:(BOOL)isBuiltin
{
	_isBuiltin = isBuiltin;
}

- (BOOL) isBuiltin
{
	return _isBuiltin;
}

- (void) setExpressionType:(EXPExpressionType) expressionType
{
	_expressionType = expressionType;
}

- (id) dimensions
{
	return nil;
}

/* - (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;

	BOOL variableArgs = [self variableArgs];
	int numArgs = [arguments count];
	
	if (variableArgs) {
		int i;
		for(i=0; (i<numArgs) && success; i++) {
			EXPExpression *argument = [arguments objectAtIndex:i];
			if ([argument isSimple]) {
				success = [argument compile:machine error:err opcode:PSH];
			} else {
				success = [argument compile:machine error:err];
				[machine putOpcode:PSHA mode:0];
			}
		}
	} else {
		if (numArgs>0) {
			EXPExpression *argument = [arguments objectAtIndex:0];
			success = [argument compile:machine error:err];
		}
	}
	
	if (success) {	
		unsigned int opcode = [self address];
		[machine putOpcode:opcode mode:0];
		
		if (variableArgs) {
			[machine putUInt:(unsigned int)[arguments count]];
		}
	}
	
	return success;
} */

@end