/*                      QBCC Project

This source file is licensed under the GNU Public Licence which should be 
included with the distribution of this source.

*/
// Operator Translations

#include "qbcc.h"
#include "scanner.h"
#include "operator.h"


///////////////////
// Translates an Operator
void TranslateOPERATOR(char *retstr,char *op,int UseBit,int UseIf)
{
	if(UseIf)
	{
		if(strcmp(op,"<>") == 0)
			strcat(retstr," != ");
		if(strcmp(op,"=") == 0)
			strcat(retstr," == ");
	}
	else
	{
		if(strcmp(op,"=") == 0)
			strcat(retstr," = ");
	}
	
	if(strcmp(op,">=") == 0)
		strcat(retstr," >= ");
	if(strcmp(op,"<=") == 0)
		strcat(retstr," <= ");
	if(strcmp(op,">") == 0)
		strcat(retstr," > ");
	if(strcmp(op,"<") == 0)
		strcat(retstr," < ");
	
	if(UseBit)
	{
		// Bit operators
		if(strcmp(op,"AND") == 0)
			strcat(retstr," & ");
		if(strcmp(op,"OR") == 0)
			strcat(retstr," | ");
	}
	else
	{
		if(strcmp(op,"AND") == 0)
			strcat(retstr," && ");
		if(strcmp(op,"OR") == 0)
			strcat(retstr," || ");
	}
	
	if(strcmp(op,"+") == 0)
		strcat(retstr," + ");
	if(strcmp(op,"-") == 0)
		strcat(retstr," - ");
	if(strcmp(op,"/") == 0)
		strcat(retstr," / ");
	if(strcmp(op,"*") == 0)
		strcat(retstr," * ");
	if(strcmp(op,"\\") == 0)
		strcat(retstr," \\ ");
	if(strcmp(op,"NOT") == 0)
		strcat(retstr," !");
	//if(strcmp(op,"MOD") == 0)
	//	strcat(retstr," % ");
	if(strcmp(op,"XOR") == 0)
		strcat(retstr," ^ ");
	
	// New Operators
	// It's my compiler! I wrote it! I get to modify the language!
	// (Or maybe we just don't tell people about this? hmmm  =)
	if(strcmp(op,"++") == 0)
		strcat(retstr,"++");
	if(strcmp(op,"--") == 0)
		strcat(retstr,"--");
	if(strcmp(op,"+=") == 0)
		strcat(retstr," += ");
	if(strcmp(op,"*=") == 0)
		strcat(retstr," *= ");
	if(strcmp(op,"/=") == 0)
		strcat(retstr," /= ");
	if(strcmp(op,"-=") == 0)
		strcat(retstr," -= ");
}

///////////////////
// Returns if the operator is a 'Special' Operator
int IsSpecialOp(char *string)
{
	char *specops[4] = {"^","IMP", "EQV","MOD"};
	int NumSpecOps = 4;
	int n;

	for(n=0;n<NumSpecOps;n++)	
		if(strcmp(string,specops[n]) == 0)
			return TRUE;

	return FALSE;
}

///////////////////
// Converts the Operator to it's function
void ConvertSpecOp(char *op,char *newop)
{
	if(strcmp(op,"^") == 0)
		strcpy(newop,"pow");
	if(strcmp(op,"IMP") == 0)
		strcpy(newop,"IMP");
	if(strcmp(op,"EQV") == 0)
		strcpy(newop,"SQV");
	if(strcmp(op,"MOD") == 0)
		strcpy(newop,"fmod");
}