/***************************************/

/*
 *		'D' compiler main module
 * Version	0.0.0.0 GPL (alpha)
 * Copyright	C.R.Chafer 2002/6/11
 * Author	C.R.Chafer
 * Contact	c.chafer@herts.ac.uk
 * License	GPL v2 (www.fsf.org)
 *		(contact me for versions
 *		 under other licenses)
 *
 */

/***************************************/

/*
 * Notes:	This is not to be assumed
 *		definitive - see
 *		http://www.digitalmars.com
 *		for definitive D spec
 */
	
/***************************************/

#include "d.h"

/***************************************/

static	CH*	currentArea = NULL	;


/***************************************/	/* error message output */

/****/	UD printError( m, s, n )		/* print standard error message */
	CH*m	/* error message */
 ;	CH*s	/* string report / NULL */
 ;	U32 n	/* number report / 0 */
;{	fprintf( stderr, "ERROR:\t" )
 ;	if( m ) fprintf( stderr, "%s", m )
 ;	if( s && n ) fprintf( stderr, " [%s:%lu]", s, n )
 ;	else
 {	if( s ) fprintf( stderr, " [%s]", s )
  ;	else if( n ) fprintf( stderr, " [%lu]", n )
 ;}	fprintf( stderr, "\n\t@ %lu [%s+%lu]\n", getLine(),currentArea,getLineInc() )
 ;	exit( EXIT_FAILURE )
;}
/****/	UD printSystemError( m, s, n )		/* system error message */
	CH*m	/* as printError() */
 ;	CH*s
 ;	U32 n
;{	fprintf( stderr, "SYSTEM\t" )
 ;	printError( m, s, n )
;}
#ifdef DEBUG
/****/	UD printOops( m, s )			/* print debug message */
	CH*m	/* oops message */
 ;	CH*s	/* report */
;{	fprintf( stderr, "OOPS:\t%s", m )
 ;	if( s ) fprintf( stderr, "%s", s )
 ;	fprintf( stderr, " @ %lu", getLine()+getLineInc() )
 ;	exit( EXIT_FAILURE )
;}
#endif /* DEBUG */

/***************************************/	/* memory allocation functions */

/****/	UD*xalloc( l )
	U32 l
;{	UD*m = malloc( l )
 ;	if( !m ) printSystemError( "Memory allocation", 0, l )
 ;	return m
;}
/****/	CH*xstrdup( s )
	CH*s
;{	CH*r = strdup( s )
 ;	if( !r ) printSystemError( "String duplication", s, 0 )
 ;	return r
;}

/***************************************/	/* symbol & constant referencing */ 

/****/	CH*newString( s )			/* convert escape sequences & return stored string */
	CH*s
;{	return xstrdup( s )	/* l8r */
;}

/****/	CH*newStringNoEsc( s )			/* store string */
	CH*s
;{	return xstrdup( s )	/* l8r */
;}

#define STR struct strings
struct	strings { CH*string; U32 len; STR*next; };
static	STR	strings = { NULL, 0, NULL }	;

/****/	CH*	newSymbol( s )			/* find symbol in table (for fast lookup later) */
	CH*s	/* string to find / create */
;{	STR*h			/* later we should convert this to a faster lookup method (hash table?) */
 ;	U32 l = strlen( s )
 ;	for( h=&strings; h->next; h=h->next )
	if( h->len == l && !strcmp( h->string, s ) )
	return h->string
 ;	h->next = xalloc( sizeof*h )
 ;	h->len = l
 ;	return h->string = xstrdup( s )
;}

/***************************************/

/***************************************/	/* initialise & finialise */

/****/	UD initialise()
{	currentArea = "$"
;}

/****/	UD finialise()
{	return
;}

/***************************************/

