-
Notifications
You must be signed in to change notification settings - Fork 2
/
prog.cc
14006 lines (13469 loc) · 508 KB
/
prog.cc
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
/* -*- mode:C++ ; compile-command: "g++ -DHAVE_CONFIG_H -I. -I.. -DIN_GIAC -DGIAC_GENERIC_CONSTANTS -g -c -fno-strict-aliasing prog.cc -Wall" -*- */
#include "giacPCH.h"
/*
* Copyright (C) 2001,14 B. Parisse, Institut Fourier, 38402 St Martin d'Heres
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using namespace std;
#ifdef __MINGW_H
#define HAVE_NO_PWD_H
#endif
#ifndef HAVE_NO_PWD_H
#ifndef BESTA_OS
#include <pwd.h>
#endif
#endif
#ifdef FXCG
extern "C" {
#include <rtc.h>
}
#endif
#include <stdexcept>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#if !defined GIAC_HAS_STO_38 && !defined NSPIRE && !defined FXCG
#include <fstream>
#endif
#include "prog.h"
#include "identificateur.h"
#include "symbolic.h"
#include "identificateur.h"
#include "usual.h"
#include "sym2poly.h"
#include "subst.h"
#include "plot.h"
#include "tex.h"
#include "input_parser.h"
#include "input_lexer.h"
#include "rpn.h"
#include "help.h"
#include "ti89.h" // for _unarchive_ti
#include "permu.h"
#include "modpoly.h"
#include "unary.h"
#include "input_lexer.h"
#include "maple.h"
#include "derive.h"
#include "giacintl.h"
#include "misc.h"
#include "lin.h"
#include "pari.h"
#include "intg.h"
#include "csturm.h"
#include "sparse.h"
#include "giacintl.h"
#include "signalprocessing.h"
// #include "input_parser.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_LIBDL
#include <dlfcn.h>
#endif // HAVE_LIBDL
#ifdef USE_GMP_REPLACEMENTS
#undef HAVE_GMPXX_H
#undef HAVE_LIBMPFR
#undef HAVE_LIBPARI
#endif
#ifdef NSPIRE_NEWLIB
#include <os.h>
extern "C" double millis();
#endif
//#ifdef BESTA_OS
//unsigned int PrimeGetNow();
//#endif
#ifdef RTOS_THREADX
u32 PrimeGetNow();
extern "C" uint32_t mainThreadStack[];
#else
#if !defined BESTA_OS && !defined FXCG
#include <time.h>
#endif
#endif
#if defined(EMCC) || defined(EMCC2)
#include <emscripten.h>
#endif
#ifdef KHICAS
#include "kdisplay.h"
#endif
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
#if 1 // defined(GIAC_HAS_STO_38) && defined(VISUALC)
const int rand_max2=2147483647;
#else
const int rand_max2=RAND_MAX;
#endif
const double inv_rand_max2_p1=1.0/(rand_max2+1.0);
#ifdef HAVE_LIBDL
modules_tab giac_modules_tab;
#endif
void alert(const string & s,GIAC_CONTEXT){
#if defined(EMCC) || defined(EMCC2)
EM_ASM_ARGS({
if (typeof UI!=='undefined' && UI.warnpy){
var msg = UTF8ToString($0);// Pointer_stringify($0); // Convert message to JS string
alert(msg); // Use JS version of alert
}
}, s.c_str());
#endif
*logptr(contextptr) << s << '\n';
}
gen equaltosto(const gen & g,GIAC_CONTEXT){
if (g.type<=_IDNT || !eval_equaltosto(contextptr))
return g;
if (g.is_symb_of_sommet(at_add_autosimplify)){
return symbolic(g._SYMBptr->sommet,equaltosto(g._SYMBptr->feuille,contextptr));
}
if (is_equal(g)){
vecteur v=*g._SYMBptr->feuille._VECTptr;
gen a;
if (v.size()==2)
a=v.back();
else
a=gen(vecteur(v.begin()+1,v.end()),g.subtype);
if (v.front().type==_IDNT)
return symb_sto(a,v.front());
if (v.front().type==_VECT){
vecteur w=*v.front()._VECTptr;
for (int i=0;i<w.size();++i){
if (w[i].type!=_IDNT)
return g;
}
return symb_sto(a,v.front());
}
}
return g;
}
vecteur equaltostov(const vecteur & v,GIAC_CONTEXT){
vecteur w(v);
iterateur it=w.begin(),itend=w.end();
for (;it!=itend;++it)
*it=equaltosto(*it,contextptr);
return w;
}
gen attoof(const gen & g){
if (g.type==_VECT){
vecteur v=*g._VECTptr;
iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it)
*it=attoof(*it);
return gen(v,g.subtype);
}
if (g.type!=_SYMB)
return g;
if (g._SYMBptr->sommet!=at_at)
return symbolic(g._SYMBptr->sommet,attoof(g._SYMBptr->feuille));
if (g._SYMBptr->feuille.type!=_VECT || g._SYMBptr->feuille._VECTptr->size()<=1) // not somehting that I recognize as a proper parameter list, just return as before
return symbolic(at_of,attoof(g._SYMBptr->feuille));
// This looks like it is a vector of at least 2 objects. The first one should be the variable to do an at/of on and all the rest should be indices that needs to be incremented
vecteur v=*g._SYMBptr->feuille._VECTptr;
iterateur it=v.begin()+1,itend=v.end(); // from item 1 to n-1 in the vector
for (;it!=itend;++it)
*it=attoof(*it)+gen(1); // add 1 to the object
return symbolic(at_of,attoof(gen(v,g._SYMBptr->feuille.subtype))); // return the of(vector with modified indices) gen
}
static int prog_eval_level(GIAC_CONTEXT){
if (int i=prog_eval_level_val(contextptr))
return i;
return std::max(1,eval_level(contextptr));
}
gen check_secure(){
if (secure_run
#ifdef KHICAS
|| exam_mode || nspire_exam_mode
#endif
)
return gensizeerr(gettext("Running in secure mode"));
return 0;
}
string indent(GIAC_CONTEXT){
if (xcas_mode(contextptr)==3)
return "\n:"+string(debug_ptr(contextptr)->indent_spaces,' ');
else
return " \n"+string(debug_ptr(contextptr)->indent_spaces,' ');
}
static string indent2(GIAC_CONTEXT){
return string(debug_ptr(contextptr)->indent_spaces,' ');
}
// static gen substsametoequal(const gen & g){ return symbolic(at_subst,apply(g,sametoequal)); }
// static gen subssametoequal(const gen & g){ return symbolic(at_subs,apply(g,sametoequal)); }
// static gen maplesubssametoequal(const gen & g){ return symbolic(at_maple_subs,apply(g,sametoequal)); }
gen equaltosame(const gen & a){
// full replacement of = by == has been commented to avoid
// problems with tests like: if (limit(...,x=0,..))
/*
unary_function_ptr equaltosametab1[]={at_equal,at_subst,at_subs,at_maple_subs};
vector<unary_function_ptr> substin(equaltosametab1,equaltosametab1+sizeof(equaltosametab1)/sizeof(unary_function_ptr));
gen_op equaltosametab2[]={symb_same,substsametoequal,subssametoequal,maplesubssametoequal};
vector<gen_op> substout(equaltosametab2,equaltosametab2+sizeof(equaltosametab2)/sizeof(gen_op));
gen tmp=subst(a,substin,substout,true);
return tmp;
*/
if ( is_equal(a))
return symb_same(a._SYMBptr->feuille._VECTptr->front(),a._SYMBptr->feuille._VECTptr->back());
else
return a;
}
gen sametoequal(const gen & a){
if ( (a.type==_SYMB) && (a._SYMBptr->sommet==at_same) )
return symb_equal(a._SYMBptr->feuille._VECTptr->front(),a._SYMBptr->feuille._VECTptr->back());
else
return a;
}
static void increment_instruction(const const_iterateur & it0,const const_iterateur & itend,debug_struct * dbgptr){
const_iterateur it=it0;
for (;it!=itend;++it)
increment_instruction(*it,dbgptr);
}
void increment_instruction(const vecteur & v,debug_struct * dbgptr){
const_iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it)
increment_instruction(*it,dbgptr);
}
void increment_instruction(const gen & arg,debug_struct * dbgptr){
// cerr << debug_ptr(contextptr)->current_instruction << " " << arg <<'\n';
++dbgptr->current_instruction;
if (arg.type!=_SYMB)
return;
const unary_function_ptr & u=arg._SYMBptr->sommet;
const gen & f=arg._SYMBptr->feuille;
const unary_function_eval * uptr=dynamic_cast<const unary_function_eval *>(u.ptr());
if (uptr && uptr->op==_ifte){
--dbgptr->current_instruction;
increment_instruction(*f._VECTptr,dbgptr);
return;
}
if ( (u==at_local) || (uptr && uptr->op==_for) ){
gen F(f._VECTptr->back());
if (F.type!=_VECT){
if (F.is_symb_of_sommet(at_bloc) && F._SYMBptr->feuille.type==_VECT)
increment_instruction(*F._SYMBptr->feuille._VECTptr,dbgptr);
else
increment_instruction(F,dbgptr);
}
else
increment_instruction(*F._VECTptr,dbgptr);
return;
}
if (u==at_bloc){
if (f.type!=_VECT)
increment_instruction(f,dbgptr);
else
increment_instruction(*f._VECTptr,dbgptr);
return;
}
if (u==at_try_catch){
increment_instruction(f._VECTptr->front(),dbgptr);
increment_instruction(f._VECTptr->back(),dbgptr);
}
}
void increment_instruction(const gen & arg,GIAC_CONTEXT){
increment_instruction(arg,debug_ptr(contextptr));
}
static string concatenate(const vector<string> & v){
vector<string>::const_iterator it=v.begin(),itend=v.end();
string res;
for (;it!=itend;++it){
res=res + " "+*it;
}
return res;
}
void debug_print(const vecteur & arg,vector<string> & v,GIAC_CONTEXT){
const_iterateur it=arg.begin(),itend=arg.end();
for (;it!=itend;++it){
if (it->is_symb_of_sommet(at_program)){
vector<string> tmp;
debug_print(*it,tmp,contextptr);
v.push_back(concatenate(tmp));
}
debug_print(*it,v,contextptr);
}
}
static string printaslocalvars(const gen &loc,GIAC_CONTEXT){
gen locals(loc);
if (locals._VECTptr->size()==1)
return locals._VECTptr->front().print(contextptr);
locals.subtype=_SEQ__VECT;
return locals.print(contextptr);
}
// convert back increment and decrement to sto
static gen from_increment(const gen & g){
int type=0;
if (g.is_symb_of_sommet(at_increment))
type=1;
if (g.is_symb_of_sommet(at_decrement))
type=-1;
if (type){
gen & f =g._SYMBptr->feuille;
if (f.type!=_VECT)
return symbolic(at_sto,gen(makevecteur(symbolic(at_plus,makevecteur(f,type)),f),_SEQ__VECT));
vecteur & v = *f._VECTptr;
if (v.size()!=2)
return gensizeerr(gettext("from_increment"));
return symbolic(at_sto,gen(makevecteur(symbolic(at_plus,gen(makevecteur(v[0],type*v[1]),_SEQ__VECT)),v[0]),_SEQ__VECT));
}
return g;
}
void debug_print(const gen & e,vector<string> & v,GIAC_CONTEXT){
if (e.type!=_SYMB){
v.push_back(indent2(contextptr)+e.print(contextptr));
return ;
}
bool is38=abs_calc_mode(contextptr)==38;
bool python=python_compat(contextptr);
unary_function_ptr u=e._SYMBptr->sommet;
gen f=e._SYMBptr->feuille;
const unary_function_eval * uptr=dynamic_cast<const unary_function_eval *>(u.ptr());
if (uptr && uptr->op==_ifte){
string s=indent2(contextptr);
s += python?"if ":(is38?"IF ":"if(");
vecteur w=*f._VECTptr;
s += w.front().print(contextptr);
s += python?":":(is38?" THEN/ELSE":")");
v.push_back(s);
debug_ptr(contextptr)->indent_spaces += 1;
debug_print(w[1],v,contextptr);
debug_ptr(contextptr)->indent_spaces += 1;
debug_print(w[2],v,contextptr);
debug_ptr(contextptr)->indent_spaces -=2;
return ;
}
if (u==at_local){
string s(indent2(contextptr));
s += python?"# local ":(is38?"LOCAL ":"local ");
gen local_global=f._VECTptr->front(),locals(gen2vecteur(local_global)),globals(vecteur(0));
if (local_global.type==_VECT && local_global._VECTptr->size()==2){
gen f=local_global._VECTptr->front(),b=local_global._VECTptr->back();
if (f.type!=_IDNT){
locals=gen2vecteur(f);
globals=gen2vecteur(b);
}
}
if (globals._VECTptr->empty())
s += printaslocalvars(locals,contextptr);
else
s += local_global.print(contextptr);
v.push_back(s);
debug_ptr(contextptr)->indent_spaces += 2;
f=f._VECTptr->back();
if (f.type!=_VECT)
debug_print(f,v,contextptr);
else
debug_print(*f._VECTptr,v,contextptr);
debug_ptr(contextptr)->indent_spaces -= 2;
return;
}
if (uptr && uptr->op==_for){
vecteur w=*f._VECTptr;
string s(indent2(contextptr));
bool done=false;
if (python){
gen inc(from_increment(w[2]));
if (inc.is_symb_of_sommet(at_sto) && inc._SYMBptr->feuille[1].type==_IDNT){
gen index=inc._SYMBptr->feuille[1];
if (inc._SYMBptr->feuille[0]==index+1 && w[0].is_symb_of_sommet(at_sto) && w[0]._SYMBptr->feuille[1]==index){
gen start=w[0]._SYMBptr->feuille[0];
if (w[1].type==_SYMB && (w[1]._SYMBptr->sommet==at_inferieur_egal || w[1]._SYMBptr->sommet==at_inferieur_strict) && w[1]._SYMBptr->feuille[0]==index){
int large=w[1]._SYMBptr->sommet==at_inferieur_egal?1:0;
gen stop=w[1]._SYMBptr->feuille[1];
s += "for "+index.print(contextptr)+" in range(";
if (start!=0)
s +=start.print(contextptr)+",";
s += (stop+large).print(contextptr)+"):";
done=true;
}
}
}
}
if (w[0].type==_INT_ && w[2].type==_INT_){
s += python?"while ":(is38?"WHILE(":"while(");
s += w[1].print(contextptr);
s += python?":":")";
done=true;
}
if (!done){
s += is38?"FOR(":"for(";
s += w[0].print(contextptr)+";"+w[1].print(contextptr)+";"+w[2].print(contextptr)+")";
}
v.push_back(s);
debug_ptr(contextptr)->indent_spaces += 2;
f=f._VECTptr->back();
if ((f.type==_SYMB) && (f._SYMBptr->sommet==at_bloc))
f=f._SYMBptr->feuille;
if (f.type!=_VECT)
debug_print(f,v,contextptr);
else
debug_print(*f._VECTptr,v,contextptr);
debug_ptr(contextptr)->indent_spaces -= 2;
return;
}
if (u==at_bloc){
v.push_back(string(indent2(contextptr)+"bloc"));
debug_ptr(contextptr)->indent_spaces += 2;
if (f.type!=_VECT)
debug_print(f,v,contextptr);
else
debug_print(*f._VECTptr,v,contextptr);
debug_ptr(contextptr)->indent_spaces -= 2;
return;
}
if (u==at_try_catch){
// cerr << f << '\n';
v.push_back(string(indent2(contextptr)+"try"));
debug_ptr(contextptr)->indent_spaces += 1;
debug_print(f._VECTptr->front(),v,contextptr);
debug_ptr(contextptr)->indent_spaces += 1;
debug_print(f._VECTptr->back(),v,contextptr);
debug_ptr(contextptr)->indent_spaces -=2;
return;
}
v.push_back(indent2(contextptr)+e.print(contextptr));
}
static vecteur rm_checktype(const vecteur & v,GIAC_CONTEXT){
vecteur addvars(v);
iterateur it=addvars.begin(),itend=addvars.end();
for (;it!=itend;++it){
if (it->is_symb_of_sommet(at_deuxpoints))
*it=it->_SYMBptr->feuille._VECTptr->front();
if (it->is_symb_of_sommet(at_check_type))
*it=it->_SYMBptr->feuille._VECTptr->back();
if (it->is_symb_of_sommet(at_sto))
*it=it->_SYMBptr->feuille._VECTptr->back();
if (it->is_symb_of_sommet(at_equal))
*it=it->_SYMBptr->feuille._VECTptr->front();
}
return addvars;
}
// res1= list of assignation with =, res2= list of non declared global vars, res3= list of declared global vars, res4=list of functions
void check_local_assign(const gen & g,const vecteur & prog_args,vecteur & res1,vecteur & res2,vecteur & res3,vecteur & res4,bool testequal,GIAC_CONTEXT){
if (g.is_symb_of_sommet(at_double_deux_points))
return;
if (g.is_symb_of_sommet(at_local)){
gen &f=g._SYMBptr->feuille;
if (f.type!=_VECT || f._VECTptr->size()!=2)
return;
gen declaredvars=f._VECTptr->front();
if (declaredvars.type==_VECT && declaredvars._VECTptr->size()==2){
vecteur f1(gen2vecteur(declaredvars._VECTptr->front()));
for (int i=0;i<int(f1.size());++i){
if (f1[i].is_symb_of_sommet(at_equal))
f1[i]=f1[i]._SYMBptr->feuille[0];
}
vecteur f2(gen2vecteur(declaredvars._VECTptr->back()));
res3=mergevecteur(res3,f2);
declaredvars=mergevecteur(f1,f2);
}
vecteur addvars(rm_checktype(gen2vecteur(declaredvars),contextptr));
vecteur newvars(mergevecteur(prog_args,addvars));
// check_local_assign(f._VECTptr->front(),newvars,res1,res2,res3,res4,testequal,contextptr);
check_local_assign(f._VECTptr->back(),newvars,res1,res2,res3,res4,testequal,contextptr);
return;
}
if (g.is_symb_of_sommet(at_sto)){
gen &f=g._SYMBptr->feuille;
if (f.type==_VECT && f._VECTptr->size()==2 && f._VECTptr->front().is_symb_of_sommet(at_program)){
res4.push_back(f._VECTptr->back());
gen & ff = f._VECTptr->front()._SYMBptr->feuille;
if (ff.type==_VECT && ff._VECTptr->size()==3){
vecteur alt_prog_args(gen2vecteur(ff._VECTptr->front()));
check_local_assign(ff._VECTptr->back(),alt_prog_args,res1,res2,res3,res4,testequal,contextptr);
}
return;
}
}
if (g.is_symb_of_sommet(at_ifte) || g.is_symb_of_sommet(at_when)){
vecteur v=lop(g,at_array_sto);
if (!v.empty() && logptr(contextptr))
*logptr(contextptr) << gettext("Warning, =< is in-place assign, check ") << v << '\n';
}
if (g.is_symb_of_sommet(at_bloc) ||
g.is_symb_of_sommet(at_for) || g.is_symb_of_sommet(at_pour) ||
g.is_symb_of_sommet(at_tantque) || g.is_symb_of_sommet(at_si) ||
g.is_symb_of_sommet(at_sialorssinon) ||
g.is_symb_of_sommet(at_ifte) || g.is_symb_of_sommet(at_when)){
check_local_assign(g._SYMBptr->feuille,prog_args,res1,res2,res3,res4,testequal,contextptr);
return;
}
if (testequal && is_equal(g)){
if (g._SYMBptr->feuille.type==_VECT && g._SYMBptr->feuille._VECTptr->size()==2 && g._SYMBptr->feuille._VECTptr->front().type!=_INT_ )
res1.push_back(g);
return;
}
if (g.is_symb_of_sommet(at_of)){
gen & f=g._SYMBptr->feuille;
if (f.type!=_VECT || f._VECTptr->size()!=2)
return;
if (python_compat(contextptr) || protecteval(f._VECTptr->front(),1,contextptr)!=f._VECTptr->front())
check_local_assign(f._VECTptr->back(),prog_args,res1,res2,res3,res4,false,contextptr);
else
check_local_assign(f,prog_args,res1,res2,res3,res4,false,contextptr);
return;
}
if (g.type==_SYMB){
check_local_assign(g._SYMBptr->feuille,prog_args,res1,res2,res3,res4,false,contextptr);
return;
}
if (g.type!=_VECT){
vecteur l(gen2vecteur(_lname(g,contextptr)));
const_iterateur it=l.begin(),itend=l.end();
for (;it!=itend;++it){
if (!equalposcomp(res2,*it) && !equalposcomp(prog_args,*it))
res2.push_back(*it);
}
return;
}
const_iterateur it=g._VECTptr->begin(),itend=g._VECTptr->end();
for (;it!=itend;++it){
check_local_assign(*it,prog_args,res1,res2,res3,res4,testequal,contextptr);
}
}
bool is_constant_idnt(const gen & g){
return g==cst_pi || g==cst_euler_gamma || is_inf(g) || is_undef(g) || (g.type==_IDNT && (strcmp(g._IDNTptr->id_name,"i")==0 || strcmp(g._IDNTptr->id_name,"None")==0 || strcmp(g._IDNTptr->id_name,"cmath")==0 || strcmp(g._IDNTptr->id_name,"math")==0 || strcmp(g._IDNTptr->id_name,"kandinsky")==0 || strcmp(g._IDNTptr->id_name,"pass")==0));
}
bool warn_equal_in_prog=true,warn_symb_program_sto=true;
gen _warn_equal_in_prog(const gen & g,GIAC_CONTEXT){
if (is_zero(g) && g.type!=_VECT){
warn_equal_in_prog=false;
return string2gen(gettext("Warning disabled"),false);
}
if (is_one(g)){
warn_equal_in_prog=true;
return string2gen(gettext("Warning enabled"),false);
}
return warn_equal_in_prog;
}
static const char _warn_equal_in_prog_s []="warn_equal_in_prog";
static define_unary_function_eval (__warn_equal_in_prog,&_warn_equal_in_prog,_warn_equal_in_prog_s);
define_unary_function_ptr5( at_warn_equal_in_prog ,alias_at_warn_equal_in_prog,&__warn_equal_in_prog,0,true);
// Return the names of variables that are not local in g
// and the equality that are not used (warning = instead of := )
string check_local_assign(const gen & g,GIAC_CONTEXT){
string res;
if (g.type==_VECT){
const_iterateur it=g._VECTptr->begin(),itend=g._VECTptr->end();
for (;it!=itend;++it)
res += check_local_assign(*it,contextptr);
return res;
}
if (g.is_symb_of_sommet(at_nodisp))
return check_local_assign(g._SYMBptr->feuille,contextptr);
if (g.is_symb_of_sommet(at_sto)){
gen & f =g._SYMBptr->feuille;
if (f.type!=_VECT || f._VECTptr->size()!=2)
return res;
res=check_local_assign(f._VECTptr->front(),contextptr);
return res.substr(0,res.size()-1)+"\n//"+gettext(" compiling ")+f._VECTptr->back().print(contextptr)+'\n';
}
if (!g.is_symb_of_sommet(at_program))
return res;
gen & f=g._SYMBptr->feuille;
if (f.type!=_VECT || f._VECTptr->size()!=3)
return gettext("// Invalid program");
vecteur & v =*f._VECTptr;
vecteur vars=gen2vecteur(v[0]),res1,res2,res3,res4;
// add implicit declaration of global var in argument optional value
for (int i=0;i<vars.size();i++){
if (vars[i].is_symb_of_sommet(at_equal)){
gen g=vars[i]._SYMBptr->feuille[1];
res2=mergevecteur(res2,lidnt(g));
}
}
res2.push_back(undef);
vars=rm_checktype(vars,contextptr);
for (unsigned i=0;i<vars.size();++i){
if (equalposcomp(vars,vars[i])!=int(i+1))
res += gettext("// Warning, duplicate argument name: ")+vars[i].print(contextptr)+'\n';
}
gen prog=v.back();
check_local_assign(prog,vars,res1,res2,res3,res4,true,contextptr);
int rs=int(res2.size());
for (int i=0;i<rs;i++){
if (is_constant_idnt(res2[i])){
res2.erase(res2.begin()+i);
--i; --rs;
}
}
if (warn_equal_in_prog && !res1.empty()){ // syntax = for := is now accepted
res += gettext("// Warning, assignation is :=, check the lines below:\n");
res += "// (Run warn_equal_in_prog(0) to disable this warning)\n";
const_iterateur it=res1.begin(),itend=res1.end();
for (;it!=itend;++it){
res += '\n'+it->print(contextptr);
}
res +="\n";
}
if (res2.size()>=1){
res+=gettext("// Warning: ");
const_iterateur it=res2.begin(),itend=res2.end();
for (;it!=itend;++it){
// pi already checked if (*it!=cst_pi)
res += it->print(contextptr)+",";
}
res +=gettext(" declared as global variable(s). If symbolic variables are required, declare them as local and run purge\n");
}
if (res.empty())
return first_error_line(contextptr)?gettext("// Error(s)\n"):gettext("// Success\n");
else
return res;
}
static string printascheck_type(const gen & feuille,const char * sommetstr,GIAC_CONTEXT){
if ( (feuille.type!=_VECT) || (feuille._VECTptr->size()!=2) )
return string(sommetstr)+('('+feuille.print(contextptr)+')');
return print_the_type(feuille._VECTptr->front().val,contextptr)+' '+feuille._VECTptr->back().print(contextptr);
}
gen symb_check_type(const gen & args,GIAC_CONTEXT){
return symbolic(at_check_type,args);
}
gen _check_type(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
if (args.type!=_VECT)
return symb_check_type(args,contextptr);
if (args._VECTptr->size()!=2)
return gensizeerr(gettext("check_type must have 2 args"));
gen res=args._VECTptr->back();
gen req=args._VECTptr->front();
if (req.type!=_INT_) // FIXME check for matrix(...) or vector(...)
return res;
int type;
switch (res.type){
case _INT_:
type=_ZINT;
break;
case _REAL:
type=_DOUBLE_;
break;
default:
type=res.type;
break;
}
if (req.val==_MAPLE_LIST){
if (type==_VECT)
return res;
return gensizeerr(contextptr);
}
if (type==req.val)
return res;
if (type==_ZINT && type==(req.val &0xff) ){
if (req.val==_POSINT && is_strictly_positive(res,contextptr))
return res;
if (req.val==_NEGINT && is_strictly_positive(-res,contextptr))
return res;
if (req.val==_NONPOSINT && is_positive(-res,contextptr))
return res;
if (req.val==_NONNEGINT && is_positive(res,contextptr))
return res;
}
return gentypeerr(gettext("Argument should be of type ")+print_the_type(args._VECTptr->front().val,contextptr));
}
static const char _check_type_s []="check_type";
static define_unary_function_eval2_index (118,__check_type,&symb_check_type,_check_type_s,&printascheck_type);
define_unary_function_ptr( at_check_type ,alias_at_check_type ,&__check_type);
// static gen symb_type(const gen & args){ return symbolic(at_type,args); }
gen _type(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
int type;
switch (args.type){
case _INT_:
type=_ZINT;
break;
case _REAL:
#ifndef NO_RTTI
if (dynamic_cast<real_interval *>(args._REALptr))
type=_FLOAT_;
else
#endif
type=_DOUBLE_;
break;
default:
if (args.is_symb_of_sommet(at_program))
type=_FUNC;
else
type=args.type;
}
gen tmp(type);
tmp.subtype=1;
return tmp;
}
static const char _type_s []="type";
static define_unary_function_eval (__type,&_type,_type_s);
define_unary_function_ptr5( at_type ,alias_at_type,&__type,0,true);
gen _nop(const gen & a,GIAC_CONTEXT){
if ( a.type==_STRNG && a.subtype==-1) return a;
if (a.type==_VECT && a.subtype==_SEQ__VECT){
// Workaround so that sequences inside spreadsheet are saved as []
gen tmp=a;
tmp.subtype=0;
return tmp;
}
return a;
}
static const char _nop_s []="nop";
static define_unary_function_eval (__nop,&_nop,_nop_s);
define_unary_function_ptr5( at_nop ,alias_at_nop,&__nop,0,true);
gen _Nop(const gen & a,GIAC_CONTEXT){
if ( a.type==_STRNG && a.subtype==-1) return a;
return a;
}
static const char _Nop_s []="Nop";
static define_unary_function_eval (__Nop,&_Nop,_Nop_s);
define_unary_function_ptr5( at_Nop ,alias_at_Nop,&__Nop,0,true);
string printasinnerbloc(const gen & feuille,GIAC_CONTEXT){
if ( (feuille.type==_SYMB) && feuille._SYMBptr->sommet==at_bloc)
return printasinnerbloc(feuille._SYMBptr->feuille,contextptr);
if (feuille.type!=_VECT)
return indent(contextptr)+feuille.print(contextptr);
const_iterateur it=feuille._VECTptr->begin(),itend=feuille._VECTptr->end();
string res;
if (it==itend)
return res;
for (;;){
res += indent(contextptr)+it->print(contextptr);
++it;
if (it==itend)
return res;
if (xcas_mode(contextptr)!=3)
res += ";";
}
}
static void local_init(const gen & e,vecteur & non_init_vars,vecteur & initialisation_seq){
vecteur v;
if (e.type!=_VECT)
v=vecteur(1,e);
else
v=*e._VECTptr;
if (v.size()==2 && v.front().type==_VECT)
v=*v.front()._VECTptr;
const_iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it){
if (it->type==_IDNT){
non_init_vars.push_back(*it);
continue;
}
if ( (it->type==_SYMB) && (it->_SYMBptr->sommet==at_sto)){
non_init_vars.push_back(it->_SYMBptr->feuille._VECTptr->back());
initialisation_seq.push_back(*it);
}
}
}
static gen add_global(const gen & i,GIAC_CONTEXT){
#ifndef NO_STDEXCEPT
if (i.type==_IDNT)
#endif
return identificateur("global_"+i.print(contextptr));
return gensizeerr(gettext("Proc Parameters"));
}
static string remove_empty_lines(const string & s){
// return s;
string res;
int ss=int(s.size()),ns=0;
bool blank=true;
for (int i=0;i<ss;++i){
char ch=s[i];
if (!blank){
res += ch;
if (ch=='\n'){
ns=0;
blank=true;
}
continue;
}
if (ch=='\n'){
ns=0;
continue;
}
if (ch!=' '){
blank=false;
res += string(ns,' ')+ch;
ns=0;
continue;
}
++ns;
}
return res;
}
static string printasbloc(const gen & feuille,const char * sommetstr,GIAC_CONTEXT);
static string in_printasprogram(const gen & feuille,const char * sommetstr,GIAC_CONTEXT){
if ( (feuille.type!=_VECT) || (feuille._VECTptr->size()!=3) )
return string(sommetstr)+('('+feuille.print(contextptr)+')');
string res;
bool python=python_compat(contextptr) && !debug_ptr(contextptr)->debug_mode;
if (python){
int & ind=debug_ptr(contextptr)->indent_spaces;
vecteur v =*feuille._VECTptr;
if (v[2].type==_VECT && v[2].subtype==_PRG__VECT)
v[2]=symb_bloc(v[2]);
if (!v[2].is_symb_of_sommet(at_bloc) && !v[2].is_symb_of_sommet(at_local)){
res=string(ind,' ')+"lambda ";
if (v[0].type==_VECT && v[0].subtype==_SEQ__VECT && v[0]._VECTptr->size()==1)
res += v[0]._VECTptr->front().print(contextptr);
else
res += v[0].print(contextptr);
res +=':';
if (v[2].is_symb_of_sommet(at_return))
res += v[2]._SYMBptr->feuille.print(contextptr);
else
res += v[2].print(contextptr);
return res;
}
res = string(ind,' ')+"def "+lastprog_name(contextptr)+"(";
if (v[0].type==_VECT && v[0].subtype==_SEQ__VECT && v[0]._VECTptr->size()==1)
res += equaltosto(v[0]._VECTptr->front(),contextptr).print(contextptr);
else
res += equaltosto(v[0],contextptr).print(contextptr);
res += "):\n";
ind += 4;
if (v[2].is_symb_of_sommet(at_bloc) || v[2].is_symb_of_sommet(at_local))
res += v[2].print(contextptr);
else
res += string(ind,' ')+v[2].print(contextptr)+'\n';
ind -= 4;
// remove empty lines in res
return remove_empty_lines(res);
}
bool calc38=abs_calc_mode(contextptr)==38;
if (!calc38){
if (xcas_mode(contextptr)==3)
res="\n:lastprog";
else
res=" "; // was res=indent(contextptr);
}
gen & feuille0=feuille._VECTptr->front();
if (feuille0.type==_VECT && feuille0.subtype==_SEQ__VECT && feuille0._VECTptr->size()==1)
res +="("+feuille0._VECTptr->front().print(contextptr)+")";
else
res +="("+feuille0.print(contextptr)+")";
if (xcas_mode(contextptr)==3)
res +="\n";
else
res += "->";
bool test;
string locals,inits;
gen proc_args=feuille._VECTptr->front();
vecteur vect,non_init_vars,initialisation_seq;
if ((xcas_mode(contextptr)>0 || abs_calc_mode(contextptr)==38) && (feuille._VECTptr->back().type==_SYMB) && (feuille._VECTptr->back()._SYMBptr->sommet==at_local)){
test=false;
gen tmp=feuille._VECTptr->back()._SYMBptr->feuille;
local_init(tmp._VECTptr->front(),non_init_vars,initialisation_seq);
// For Maple add proc parameters to local vars
if (xcas_mode(contextptr) ==1+_DECALAGE){
if (proc_args.type==_VECT){
vecteur v=*proc_args._VECTptr;
non_init_vars=mergevecteur(non_init_vars,v);
iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it){
gen tmp=add_global(*it,contextptr);
initialisation_seq.push_back(symb_sto(tmp,*it));
*it=tmp;
}
proc_args=gen(v,_SEQ__VECT);
}
else {
non_init_vars.push_back(proc_args);
gen tmp=add_global(proc_args,contextptr);
initialisation_seq.push_back(symb_sto(tmp,proc_args));
proc_args=tmp;
}
}
if (!non_init_vars.empty()){
if (xcas_mode(contextptr)==3)
locals=indent(contextptr)+"Local "+printinner_VECT(non_init_vars,_SEQ__VECT,contextptr);
else
locals=indent(contextptr)+(calc38?" LOCAL ":" local ")+printinner_VECT(non_init_vars,_SEQ__VECT,contextptr)+";";
}
inits=printasinnerbloc(gen(initialisation_seq,_SEQ__VECT),contextptr);
if (tmp._VECTptr->back().type==_VECT)
vect=*tmp._VECTptr->back()._VECTptr;
else
vect=makevecteur(tmp._VECTptr->back());
}
else {
test=(feuille._VECTptr->back().type!=_VECT ||feuille._VECTptr->back().subtype );
if (!test)
vect=*feuille._VECTptr->back()._VECTptr;
}
if (test){
gen & fb=feuille._VECTptr->back();
if (xcas_mode(contextptr)==3)
return res+":Func "+fb.print(contextptr)+"\n:EndFunc\n";
if (fb.is_symb_of_sommet(at_local)){
gen fb0=fb._SYMBptr->feuille[0];
//fb0=fb0[0];
if (fb0.type==_VECT && fb0._VECTptr->empty())
return res+'{'+fb.print(contextptr)+'}';
}
return res+(fb.type==_VECT?printasbloc(fb,sommetstr,contextptr):fb.print(contextptr));
}
if (xcas_mode(contextptr)>0){
if (xcas_mode(contextptr)==3)
res+=":Func"+locals;
else {
res="proc("+proc_args.print(contextptr)+")"+locals;
if (xcas_mode(contextptr)==2)
res +=indent(contextptr)+"begin ";
if (inits.size())
res += indent(contextptr)+inits+";";
}
}
else {
if (calc38)
res += "BEGIN "+locals;
else
res += "{";
}
const_iterateur it=vect.begin(),itend=vect.end();
debug_ptr(contextptr)->indent_spaces +=2;
for (;;){
if (xcas_mode(contextptr)==3)
res += indent(contextptr)+it->print(contextptr);
else
res += indent(contextptr)+it->print(contextptr);
++it;
if (it==itend){
debug_ptr(contextptr)->indent_spaces -=2;
if (xcas_mode(contextptr)!=3)
res += "; "+indent(contextptr);
switch (xcas_mode(contextptr)){
case 0:
res += calc38?"END;":"}";
break;
case 1: case 1+_DECALAGE:
res+=indent(contextptr)+"end;";
break;
case 2:
return res+=indent(contextptr)+"end_proc;";
case 3:
return res+=indent(contextptr)+"EndFunc\n";
}
break;
}
else {
if (xcas_mode(contextptr)!=3)