//
//  EXPSymbolTable.m
//  SimpleCalc
//
//  Created by Ashley on 03/04/2006.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "EXPExpression.h"
#import "EXPConstant.h"
#import "EXPSymbolTable.h"

@implementation EXPSymbolTable

- (id) init
{
	[super init];
	
	_symbolTable = [[NSMutableDictionary alloc] init];
//	_builtins = [[EXPBuiltin alloc] init]; 
	_nextParamAddress = 0;
	_nextVariableAddress = 0;
	
	return self;
}

- (NSMutableDictionary *)table
{
	return _symbolTable;
}

- (int) nextParamAddress
{
	return _nextParamAddress;
}

- (void) setNextParamAddress:(int)nextParamAddress
{
	_nextParamAddress = nextParamAddress;
}

- (void) incParamAddress
{
	_nextParamAddress++;
}

- (int) nextVariableAddress
{
	return _nextVariableAddress;
}

- (void) setNextVariableAddress:(int)nextVariableAddress
{
	_nextVariableAddress = nextVariableAddress;
}

- (void) incVariableAddress
{
	_nextVariableAddress++;
}

- (id) symbolForName:(NSString *) symbolName
{
	return [_symbolTable objectForKey:symbolName];
}

- (void) setSymbolForName:(NSString *) symbolName withEntry:(id) entry
{
	[_symbolTable setObject:entry forKey:symbolName];
}

- (BOOL) declareSymbol:(id) entry
{
	NSString *name = [entry name];
	if ([_symbolTable objectForKey:name]==nil) {
		[_symbolTable setObject:entry forKey:name];
		return YES;
	} else {
		return NO;
	}
}

/*- (void) setSymbolValue:(NSString *) symbolName withValue:(double) value
{
	id identifier = [self symbolForName:symbolName];
	if (identifier == nil) {
		return;
	} else {
		[identifier setValue:value withSymbolTable:self];
	}
} */

- (double *) constants
{
	return _constants;
}

- (void) setConstants:(double *)constants
{
	_constants = constants;
}

- (double *) state
{
	return _state;
}

- (void) setState:(double *)state
{
	_state = state;
}

- (double *) gradients
{
	return _gradients;
}

- (void) setGradients:(double *)gradients
{
	_gradients = gradients;
}

/* - (EXPBuiltin *) builtins
{
	return _builtins;
} */

/*- (void) registerFunction:(NSString *) functionName 
{
	EXPFunction *functionEntry = [[EXPFunction alloc] initWithName:functionName];
	NSString *selectorName = [functionName stringByAppendingString:@":withSymbolTable:"];
	SEL sel = NSSelectorFromString(selectorName);
	[functionEntry setSelector:sel]; 
	[self setSymbolForName:functionName withEntry:functionEntry];
	
	[functionEntry release];
} */

- (void) loadConstant:(NSString *)name withValue:(double)value
{
	EXPConstant *constant = [[EXPConstant alloc] initWithName:name];
	[constant setValue:value];
	[self setSymbolForName:name withEntry:constant];
	[constant release];
}

- (void) loadConstants
{
	[self loadConstant:@"e" withValue:2.718281828459045235360287471352662497757247093699959574966];
	[self loadConstant:@"pi" withValue:3.14159265358979323846264338327950288419716939937510582097];
	[self loadConstant:@"gamma" withValue:0.577215664901532861];
	[self loadConstant:@"cvacuum" withValue:299792458];
	[self loadConstant:@"mu0" withValue:1.25663706144e-6];
	[self loadConstant:@"epsilon0" withValue:8.854187817e-12];
	[self loadConstant:@"h" withValue:6.6260755e-34];
	
/*	NSArray *constantNames = [[NSArray alloc] initWithObjects:@"e", @"pi", @"gamma", @"cvacuo", @"mu0", @"epsilon0", @"h", nil];
	double constantValues[] = {2.718281828459045235360287471352662497757247093699959574966,
							  3.14159265358979323846264338327950288419716939937510582097,
							  0.577215664901532861,
							  299792458,
							  1.25663706144e-6,
							  8.854187817e-12,
							  6.6260755e-34};
	int i;
	for(i=0; i<[constantNames count]; i++) {
		[self setSymbolValue:[constantNames objectAtIndex:i] withValue: constantValues[i]];
	}
	[constantNames release];*/
}

- (void) dump
{
	NSEnumerator *enumerator = [_symbolTable objectEnumerator];
//	int i=0;
	id symbol;
	while (symbol = [enumerator nextObject]) {
//		printf("%02d %s\n", i, [[symbol name] UTF8String]);
//		i++;
		[symbol dump];
	} 
}

- (void) dealloc
{
//	printf("EXPSymbolTable - (void)dealloc\n");
	[_symbolTable release];
//	[_builtins release];
	[super dealloc];
}

@end

