ident.c 3.11 KB
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#include <expValue.h>
#include <identList.h>
#include <tepal.h>
#include <ident.h>


/*
 * Typdeklaration, etc
 */
union identTypes
{
	s_expVal *    base;
	s_identList * idl;
};

struct ident
{
	char * key;
	int    idx;

	int    queued;

	union identTypes val;
};


/*
 * statische Funktionen (interner gebrauch)
 */

/*
 * nicht statische Funktionen (Interface)
 */

/*
 * Contructors / Destructors for ident
 */
	s_ident *
identNew (int idx, const char * key)
{
	int       keyLen = (key == NULL) ? 2 : 2 + strlen (key);
	s_ident * ident  = (s_ident *) malloc (sizeof (s_ident));

	ident->idx = idx;
	ident->key = (char *) malloc (sizeof (char) * keyLen);

	ident->key[0] = ID_TYP_UNDEF;
	ident->key[1] = '\0';

	if (key != NULL)
		strcpy (ident->key + 1, key);

	ident->queued = 0;

	return ident;
}

	s_ident *
identUndefNew (int idx, const char * key)
{
#ifdef DEBUG2
	printf ("[!DEBUG!] identUndefNew: %d/%s\n", idx, key);
#endif

	return identNew (idx, key);
}

	s_ident *
identExpNew (int idx, const char * key, s_expVal * val)
{
	s_ident * ident = identNew (idx, key);

#ifdef DEBUG2
	printf ("[!DEBUG!] identExpNew: %d/%s\n", idx, key);
#endif

	ident->key[0]   = ID_TYP_EXP;
	ident->val.base = expValueClone (val);

	return ident;
}

	s_ident *
identIdlNew (int idx, const char * key, s_identList * val)
{
	s_ident * ident = identNew (idx, key);

#ifdef DEBUG2
	printf ("[!DEBUG!] identIdlNew: %d/%s\n", idx, key);
#endif

	ident->key[0]  = ID_TYP_IDL;
	ident->val.idl = val;

	return ident;
}

	void
identFree (s_ident * id)
{
#ifdef DEBUG2
	printf ("[!DEBUG!] identFree: %d/%s\n", id->idx, id->key);
#endif

	switch (identGetType (id))
	{
		case ID_TYP_EXP:	expValueFree (id->val.base); break;
		case ID_TYP_IDL:	identListFree (id->val.idl); break;
	}

	free (id->key);
	free (id);
}

/*
 * analyse ident
 */
	int
identIsQueued (s_ident * id)
{
	return id->queued;
}

	void
identEnqueue (s_ident * id)
{
	id->queued ++;
}

	void
identDequeue (s_ident * id)
{
	id->queued --;
}

	int
identGetType (s_ident * id)
{
	return id->key[0];
}

	char *
identGetKey (s_ident * id)
{
	return id->key + 1;
}

	int
identGetIdx (s_ident * id)
{
	return id->idx;
}

/* identifier to value */
	s_expVal *
identExp (s_ident * id)
{
	s_expVal * ret = NULL;

	if (id == NULL) 
		exitError (ERR_UNDEF_VAR, identGetKey (id));

	if (identGetType (id) == ID_TYP_EXP)
	{
		ret = expValueClone (id->val.base);
	}

	return ret;
}

	s_identList *
identIdl (s_ident * id)
{
	s_identList * ret = NULL;

	if (identGetType (id) == ID_TYP_IDL)
	{
		ret = id->val.idl;
	}

	return ret;
}

	s_ident *
identSetExp (s_ident * id, s_expVal * val)
{
	switch (identGetType (id))
	{
		case ID_TYP_EXP:	expValueFree (id->val.base); break;
		case ID_TYP_IDL:	identListFree (id->val.idl); break;
	}

	id->key[0]   = ID_TYP_EXP;
	id->val.base = expValueClone (val);

	return id;
}

	s_ident *
identSetIdl (s_ident * id, s_identList * val)
{
	switch (identGetType (id))
	{
		case ID_TYP_EXP:	expValueFree (id->val.base);
		case ID_TYP_IDL:	identListFree (id->val.idl);
	}

	id->key[0]  = ID_TYP_IDL;
	id->val.idl = val;
	
	return id;
}