mud++0.35/etc/
mud++0.35/etc/guilds/
mud++0.35/help/propert/
mud++0.35/mudC/
mud++0.35/player/
mud++0.35/src/interface/
mud++0.35/src/os/cygwin32/
mud++0.35/src/os/win32/
mud++0.35/src/os/win32/bcppbuilder/
mud++0.35/src/osaddon/
mud++0.35/src/util/
/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@hom.net
MUD++ development mailing list    mudpp@van.ml.org
------------------------------------------------------------------------------
emit.h
*/

/* For generating 3-address or assembly code by traversing the
   parse tree, using syntax-directed translation.

   Very primitive because I am learning as I go. --Melvin
*/

#include "parse.h"
#include "yacc.tab.h"

int emit_function( node * );
int emit_id_simple( node * );

void emit_asm()
{
	int i;
	node * ptr;
	for( i = 0; i < cur_unit; i++ )
	{
		ptr = unit[i];
		if( ptr->op == OP_DEFINE )
		{
			if( ptr->sym_ptr->type & TYPE_FUNCTION )
				emit_fun_def( ptr );
			else
				emit_data_def( ptr );
		}
		else
		{
			printf( "Don't know how to emit parse tree for op %d.\n",
						ptr->op );
		}
	} 
}


int emit_arg_list( sym * sym_ptr )
{
	char * type;
	if( sym_ptr->type == TYPE_INT )
		type = "int";
	else if( sym_ptr->type == TYPE_STRING )
		type = "string";

	printf( "!A %s %s\n", type, sym_ptr->name );

	if( sym_ptr->next )
		emit_arg_list( sym_ptr->next );
}


int emit_local_list( node * tree )
{
	printf( "!L %s\n", tree->sym_ptr->name );
	if( tree->right )
		emit_local_list( tree->right );
}


int emit_fun_def( node * tree )
{
	printf( "!funstart %s\n", tree->sym_ptr->name );
/*
	if( tree->u.args )
		emit_arg_list( tree->u.args );
*/
	if( tree->sym_ptr->next )
		emit_arg_list( tree->sym_ptr->next );

	if( tree->right )
		emit_statement( tree->right );

	printf( "\tret\n!funend\n" );
}



int emit_statement( node * tree )
{
	if( tree->op == OP_DECLARE )
		printf( "!L %s\n", tree->sym_ptr->name );
	else if( tree->op == OP_CALL )
		printf( "\tcall %s\n", tree->sym_ptr->name );
	else printf( "Don't know how to emit parse tree for statement %d.\n",
					tree->op );

	if( tree->right )
		emit_statement( tree->right );
}


int emit_data_def( node * tree )
{
	if( tree->sym_ptr->type == TYPE_INT )
		printf( "\tdword %s\n", tree->sym_ptr->name );
	else if( tree->sym_ptr->type == TYPE_STRING )
		printf( "\tdstring %s\n", tree->sym_ptr->name );
	else
		printf( "\tUnknown simple type %d.\n", tree->type );
}

/*
	else if( tree->type & NODE_CALL )
	{
		node * ptr = tree->left;
		int count;
		for( count = 0; ptr; ptr = ptr->right )
			count++;

		for( ptr = tree->left; ptr; ptr = ptr->right )
			printf( "\tpush %s\n", ptr->sym_ptr->name );

		printf( "\tcall %s\n", tree->sym_ptr->name );
	}

	if( tree->right )
		emit_id_simple( tree->right );
}
*/