EaseaSym.cpp 37.7 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

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

/////////////////////////////////////////////////////////////////////////////
// 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
42
43
44
45
46
47
48
49
50
51
52
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;
53
}
Cancino Waldo's avatar
Cancino Waldo committed
54

moh_lo's avatar
moh_lo committed
55
56
57
/////////////////////////////////////////////////////////////////////////////
// symbol construction/destruction

Cancino Waldo's avatar
Cancino Waldo committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
CSymbol::CSymbol(char *s)
{
	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;
73
}
Cancino Waldo's avatar
Cancino Waldo committed
74
75
76
77

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

moh_lo's avatar
moh_lo committed
80
81


maitre's avatar
maitre committed
82
83
/////////////////////////////////////////////////////////////////////////////
// symbol  commands
Cancino Waldo's avatar
Cancino Waldo committed
84
85
86
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
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();
113
			while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
114
115
			{
				//DEBUG_PRT("analyse flat %s",pSym->Object->pType->sName);
116
				if( pSym->Object->ObjectType == oPointer ) //|| (pSym->Object->pType->ObjectType == oObject) )
Cancino Waldo's avatar
Cancino Waldo committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
				{  
					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);

139
				while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
140
				{
141
					if( pSym->Object->ObjectType == oPointer )  //|| (pSym->Object->pType->ObjectType == oObject) )
Cancino Waldo's avatar
Cancino Waldo committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
					{
						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);      
				fprintf(fp,"  }\n\n");
			}
		}
moh_lo's avatar
moh_lo committed
167

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

172
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		{
			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();

189
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
190
191
192
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
		{
			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();
220
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
		{
			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();
240
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
241
242
		{
			// check: is it a user-defined class?
243
			if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
			{
				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");
				}
				else
				{
					// 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,"}\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();
295
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
296
		{
297
			if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
			{
				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}");
				}
				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");
320
					fprintf(fpOutputFile,"\t\tthis->%s = new %s;\n", pSym->Object->sName, pSym->Object->pType->sName);
Cancino Waldo's avatar
Cancino Waldo committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
					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();

347
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
348
349
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
376
377
378
379
		{
			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();
380
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
		{
			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,
404
						pSym->Object->sName);
Cancino Waldo's avatar
Cancino Waldo committed
405
					fprintf(fp,"    if ((%s)&&(%s!=EASEA_Var.%s)) return false;\n",
406
						pSym->Object->sName,pSym->Object->sName,pSym->Object->sName);
Cancino Waldo's avatar
Cancino Waldo committed
407
408
409
410
411
412
413
414
415
416
417
418
				}                                               
			}
		}
		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();
419
		while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
		{
			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);
			}
439
			
Cancino Waldo's avatar
Cancino Waldo committed
440
441
442
			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);
443
		}
Cancino Waldo's avatar
Cancino Waldo committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
		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);
463
		}
464
	}
Cancino Waldo's avatar
Cancino Waldo committed
465
466
467

	fprintf(fp,"// Class members \n"); // Now, we must print the class members
	pSymbolList->reset();
468
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
469
470
471
472
473
	{
		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);
474
		if (pSym->Object->ObjectType==oPointer)
Cancino Waldo's avatar
Cancino Waldo committed
475
			fprintf(fp,"  %s *%s;\n",pSym->Object->pType->sName,pSym->Object->sName);
476
		if (pSym->Object->ObjectType==oArray)
Cancino Waldo's avatar
Cancino Waldo committed
477
			fprintf(fp,"  %s %s[%d];\n",pSym->Object->pType->sName,pSym->Object->sName,pSym->Object->nSize/pSym->Object->pType->nSize);
478
		if (pSym->Object->ObjectType==oArrayPointer)
Cancino Waldo's avatar
Cancino Waldo committed
479
480
			fprintf(fp,"  %s* %s[%d];\n",
			pSym->Object->pType->sName,pSym->Object->sName,(int)(pSym->Object->nSize/sizeof(char*)));
481
	}
moh_lo's avatar
moh_lo committed
482

Cancino Waldo's avatar
Cancino Waldo committed
483
	if (strcmp(sName,"Genome"))
484
		fprintf(fp,"};\n");
Cancino Waldo's avatar
Cancino Waldo committed
485
486
487
488
489
490
491
492
493
494
495
	
	return;
}
//*/

void CSymbol::printClasses(FILE *fp)
{
	CListItem<CSymbol*> *pSym;
	if (bAlreadyPrinted) return;
	bAlreadyPrinted=true;
	pSymbolList->reset();
496
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
497
498
499
		if ((pSym->Object->pType->ObjectType==oUserClass)&&(!pSym->Object->pType->bAlreadyPrinted))
			pSym->Object->pType->printClasses(fp);
	print(fp);
moh_lo's avatar
moh_lo committed
500
501
}

Cancino Waldo's avatar
Cancino Waldo committed
502
503
504
505
506
507
508
509
510
// 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
511

Cancino Waldo's avatar
Cancino Waldo committed
512
	return;
moh_lo's avatar
moh_lo committed
513
514
}

Cancino Waldo's avatar
Cancino Waldo committed
515
516
517
518
519
// prints user classes
void CSymbol::printUserClasses(FILE *fp)
{
	CListItem<CSymbol*> *pSym;
	pSymbolList->reset();
maitre's avatar
maitre committed
520

Cancino Waldo's avatar
Cancino Waldo committed
521
522
523
	if (bAlreadyPrinted) return;

	bAlreadyPrinted=true;  
524
	while ((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
525
	{
526
		if (pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
527
528
529
530
			pSym->Object->pType->printUC(fp);
	}

	return;
moh_lo's avatar
moh_lo committed
531
532
}

Cancino Waldo's avatar
Cancino Waldo committed
533
534
535
536
537
538
539
540
541
542
// 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);

543
	while((pSym=pSymbolList->walkToNextItem()))
Cancino Waldo's avatar
Cancino Waldo committed
544
545
	{
		// check the type of object 
546
		if(pSym->Object->pType->ObjectType==oUserClass)
Cancino Waldo's avatar
Cancino Waldo committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
		{
			// 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");

			}
			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");
			}
572
		}
Cancino Waldo's avatar
Cancino Waldo committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
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
		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
657
658
659
}

void CSymbol::deserializeIndividual(FILE *fp, char* sCompleteName){
Cancino Waldo's avatar
Cancino Waldo committed
660
661
	CListItem<CSymbol*> *pSym;
	pSymbolList->reset();
662
663
	while ((pSym=pSymbolList->walkToNextItem())){
		if(pSym->Object->pType->ObjectType==oUserClass){
Cancino Waldo's avatar
Cancino Waldo committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
			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}");
			}
			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}");
			}
684
		}
Cancino Waldo's avatar
Cancino Waldo committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
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
		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
769
770
}

moh_lo's avatar
moh_lo committed
771

moh_lo's avatar
moh_lo committed
772
void CSymbol::printAllSymbols(FILE *fp, char *sCompleteName, EObjectType FatherType, CListItem<CSymbol *> *pSym){
Cancino Waldo's avatar
Cancino Waldo committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
	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);
797
			if (pSym->Object->ObjectType==oArray) {
Cancino Waldo's avatar
Cancino Waldo committed
798
799
800
801
802
803
804
805
				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);
		}
806
	} while ((pSym=pSym->pNext));
moh_lo's avatar
moh_lo committed
807
}
Cancino Waldo's avatar
Cancino Waldo committed
808

moh_lo's avatar
moh_lo committed
809
810
811
812
813

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

CSymbolTable::CSymbolTable(){
Cancino Waldo's avatar
Cancino Waldo committed
814
815
816
	for (int i = 0; i < BUCKET_SIZE; i++) {
		saBucket[i] = NULL;
	}
817
}
moh_lo's avatar
moh_lo committed
818
819

CSymbolTable::~CSymbolTable(){
Cancino Waldo's avatar
Cancino Waldo committed
820
821
822
823
824
825
826
827
	for (int i = 0; i < BUCKET_SIZE; i++) {
		CSymbol* pSym = saBucket[i];
		while (pSym!= NULL) {
			CSymbol* pNextSym = pSym->pNextInBucket;
			delete pSym;
			pSym = pNextSym;
		}
	}
828
}
moh_lo's avatar
moh_lo committed
829

maitre's avatar
maitre committed
830

moh_lo's avatar
moh_lo committed
831
832
833
834
/////////////////////////////////////////////////////////////////////////////
// symbol table  commands

int CSymbolTable::hash(const char* s) const{
Cancino Waldo's avatar
Cancino Waldo committed
835
836
837
838
839
840
841
842
	assert(s != NULL);
	int i = 0;
	while (*s != '\0') {
		i = i << 1 ^ *s++;
	}
	i %= BUCKET_SIZE;
	if (i < 0)  i *= -1;
	return i;
843
}
moh_lo's avatar
moh_lo committed
844
845

CSymbol* CSymbolTable::insert(CSymbol *pSymbol){
Cancino Waldo's avatar
Cancino Waldo committed
846
847
848
849
850
851
852
853
854
855
856
	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;
857
}
moh_lo's avatar
moh_lo committed
858
859

CSymbol* CSymbolTable::find(const char *s){
Cancino Waldo's avatar
Cancino Waldo committed
860
861
862
863
864
	int i = hash(s);
	for (CSymbol* pSym = saBucket[i]; pSym != NULL; pSym = pSym->pNextInBucket)
		if (strcmp(pSym->sName,s) == 0)
			return pSym;     
	return NULL;
865
866
867
868
}


OPCodeDesc::OPCodeDesc(){
Cancino Waldo's avatar
Cancino Waldo committed
869
870
	isERC = false;
	arity = 0;
871
872
}
int OPCodeDescCompare(const void* a, const void* b){
Cancino Waldo's avatar
Cancino Waldo committed
873
874
875
876
877
878
879
880
881
882
	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;
	}
883
884
885
}

void OPCodeDesc::sort(OPCodeDesc** opDescs, unsigned len){
Cancino Waldo's avatar
Cancino Waldo committed
886
	qsort(opDescs,len,sizeof(OPCodeDesc*),OPCodeDescCompare);
887
888
889
}

void OPCodeDesc::show(void){
Cancino Waldo's avatar
Cancino Waldo committed
890
891
892
893
894
895
896

	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;

897
}