EaseaSym.cpp 39.9 KB
Newer Older
kruger's avatar
kruger committed
1 2 3
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
moh_lo's avatar
moh_lo committed
4
/****************************************************************************
Cancino Waldo's avatar
Cancino Waldo committed
5 6
  EaseaSym.cpp
  Symbol table and other functions for the EASEA language (EAsy Specification for Evolutionary Algorithms)
moh_lo's avatar
moh_lo committed
7

Cancino Waldo's avatar
Cancino Waldo committed
8 9 10 11 12 13 14
  Pierre COLLET (Pierre.Collet@inria.fr)
  Copyright EVOlutionary LABoratory
  INRIA Rocquencourt, Projet FRACTALES
  Domaine de Voluceau
  Rocquencourt BP 105
  78153 Le Chesnay CEDEX
 ****************************************************************************/
moh_lo's avatar
moh_lo committed
15 16 17 18 19

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "EaseaSym.h"
maitre's avatar
maitre committed
20
#include "debug.h"
Cancino Waldo's avatar
Cancino Waldo committed
21

22 23 24
using std::cout;
using std::endl;

Cancino Waldo's avatar
Cancino Waldo committed
25 26
void debug(char *s)
{
27
#ifdef _DEBUG
Cancino Waldo's avatar
Cancino Waldo committed
28 29
	printf (s);
	getchar ();
30
#endif
Cancino Waldo's avatar
Cancino Waldo committed
31
	return;
32
}
moh_lo's avatar
moh_lo committed
33 34 35 36 37 38 39 40 41 42 43 44

/////////////////////////////////////////////////////////////////////////////
// LinkedList commands

// template <class T> void CLList<T>::addLast(const T &p){
//   CListItem<T> *pSentinel, *pCurrent;
//   pSentinel=pCurrent=pHead;
//   while (pCurrent!=NULL){pSentinel=pCurrent; pCurrent=pCurrent->pNext;}
//   if (pSentinel != NULL) pSentinel->pNext=new CListItem<T>(pCurrent,NewObject);
//   else pHead=new CListItem<T>(pCurrent,NewObject);
//   }

Cancino Waldo's avatar
Cancino Waldo committed
45 46 47 48 49 50 51 52 53 54 55
template <class T> CListItem<T> *CLList<T>::walkToNextItem()
{
	if (pNextItem==NULL) return NULL;
	if (pNextItem==pHead)
	{
		pNextItem=pHead->pNext;
		return pHead;
	}
	pCurrentObject=pNextItem;
	pNextItem=pNextItem->pNext;
	return pCurrentObject;
56
}
Cancino Waldo's avatar
Cancino Waldo committed
57

moh_lo's avatar
moh_lo committed
58 59 60
/////////////////////////////////////////////////////////////////////////////
// symbol construction/destruction

61
CSymbol::CSymbol(const char *s)
Cancino Waldo's avatar
Cancino Waldo committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
{
	assert(s != NULL);

	int nLength = strlen(s);
	sName = new char[nLength + 1];
	strcpy(sName, s);      
	dValue = 0.0;
	nSize = 0;
	ObjectType=oUndefined;   
	bAlreadyPrinted=false;
	pType=pClass=NULL;
	pNextInBucket = NULL;
	pSymbolList=NULL;
	sString=NULL;
76
}
Cancino Waldo's avatar
Cancino Waldo committed
77 78 79 80

CSymbol::~CSymbol()
{
	delete[] sName;
81
}
moh_lo's avatar
moh_lo committed
82

moh_lo's avatar
moh_lo committed
83 84


maitre's avatar
maitre committed
85 86
/////////////////////////////////////////////////////////////////////////////
// symbol  commands
Cancino Waldo's avatar
Cancino Waldo committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
void CSymbol::print(FILE *fp)
{
///*
	CListItem<CSymbol*> *pSym;
	int i;

	// check: are we printing a user class that is different from Genome?
	if (strcmp(sName,"Genome"))
	{   
		// if we are printing a user class other than the genome
		fprintf(fp,"\nclass %s {\npublic:\n// Default methods for class %s\n",sName,sName); // class  header

		//fprintf(fp,"// Class members \n"); // Now, we must print the class members
		//  pSymbolList->reset();
		//  while (pSym=pSymbolList->walkToNextItem()){
		//  if (pSym->Object->ObjectType==oObject)
		//  fprintf(fp,"  %s %s;\n",pSym->Object->pType->sName,pSym->Object->sName);
		//  if (pSym->Object->ObjectType==oPointer)
		//  fprintf(fp,"  %s *%s;\n",pSym->Object->pType->sName,pSym->Object->sName);
		//  if (pSym->Object->ObjectType==oArray)
		//  fprintf(fp,"  %s %s[%d];\n",pSym->Object->pType->sName,pSym->Object->sName,pSym->Object->nSize/pSym->Object->pType->nSize);
		// }

		// check on the type of target
		if( TARGET==CUDA )
		{ 
			// here we we are generating function to copy objects from host memory to gpu's.
			bool isFlatClass = true;
			pSymbolList->reset();
116
			while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
117 118
			{
				//DEBUG_PRT("analyse flat %s",pSym->Object->pType->sName);
119
				if( pSym->Object->ObjectType == oPointer ) //|| (pSym->Object->pType->ObjectType == oObject) )
Cancino Waldo's avatar
Cancino Waldo committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
				{  
					isFlatClass = false;
					break;
				}
			}

			//DEBUG_PRT("Does %s flat class : %s",sName,(isFlatClass?"yes":"no"));
			pSymbolList->reset();      
			fprintf(fp,"  %s* cudaSendToGpu%s(){\n",sName,sName);
			fprintf(fp,"    %s* ret=NULL;\n",sName);

			if( isFlatClass )
			{
				fprintf(fp,"    cudaMalloc((void**)&ret,sizeof(%s));\n",sName);
				fprintf(fp,"    cudaMemcpy(ret,this,sizeof(%s),cudaMemcpyHostToDevice);\n",sName);
				fprintf(fp,"    return ret;\n");	
			}
			else
			{
				fprintf(fp,"    %s tmp;\n",sName);
				fprintf(fp,"    memcpy(&tmp,this,sizeof(%s));\n",sName);

142
				while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
143
				{
144
					if( pSym->Object->ObjectType == oPointer )  //|| (pSym->Object->pType->ObjectType == oObject) )
Cancino Waldo's avatar
Cancino Waldo committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
					{
						fprintf(fp,"    tmp.%s=this->%s->cudaSendToGpu%s();\n",
								pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName);
					}
				}
				fprintf(fp,"    cudaMalloc((void**)&ret,sizeof(%s));\n",sName);
				fprintf(fp,"    cudaMemcpy(ret,&tmp,sizeof(%s),cudaMemcpyHostToDevice);\n",sName);
				fprintf(fp,"    return ret;\n");
			}

			fprintf(fp,"  }\n\n");

			// another CUDA-specific function
			fprintf(fp,"  void cudaGetFromGpu%s(%s* dev_ptr){\n",sName,sName);
			fprintf(fp,"    %s* ret=NULL;\n",sName); 	

			if( isFlatClass )
			{
				fprintf(fp,"    ret = (%s*)malloc(sizeof(%s));\n",sName,sName);
				fprintf(fp,"    cudaMemcpy(ret,dev_ptr,sizeof(%s),cudaMemcpyDeviceToHost);\n",sName);
				//while (pSym=pSymbolList->walkToNextItem())
				//fprintf(fp,"    this->%s=ret->%s;\n",pSym->Object->sName,pSym->Object->sName);      
			}
168
		  fprintf(fp,"  }\n\n");
Cancino Waldo's avatar
Cancino Waldo committed
169
		}
moh_lo's avatar
moh_lo committed
170

Cancino Waldo's avatar
Cancino Waldo committed
171 172 173 174
		// creation of class constructor
		fprintf(fp,"  %s(){  // Constructor\n",sName); // constructor
		pSymbolList->reset(); // in which we initialise all pointers to NULL

175
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		{
			if (pSym->Object->ObjectType==oPointer)
				fprintf(fp,"    %s=NULL;\n",pSym->Object->sName);

			if (pSym->Object->ObjectType==oArrayPointer)
			{
				fprintf(fp,"    for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fp,"         %s[EASEA_Ndx]=NULL;\n",pSym->Object->sName);
			}
		}
		fprintf(fp,"  }\n"); // end of constructor

		// creation of copy constructor
		fprintf(fp,"  %s(const %s &EASEA_Var) {  // Copy constructor\n",sName,sName); // copy constructor
		pSymbolList->reset();

192
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		{
			if (pSym->Object->ObjectType==oObject)
				fprintf(fp,"    %s=EASEA_Var.%s;\n",pSym->Object->sName,pSym->Object->sName);

			if (pSym->Object->ObjectType==oArray)
			{
				fprintf(fp,"    {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fp,"       %s[EASEA_Ndx]=EASEA_Var.%s[EASEA_Ndx];}\n",pSym->Object->sName,pSym->Object->sName);
			}

			if (pSym->Object->ObjectType==oPointer)
			{
				fprintf(fp,"    %s=(EASEA_Var.%s ? new %s(*(EASEA_Var.%s)) : NULL);\n",
						pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
			}

			if( pSym->Object->ObjectType==oArrayPointer )
			{
				fprintf(fp,"    for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fp,"        if( EASEA_Var.%s[EASEA_Ndx] ) %s[EASEA_Ndx] = new %s(*(EASEA_Var.%s[EASEA_Ndx]));\n",
						pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
				fprintf(fp,"        else  %s[EASEA_Ndx] = NULL;\n",pSym->Object->sName);
			}
		}
		fprintf(fp,"  }\n"); // end of copy constructor

		// creation of destructor
		fprintf(fp,"  virtual ~%s() {  // Destructor\n",sName); // destructor
		pSymbolList->reset();
223
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		{
			if (pSym->Object->ObjectType==oPointer)
				fprintf(fp,"    if (%s) delete %s;\n    %s=NULL;\n",
						pSym->Object->sName,pSym->Object->sName,pSym->Object->sName);

			if( pSym->Object->ObjectType==oArrayPointer )
			{
				fprintf(fp,"    for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fp,"        if( %s[EASEA_Ndx] ) delete %s[EASEA_Ndx];\n",
						pSym->Object->sName,pSym->Object->sName);
			}
		}
		fprintf(fp,"  }\n"); // end of destructor

		// creation of serializer
		fprintf(fp,"  string serializer() {  // serialize\n"); // serializer
		fprintf(fp,"  \tostringstream EASEA_Line(ios_base::app);\n");
		pSymbolList->reset();
243
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
244 245
		{
			// check: is it a user-defined class?
246
			if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
			{
				if (pSym->Object->ObjectType==oArrayPointer)
				{
					// it's an array of pointers
					fprintf(fp,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++){\n",
							(int)(pSym->Object->nSize/sizeof(char*)));
					fprintf(fpOutputFile,"\t\tif(this->%s[EASEA_Ndx] != NULL){\n",pSym->Object->sName);
					fprintf(fpOutputFile,"\t\t\tEASEA_Line << \"\\a \";\n");
					fprintf(fpOutputFile,"\t\t\tEASEA_Line << this->%s[EASEA_Ndx]->serializer() << \" \";\n",
							pSym->Object->sName);
					fprintf(fpOutputFile,"\t}\n");
					fprintf(fpOutputFile,"\t\telse\n");
					fprintf(fpOutputFile,"\t\t\tEASEA_Line << \"NULL\" << \" \";\n");
					fprintf(fpOutputFile,"}\n");
				}
262 263 264 265 266
				// it's a classical array
				else if(pSym->Object->ObjectType==oArray)
				{
					fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
							pSym->Object->nSize/pSym->Object->pType->nSize);
267 268 269 270 271 272
					fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s[EASEA_Ndx].serializer() <<\" \";\n", pSym->Object->sName);
				}
				// it's a simple struct/class
				else if(pSym->Object->ObjectType==oObject)
				{
					fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s.serializer() <<\" \";\n", pSym->Object->sName);
273
				}
Cancino Waldo's avatar
Cancino Waldo committed
274 275
				else
				{
276
					// it's a pointer to an user-defined clas
Cancino Waldo's avatar
Cancino Waldo committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
					fprintf(fpOutputFile,"\tif(this->%s != NULL){\n",pSym->Object->sName);
					fprintf(fpOutputFile,"\t\tEASEA_Line << \"\\a \";\n");
					fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s->serializer() << \" \";\n",pSym->Object->sName);
					fprintf(fpOutputFile,"}\n");
					fprintf(fpOutputFile,"\telse\n");
					fprintf(fpOutputFile,"\t\tEASEA_Line << \"NULL\" << \" \";\n");
				}
			}
			else
			{
				// it's not a user-defined class
				if (pSym->Object->ObjectType==oObject)
				{
					fprintf(fpOutputFile,"\tEASEA_Line << this->%s << \" \";\n",pSym->Object->sName);
				}

				// it's a classical array
				if(pSym->Object->ObjectType==oArray)
				{
					fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
							pSym->Object->nSize/pSym->Object->pType->nSize);
					fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s[EASEA_Ndx] <<\" \";\n", pSym->Object->sName);
				}
			}
		} // end while

		fprintf(fp,"  \treturn EASEA_Line.str();\n");
		fprintf(fp,"  }\n"); // end of serializer

		// creation of deserializer
		fprintf(fp,"  void deserializer(istringstream* EASEA_Line) {  // deserialize\n"); // deserializer
		fprintf(fp,"  \tstring line;\n");
		pSymbolList->reset();
310
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
311
		{
312
			if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
313
			{
314
				/* it's an array of pointer */
Cancino Waldo's avatar
Cancino Waldo committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
				if (pSym->Object->ObjectType==oArrayPointer)
				{
					fprintf(fp,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++){\n",
							(int)(pSym->Object->nSize/sizeof(char*)));
					fprintf(fpOutputFile,"\t\t(*EASEA_Line) >> line;\n");
					fprintf(fpOutputFile,"\t\tif(strcmp(line.c_str(),\"NULL\")==0)\n");
					fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx] = NULL;\n",pSym->Object->sName);
					fprintf(fpOutputFile,"\t\telse{\n");
					fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx] = new %s;\n",
							pSym->Object->sName, pSym->Object->pType->sName);
					fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx]->deserializer(EASEA_Line);\n",
							pSym->Object->sName);
					fprintf(fpOutputFile,"\t\t}");
					fprintf(fpOutputFile,"\t}");
				}
330 331 332 333 334 335 336 337 338 339 340 341 342
				/* it's a classical array*/
				else if(pSym->Object->ObjectType==oArray)
				{
					fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
							pSym->Object->nSize/pSym->Object->pType->nSize);
					fprintf(fpOutputFile,"\t\tthis->%s[EASEA_Ndx].deserializer(&EASEA_Line) ;\n", pSym->Object->sName);
				}
				/* it's a simple struct/class */
				else if(pSym->Object->ObjectType==oObject)
				{
					fprintf(fpOutputFile,"\t\tthis->%s.deserializer(EASEA_Line);\n", pSym->Object->sName);
				}
				/*it's a pointer*/
Cancino Waldo's avatar
Cancino Waldo committed
343 344 345 346 347 348
				else
				{
					fprintf(fpOutputFile,"\t(*EASEA_Line) >> line;\n");
					fprintf(fpOutputFile,"\tif(strcmp(line.c_str(),\"NULL\")==0)\n");
					fprintf(fpOutputFile,"\t\tthis->%s = NULL;\n",pSym->Object->sName);
					fprintf(fpOutputFile,"\telse{\n");
349
					fprintf(fpOutputFile,"\t\tthis->%s = new %s;\n",pSym->Object->sName, sName);
Cancino Waldo's avatar
Cancino Waldo committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
					fprintf(fpOutputFile,"\t\tthis->%s->deserializer(EASEA_Line);\n",pSym->Object->sName);
					fprintf(fpOutputFile,"\t}");
				}
			}
			else
			{
				if (pSym->Object->ObjectType==oObject)
				{
					fprintf(fpOutputFile,"\t(*EASEA_Line) >> this->%s;\n",pSym->Object->sName);
				}

				if(pSym->Object->ObjectType==oArray)
				{
					fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
							pSym->Object->nSize/pSym->Object->pType->nSize);
					fprintf(fpOutputFile,"\t\t(*EASEA_Line) >> this->%s[EASEA_Ndx];\n", pSym->Object->sName);
				}
			}
		} // end while
		fprintf(fp,"  }\n"); // end of deserializer

		// creation of operator to assess individual equality
		fprintf(fp,"  %s& operator=(const %s &EASEA_Var) {  // Operator=\n",sName,sName); // operator=
		fprintf(fp,"    if (&EASEA_Var == this) return *this;\n");
		pSymbolList->reset();

376
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		{
			if (pSym->Object->ObjectType==oObject)
				fprintf(fp,"    %s = EASEA_Var.%s;\n",pSym->Object->sName,pSym->Object->sName);

			if (pSym->Object->ObjectType==oArray)
			{
				fprintf(fp,"    {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fp,"       %s[EASEA_Ndx] = EASEA_Var.%s[EASEA_Ndx];}\n",
						pSym->Object->sName,pSym->Object->sName);
			}

			if (pSym->Object->ObjectType==oArrayPointer)
			{
				fprintf(fp,"    for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fp,"      if(EASEA_Var.%s[EASEA_Ndx]) %s[EASEA_Ndx] = new %s(*(EASEA_Var.%s[EASEA_Ndx]));\n",
						pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
			}

			if (pSym->Object->ObjectType==oPointer)
			{
				fprintf(fp,"    if (%s) delete %s;\n",pSym->Object->sName,pSym->Object->sName);
				fprintf(fp,"    %s = (EASEA_Var.%s? new %s(*(EASEA_Var.%s)) : NULL);\n",
						pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
			}

		}// end while
		fprintf(fp,"  return *this;\n  }\n\n"); // end of operator <=

		// creation of operator ==
		fprintf(fp,"  bool operator==(%s &EASEA_Var) const {  // Operator==\n",sName); // operator==
		pSymbolList->reset();
409
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		{
			if (TARGET==CUDA || TARGET==STD)
			{
				if (pSym->Object->ObjectType==oObject)
					fprintf(fp,"    if (%s!=EASEA_Var.%s) return false;\n",pSym->Object->sName,pSym->Object->sName);
				if (pSym->Object->ObjectType==oArray )
				{
					fprintf(fp,"    {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						pSym->Object->nSize/pSym->Object->pType->nSize);
					fprintf(fp,"       if (%s[EASEA_Ndx]!=EASEA_Var.%s[EASEA_Ndx]) return false;}\n",
						pSym->Object->sName,pSym->Object->sName);
				}
				if ( pSym->Object->ObjectType==oArrayPointer)
				{
					fprintf(fp,"    {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						(int)(pSym->Object->nSize/sizeof(char*)));
					fprintf(fp,"       if (%s[EASEA_Ndx]!=EASEA_Var.%s[EASEA_Ndx]) return false;}\n",
						pSym->Object->sName,pSym->Object->sName);
				}
				if (pSym->Object->ObjectType==oPointer)
				{
					fprintf(fp,"    if (((%s) && (!EASEA_Var.%s)) || ((!%s) && (EASEA_Var.%s))) return false;\n",
						pSym->Object->sName,pSym->Object->sName, pSym->Object->sName,
433
						pSym->Object->sName);
Cancino Waldo's avatar
Cancino Waldo committed
434
					fprintf(fp,"    if ((%s)&&(%s!=EASEA_Var.%s)) return false;\n",
435
						pSym->Object->sName,pSym->Object->sName,pSym->Object->sName);
Cancino Waldo's avatar
Cancino Waldo committed
436 437 438 439 440 441 442 443 444 445 446 447
				}                                               
			}
		}
		if (TARGET==CUDA || TARGET==STD)  
			fprintf(fp,"  return true;\n  }\n\n"); // end of operator==

		// creation of operator !=
		fprintf(fp,"  bool operator!=(%s &EASEA_Var) const {return !(*this==EASEA_Var);} // operator!=\n\n",sName); // operator!=
		
		// creation of output stream insertion operator
		fprintf(fp,"  friend ostream& operator<< (ostream& os, const %s& EASEA_Var) { // Output stream insertion operator\n",sName); 
		pSymbolList->reset();
448
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		{
			if (pSym->Object->ObjectType==oObject)
				fprintf(fp,"    os <<  \"%s:\" << EASEA_Var.%s << \"\\n\";\n",pSym->Object->sName,pSym->Object->sName);
			
			if (pSym->Object->ObjectType==oArray )
			{
				fprintf(fp,"    {os << \"Array %s : \";\n",pSym->Object->sName);
				fprintf(fp,"     for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
					pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fp,"       os << \"[\" << EASEA_Ndx << \"]:\" << EASEA_Var.%s[EASEA_Ndx] << \"\\t\";}\n    os << \"\\n\";\n",pSym->Object->sName);
			}
			
			if( pSym->Object->ObjectType==oArrayPointer)
			{
				fprintf(fp,"    {os << \"Array %s : \";\n",pSym->Object->sName);
				fprintf(fp,"     for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
					(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fp,"       if( EASEA_Var.%s[EASEA_Ndx] ) os << \"[\" << EASEA_Ndx << \"]:\" << *(EASEA_Var.%s[EASEA_Ndx]) << \"\\t\";}\n    os << \"\\n\";\n",pSym->Object->sName,pSym->Object->sName);
			}
468
			
Cancino Waldo's avatar
Cancino Waldo committed
469 470 471
			if (pSym->Object->ObjectType==oPointer)
				fprintf(fp,"    if (EASEA_Var.%s) os << \"%s:\" << *(EASEA_Var.%s) << \"\\n\";\n",
				pSym->Object->sName,pSym->Object->sName,pSym->Object->sName);
472
		}
Cancino Waldo's avatar
Cancino Waldo committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		fprintf(fp,"    return os;\n  }\n\n"); // end of output stream insertion operator

		//     fprintf(fp,"  friend istream& operator>> (istream& is, %s& EASEA_Var) { // Input stream extraction operator\n",sName); // Output stream insertion operator
		//           pSymbolList->reset();
		//           while (pSym=pSymbolList->walkToNextItem()){
		//             if ((pSym->Object->ObjectType==oObject)&&(strcmp(pSym->Object->pType->sName, "bool"))) 
		//               fprintf(fp,"    is >> EASEA_Var.%s;\n",pSym->Object->sName);
		//             if ((pSym->Object->ObjectType==oArray)&&(strcmp(pSym->Object->pType->sName, "bool"))) {
		//               fprintf(fp,"    {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",pSym->Object->nSize/pSym->Object->pType->nSize);
		//               fprintf(fp,"       is >> EASEA_Var.%s[EASEA_Ndx];}\n",pSym->Object->sName);
		//             }                                         
		//           }
		//     fprintf(fp,"    return is;\n  }\n\n"); // Input stream extraction operator

		if (sString) 
		{
			if (bVERBOSE) printf ("Inserting Methods into %s Class.\n",sName);
			fprintf(fpOutputFile,"// User-defined methods:\n\n");
			fprintf(fpOutputFile,"%s\n",sString);
492
		}
493
	}
Cancino Waldo's avatar
Cancino Waldo committed
494 495 496

	fprintf(fp,"// Class members \n"); // Now, we must print the class members
	pSymbolList->reset();
497
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
498 499 500 501 502
	{
		if (pSym->Object->ObjectQualifier==1) // 1=Static
			fprintf(fp,"  static");
		if (pSym->Object->ObjectType==oObject)
			fprintf(fp,"  %s %s;\n",pSym->Object->pType->sName,pSym->Object->sName);
503
		if (pSym->Object->ObjectType==oPointer)
Cancino Waldo's avatar
Cancino Waldo committed
504
			fprintf(fp,"  %s *%s;\n",pSym->Object->pType->sName,pSym->Object->sName);
505
		if (pSym->Object->ObjectType==oArray)
Cancino Waldo's avatar
Cancino Waldo committed
506
			fprintf(fp,"  %s %s[%d];\n",pSym->Object->pType->sName,pSym->Object->sName,pSym->Object->nSize/pSym->Object->pType->nSize);
507
		if (pSym->Object->ObjectType==oArrayPointer)
Cancino Waldo's avatar
Cancino Waldo committed
508 509
			fprintf(fp,"  %s* %s[%d];\n",
			pSym->Object->pType->sName,pSym->Object->sName,(int)(pSym->Object->nSize/sizeof(char*)));
510
	}
moh_lo's avatar
moh_lo committed
511

Cancino Waldo's avatar
Cancino Waldo committed
512
	if (strcmp(sName,"Genome"))
513
		fprintf(fp,"};\n");
Cancino Waldo's avatar
Cancino Waldo committed
514 515 516 517 518 519 520 521 522 523 524
	
	return;
}
//*/

void CSymbol::printClasses(FILE *fp)
{
	CListItem<CSymbol*> *pSym;
	if (bAlreadyPrinted) return;
	bAlreadyPrinted=true;
	pSymbolList->reset();
525
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
526 527 528
		if ((pSym->Object->pType->ObjectType==oUserClass)&&(!pSym->Object->pType->bAlreadyPrinted))
			pSym->Object->pType->printClasses(fp);
	print(fp);
moh_lo's avatar
moh_lo committed
529 530
}

Cancino Waldo's avatar
Cancino Waldo committed
531 532 533 534 535 536 537 538 539
// prints user class definitions
void CSymbol::printUC(FILE* fp)
{
	//DEBUG_PRT("print user classes definitions");
	if (strcmp(sName,"Genome") && (TARGET==CUDA || TARGET==STD)) // If we are printing a user class other than the genome
	{   
		fprintf(fp,"\nclass %s;\n",sName); // class  header
	}
	//DEBUG_PRT("%s",sName);
maitre's avatar
maitre committed
540

Cancino Waldo's avatar
Cancino Waldo committed
541
	return;
moh_lo's avatar
moh_lo committed
542 543
}

Cancino Waldo's avatar
Cancino Waldo committed
544 545 546 547 548
// prints user classes
void CSymbol::printUserClasses(FILE *fp)
{
	CListItem<CSymbol*> *pSym;
	pSymbolList->reset();
maitre's avatar
maitre committed
549

Cancino Waldo's avatar
Cancino Waldo committed
550 551 552
	if (bAlreadyPrinted) return;

	bAlreadyPrinted=true;  
553
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
554
	{
555
		if (pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
556 557 558 559
			pSym->Object->pType->printUC(fp);
	}

	return;
moh_lo's avatar
moh_lo committed
560 561
}

Cancino Waldo's avatar
Cancino Waldo committed
562 563 564 565 566 567 568 569 570 571
// This function fills the "serialize" part of the individual class produced by EASEA
// the "serialize" is used to code individuals in strings, in order to push them into UDP
// packets and send them to other instances of EASEA running on other network-connected machines
void CSymbol::serializeIndividual(FILE *fp, char* sCompleteName)
{
	CListItem<CSymbol*> *pSym;
	pSymbolList->reset();
	char sNewCompleteName[1000];
	strcpy(sNewCompleteName, sCompleteName);

572
	while((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
573 574
	{
		// check the type of object 
575
		if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
		{
			// if it's an user-defined class
			if (pSym->Object->ObjectType==oArrayPointer)
			{
				// if it's an array of pointers
				fprintf(fp,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++){\n",(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fpOutputFile,"\t\tif(this->%s[EASEA_Ndx] != NULL){\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t\t\tEASEA_Line << \"\\a \";\n");
				fprintf(fpOutputFile,"\t\t\tEASEA_Line << this->%s[EASEA_Ndx]->serializer() << \" \";\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t}\n");
				fprintf(fpOutputFile,"\t\telse\n");
				fprintf(fpOutputFile,"\t\t\tEASEA_Line << \"NULL\" << \" \";\n");
				fprintf(fpOutputFile,"}\n");

			}
591 592 593 594 595 596 597 598
			// it's a classical array
			else if(pSym->Object->ObjectType==oArray)
			{
				/*TODO: not clean at all*/
				fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",
						pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s[EASEA_Ndx].serializer() <<\" \";\n", pSym->Object->sName);
			}
599
			// it's a simple struct/class 
600 601 602 603 604
			else if(pSym->Object->ObjectType==oObject)
			{
				fprintf(fpOutputFile,"\tEASEA_Line << \"\\a \";\n");
				fprintf(fpOutputFile,"\tEASEA_Line << this->%s.serializer() << \" \";\n",pSym->Object->sName);
			}
Cancino Waldo's avatar
Cancino Waldo committed
605 606 607 608 609 610 611 612 613 614
			else
			{
				// if it's not an array of pointers
				fprintf(fpOutputFile,"\tif(this->%s != NULL){\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t\tEASEA_Line << \"\\a \";\n");
				fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s->serializer() << \" \";\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t}\n");
				fprintf(fpOutputFile,"\telse\n");
				fprintf(fpOutputFile,"\t\tEASEA_Line << \"NULL\" << \" \";\n");
			}
615
		}
Cancino Waldo's avatar
Cancino Waldo committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
		else
		{
			// if it's not a user-defined class
			if (pSym->Object->ObjectType==oObject)
			{
				fprintf(fpOutputFile,"\tEASEA_Line << this->%s << \" \";\n",pSym->Object->sName);
			}
			else if(pSym->Object->ObjectType==oArray)
			{
				// if it's an array of floats	
				fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fpOutputFile,"\t\tEASEA_Line << this->%s[EASEA_Ndx] <<\" \";\n", pSym->Object->sName);
			}
			else if(pSym->Object->ObjectType==oPointer && strcmp(pSym->Object->pType->sName, "GPNode") == 0)
			{
				// it's a pointer to a GPNode!
				fprintf(fpOutputFile,"\t// Serialize function for \"%s\"\n", pSym->Object->pType->sName);
					
				// serialize function: it needs the <map> and <vector> includes, but those are added
				// at the top of the class if it's a GPNode individual
				fprintf(fpOutputFile,"\tcout << \"Now serializing individual \" << toString(this->root) << endl;\n");
				fprintf(fpOutputFile,"\t// build map used to associate GPNode pointers to indexes\n");
				fprintf(fpOutputFile,"\tmap<GPNode*,int> indexes;\n");
				fprintf(fpOutputFile,"\n");
				fprintf(fpOutputFile,"\t// breadth-first visit of the tree\n");
				fprintf(fpOutputFile,"\tint currentIndex = 0;\n");
				fprintf(fpOutputFile,"\tlist<GPNode*> nodesToVisit;\n");
				fprintf(fpOutputFile,"\tnodesToVisit.push_back(this->root);\n");
				fprintf(fpOutputFile,"\n");
				fprintf(fpOutputFile,"\twhile(nodesToVisit.size() != 0)\n");
				fprintf(fpOutputFile,"\t{\n");
				fprintf(fpOutputFile,"\t // remove current node from nodes to visit\n");
				fprintf(fpOutputFile,"\t GPNode* currentNode = nodesToVisit.front();\n");
				fprintf(fpOutputFile,"\t nodesToVisit.pop_front();\n");
				fprintf(fpOutputFile,"\t\n");
				fprintf(fpOutputFile,"\t // add children of current node (by default, the tree is binary)\n");
				fprintf(fpOutputFile,"\t if( currentNode->children[0] != NULL ) nodesToVisit.push_back( currentNode->children[0] ); \n");
				fprintf(fpOutputFile,"\t if( currentNode->children[1] != NULL ) nodesToVisit.push_back( currentNode->children[1] ); \n");
				fprintf(fpOutputFile,"\t // assign code to current node       \n");
				fprintf(fpOutputFile,"\t indexes[currentNode] = currentIndex; \n");
				fprintf(fpOutputFile,"\t currentIndex++;\n");
				fprintf(fpOutputFile,"\t}\n");
				fprintf(fpOutputFile,"\t// the very first item in the line is the number of nodes in the tree \n");
				fprintf(fpOutputFile,"\tEASEA_Line << currentIndex << \" \";\n");
				fprintf(fpOutputFile,"\t// another visit to finally serialize the nodes \n");
				fprintf(fpOutputFile,"\tvector<double> ercValues;          \n");
				fprintf(fpOutputFile,"\tnodesToVisit.push_back(this->root);\n");
				fprintf(fpOutputFile,"\twhile(nodesToVisit.size() != 0)                     \n");
				fprintf(fpOutputFile,"\t{                                                   \n");
				fprintf(fpOutputFile,"\t // remove current node from nodes to visit  \n");
				fprintf(fpOutputFile,"\t GPNode* currentNode = nodesToVisit.front(); \n");
				fprintf(fpOutputFile,"\t nodesToVisit.pop_front();	\n");
				fprintf(fpOutputFile,"\t // add children of current node (hoping it's binary)                                                       \n");
				fprintf(fpOutputFile,"\t if( currentNode->children[0] != NULL ) nodesToVisit.push_back( currentNode->children[0] );                 \n");
				fprintf(fpOutputFile,"\t if( currentNode->children[1] != NULL ) nodesToVisit.push_back( currentNode->children[1] );                 \n");
				fprintf(fpOutputFile,"\t                                                                                                           \n");
				fprintf(fpOutputFile,"\t // node to string: format is <index> <var_id> <opCode> <indexOfChild1> <indexOfChild2>                     \n");
				fprintf(fpOutputFile,"\t EASEA_Line << indexes[currentNode] << \" \" << currentNode->var_id << \" \" << (int)currentNode->opCode << \" \";\n");
				fprintf(fpOutputFile,"\t // if the children are not NULL, put their index; otherwise, put \"0\"              \n");
				fprintf(fpOutputFile,"\t if( currentNode->children[0] != NULL )                                            \n");
				fprintf(fpOutputFile,"\t  EASEA_Line << indexes[ currentNode->children[0] ] << \" \";                 \n");
				fprintf(fpOutputFile,"\t else                                                                              \n");
				fprintf(fpOutputFile,"\t  EASEA_Line << \"0 \";                                                       \n");
				fprintf(fpOutputFile,"\t                                                                                  \n");
				fprintf(fpOutputFile,"\t if( currentNode->children[1] != NULL )                                            \n");
				fprintf(fpOutputFile,"\t  EASEA_Line << indexes[ currentNode->children[1] ] << \" \";                 \n");
				fprintf(fpOutputFile,"\t else             \n");
				fprintf(fpOutputFile,"\t  EASEA_Line << \"0 \";                                                       \n");
				fprintf(fpOutputFile,"\t                 \n");
				fprintf(fpOutputFile,"\t // if the node is an ERC, the floating point value is stored for later            \n");
				fprintf(fpOutputFile,"\t if( currentNode->opCode == OP_ERC ) ercValues.push_back( currentNode->erc_value );\n");
				fprintf(fpOutputFile,"\t} \n");
				fprintf(fpOutputFile,"\t// finally, put all the floating point ERC values             \n");
				fprintf(fpOutputFile,"\tfor(unsigned int i = 0; i < ercValues.size(); i++)            \n");
				fprintf(fpOutputFile,"\t EASEA_Line << ercValues[i] << \" \";                    \n");
				fprintf(fpOutputFile,"\t                                                              \n");
				fprintf(fpOutputFile,"\t// debug                                                      \n");
				fprintf(fpOutputFile,"\t//cout << \"EASEA_Line: \" << EASEA_Line.str() << endl; \n");
			}

		} // end if it's a user-defined class
	}// end while

	return;
kruger's avatar
kruger committed
700 701 702
}

void CSymbol::deserializeIndividual(FILE *fp, char* sCompleteName){
Cancino Waldo's avatar
Cancino Waldo committed
703 704
	CListItem<CSymbol*> *pSym;
	pSymbolList->reset();
705 706
	while ((pSym=pSymbolList->walkToNextItem())){
		if(pSym->Object->pType->ObjectType==oUserClass){
Cancino Waldo's avatar
Cancino Waldo committed
707 708 709 710 711 712 713 714 715 716 717
			if (pSym->Object->ObjectType==oArrayPointer){
				fprintf(fpOutputFile,"\tEASEA_Line >> line;\n");
				fprintf(fp,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++){\n",(int)(pSym->Object->nSize/sizeof(char*)));
				fprintf(fpOutputFile,"\t\tif(strcmp(line.c_str(),\"NULL\")==0)\n");
				fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx] = NULL;\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t\telse{\n");
				fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx] = new %s;\n",pSym->Object->sName, pSym->Object->pType->sName);
				fprintf(fpOutputFile,"\t\t\tthis->%s[EASEA_Ndx]->deserializer(&EASEA_Line);\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t\t}");
				fprintf(fpOutputFile,"\t}");
			}
718 719 720 721 722 723 724 725
			else if(pSym->Object->ObjectType==oArray){
				fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fpOutputFile,"\t\tthis->%s[EASEA_Ndx].deserializer(&EASEA_Line);\n", pSym->Object->sName);
			}
			else if(pSym->Object->ObjectType==oObject)
			{
				fprintf(fpOutputFile,"\t this->%s.deserializer(&EASEA_Line);",pSym->Object->sName);
			}
Cancino Waldo's avatar
Cancino Waldo committed
726 727 728 729 730 731 732 733 734
			else{
				fprintf(fpOutputFile,"\tEASEA_Line >> line;\n");
				fprintf(fpOutputFile,"\tif(strcmp(line.c_str(),\"NULL\")==0)\n");
				fprintf(fpOutputFile,"\t\tthis->%s = NULL;\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\telse{\n");
				fprintf(fpOutputFile,"\t\tthis->%s = new %s;\n",pSym->Object->sName, pSym->Object->pType->sName);
				fprintf(fpOutputFile,"\t\tthis->%s->deserializer(&EASEA_Line);\n",pSym->Object->sName);
				fprintf(fpOutputFile,"\t}");
			}
735
		}
Cancino Waldo's avatar
Cancino Waldo committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		else
		{
			if (pSym->Object->ObjectType==oObject){
				fprintf(fpOutputFile,"\tEASEA_Line >> this->%s;\n",pSym->Object->sName);
			}

			if(pSym->Object->ObjectType==oArray){
				fprintf(fpOutputFile,"\tfor(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",pSym->Object->nSize/pSym->Object->pType->nSize);
				fprintf(fpOutputFile,"\t\tEASEA_Line >> this->%s[EASEA_Ndx];\n", pSym->Object->sName);
			}

			if(pSym->Object->ObjectType==oPointer && strcmp(pSym->Object->pType->sName, "GPNode") == 0)
			{
				// it's a GPNode, so a tree-like structure used for GP
				fprintf(fpOutputFile, "\t// debug\n");	
				fprintf(fpOutputFile, "\t//cout << \"Reading received individual...\" << endl;\n");	
				fprintf(fpOutputFile, "\t//cout << Line << endl;\n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t// first, read number of nodes\n");	
				fprintf(fpOutputFile, "\tint numberOfNodes; \n");	
				fprintf(fpOutputFile, "\tEASEA_Line >> numberOfNodes;\n");	
				fprintf(fpOutputFile, "\t// debug\n");	
				fprintf(fpOutputFile, "\t//cout << \"The received individual has \" << numberOfNodes << \" nodes.\" << endl; \n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t// iterate through the line, creating a map of <index> <GPNode*> <indexOfChild1> <indexOfChild2> \n");	
				fprintf(fpOutputFile, "\tmap< int, pair< GPNode*,vector<int> > > nodeMap;                              \n");	
				fprintf(fpOutputFile, "\tfor(int n = 0; n < numberOfNodes; n++)                                        \n");	
				fprintf(fpOutputFile, "\t{                                                                             \n");	
				fprintf(fpOutputFile, "\t int index, temp;                                                      \n");	
				fprintf(fpOutputFile, "\t int opCode;                                                           \n");	
				fprintf(fpOutputFile, "\t int var_id;                                                           \n");	
				fprintf(fpOutputFile, "\t vector<int> childrenIndexes;                                          \n");	
				fprintf(fpOutputFile, "\t  \n");	
				fprintf(fpOutputFile, "\t // format is <index> <var_id> <opCode> <indexOfChild1> <indexOfChild2>\n");	
				fprintf(fpOutputFile, "\t EASEA_Line >> index;             \n");	
				fprintf(fpOutputFile, "\t EASEA_Line >> var_id;            \n");	
				fprintf(fpOutputFile, "\t EASEA_Line >> opCode;            \n");	
				fprintf(fpOutputFile, "\t EASEA_Line >> temp;              \n");	
				fprintf(fpOutputFile, "\t childrenIndexes.push_back(temp); \n");	
				fprintf(fpOutputFile, "\t EASEA_Line >> temp;              \n");	
				fprintf(fpOutputFile, "\t childrenIndexes.push_back(temp); \n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t // create GPNode                   \n");	
				fprintf(fpOutputFile, "\t GPNode* currentNode = new GPNode();\n");	
				fprintf(fpOutputFile, "\t currentNode->var_id = var_id;\n");	
				fprintf(fpOutputFile, "\t currentNode->opCode = opCode;\n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t // debug \n");	
				fprintf(fpOutputFile, "\t //cout 	<< \"Read node: \" << index << \" \" << var_id << \" \" << opCode << \" \" \n");	
				fprintf(fpOutputFile, "\t //<< childrenIndexes[0] << \" \" << childrenIndexes[1] << endl;\n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t // put everything into the map                                        \n");	
				fprintf(fpOutputFile, "\t pair< GPNode*, vector<int> > tempPair (currentNode, childrenIndexes); \n");	
				fprintf(fpOutputFile, "\t nodeMap[index] = tempPair;\n"); 
				fprintf(fpOutputFile, "\t}\n");	
				fprintf(fpOutputFile, "\t \n");	
				fprintf(fpOutputFile, "\t// rebuild the individual structure \n");	
				fprintf(fpOutputFile, "\tfor(int n = 0; n < numberOfNodes; n++) \n");	
				fprintf(fpOutputFile, "\t{ \n");	
				fprintf(fpOutputFile, "\t // now, rebuild the individual by adding the pointers to the children                                  \n");	
				fprintf(fpOutputFile, "\t if( nodeMap[n].second[0] != 0 ) nodeMap[n].first->children[0] = nodeMap[ nodeMap[n].second[0] ].first; \n");	
				fprintf(fpOutputFile, "\t if( nodeMap[n].second[1] != 0 ) nodeMap[n].first->children[1] = nodeMap[ nodeMap[n].second[1] ].first; \n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t // also, if the opCode of the node is the same as the OP_ERC, find the\n");	
				fprintf(fpOutputFile, "\t // corresponding real value at the end of the EASEA_Line\n");	
				fprintf(fpOutputFile, "\t if( nodeMap[n].first->opCode == OP_ERC )\n");	
				fprintf(fpOutputFile, "\t {                                          \n");	
				fprintf(fpOutputFile, "\t                                           \n");	
				fprintf(fpOutputFile, "\t  double temp;                       \n");	
				fprintf(fpOutputFile, "\t  EASEA_Line >> temp;                \n");	
				fprintf(fpOutputFile, "\t  nodeMap[n].first->erc_value = temp;\n");	
				fprintf(fpOutputFile, "\t// debug\n");	
				fprintf(fpOutputFile, "\t//cout << \"-- Found ERC variable! Read value \" << temp << \" from the end of EASEA_Line.\" << endl;\n");	
				fprintf(fpOutputFile, "\t }\n");	
				fprintf(fpOutputFile, "\t}\n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t// link the tree to the current individual \n");	
				fprintf(fpOutputFile, "\tthis->root = nodeMap[0].first;\n");	
				fprintf(fpOutputFile, "\t\n");	
				fprintf(fpOutputFile, "\t// debug \n");	
				fprintf(fpOutputFile, "\t//cout << \"Individual received: \" << toString(this->root) << endl;\n");	
			}
		}
	}
kruger's avatar
kruger committed
820 821
}

moh_lo's avatar
moh_lo committed
822

moh_lo's avatar
moh_lo committed
823
void CSymbol::printAllSymbols(FILE *fp, char *sCompleteName, EObjectType FatherType, CListItem<CSymbol *> *pSym){
Cancino Waldo's avatar
Cancino Waldo committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	char sNewCompleteName[000], s[20];
	strcpy(sNewCompleteName, sCompleteName);
	do {
		if (pSym->Object->pType->ObjectType==oUserClass){
			if (FatherType==oPointer) 
				strcat(sNewCompleteName,"->");
			else strcat(sNewCompleteName,".");
			strcat(sNewCompleteName,pSym->Object->sName);
			if (pSym->Object->ObjectType==oArray) {
				strcat(sNewCompleteName,"[");
				sprintf(s,"%d",pSym->Object->nSize/pSym->Object->pType->nSize);
				strcat(sNewCompleteName,s);
				strcat(sNewCompleteName,"]");
			}
			if (pSym->Object->pType==pSym->Object->pClass) 
				fprintf(fp,"%s\n",sNewCompleteName);
			else printAllSymbols(fp, sNewCompleteName, pSym->Object->ObjectType, pSym->Object->pType->pSymbolList->getHead());
			strcpy(sNewCompleteName, sCompleteName);
		}
		else {
			if (FatherType==oPointer) 
				strcat(sNewCompleteName,"->");
			else strcat(sNewCompleteName,".");
			strcat(sNewCompleteName,pSym->Object->sName);
848
			if (pSym->Object->ObjectType==oArray) {
Cancino Waldo's avatar
Cancino Waldo committed
849 850 851 852 853 854 855 856
				strcat(sNewCompleteName,"[");
				sprintf(s,"%d",pSym->Object->nSize/pSym->Object->pType->nSize);
				strcat(sNewCompleteName,s);
				strcat(sNewCompleteName,"]");
			}
			fprintf(fp,"%s\n",sNewCompleteName);
			strcpy(sNewCompleteName, sCompleteName);
		}
857
	} while ((pSym=pSym->pNext));
moh_lo's avatar
moh_lo committed
858
}
Cancino Waldo's avatar
Cancino Waldo committed
859

moh_lo's avatar
moh_lo committed
860 861 862 863 864

/////////////////////////////////////////////////////////////////////////////
// symboltable construction/destruction

CSymbolTable::CSymbolTable(){
Cancino Waldo's avatar
Cancino Waldo committed
865 866 867
	for (int i = 0; i < BUCKET_SIZE; i++) {
		saBucket[i] = NULL;
	}
868
}
moh_lo's avatar
moh_lo committed
869 870

CSymbolTable::~CSymbolTable(){
Cancino Waldo's avatar
Cancino Waldo committed
871 872 873 874 875 876 877 878
	for (int i = 0; i < BUCKET_SIZE; i++) {
		CSymbol* pSym = saBucket[i];
		while (pSym!= NULL) {
			CSymbol* pNextSym = pSym->pNextInBucket;
			delete pSym;
			pSym = pNextSym;
		}
	}
879
}
moh_lo's avatar
moh_lo committed
880

maitre's avatar
maitre committed
881

moh_lo's avatar
moh_lo committed
882 883 884 885
/////////////////////////////////////////////////////////////////////////////
// symbol table  commands

int CSymbolTable::hash(const char* s) const{
Cancino Waldo's avatar
Cancino Waldo committed
886 887 888 889 890 891 892 893
	assert(s != NULL);
	int i = 0;
	while (*s != '\0') {
		i = i << 1 ^ *s++;
	}
	i %= BUCKET_SIZE;
	if (i < 0)  i *= -1;
	return i;
894
}
moh_lo's avatar
moh_lo committed
895 896

CSymbol* CSymbolTable::insert(CSymbol *pSymbol){
Cancino Waldo's avatar
Cancino Waldo committed
897 898 899 900 901 902 903 904 905 906 907
	int i = hash(pSymbol->sName);
	CSymbol* pSym;
	for (pSym = saBucket[i]; pSym != NULL; pSym = pSym->pNextInBucket)
		if (strcmp(pSym->sName, pSymbol->sName) == 0){
			delete pSymbol;
			return pSym;
		}
	pSym = pSymbol;
	pSym->pNextInBucket = saBucket[i];
	saBucket[i] = pSym;
	return pSym;
908
}
moh_lo's avatar
moh_lo committed
909 910

CSymbol* CSymbolTable::find(const char *s){
Cancino Waldo's avatar
Cancino Waldo committed
911 912 913 914 915
	int i = hash(s);
	for (CSymbol* pSym = saBucket[i]; pSym != NULL; pSym = pSym->pNextInBucket)
		if (strcmp(pSym->sName,s) == 0)
			return pSym;     
	return NULL;
916 917 918 919
}


OPCodeDesc::OPCodeDesc(){
Cancino Waldo's avatar
Cancino Waldo committed
920 921
	isERC = false;
	arity = 0;
922 923
}
int OPCodeDescCompare(const void* a, const void* b){
Cancino Waldo's avatar
Cancino Waldo committed
924 925 926 927 928 929 930 931 932 933
	OPCodeDesc** f1, ** f2;
	f1 = (OPCodeDesc**)a;
	f2 = (OPCodeDesc**)b;

	if( (*f1)->arity==(*f2)->arity && (*f1)->arity==0 ){
		return (*f1)->isERC-(*f2)->isERC;
	}
	else{
		return (*f1)->arity-(*f2)->arity;
	}
934 935 936
}

void OPCodeDesc::sort(OPCodeDesc** opDescs, unsigned len){
Cancino Waldo's avatar
Cancino Waldo committed
937
	qsort(opDescs,len,sizeof(OPCodeDesc*),OPCodeDescCompare);
938 939 940
}

void OPCodeDesc::show(void){
Cancino Waldo's avatar
Cancino Waldo committed
941 942 943 944 945 946 947

	cout << "OPCode : " << *this->opcode << endl;
	cout << "Real name : " << *this->realName << endl;
	cout << "Arity : " << this->arity << endl;
	cout << "cpu code : \n" << this->cpuCodeStream.str() << endl;
	cout << "gpu code : \n" << this->gpuCodeStream.str() << endl;

948
}