ca5cf9e3f15e2230d0cb659699488432bcc84b8f
[scilab.git] / scilab / modules / abstractSyntaxTree / includes / run_OpExp.hxx
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 *  Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
5 *
6 *  This file must be used under the terms of the CeCILL.
7 *  This source file is licensed as described in the file COPYING, which
8 *  you should have received as part of this distribution.  The terms
9 *  are also available at
10 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11 *
12 */
13
14 // This code is separated in run_AssignExp.hxx
15 // but will be inlined in runvisitor.hxx
16 // using #include with RunVisitorT class declaration.
17 //
18 // If you need additionnal headers, please add it in runvisitor.hxx
19
20 void visitprivate(const OpExp &e)
21 {
22     T execMeL;
23     T execMeR;
24
25     try
26     {
27
28         /*getting what to assign*/
29         e.left_get().accept(execMeL);
30         /*getting what to assign*/
31         e.right_get().accept(execMeR);
32         GenericType::RealType TypeL = execMeL.result_get()->getType();
33         GenericType::RealType TypeR = execMeR.result_get()->getType();
34
35         if(TypeL == GenericType::RealImplicitList)
36         {
37             ImplicitList* pIL = execMeL.result_get()->getAsImplicitList();
38             if(pIL->computable())
39             {
40                 InternalType *pIT = pIL->extract_matrix();
41                 execMeL.result_set(pIT);
42                 TypeL = pIT->getType();
43             }
44         }
45
46         if(TypeR == GenericType::RealImplicitList)
47         {
48             ImplicitList* pIL = execMeR.result_get()->getAsImplicitList();
49             if(pIL->computable())
50             {
51                 InternalType *pIT = pIL->extract_matrix();
52                 execMeR.result_set(pIT);
53                 TypeR = pIT->getType();
54             }
55         }
56
57         InternalType *pResult = NULL;
58         switch(e.oper_get())
59         {
60         case OpExp::plus :
61             {
62                 pResult = GenericPlus(execMeL.result_get(), execMeR.result_get());
63                 if (pResult == NULL)
64                 {
65                     // We did not have any algorithm matching, so we try to call OverLoad
66                     pResult = callOverload(e.oper_get(), &execMeL, &execMeR);
67
68                 }
69                 result_set(pResult);
70                 break;
71             }
72         case OpExp::minus :
73             {
74                 pResult = GenericMinus(execMeL.result_get(), execMeR.result_get());
75                 if (pResult == NULL)
76                 {
77                     // We did not have any algorithm matching, so we try to call OverLoad
78                     pResult = callOverload(e.oper_get(), &execMeL, &execMeR);
79
80                 }
81                 result_set(pResult);
82                 break;
83             }
84         case OpExp::times:
85               {
86                   pResult = GenericTimes(execMeL.result_get(), execMeR.result_get());
87                   if (pResult == NULL)
88                   {
89                       // We did not have any algorithm matching, so we try to call OverLoad
90                       pResult = callOverload(e.oper_get(), &execMeL, &execMeR);
91
92                   }
93                   result_set(pResult);
94                   break;
95               }
96         case OpExp::rdivide:
97             {
98                 pResult = GenericRDivide(execMeL.result_get(), execMeR.result_get());
99                 if (pResult == NULL)
100                 {
101                     // We did not have any algorithm matching, so we try to call OverLoad
102                     pResult = callOverload(e.oper_get(), &execMeL, &execMeR);
103
104                 }
105                 result_set(pResult);
106                 break;
107             }
108         case OpExp::dottimes :
109             {
110                 pResult = GenericDotTimes(execMeL.result_get(), execMeR.result_get());
111                 if (pResult == NULL)
112                 {
113                     // We did not have any algorithm matching, so we try to call OverLoad
114                     pResult = callOverload(e.oper_get(), &execMeL, &execMeR);
115                 }
116                 result_set(pResult);
117                 break;
118             }
119         case OpExp::eq :
120             {
121                 if(TypeL == GenericType::RealDouble && execMeL.result_get()->getAsDouble()->size_get() == 0)
122                 {//[] == xx
123                     if(TypeR != InternalType::RealDouble)
124                     {
125                         result_set(new Bool(false));
126                         return;
127                     }
128                 }
129
130                 if(TypeR == GenericType::RealDouble && execMeR.result_get()->getAsDouble()->size_get() == 0)
131                 {//xx == []
132                     if(TypeL != InternalType::RealDouble)
133                     {
134                         result_set(new Bool(false));
135                         return;
136                     }
137                 }
138
139                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
140                 {
141                     Double *pL                  = execMeL.result_get()->getAsDouble();
142                     Double *pR                  = execMeR.result_get()->getAsDouble();
143
144                     if(pR->size_get() == 0 && pL->size_get() == 0)
145                     {
146                         pResult = new Bool(true);
147                     }
148                     else if(pL->size_get() == 0  || pR->size_get() == 0)
149                     {
150                         pResult = new Bool(false);
151                     }
152                     else if(pR->size_get() == 1)
153                     {
154                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
155                         double dblRef   = pR->real_get(0,0);
156                         for(int i = 0 ; i < pL->rows_get() ; i++)
157                         {
158                             for(int j = 0 ; j < pL->cols_get() ; j++)
159                             {
160                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) == dblRef);
161                             }
162                         }
163                     }
164                     else if(pL->size_get() == 1)
165                     {
166                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
167                         double dblRef   = pL->real_get(0,0);
168                         for(int i = 0 ; i < pR->rows_get() ; i++)
169                         {
170                             for(int j = 0 ; j < pR->cols_get() ; j++)
171                             {
172                                 pResult->getAsBool()->bool_set(i, j, dblRef == pR->real_get(i, j));
173                             }
174                         }
175                     }
176                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
177                     {
178                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
179                         for(int i = 0 ; i < pR->rows_get() ; i++)
180                         {
181                             for(int j = 0 ; j < pR->cols_get() ; j++)
182                             {
183                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) == pR->real_get(i, j));
184                             }
185                         }
186                     }
187                     else
188                     {
189                         pResult = new Bool(false);
190                     }
191
192                     result_set(pResult);
193                 }
194                 else if(TypeL == GenericType::RealString && TypeR == GenericType::RealString)
195                 {
196                     String *pL                  = execMeL.result_get()->getAsString();
197                     String *pR                  = execMeR.result_get()->getAsString();
198
199                     if(pL->size_get() == 1)
200                     {
201                         pResult = new Bool(pR->rows_get(), pR->cols_get());
202
203                         wchar_t* pstL = pL->string_get()[0];
204                         for(int i = 0 ; i < pR->rows_get() ; i++)
205                         {
206                             for(int j = 0 ; j < pR->cols_get() ; j++)
207                             {
208                                 wchar_t* pstR = pR->string_get(i,j);
209                                 if(wcscmp(pstL, pstR) == 0)
210                                 {
211                                     pResult->getAsBool()->bool_set(i,j,true);
212                                 }
213                                 else
214                                 {
215                                     pResult->getAsBool()->bool_set(i,j,false);
216                                 }
217                             }
218                         }
219                     }
220                     else if(pR->size_get() == 1)
221                     {
222                         pResult = new Bool(pL->rows_get(), pL->cols_get());
223
224                         wchar_t* pstR = pR->string_get()[0];
225                         for(int i = 0 ; i < pL->rows_get() ; i++)
226                         {
227                             for(int j = 0 ; j < pL->cols_get() ; j++)
228                             {
229                                 wchar_t* pstL = pL->string_get(i,j);
230                                 if(wcscmp(pstL, pstR) == 0)
231                                 {
232                                     pResult->getAsBool()->bool_set(i,j,true);
233                                 }
234                                 else
235                                 {
236                                     pResult->getAsBool()->bool_set(i,j,false);
237                                 }
238                             }
239                         }
240                     }
241                     else if(pL->rows_get() == pR->rows_get() && pL->cols_get() == pR->cols_get())
242                     {
243                         pResult = new Bool(pL->rows_get(), pL->cols_get());
244
245                         for(int i = 0 ; i < pL->rows_get() ; i++)
246                         {
247                             for(int j = 0 ; j < pL->cols_get() ; j++)
248                             {
249                                 wchar_t* pstR = pR->string_get(i,j);
250                                 wchar_t* pstL = pL->string_get(i,j);
251                                 if(wcscmp(pstL, pstR) == 0)
252                                 {
253                                     pResult->getAsBool()->bool_set(i,j,true);
254                                 }
255                                 else
256                                 {
257                                     pResult->getAsBool()->bool_set(i,j,false);
258                                 }
259                             }
260                         }
261                     }
262                     else
263                     {
264                         pResult = new Bool(false);
265                     }
266                     result_set(pResult);
267                 }
268                 else if(TypeL == GenericType::RealBool && TypeR == GenericType::RealBool)
269                 {
270                     Bool *pL                    = execMeL.result_get()->getAsBool();
271                     Bool *pR                    = execMeR.result_get()->getAsBool();
272
273                     if(pL->size_get() == 1)
274                     {
275                         pResult = new Bool(pR->rows_get(), pR->cols_get());
276
277                         int iL = pL->bool_get()[0];
278                         for(int i = 0 ; i < pR->rows_get() ; i++)
279                         {
280                             for(int j = 0 ; j < pR->cols_get() ; j++)
281                             {
282                                 if(iL == pR->bool_get(i,j))
283                                 {
284                                     pResult->getAsBool()->bool_set(i,j,true);
285                                 }
286                                 else
287                                 {
288                                     pResult->getAsBool()->bool_set(i,j,false);
289                                 }
290                             }
291                         }
292                     }
293                     else if(pR->size_get() == 1)
294                     {
295                         pResult = new Bool(pL->rows_get(), pL->cols_get());
296
297                         int iR = pR->bool_get()[0];
298                         for(int i = 0 ; i < pL->rows_get() ; i++)
299                         {
300                             for(int j = 0 ; j < pL->cols_get() ; j++)
301                             {
302                                 if(iR == pL->bool_get(i,j))
303                                 {
304                                     pResult->getAsBool()->bool_set(i,j,true);
305                                 }
306                                 else
307                                 {
308                                     pResult->getAsBool()->bool_set(i,j,false);
309                                 }
310                             }
311                         }
312                     }
313                     else if(pL->rows_get() == pR->rows_get() && pL->cols_get() == pR->cols_get())
314                     {
315                         pResult = new Bool(pL->rows_get(), pL->cols_get());
316
317                         for(int i = 0 ; i < pL->rows_get() ; i++)
318                         {
319                             for(int j = 0 ; j < pL->cols_get() ; j++)
320                             {
321                                 if(pL->bool_get(i,j) == pR->bool_get(i,j))
322                                 {
323                                     pResult->getAsBool()->bool_set(i,j,true);
324                                 }
325                                 else
326                                 {
327                                     pResult->getAsBool()->bool_set(i,j,false);
328                                 }
329                             }
330                         }
331                     }
332                     else
333                     {
334                         pResult = new Bool(false);
335                     }
336                     result_set(pResult);
337                 }
338                 else
339                 {
340                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
341                 }
342                 break;
343             }
344         case OpExp::ne :
345             {
346                 if(TypeL == GenericType::RealDouble && execMeL.result_get()->getAsDouble()->size_get() == 0)
347                 {//[] <> xx
348                     if(TypeR != InternalType::RealDouble)
349                     {
350                         result_set(new Bool(true));
351                         return;
352                     }
353                 }
354
355                 if(TypeR == GenericType::RealDouble && execMeR.result_get()->getAsDouble()->size_get() == 0)
356                 {//xx <> []
357                     if(TypeL != InternalType::RealDouble)
358                     {
359                         result_set(new Bool(true));
360                         return;
361                     }
362                 }
363
364                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
365                 {
366                     Double *pL                  = execMeL.result_get()->getAsDouble();
367                     Double *pR                  = execMeR.result_get()->getAsDouble();
368
369                     if(pR->size_get() == 0 && pL->size_get() == 0)
370                     {
371                         pResult = new Bool(false);
372                     }
373                     else if(pL->size_get() == 0  || pR->size_get() == 0)
374                     {
375                         pResult = new Bool(true);
376                     }
377                     else if(pR->size_get() == 1)
378                     {
379                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
380                         double dblRef   = pR->real_get(0,0);
381                         for(int i = 0 ; i < pL->rows_get() ; i++)
382                         {
383                             for(int j = 0 ; j < pL->cols_get() ; j++)
384                             {
385                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) != dblRef);
386                             }
387                         }
388                     }
389                     else if(pL->size_get() == 1)
390                     {
391                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
392                         double dblRef   = pL->real_get(0,0);
393                         for(int i = 0 ; i < pR->rows_get() ; i++)
394                         {
395                             for(int j = 0 ; j < pR->cols_get() ; j++)
396                             {
397                                 pResult->getAsBool()->bool_set(i, j, dblRef != pR->real_get(i, j));
398                             }
399                         }
400                     }
401                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
402                     {
403                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
404                         for(int i = 0 ; i < pR->rows_get() ; i++)
405                         {
406                             for(int j = 0 ; j < pR->cols_get() ; j++)
407                             {
408                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) != pR->real_get(i, j));
409                             }
410                         }
411                     }
412                     else
413                     {
414                         pResult = new Bool(false);
415                     }
416
417                     result_set(pResult);
418                 }
419                 else if(TypeL == GenericType::RealString && TypeR == GenericType::RealString)
420                 {
421                     String *pL                  = execMeL.result_get()->getAsString();
422                     String *pR                  = execMeR.result_get()->getAsString();
423
424                     if(pL->size_get() == 1)
425                     {
426                         pResult = new Bool(pR->rows_get(), pR->cols_get());
427
428                         wchar_t* pstL = pL->string_get()[0];
429                         for(int i = 0 ; i < pR->rows_get() ; i++)
430                         {
431                             for(int j = 0 ; j < pR->cols_get() ; j++)
432                             {
433                                 wchar_t* pstR = pR->string_get(i,j);
434                                 if(wcscmp(pstL, pstR) == 0)
435                                 {
436                                     pResult->getAsBool()->bool_set(i,j,false);
437                                 }
438                                 else
439                                 {
440                                     pResult->getAsBool()->bool_set(i,j,true);
441                                 }
442                             }
443                         }
444                     }
445                     else if(pR->size_get() == 1)
446                     {
447                         pResult = new Bool(pL->rows_get(), pL->cols_get());
448
449                         wchar_t* pstR = pR->string_get()[0];
450                         for(int i = 0 ; i < pL->rows_get() ; i++)
451                         {
452                             for(int j = 0 ; j < pL->cols_get() ; j++)
453                             {
454                                 wchar_t* pstL = pL->string_get(i,j);
455                                 if(wcscmp(pstL, pstR) == 0)
456                                 {
457                                     pResult->getAsBool()->bool_set(i,j,false);
458                                 }
459                                 else
460                                 {
461                                     pResult->getAsBool()->bool_set(i,j,true);
462                                 }
463                             }
464                         }
465                     }
466                     else if(pL->rows_get() == pR->rows_get() && pL->cols_get() == pR->cols_get())
467                     {
468                         pResult = new Bool(pL->rows_get(), pL->cols_get());
469
470                         for(int i = 0 ; i < pL->rows_get() ; i++)
471                         {
472                             for(int j = 0 ; j < pL->cols_get() ; j++)
473                             {
474                                 wchar_t* pstR = pR->string_get(i,j);
475                                 wchar_t* pstL = pL->string_get(i,j);
476                                 if(wcscmp(pstL, pstR) == 0)
477                                 {
478                                     pResult->getAsBool()->bool_set(i,j,false);
479                                 }
480                                 else
481                                 {
482                                     pResult->getAsBool()->bool_set(i,j,true);
483                                 }
484                             }
485                         }
486                     }
487                     else
488                     {
489                         pResult = new Bool(true);
490                     }
491                     result_set(pResult);
492                 }
493                 else
494                 {
495                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
496                 }
497                 break;
498             }
499         case OpExp::lt :
500             {
501                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
502                 {
503                     Double *pL                  = execMeL.result_get()->getAsDouble();
504                     Double *pR                  = execMeR.result_get()->getAsDouble();
505
506                     if(pR->size_get() == 1)
507                     {
508                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
509                         double dblRef   = pR->real_get(0,0);
510                         for(int i = 0 ; i < pL->rows_get() ; i++)
511                         {
512                             for(int j = 0 ; j < pL->cols_get() ; j++)
513                             {
514                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) < dblRef);
515                             }
516                         }
517                     }
518                     else if(pL->size_get() == 1)
519                     {
520                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
521                         double dblRef   = pL->real_get(0,0);
522                         for(int i = 0 ; i < pR->rows_get() ; i++)
523                         {
524                             for(int j = 0 ; j < pR->cols_get() ; j++)
525                             {
526                                 pResult->getAsBool()->bool_set(i, j, dblRef < pR->real_get(i, j));
527                             }
528                         }
529                     }
530                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
531                     {
532                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
533                         for(int i = 0 ; i < pR->rows_get() ; i++)
534                         {
535                             for(int j = 0 ; j < pR->cols_get() ; j++)
536                             {
537                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) < pR->real_get(i, j));
538                             }
539                         }
540                     }
541                     else
542                     {
543                         pResult = new Bool(false);
544                     }
545
546                     result_set(pResult);
547                 }
548                 else
549                 {
550                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
551                 }
552                 break;
553             }
554         case OpExp::le :
555             {
556                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
557                 {
558                     Double *pL                  = execMeL.result_get()->getAsDouble();
559                     Double *pR                  = execMeR.result_get()->getAsDouble();
560
561                     if(pR->size_get() == 1)
562                     {
563                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
564                         double dblRef   = pR->real_get(0,0);
565                         for(int i = 0 ; i < pL->rows_get() ; i++)
566                         {
567                             for(int j = 0 ; j < pL->cols_get() ; j++)
568                             {
569                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) <= dblRef);
570                             }
571                         }
572                     }
573                     else if(pL->size_get() == 1)
574                     {
575                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
576                         double dblRef   = pL->real_get(0,0);
577                         for(int i = 0 ; i < pR->rows_get() ; i++)
578                         {
579                             for(int j = 0 ; j < pR->cols_get() ; j++)
580                             {
581                                 pResult->getAsBool()->bool_set(i, j, dblRef <= pR->real_get(i, j));
582                             }
583                         }
584                     }
585                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
586                     {
587                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
588                         for(int i = 0 ; i < pR->rows_get() ; i++)
589                         {
590                             for(int j = 0 ; j < pR->cols_get() ; j++)
591                             {
592                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) <= pR->real_get(i, j));
593                             }
594                         }
595                     }
596                     else
597                     {
598                         pResult = new Bool(false);
599                     }
600
601                     result_set(pResult);
602                 }
603                 else
604                 {
605                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
606                 }
607                 break;
608             }
609         case OpExp::gt :
610             {
611                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
612                 {
613                     Double *pL                  = execMeL.result_get()->getAsDouble();
614                     Double *pR                  = execMeR.result_get()->getAsDouble();
615
616                     if(pR->size_get() == 1)
617                     {
618                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
619                         double dblRef   = pR->real_get(0,0);
620                         for(int i = 0 ; i < pL->rows_get() ; i++)
621                         {
622                             for(int j = 0 ; j < pL->cols_get() ; j++)
623                             {
624                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) > dblRef);
625                             }
626                         }
627                     }
628                     else if(pL->size_get() == 1)
629                     {
630                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
631                         double dblRef   = pL->real_get(0,0);
632                         for(int i = 0 ; i < pR->rows_get() ; i++)
633                         {
634                             for(int j = 0 ; j < pR->cols_get() ; j++)
635                             {
636                                 pResult->getAsBool()->bool_set(i, j, dblRef > pR->real_get(i, j));
637                             }
638                         }
639                     }
640                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
641                     {
642                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
643
644                         for(int i = 0 ; i < pR->rows_get() ; i++)
645                         {
646                             for(int j = 0 ; j < pR->cols_get() ; j++)
647                             {
648                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) > pR->real_get(i, j));
649                             }
650                         }
651                     }
652                     else
653                     {
654                         pResult = new Bool(false);
655                     }
656
657                     result_set(pResult);
658                 }
659                 else
660                 {
661                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
662                 }
663                 break;
664             }
665         case OpExp::ge :
666             {
667                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
668                 {
669                     Double *pL                  = execMeL.result_get()->getAsDouble();
670                     Double *pR                  = execMeR.result_get()->getAsDouble();
671
672                     if(pR->size_get() == 1)
673                     {
674                         pResult                         = new Bool(pL->rows_get(), pL->cols_get());
675                         double dblRef   = pR->real_get(0,0);
676                         for(int i = 0 ; i < pL->rows_get() ; i++)
677                         {
678                             for(int j = 0 ; j < pL->cols_get() ; j++)
679                             {
680                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) >= dblRef);
681                             }
682                         }
683                     }
684                     else if(pL->size_get() == 1)
685                     {
686                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
687                         double dblRef   = pL->real_get(0,0);
688                         for(int i = 0 ; i < pR->rows_get() ; i++)
689                         {
690                             for(int j = 0 ; j < pR->cols_get() ; j++)
691                             {
692                                 pResult->getAsBool()->bool_set(i, j, dblRef >= pR->real_get(i, j));
693                             }
694                         }
695                     }
696                     else if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
697                     {
698                         pResult                         = new Bool(pR->rows_get(), pR->cols_get());
699                         for(int i = 0 ; i < pR->rows_get() ; i++)
700                         {
701                             for(int j = 0 ; j < pR->cols_get() ; j++)
702                             {
703                                 pResult->getAsBool()->bool_set(i, j, pL->real_get(i, j) >= pR->real_get(i, j));
704                             }
705                         }
706                     }
707                     else
708                     {
709                         pResult = new Bool(false);
710                     }
711
712                     result_set(pResult);
713                 }
714                 else
715                 {
716                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
717                 }
718                 break;
719             }
720         case OpExp::power :
721             {
722                 if(TypeL == GenericType::RealDouble && TypeR == GenericType::RealDouble)
723                 {
724                     Double *pL                  = execMeL.result_get()->getAsDouble();
725                     Double *pR                  = execMeR.result_get()->getAsDouble();
726
727                     int iResult = PowerDoubleByDouble(pL, pR, (Double**)&pResult);
728                     if(iResult != 0)
729                     {
730                         std::wostringstream os;
731                         os << _W("Inconsistent row/column dimensions.\n");
732                         //os << ((Location)e.right_get().location_get()).location_string_get() << std::endl;
733                         throw ScilabError(os.str(), 999, e.right_get().location_get());
734                     }
735                     result_set(pResult);
736                 }
737                 else if(TypeL == GenericType::RealPoly && TypeR == GenericType::RealDouble)
738                 {
739                     MatrixPoly *pL      = execMeL.result_get()->getAsPoly();
740                     Double *pR                  = execMeR.result_get()->getAsDouble();
741
742                     int iResult = PowerPolyByDouble(pL, pR, (MatrixPoly**)&pResult);
743                     if(iResult != 0)
744                     {
745                         std::wostringstream os;
746                         os << _W("Inconsistent row/column dimensions.\n");
747                         //os << ((Location)e.right_get().location_get()).location_string_get() << std::endl;
748                         throw ScilabError(os.str(), 999, e.right_get().location_get());
749                     }
750                     result_set(pResult);
751                 }
752                 else
753                 {
754                     result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
755                 }
756                 break;
757             }
758         default :
759             // By default call overload if we do not know this operator ...
760             result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
761             break;
762         }
763     }
764     catch(ScilabError error)
765     {
766         error.SetErrorLocation(e.location_get());
767         throw error;
768     }
769 }
770
771 void visitprivate(const LogicalOpExp &e)
772 {
773     T execMeL;
774     T execMeR;
775
776     e.left_get().accept(execMeL);
777     GenericType::RealType TypeL = execMeL.result_get()->getType();
778     Bool *pL    = NULL;
779
780     if(TypeL == GenericType::RealBool)
781     {
782         pL      = execMeL.result_get()->getAsBool();
783     }
784     else
785     {
786         //TODO YaSp : Overloading %*_oper_*
787         e.right_get().accept(execMeR);
788         switch(e.oper_get())
789         {
790         case LogicalOpExp::logicalShortCutOr :
791         case LogicalOpExp::logicalOr :
792             result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
793             break;
794         case LogicalOpExp::logicalShortCutAnd :
795         case LogicalOpExp::logicalAnd :
796             result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
797             break;
798         }
799         return;
800     }
801
802     InternalType *pResult = NULL;
803     switch(e.oper_get())
804     {
805     case LogicalOpExp::logicalShortCutOr :
806         {
807             Bool *pL    = execMeL.result_get()->getAsBool();
808             int *piL    = pL->bool_get();
809             bool iL     = true;
810             // Check if all values are true
811             // true || <something is always true>
812             for(int i = 0 ; i < pL->size_get() ; i++)
813             {
814                 if(piL[i] == false)
815                 {
816                     iL = false;
817                     break;
818                 }
819             }
820
821             if(iL)
822             {//we don't need to look at ohers exp
823                 pResult = new Bool(true);
824                 result_set(pResult);
825                 return;
826             }
827             // DO NOT break here, falling into normal Or if this can not be shotcutted.
828         }
829     case LogicalOpExp::logicalOr :
830         {
831             e.right_get().accept(execMeR);
832             GenericType::RealType TypeR = execMeR.result_get()->getType();
833
834             if(TypeR == GenericType::RealBool)
835             {
836                 Bool *pR        = execMeR.result_get()->getAsBool();
837                 int* piR = pR->bool_get();
838                 int* piL = pL->bool_get();
839                 int* piB        = NULL;
840
841                 // M | scalar
842                 if(pR->size_get() == 1)
843                 {
844                     pResult = new Bool(pL->rows_get(), pL->cols_get(), &piB);
845                     for(int i = 0 ; i < pL->size_get(); i++)
846                     {
847                         piB[i] = (piR[0] == 1) || (piL[i] == 1);
848                     }
849                     result_set(pResult);
850                     return;
851                 }
852
853                 // scalar | M
854                 if(pL->size_get() == 1)
855                 {
856                     pResult = new Bool(pR->rows_get(), pR->cols_get(), &piB);
857                     for(int i = 0 ; i < pR->size_get(); i++)
858                     {
859                         piB[i] = (piR[i] == 1) || (piL[0] == 1);
860                     }
861                     result_set(pResult);
862                     return;
863                 }
864
865                 // M | N (generic case)
866                 if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
867                 {
868                     int* piB    = NULL;
869                     int* piR    = pR->bool_get();
870                     int* piL    = pL->bool_get();
871
872                     pResult = new Bool(pR->rows_get(), pR->cols_get(), &piB);
873                     for(int i = 0 ; i < pL->size_get(); i++)
874                     {
875                         piB[i] = (piR[i] == 1) || (piL[i] == 1);
876                     }
877                     result_set(pResult);
878                 }
879                 else
880                 {
881                     std::wostringstream os;
882                     os << _W("Inconsistent row/column dimensions.\n");
883                     //os << ((Location)e.right_get().location_get()).location_string_get() << std::endl;
884                     throw ScilabError(os.str(), 999, e.right_get().location_get());
885                 }
886             }
887             else
888             {
889                 result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
890             }
891             break;
892         }
893     case LogicalOpExp::logicalShortCutAnd :
894         {
895             Bool *pL    = execMeL.result_get()->getAsBool();
896             int *piL    = pL->bool_get();
897             // Check if one value is false
898             // false && <something> is always false
899             for(int i = 0 ; i < pL->size_get() ; i++)
900             {
901                 if(piL[i] == false)
902                 {
903                     pResult = new Bool(false);
904                     result_set(pResult);
905                     return;
906                 }
907             }
908             // DO NOT break here, falling into normal And if this can not be shotcutted.
909         }
910     case LogicalOpExp::logicalAnd :
911         {
912             e.right_get().accept(execMeR);
913             GenericType::RealType TypeR = execMeR.result_get()->getType();
914
915             if(TypeR == GenericType::RealBool)
916             {
917                 Bool *pR        = execMeR.result_get()->getAsBool();
918                 int* piR = pR->bool_get();
919                 int* piL = pL->bool_get();
920                 int* piB        = NULL;
921
922                 // M & scalar
923                 if(pR->size_get() == 1)
924                 {
925                     pResult = new Bool(pL->rows_get(), pL->cols_get(), &piB);
926                     for(int i = 0 ; i < pL->size_get(); i++)
927                     {
928                         piB[i] = (piR[0] == 1) && (piL[i] == 1);
929                     }
930                     result_set(pResult);
931                     return;
932                 }
933
934                 // scalar & M
935                 if(pL->size_get() == 1)
936                 {
937                     pResult = new Bool(pR->rows_get(), pR->cols_get(), &piB);
938                     for(int i = 0 ; i < pR->size_get(); i++)
939                     {
940                         piB[i] = (piR[i] == 1) && (piL[0] == 1);
941                     }
942                     result_set(pResult);
943                     return;
944                 }
945
946                 // M & N (generic case)
947                 if(pR->rows_get() == pL->rows_get() && pR->cols_get() == pL->cols_get())
948                 {
949                     int* piB    = NULL;
950                     int* piR    = pR->bool_get();
951                     int* piL    = pL->bool_get();
952
953                     pResult = new Bool(pR->rows_get(), pR->cols_get(), &piB);
954                     for(int i = 0 ; i < pL->size_get(); i++)
955                     {
956                         piB[i] = (piR[i] == 1) && (piL[i] == 1);
957                     }
958                     result_set(pResult);
959                 }
960                 else
961                 {
962                     std::wostringstream os;
963                     os << _W("Inconsistent row/column dimensions.\n");
964                     //os << ((Location)e.right_get().location_get()).location_string_get() << std::endl;
965                     throw ScilabError(os.str(), 999, e.right_get().location_get());
966                 }
967             }
968             else
969             {
970                 result_set(callOverload(e.oper_get(), &execMeL, &execMeR));
971             }
972             break;
973         }
974     }
975 }
976
977 InternalType* callOverload(OpExp::Oper _oper, T *_paramL, T *_paramR)
978 {
979     T execMe;
980     types::typed_list in;
981     types::typed_list out;
982     _paramL->result_get()->IncreaseRef();
983     _paramR->result_get()->IncreaseRef();
984     in.push_back(_paramL->result_get());
985     in.push_back(_paramR->result_get());
986
987     Overload::generateNameAndCall(Overload::getNameFromOper(_oper), in, 1, out, &execMe);
988     if(out[0] != NULL)
989     {
990         out[0]->IncreaseRef();
991     }
992     _paramL->result_get()->DecreaseRef();
993     _paramR->result_get()->DecreaseRef();
994     return out[0];
995 }