forked from FreeFem/FreeFem-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFunction.hpp
3388 lines (2829 loc) · 120 KB
/
AFunction.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
347
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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
572
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
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
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
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// \file
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : [email protected]
//
/*
This file is part of Freefem++
Freefem++ is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Freefem++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//file afonction.h
#ifndef __AFONCTION__
#define __AFONCTION__
#include "showverb.hpp"
#include "InitFunct.hpp"
#include <typeinfo>
#include <cstddef>
#include <iostream>
#include <fstream>
#include <cstring>
#include "error.hpp"
#include <map>
#include <deque>
#include <list>
#include <vector>
#include <queue>
#include <complex>
#include <string>
#include <cstdlib>
#include <algorithm>
extern bool showCPU;
#include "RNM.hpp"
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
// #include <time.h>
#include "CodeAlloc.hpp"
inline double CPUtime(){
#ifdef SYSTIMES
struct tms buf;
if (times(&buf)!=-1)
return ((double)buf.tms_utime+(double)buf.tms_stime)/(long) sysconf(_SC_CLK_TCK);
else
#endif
return ((double) clock())/CLOCKS_PER_SEC;
}
extern long verbosity; // level off printing
extern long searchMethod; //pichon
extern bool withrgraphique;
using namespace std;
#include "ffstack.hpp"
#include "AnyType.hpp"
#include "String.hpp"
class basicForEachType;
class E_F1_funcT_Type;
class E_F0; // une instruction exec time
class C_F0; // une instruction complie time
class ListOfInst;
class Polymorphic;
class OneOperator;
/// <<Expression>> is used as the type of the local list contained in ListOfInst
typedef E_F0 * Expression; // [[E_F0]]
class AC_F0;
class basicAC_F0;
typedef complex<double> Complex;
/// <<Type_Expr>> [[file:AnyType.hpp::aType]] [[E_F0]]
typedef pair<aType, E_F0 *> Type_Expr ;// to store the type and the expression 29042005 FH
int FindType(const char * name) ;
void lgerror (const char* s) ;
void CompileError(string msg="",aType r=0);
void ExecError(string msg="");
struct UnId {
const char * id;
aType r;
Expression e;
deque<UnId> * array; // to store a array
aType re;
bool ref; // a ref or non
UnId() :id(0),r(0),e(0),array(0),re(0),ref(false) {}
UnId(const char * idd) :id(idd),r(0),e(0),array(0),re(0),ref(false) {}
UnId(const char * idd,const C_F0 & ee,aType rr,bool reff) ;
UnId(deque<UnId> * d) : id(0),r(0),e(0),array(d?new deque<UnId>(*d):0),re(0),ref(false) {}
UnId(const UnId & u) :
id(u.id),r(u.r),e(u.e),
array(u.array?new deque<UnId>(*u.array):0),
re(u.re),ref(u.ref) {}
// Modif 24032005
void operator= (const UnId & u) {
id=u.id;
r=u.r;
e=u.e;
re=u.re;
ref=u.ref;
if(array) delete array;
array=0;
if(u.array) array= new deque<UnId>(*u.array);
}
~UnId(){ if( array) delete array;} // Modif 24032005
};
/// <<ListOfId>>
typedef deque<UnId> ListOfId;
// xxx is a type so xxx can't be a parameter
#define ATYPE(xxx) map_type[typeid(xxx).name()]
/* #define NEW_TYPE(type) map_type[typeid(type).name()] = new ForEachType<type >(0,0)
//#define NEW_TYPE(type) map_type[typeid(type).name()] = new ForEachType<type >()
#define NEW_TYPE_I(type,i,d) map_type[typeid(type).name()] = new ForEachType<type>(i,d)
#define NEW_TYPE_Ptr(type) map_type[typeid(type*).name()] = new ForEachTypePtr<type >()
#define NEW_TYPE_PtrND(type) map_type[typeid(type*).name()] = new ForEachTypePtr<type >(0)
#define NEW_TYPE_PtrNIND(type) map_type[typeid(type*).name()] = new ForEachTypePtr<type >(0,0)
//#define NEW_TYPE_PtrI(type) map_type[typeid(type*).name()] = new ForEachTypePtr<type*>(Initialize<type>)
*/
/// Doxygen doc
extern Polymorphic * TheOperators, * TheRightOperators;
// -------------
extern C_F0 *pOne,*pZero,*pminusOne;
typedef AnyType (* Function1)(Stack, const AnyType &);
typedef AnyType (* Function2)(Stack, const AnyType &,const AnyType &);
typedef AnyType (* CFunction2)(Stack, E_F0 *, E_F0 *);
typedef AnyType (* CFunction4)(Stack, E_F0 *, E_F0 *, E_F0 *, E_F0 *);
Expression NewExpression(Function1,Expression);
Expression NewExpression(Function2,Expression,Expression);
inline Type_Expr make_Type_Expr(aType t, E_F0 * e) {return make_pair(t,e);}
inline Type_Expr make_Type_Expr( E_F0 * e,aType t) {return make_pair(t,e);}
struct Keyless : binary_function<const char *,const char *, bool>
{
typedef const char * Key;
bool operator()(const Key& x, const Key& y) const { return strcmp(x,y)<0;} };
// <<TableOfIdentifier>>
class vectorOfInst;
class TableOfIdentifier: public CodeAlloc {
public:
struct Value;
typedef const char * Key;
typedef map<Key,Value,Keyless> maptype;
typedef pair<const Key,Value> pKV;
typedef maptype::iterator iterator;
typedef maptype::const_iterator const_iterator;
struct Value :public Type_Expr {
pKV * next; // link all the variable in reverse order to call delete on each variable
bool del;
Value(const Type_Expr & vv,pKV * n,bool dd=true) : Type_Expr(vv),next(n),del(dd) {}
Value(aType t,E_F0 *f,pKV *n,bool dd=true): Type_Expr(t,f),next(n),del(dd) {}
};// to store the type and the expression
pKV * listofvar;
// struct Keyless : binary_function<Key,Key, bool>
// { bool operator()(const Key& x, const Key& y) const{ return strcmp(x,y)<0;} };
maptype m;
int nIdWithDelete;
C_F0 Find(Key) const ;
C_F0 Find(Key,const basicAC_F0 &) const ;
const Type_Expr & New(Key k,const Type_Expr & v,bool del=true);
void Add(Key k,Key op,OneOperator *p0,OneOperator *p1=0,
OneOperator *p2=0,OneOperator *p3=0,OneOperator *p4=0,
OneOperator *p5=0,OneOperator *p6=0) ;
void clear();
template<class T>
C_F0 NewVar(Key k,aType t,size_t & top,const C_F0 &i) ;
template<class T>
C_F0 NewVar(Key k,aType t,size_t & top,const basicAC_F0 &args) ;
template<class T,class U>
C_F0 NewVar(Key k,aType t,size_t & top,const basicAC_F0 &args,const U & data) ;
// C_F0 NewVar(Key k,aType t,size_t & top,const basicAC_F0 &args,const C_F0& f) ;
template<class T>
C_F0 NewVar(Key k,aType t,size_t & top) ;
C_F0 NewID(aType t,Key k, C_F0 & c,size_t & top,bool del=true);
C_F0 NewID(aType t,Key k, C_F0 & c,const ListOfId & l,size_t & top,bool del=true);
template<class T>
C_F0 NewFESpace(Key k,aType t,size_t & top,const basicAC_F0 &args);
friend ostream & operator<<(ostream & f,const TableOfIdentifier & );
vectorOfInst* newdestroy();
C_F0 destroy();
TableOfIdentifier() ; //: listofvar(0) {};
~TableOfIdentifier(); //
};
// <<basicForEachType>> for all the type of the language
class basicForEachType : public CodeAlloc {
const type_info * ktype; // the real type_info
// const type_info *ktypefunc;// the type of code
public:
static basicForEachType * tnull;
const size_t size;
typedef OneOperator * CastFunc;
typedef map<aType,CastFunc>::const_iterator const_cast_iterator;
typedef const char * Key;
// virtual void print(ostream &f,const void *p) const =0;
friend ostream & operator<<(ostream & f,const basicForEachType & e)
{ f << '<' << e.name() << '>' ;return f;}
void Show(ostream & f) const ;
const char * name() const { return this!=tnull ? ktype->name() :"NULL" ;}
virtual bool CastingFrom(const basicForEachType * t) const ;
// modif FH ----- A TESTER //
virtual bool SametypeRight(const basicForEachType * t) const {return (this == t) || (t == un_ptr_type) || (t == type_C_F0);}
// virtual Type_Expr init(const Type_Expr & te) const { return Type_Expr(0,0);}
virtual int TYPEOFID() const {return 0;}
// bool SametypeLeft(const basicForEachType * t) const {return t == this;}
// bool To(const basicForEachType * t) const { throwassert(t && this);return un_ptr_type == this ? t->un_ptr_type == this : t == this;}
virtual C_F0 CastTo(const C_F0 & e) const ;
virtual void SetArgs(const ListOfId *lid) const ;// { cout << "SetArgs::\n " ;throwassert(lid==0 || lid->size()==0);}
aType right() const {return un_ptr_type;};
Expression RightValueExpr(Expression f) const;
// Type_Expr NewVar(Key k,aType t,size_t & top,const C_F0 &i);
virtual C_F0 Initialization(const Type_Expr & e) const ;
virtual Expression Destroy(const C_F0 &) const ;
virtual bool ExistDestroy() const {return destroy;}
virtual Type_Expr SetParam(const C_F0 & c,const ListOfId * l,size_t & top) const;
virtual Expression OnReturn(Expression f) const;
// { return make_pair<aType,const E_F0 *>(this,c.left());}
protected:
basicForEachType(const type_info & k ,const size_t ,
const E_F1_funcT_Type * p=0,basicForEachType *rr=0,
Function1 iv=0,Function1 id=0, Function1 dreturn=0) ;
/* inline basicForEachType(const type_info & k ,const type_info & kf ,const size_t ,
const E_F1_funcT_Type * p=0,basicForEachType *rr=0,
Function1 iv=0,Function1 id=0) ;*/
public:
static const basicForEachType * type_C_F0; // for any type un formal operation .... FH add 09/2012
const basicForEachType * un_ptr_type; // type of right exp
private:
// map<aType,CastFunc> mapofcast;
OneOperator * casting; // <<casting>> list of operator for casting to this type
const E_F1_funcT_Type * un_ptr; // is ptr -> get value function
Function1 DoOnReturn; // to call some thing on return.
Function1 InitExp; // to init the ptr value
Function1 destroy;// the destroy function
TableOfIdentifier ti; // all polymorphisme of the Identifier
public:
// basicForEachType * FunctionType() const;// { return funct_type ? funct_type : (funct_type= new FuncForEachType(this));}
C_F0 Find(const char * k) const; // {return ti->Find(k);}
C_F0 Find(const char * k,const basicAC_F0 & args) const; // {return ti->Find(k);}
void New(Key k,Type_Expr v,bool del=true){ti.New(k,v,del);}
void Add(Key k,Key op,OneOperator *p0,OneOperator *p1=0,
OneOperator *p2=0,OneOperator *p3=0,OneOperator *p4=0,
OneOperator *p5=0,OneOperator *p6=0)
{ti.Add(k,op,p0,p1,p2,p3,p4,p5,p6);}
void AddCast(CastFunc f1,CastFunc f2=0,CastFunc f3=0,CastFunc f4=0,
CastFunc f5=0,CastFunc f6=0,CastFunc f7=0,CastFunc f8=0);
ostream & ShowTable(ostream & f) const { f << ti; return f;}
// basicForEachType * funct_type;
virtual ~basicForEachType();
// Add FH: for implicite loop FH. Jan 2016
// type for i, type for j, type valeur
basicForEachType *typei,*typej,*typev;
void SetTypeLoop(basicForEachType *v,basicForEachType *i=0,basicForEachType *j=0)
{ typev=v; typei=i;typej=j;}
};
template<typename T>
inline basicForEachType * atype() {
map<const string,basicForEachType *>::iterator ir=map_type.find(typeid(T).name());
// basicForEachType * r=map_type[];
if (ir == map_type.end()) { cerr << "Error: aType '" << typeid(T).name() << "', doesn't exist\n";
ShowType(cerr);
throw(ErrorExec("exit",1));}
return ir->second;}
template<typename T>
inline basicForEachType * atype0() {
map<const string,basicForEachType *>::iterator ir=map_type.find(typeid(T).name());
if (ir == map_type.end()) return 0;
return ir->second;}
// --------
//typedef basicForEachType TheType;
// const basicForEachType * ktype; // compilation time
// class for all exp
// a left exp is a pointer expression
// -------
// -- exec times le code is just E_F0*(fonction without args)
class C_LF2;
class C_LF1;
// 3 types of function/expression 0,1,2 args
/// <<E_F0>> is the base class for all expressions built by parsing an EDP script in the grammar of the FreeFem++
/// language (see [[file:../lglib/lg.ypp]]). E_F0 pointers are typed as [[Expression]], stored as a list in
/// [[ListOfInst]], and evaluated when CListOfInst::eval() [[file:AFunction.hpp::CListOfInst::eval]] is called at
/// [[file:../lglib/lg.ypp::evaluate_parsed_FF_script]] (see \ref index). No internal data member.
class E_F0 :public CodeAlloc
{
public:
static E_F0 *tnull;
struct kless : binary_function<Expression,Expression, bool>
{ bool operator()(const Expression& x, const Expression& y) const{
//cout << x << " " << y << x->compare(y) << " ::: ";
int r1 = x->compare(y);// , r2 = y->compare(x);
//assert(r1+r2==0);
return r1<0;} };
typedef map< E_F0 *,int,kless> MapOfE_F0;
virtual AnyType operator()(Stack) const =0;
virtual bool Empty() const {return this==tnull; }
// virtual E_F0 * destroy(Stack ) const {return 0;}
// virtual const E_F0 * Parameter(Stack ) const {return this;}
virtual size_t nbitem() const {return 1;}
virtual bool EvaluableWithOutStack() const {return false;} //
virtual bool MeshIndependent() const {return true;} //
virtual bool Zero() const {return false;} //
virtual E_F0 * right_E_F0() const { return 0;}
virtual bool ReadOnly() const { return true;} // the expression do not change the memory
virtual ~E_F0() {}
virtual int compare (const E_F0 *t) const { int r= (t==this) ? 0 : ( ( this<t) ?-1 : 1);
//cout << "cmp " << typeid(*this).name() << r << endl;
return r;} // to give a order in instuction
virtual int Optimize(deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n) ; // build optimisation
virtual AnyType operator()(Stack stack,AnyType *) const { return operator()(stack);} // call optim code
virtual operator aType () const { assert(0);return 0;} // the type of the expression
virtual ostream & dump(ostream &f) const { f << ' ' << typeid(*this).name() << ' ' << this << ' ' ;return f; }
// for OPTIMIZATION
int find(const MapOfE_F0 & m) ;
int insert(Expression opt,deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n) ;
// ajoute for optimisation to say if a expression in meshindep a exec time
// to solve 0*x // question
// juin 2007 FH
virtual AnyType eval(Stack stack, bool & meshindep ) const
{ meshindep=MeshIndependent();return operator()(stack);}
};
inline ostream & operator<<(ostream & f,const E_F0 &e) { if(!e.Empty()) e.dump(f); else f << " --0-- " ;return f;}
/// <<E_F0mps>> Specialization of [[E_F0]] where MeshIndependent() always returns false instead of true.
class E_F0mps : public E_F0 { public:
virtual bool MeshIndependent() const {return false;} //
};
class E_F0info : public E_F0 { public:
// not a real expression just to pass information
virtual bool EvaluableWithOutStack() const {return true;} //
virtual bool MeshIndependent() const {return true;} //
virtual AnyType operator()(Stack ) const {
return SetAny<const E_F0 *>(this);}
operator aType () const { return atype<Expression>();}
};
class E_F1 : public CodeAlloc{ public: virtual AnyType operator()(Stack,AnyType &) const =0;};
class E_F2 : public CodeAlloc{ public: virtual AnyType operator()(Stack,AnyType &,AnyType &) const =0;};
class E_FN : public CodeAlloc{ public: virtual AnyType operator()(Stack,size_t N,...) const =0;};
// class to play with polymorphisme
// ---------------------------------
class basicAC_F0;
class ArrayOfaType : public CodeAlloc{
// class for the type of parameter
aType tt[11];
protected:
int n;
aType * t; // array of type
bool ellipse;
void operator=(const ArrayOfaType &); // no set operator
public:
// ArrayOfaType() :n(0),t(0),ellipse(false) {}
explicit ArrayOfaType(bool ell=false)
:n(0),t(0),ellipse(ell) {}
explicit ArrayOfaType(const aType & a,bool ell=false)
:n(1),t(tt),ellipse(ell) {t[0]=a;}
explicit ArrayOfaType(const aType & a,const aType & b,bool ell=false)
:n(2),t(tt),ellipse(ell) {t[0]=a,t[1]=b;}
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,bool ell=false)
:n(3),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;}
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,bool ell=false)
:n(4),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d;
/* cout << * a << *b << * c << * d << " ---------" << endl; */}
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,bool ell=false)
:n(5),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; }
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,const aType & f,bool ell=false)
:n(6),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; }
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,
const aType & f,const aType & g,
bool ell=false)
:n(7),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; t[6]=g; } // (6 args) Added by Fabian Dortu
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,
const aType & f,const aType & g,const aType & h,
bool ell=false)
:n(8),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; t[6]=g; t[7]=h; } // (7 args) Added by Fabian Dortu
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,
const aType & f,const aType & g,const aType & h, const aType & i,
bool ell=false)
:n(9),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; t[6]=g; t[7]=h; t[8]=i; } // (8 args) Added by Fabian Dortu
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d, const aType & e,
const aType & f,const aType & g,const aType & h, const aType & i, const aType & j,
bool ell=false)
:n(10),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; t[6]=g; t[7]=h; t[8]=i; t[9]=j; } // (10 args) Added by Fabian Dortu
explicit ArrayOfaType(const aType & a,const aType & b,const aType & c,const aType & d,const aType & e,const aType & f,const aType & g,const aType & h, const aType & i, const aType & j, const aType & k,bool ell=false)
:n(11),t(tt),ellipse(ell) {t[0]=a,t[1]=b;t[2]=c;t[3]=d; t[4]=e; t[5]=f; t[6]=g; t[7]=h; t[8]=i; t[9]=j; t[10]=k; } // (10 args) Added by Fabian Dortu
ArrayOfaType(const basicAC_F0 & ) ;
ArrayOfaType(const ArrayOfaType & ); //
ArrayOfaType(const ListOfId * l);
~ArrayOfaType() { if(t && t != tt) delete [] t;t=0;n=0;}
bool WithOutCast( const ArrayOfaType & a) const ;
bool WithCast( const ArrayOfaType & a,int nbcast=100000) const ; // return the number of cast
// exactly comparaison
bool operator==( const ArrayOfaType & a) const {
if (a.n != n || a.ellipse !=ellipse) return false;
for (int i=0;i<n;i++)
if (t[i] != a.t[i])
return false;
return true;}
friend ostream & operator<<(ostream & f,const ArrayOfaType & a);
};
/// <<OneOperator>> Base class for all language operators. Daughter classes have the same name with several extensions:
/// "[1-9]" represent the number of operator arguments, "_" designates operators that take a reference instead of a
/// copied argument, "s" designates operators that require a stack argument.
class OneOperator : public ArrayOfaType {
friend class MakeVectSpaceN;
friend class basicForEachType;
const basicForEachType * r; // return type
OneOperator *next; // to make a list of OneOperator
public:
int pref; // to try to solve ambiguity for binary operator
// 10 for bool, 20 for int , 30 for long , 40, for float, 50 double, 60 for complex, 70 string
// string+ 1 => string
// 1+string => string
OneOperator(aType rr) ;// : r(rr),ArrayOfaType(),next(0),pref(0) {throwassert(r);}
OneOperator(aType rr,aType a) ;//: r(rr),ArrayOfaType(a,false),next(0),pref(0) {throwassert(rr && a );}
OneOperator(aType rr,aType a,aType b);// : r(rr),ArrayOfaType(a,b,false),next(0),pref(0) {
// throwassert(rr && a && b);}
OneOperator(aType rr,aType a,aType b,aType c) ;
//: r(rr),ArrayOfaType(a,b,c,false),next(0),pref(0) {throwassert(rr && a && b && c);}
OneOperator(aType rr,aType a,aType b,aType c,aType d) ;
//: r(rr),ArrayOfaType(a,b,c,d,false),next(0),pref(0) {throwassert(rr && a && b && c);}
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e) ;
//: r(rr),ArrayOfaType(a,b,c,d,e,false),next(0),pref(0) {throwassert(rr && a && b && c && d);} // Added by Fabian Dortu (5 parameters)
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f) ;
//: r(rr),ArrayOfaType(a,b,c,d,e,f,false),next(0),pref(0) {throwassert(rr && a && b && c && d && e && f);} // Added by Fabian Dortu (6 parameters)
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g);
// : r(rr),ArrayOfaType(a,b,c,d,e,f,g,false),next(0),pref(0) {throwassert(rr && a && b && c && d && e && f && g);} // Added by Fabian Dortu (7 parameters)
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h);
// : r(rr),ArrayOfaType(a,b,c,d,e,f,g,h,false),next(0),pref(0) {throwassert(rr && a && b && c && d && e && f && g && h);} // Added by Fabian Dortu (8 parameters)
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h, aType i) ;
//: r(rr),ArrayOfaType(a,b,c,d,e,f,g,h,i,false),next(0),pref(0) {throwassert(rr && a && b && c && d && e && f && g && h && i);} // Added by Fabian Dortu (9 parameters)
OneOperator(aType rr,aType a,aType b,aType c,aType d,aType e,aType f, aType g, aType h, aType i, aType j);
// : r(rr),ArrayOfaType(a,b,c,d,e,f,g,h,i,j,false),next(0),pref(0) {throwassert(rr && a && b && c && d && e && f && g && h && i && j);} // Added by Fabian Dortu (10 parameters)
OneOperator(aType rr,const ArrayOfaType &ta) ;
//: r(rr),ArrayOfaType(ta),next(0),pref(0) {throwassert(rr);}
OneOperator(aType rr,bool ellipse) ;
//: r(rr),ArrayOfaType(ellipse),next(0),pref(0) {throwassert(rr );}
OneOperator(aType rr,const ListOfId *l) ;
//: r(rr),ArrayOfaType(l),next(0),pref(0) {throwassert(rr );}
typedef pair<const OneOperator *,int> pair_find;
void operator+=(OneOperator &a){throwassert(a.next==0);a.next=next;next=&a;}
// a way to make none recurve delete good
virtual ~OneOperator();
pair_find Find(const ArrayOfaType & at) const ;
pair_find FindWithOutCast(const ArrayOfaType & at) const ; // for
OneOperator * FindSameR(const ArrayOfaType & at) ;
void Show(const ArrayOfaType & at,ostream &f=cerr) const;
void Show(ostream &f=cerr) const;
operator aType () const { return r;}
// <<OneOperator_code_decl>>
virtual E_F0 * code(const basicAC_F0 &) const =0;
virtual C_F0 code2(const basicAC_F0 &a) const ; // {return code(code(a),r);}
const OneOperator * Simple() const { return next||n?0:this;}
friend ostream & operator<<(ostream & f,const OneOperator & a);
};
/// <<Polymorphic>>
class Polymorphic:
public E_F0mps // [[E_F0mps]]
{
// a list of type
// simple, array or function
private:
typedef const char * Key;
typedef OneOperator * Value;
// struct Keyless : binary_function<Key,Key, bool>
// { bool operator()(const Key& x, const Key& y) const{ return strcmp(x,y)<0;} };
typedef map<Key,Value,Keyless> maptype; //
typedef maptype::const_iterator const_iterator; //
typedef maptype::iterator iterator; //
// remark the map is mutable because
// a expression is const E_F0 *
// So There is a incompatibility between
// we save an expression in a variable
// we have to add thing to a polymorphisme expression
mutable maptype m; // all polymorphisme of the Identifier
Expression e; // default expression
public:
Polymorphic() : m(),e(0) {}
// by default Empty and do nothing
virtual AnyType operator()(Stack ) const { return Nothing;}
virtual bool Empty() const {return true;} // by default Empty
void clear() { m.clear();}
const OneOperator * Find(const char *op, const ArrayOfaType &at) const;
const OneOperator * FindWithOutCast(const char *op,const ArrayOfaType &at) const;
void Show(const char *op,const ArrayOfaType & at,ostream &f=cerr)const ;
void Add(const char * op,OneOperator * p0 ,OneOperator * p1=0,OneOperator * p2=0,
OneOperator * p3=0,OneOperator * p4=0,OneOperator * p5=0,
OneOperator * p6=0,OneOperator * p7=0,OneOperator * p8=0,
OneOperator * p9=0,OneOperator * pa=0,OneOperator * pb=0,
OneOperator * pc=0,OneOperator * pd=0,OneOperator * pe=0
) const
{Addp(op,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pa,pb,pc,pd,pe,0);}
void Add(const char * op,OneOperator ** pp) const ;
private:
void Addp(const char * op,OneOperator * pp,...) const ;
friend ostream & operator<<(ostream & f,const Polymorphic & a);
};
/// <<C_F0>> The type for polymorphisme of id. Contains one [[Expression]] and the type that the expression will return
/// [[file:../fflib/AnyType.hpp::aType]] (useful to select the right operator when using that type).
class basicAC_F0;
class C_F0 {
friend class CC_F0; // cf [[CC_F0]]
protected:
Expression f; // the expression code, cf [[Expression]]
aType r; // the expression type, cf [[file:../fflib/AnyType.hpp::aType]]
public:
// the constructeur
C_F0() :f(0),r(0) {}
C_F0(const C_F0 & c):f(c.f),r(c.r) {}
C_F0(const C_F0 & a,const C_F0 & b); // concatenation
/// cf [[Type_Expr]]
C_F0(const Type_Expr & a):f(a.second),r(a.first) {}
/// <<C_F0_constructor_pop_char_basicAC_F0_decl>> [[file:AFunction2.cpp::C_F0_constructor_pop_char_basicAC_F0_impl]]
/// cf [[Polymorphic]]
C_F0(const Polymorphic *,const char *,const basicAC_F0 & );
C_F0(const Polymorphic *,const char *, AC_F0 & );
// function, array ..
C_F0(const C_F0 & e,const char *op,const basicAC_F0 & p) ;
C_F0(const C_F0 & e,const char *op, AC_F0 & p) ;
// <<C_F0_constructor_char_C_F0_decl>> [[C_F0_constructor_char_C_F0_impl]]
C_F0(const C_F0 & e,const char *op,const C_F0 & ee);
C_F0(const C_F0 & e,const char *op,const C_F0 & a,const C_F0 & b) ;
C_F0(const C_F0 & e,const char *nm) ;
// without parameter ex f(). cf [[Polymorphic]]
C_F0(const Polymorphic * pop,const char *op);
// unary operator
C_F0(const Polymorphic * pop,const char *op,const C_F0 & a);
// <<C_F0_constructor_binary_decl>> binary operator [[file:AFunction2.cpp::C_F0_constructor_binary]]
C_F0(const Polymorphic * pop,const char *op,const C_F0 & a,const C_F0 & b);
// ternary operator
C_F0(const Polymorphic * pop,const char *op,const C_F0 & a,const C_F0 & b,const C_F0 & c);
C_F0( Expression ff,aType rr ): f(ff),r(rr) {
// cout << "C_F0: " << * rr << endl;// dec 2007 FH
// if (!rr && ff) cerr << "Type Null" << endl;
}
// operator Expression() const {return f;}
AnyType eval(Stack s) const {return (*f)(s);}
Expression RightValue() const { return r->RightValueExpr(f);}
Expression LeftValue() const;
aType left() const {return r;}
aType right() const {return r->right();}
C_F0 RightExp() const { return C_F0(RightValue(),right());} // FH add 07/2005
operator E_F0 * () const {return f;}
bool Empty() const {return !f || f->Empty();}
bool NotNull() const {return f;}
int TYPEOFID() const { return r ? r->TYPEOFID(): 0;}
int nbitem() const { return f ? f->nbitem() : 0;}
bool EvaluableWithOutStack() const { return f && f->EvaluableWithOutStack();}
bool Zero() const { return !f || f->Zero();}
Expression Destroy() { return r->Destroy(*this);}
operator const Polymorphic * () const {return dynamic_cast<const Polymorphic *>(f);}
bool operator==(const C_F0 & a) const {return f==a.f && r == a.r;}
bool operator!=(const C_F0 & a) const {return f!=a.f || r != a.r;}
// Type_Expr SetParam(const ListOfId * l,size_t & top) const ;
bool MeshIndependent() const { return f ==0 ? f->MeshIndependent() : false;}
C_F0 OnReturn() { f=r->OnReturn(f); return *this; } // Add mai 2009 (for return statment.
private:
friend class Block;
friend class TableOfIdentifier;
C_F0( Expression ff ): f(ff),r(0) {}
};
// for bison [[CListOfInst]]
class CListOfInst;
// a => b
// f => t||f
// t => t
// (a =>b) <=> (!a || b )
// warning ------------------
class ForTypeVoid: public basicForEachType{public:
ForTypeVoid():basicForEachType(typeid(void),0,0,0,0,0) {}
};
template<class T>
class ForEachType: public basicForEachType{public:
ForEachType(Function1 iv=0,Function1 id=0,Function1 OOnReturn=0):basicForEachType(typeid(T),sizeof(T),0,0,iv,id,OOnReturn) {
if (sizeof(T) > sizeof(AnyTypeWithOutCheck) )
{
cout << " Sorry the " <<typeid(T).name() << " is too large ( " << sizeof(T)
<< " > " << sizeof(AnyTypeWithOutCheck) << " ) " << endl;
throwassert(sizeof(T) <= sizeof(AnyTypeWithOutCheck) );
}
}
};
template<class T>
class ForEachType<T*>: public basicForEachType{public:// coorection july 2009..... FH Hoooo.... (Il y a un bug DUR DUR FH ...)
ForEachType(Function1 iv=0,Function1 id=0,Function1 OOnReturn=0):basicForEachType(typeid(T),sizeof(T),0,0,iv,id,OOnReturn) {
//T i= 0.0;
}
};
template<class A,class B> AnyType UnRef(Stack,const AnyType &a) ;
template<class A> AnyType Initialize(Stack,const AnyType &a) ;
template<class A> AnyType Destroy(Stack,const AnyType &a) ;
// the type of variable is pointer because we need to write in
template<class T,class PT=T*>
class ForEachTypePtr: public basicForEachType { public:
ForEachTypePtr();
ForEachTypePtr(Function1 init,Function1 dl,Function1 onreturn=0);
ForEachTypePtr(Function1 dl);
};
template<class T>
class ForEachTypePtr<T*,T**>: public basicForEachType { public:
ForEachTypePtr(T* bb=0,Function1 onreturn=0);
ForEachTypePtr(Function1 init,Function1 dl,Function1 onreturn=0);
ForEachTypePtr(Function1 dl);
};
template<class T,int RTYPE>
class ForEachTypePtrfspace: public ForEachTypePtr<T> { public:
ForEachTypePtrfspace():ForEachTypePtr<T>() {}
int TYPEOFID() const {return RTYPE;}
};
class ForTypeAnyType: public basicForEachType{public:
ForTypeAnyType(): basicForEachType(typeid(AnyType),sizeof(AnyType)) {}
bool CastingFrom(const basicForEachType * ) const {return true;}
C_F0 CastTo(const C_F0 & e) const {return e;}
};
// for cast and get value associed to a pointer
template<class A,class B>
AnyType Cast(Stack,const AnyType &b) {
return SetAny<A>(static_cast<A>(GetAny<B>(b)));}
template<class A,class B,A F(const B &)>
AnyType FCast(Stack s,const AnyType &b) {
return SetAny<A>(Add2StackOfPtr2Free(s,F(GetAny<B>(b))));}
template<class A>
AnyType UnRef(Stack,const AnyType &a) {
return SetAny<A>(*PGetAny<A>(a));}
template<class A,class B>
AnyType UnRef(Stack,const AnyType &a) {
return SetAny<A>(*GetAny<B>(a));}
template<class A>
AnyType UnRefCopyPtr(Stack s,const AnyType &a) {
A ** ppa=PGetAny<A*>(a);
A * pc = new A(**ppa);
return SetPtrAny(Add2StackOfPtr2Free(s,pc)) ;}
template<class A> AnyType Initialize(Stack,const AnyType &x){
A * a=PGetAny<A>(x);
A *b=new A;//
memcpy(a,b,sizeof(A));// bitcopy
::operator delete(b); // delete with no destruction
return SetPtrAny(a);
}
template<class A> AnyType InitializePtr(Stack stack,const AnyType &x){
A * a=PGetAny<A>(x);
SHOWVERB( cout << " init ptr " << typeid(A*).name() << (char *) a - (char*) stack<< endl);
*a=0;
return x;
}
template<class A> AnyType InitializeDef(Stack stack,const AnyType &x){
A * a=PGetAny<A>(x);
SHOWVERB( cout << " init ptr " << typeid(A*).name() << (char *) a - (char*) stack<< endl);
*a=A();
return x;
}
template<class A> inline AnyType Delete(Stack,const AnyType &x){
A * a=PGetAny<A>(x);
SHOWVERB(cout << "DESTROY " <<typeid(A).name() << " " << a << endl);
(*a).~A();
return Nothing;
}
template<class A> inline AnyType Destroy(Stack,const AnyType &x){
A * a=PGetAny<A>(x);
SHOWVERB(cout << "DESTROY " <<typeid(A).name() << " " << a << endl);
a->destroy();
return Nothing;
}
template<class A> inline AnyType DestroyS(Stack,const AnyType &x){
A a=GetAny<A>(x);
SHOWVERB(cout << "DESTROY " <<typeid(A).name() << " " << a << endl);
a.destroy();
return Nothing;
}
template<class A> inline AnyType InitS(Stack,const AnyType &x){
A a=GetAny<A>(x);
SHOWVERB(cout << "InitS " <<typeid(A).name() << " " << a << endl);
a.init();
return Nothing;
}
template<class A> inline AnyType InitP(Stack,const AnyType &x){
A *a=PGetAny<A>(x);
SHOWVERB(cout << "InitP " <<typeid(A).name() << " " << a << endl);
a->init();
return Nothing;
}
template<class A> inline AnyType DestroyPtr(Stack,const AnyType &x) {
const A * a=PGetAny<A>(x);
SHOWVERB(cout << "DestroyPtr " << typeid(A).name() << *a << endl);
(*a)->destroy();
// delete *a;
return Nothing;
};
template<class A> inline AnyType DeletePtr(Stack,const AnyType &x) {
const A * a=PGetAny<A>(x);
if(verbosity>99)cout << "DeletePtr " << typeid(A).name() << *a << endl;
// (*a)->destroy();
delete *a;
return Nothing;
};
template<> AnyType inline DestroyPtr<string *>(Stack,const AnyType &x) {
string ** a=PGetAny<string*>(x);
SHOWVERB( cout << "DestroyPtr " << typeid(string*).name() << *a << endl);
freestring(*a);
return Nothing;
};
template<class A> AnyType Initialize(Stack,const AnyType &x,const AnyType &y){
A * a=PGetAny<A>(x);
A *b=new A(GetAny<A>(x));//
memcpy(a,b,sizeof(A));// bitcopy
::operator delete(b); // delete with no destruction
return PtrtoAny(a);
}
class E_F0_CFunc2 :public E_F0mps { public:
CFunction2 f2;
E_F0 *a,*b;
AnyType operator()(Stack s) const {return f2(s,a,b);}
E_F0_CFunc2( CFunction2 ff,E_F0 *aa,E_F0 *bb) : f2(ff),a(aa),b(bb){}
bool EvaluableWithOutStack() const
{return a->EvaluableWithOutStack() && b->EvaluableWithOutStack();} //
operator aType () const { return atype<void>();}
};
class E_F0_CFunc4 :public E_F0mps { public:
CFunction4 f4;
E_F0 *a,*b,*c,*d;
AnyType operator()(Stack s) const {return f4(s,a,b,c,d);}
E_F0_CFunc4( CFunction4 ff,E_F0 *aa,E_F0 *bb,E_F0 *cc,E_F0 *dd)
: f4(ff),a(aa),b(bb),c(cc),d(dd){}
operator aType () const { return atype<void>();}
};
template<class R,class A>
class E_F1_F :public E_F1 { public:
typedef R (*func)(A) ;
func f;
E_F1_F(func ff) : f(ff) {}
AnyType operator()(Stack s,AnyType & a) const
{return SetAny<R>(f(GetAny<A>(a)));}
};
template<class R,class A0,class A1>
class E_F2_F :public E_F2 { public:
typedef R (*func)(const A0 &,const A1&) ;
func f;
E_F2_F(func ff) : f(ff) {}
AnyType operator()(Stack s,AnyType & a0,AnyType & a1) const
{return SetAny<R>(f(GetAny<A0>(a0),GetAny<A1>(a1)));}
};
template<class R,class TA0,bool RO=true>
class E_F_F0 :public E_F0 { public:
template <class T> struct remove_reference {typedef T type;};
// template <class T> struct remove_reference<T&> {typedef T type;};
template <class T> struct remove_reference<const T&> {typedef T type;};
typedef typename remove_reference<TA0>::type A0;
typedef R (*func)( TA0 ) ;
func f;
Expression a;
E_F_F0(func ff,Expression aa) : f(ff),a(aa) {}
AnyType operator()(Stack s) const
{return SetAny<R>(f(GetAny<A0>( (*a)(s) )));}
bool EvaluableWithOutStack() const
{return a->EvaluableWithOutStack();} //
bool MeshIndependent() const {return a->MeshIndependent();} //
bool ReadOnly() const { return RO ;}
int compare (const E_F0 *t) const {
int rr;
// cout << "cmp " << typeid(*this).name() << " and " << typeid(t).name() << endl;
const E_F_F0* tt=dynamic_cast<const E_F_F0 *>(t);
if (tt && f == tt->f) rr = a->compare(tt->a);
else rr = E_F0::compare(t);
return rr;
} // to give a order in instuction
int Optimize(deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n) ;
virtual ostream & dump(ostream &ff) const { ff << typeid(*this).name() <<" f= " << f << " a= "<< *a << ' ' ;return ff; }
};
// modif for xlc++ FH
template<class R,class TA0,bool RO=true>
class E_F_F0_Opt: public E_F_F0<R,TA0,RO> { public :
size_t ia;
E_F_F0_Opt(const E_F_F0<R,TA0,RO> &t,size_t iaa)
: E_F_F0<R,TA0,RO>(t) , ia(iaa) {assert(iaa<2000000 && iaa >0);}
AnyType operator()(Stack s) const
{
// A0 x = *static_cast<A0 *>(static_cast<void*>(static_cast<char *>(s)+ia));
// cout << " opt f (" << x << " ) = " << ": " << ia << endl;
return SetAny<R>( this->f( *static_cast<typename E_F_F0<R,TA0>::A0 *>(static_cast<void*>(static_cast<char *>(s)+ia)) ) );}
};
template<class R,class TA0,bool RO>
int E_F_F0<R,TA0,RO>::Optimize(deque<pair<Expression,int> > &l,MapOfE_F0 & m, size_t & n)
{
int rr = find(m);
if (rr) return rr;
return insert(new E_F_F0_Opt<R,TA0,RO>(*this,a->Optimize(l,m,n)),l,m,n);
}
// fin modif xlc++
template<class A0>
class E_VF_F0 :public E_F0 { public:
typedef void (*func)( A0 ) ;
func f;
Expression a;
E_VF_F0(func ff,Expression aa) : f(ff),a(aa) {}
AnyType operator()(Stack s) const
{f(GetAny<A0>( (*a)(s) ));return Nothing;}
bool EvaluableWithOutStack() const
{return a->EvaluableWithOutStack();} //
bool MeshIndependent() const { return a->MeshIndependent(); }
};