-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
1657 lines (1486 loc) · 37 KB
/
functions.c
File metadata and controls
1657 lines (1486 loc) · 37 KB
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
/*
* Copyright 2011 Sergey Kolotsey.
*
* This file is part of libexpression library.
*
* libexpression 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.
*
* libexpression 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 Licenise
* along with libexpression. If not, see <http://www.gnu.org/licenses/>.
*
* =====================================================================
*
* Definitions of various math/string/logical functions.
* These functions are available to use in expressions that are processed by
* libexpression library.
* Function exp_call_function() is called from rpn.c.
*/
#include "libexpression-private.h"
/**
* @file functions.c
* This file implements all embedded functions that are avaiable to use in
* expressions that are processed by libexpression.
* @ingroup libexpression
* @{
*/
/**
* @page embedded_function Embedded Functions Overview
*
* @section embedded_functions_overview Embedded Functions in libexpression
*
* @c Libexpression library contains several embedded functions that can be used
* in expressions. When @c libexpression locates a function in an expression, it
* tries to find the implementation of this function in the list of embedded
* functions. If @c libexpression can not find an implementation of the function
* with given name, it calls a callback. The callback is implemented in code
* that uses this library.
*
* You can implement any required function in your callback. See
* exp_set_function_handler() for more information.
*
* Almost all function that are available in @c libexpression are
* implemented in standard @c c libraries, such as libm. The @c libexpression
* library only defines interfaces for these functions.
*
*
*/
/**
* @page math_functions Math functions
*
* In this section all available embedded math functions are documented. These
* functions can be used in expressions.
*
* Almost all math function that are available in @c libexpression are
* implemented in @c libm C library. The @c libexpression
* library only defines interfaces for these functions.
*/
/**
* @page math_functions
* @section abs abs(x)
*
* The @c abs(a) function returns the absolute value of the integer or double
* argument @a @c x.
*/
static int call_abs( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=fabs(d1);
//ret->value.real=d1<0? -d1 : d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section acos acos(x)
*
* This function computes the arccosine (specified in radians) of an argument
* and returns its value in the range (0, PI). The argument of the function
* should be a number in the range (-1, 1). The function may return
* @c EXP_ER_TRIGONOMETRIC error if computation is not possible.
*/
static int call_acos( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if( d1<-1 || d1>1){
return EXP_ER_TRIGONOMETRIC;
}else{
ret->value.real=acos( d1);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section asin asin(x)
*
* This function computes the arcsine (specified in radians) of an argument
* and returns its value in the range (-PI/2, PI/2). The argument of the function
* should be a number in the range (-1, 1). The function may return
* @c EXP_ER_TRIGONOMETRIC error if computation is not possible.
*/
static int call_asin( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if( d1<-1 || d1>1){
return EXP_ER_TRIGONOMETRIC;
}else{
ret->value.real=asin( d1);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section atan atan(x)
*
* This function computes the arctangent (specified in radians) of an argument
* and returns its value in the range (-PI/2, PI/2).
*
*/
static int call_atan( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=atan( d1);
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section atan2 atan2(y, x)
*
* This function computes the value of the arctangent (specified in radians)
* of @c y/x, using the signs of both arguments to determine the quadrant of the
* return value. A @c EXP_ER_DIVBYZERO error occurs if @a @c y is zero.
*
*/
static int call_atan2( token_t *op, value_t *ret){
int status;
double d1, d2;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next==NULL) return EXP_ER_INVALARGCLOW;
if( op->next->next) return EXP_ER_INVALARGCHIGH;
if((0 !=(status=exp_to_double( &op->param, &d1))) ||(0 !=(status=exp_to_double( &op->next->param, &d2)))){
return status;
}else{
if( d1 !=0 && d2==0){
return EXP_ER_DIVBYZERO;
}else{
ret->value.real=atan2( d1, d2);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section ceil ceil(x)
*
* The @c ceil() function rounds the value of @a @c x up to the next integer (rounding
* towards the "ceiling").
*
*/
static int call_ceil( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if(d1<INT64_MIN || d1>INT64_MAX){
return EXP_ER_INTOVERFLOW;
}else{
ret->value.integer=(int64_t)ceil(d1);
ret->type=T_INTEGER;
return 0;
}
}
}
/**
* @page math_functions
* @section cos cos(a)
*
* This function computes the cosine of @a @c a (specified in radians).
*
*/
static int call_cos( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=cos( d1);
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section cosh cosh(a)
*
* This function computes the hyperbolic cosine (specified in radians) of @a @c a.
* A @c EXP_ER_TRIGONOMETRIC error occurs if the magnitude of @a @c a is too large
*/
static int call_cosh( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
errno=0;
d1=cosh( d1);
if( errno !=0){
return EXP_ER_TRIGONOMETRIC;
}
ret->value.real=d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section exp exp(a)
*
* The @c exp() function computes the exponential function of @a @c a (e^a).
*/
static int call_exp( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=exp( d1);
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section floor floor(x)
*
* This function computes the largest integer <= @a @c x (rounding towards the "floor").
*/
static int call_floor( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if(d1<INT64_MIN || d1>INT64_MAX){
return EXP_ER_INTOVERFLOW;
}else{
ret->value.integer=(int64_t)floor(d1);
ret->type=T_INTEGER;
return 0;
}
}
}
/**
* @page math_functions
* @section fmod fmod(x, y)
*
* The @c fmod() function computes the floating-point residue of @a @c x (mod @a @c y),
* which is the remainder of @c x/y, even if the quotient @c x/y isn't representable.
* If @a @c y is zero, the function returns 0.
*/
static int call_fmod( token_t *op, value_t *ret){
int status;
double d1, d2;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next==NULL) return EXP_ER_INVALARGCLOW;
if( op->next->next) return EXP_ER_INVALARGCHIGH;
if((0 !=(status=exp_to_double( &op->param, &d1))) ||(0 !=(status=exp_to_double( &op->next->param, &d2)))){
return status;
}else{
errno=0;
d1=atan2( d1, d2);
if( 0 !=errno){
return EXP_ER_INVALARGV;
}else{
ret->value.real=d1;
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section log log(x)
*
* The @c log() function computes the natural logarithm (base @c e) of @a @c x.
* A @c EXP_ER_COMPLEX error occurs if @a @c x is not positive.
*/
static int call_log( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if( d1<=0){
return EXP_ER_COMPLEX;
}else{
ret->value.real=log(d1);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section log10 log10(x)
*
* The @c log10() function computes the base 10 logarithm of @a @c x.
* A @c EXP_ER_COMPLEX error occurs if @a @c x is not positive.
*/
static int call_log10( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if( d1<=0){
return EXP_ER_COMPLEX;
}else{
ret->value.real=log10(d1);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section min min(a,...)
*
* The @c min() function returns the minimum argument from the list of supplied
* arguments. The @c min() function accepts one or more arguments.
*/
static int call_min( token_t *op, value_t *ret){
int status;
double d1, min;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if((0 !=(status=exp_to_double( &op->param, &d1)))){
return status;
}
min=d1;
op=op->next;
while( op){
if((0 !=(status=exp_to_double( &op->param, &d1)))){
return status;
}
min=d1<min? d1 : min;
op=op->next;
}
ret->value.real=min;
ret->type=T_REAL;
return 0;
}
/**
* @page math_functions
* @section max max(a,...)
*
* The @c max() function returns the maximum argument from the list of supplied
* arguments. The @c max() function acepts one or more arguments.
*/
static int call_max( token_t *op, value_t *ret){
int status;
double d1, max;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if((0 !=(status=exp_to_double( &op->param, &d1)))){
return status;
}
max=d1;
op=op->next;
while( op){
if((0 !=(status=exp_to_double( &op->param, &d1)))){
return status;
}
max=d1>max? d1 : max;
op=op->next;
}
ret->value.real=max;
ret->type=T_REAL;
return 0;
}
/**
* @page math_functions
* @section pow pow(x, y)
*
* The @c pow() function computes @a @c x raised to the power of @a @c y.
* A @c EXP_ER_DIVBYZERO error occurs if @a @c x = 0, and @a @c y <= 0, or if @a @c x is
* negative, and @a @c y isn't an integer.
*/
static int call_pow( token_t *op, value_t *ret){
int status;
double d1, d2;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next==NULL) return EXP_ER_INVALARGCLOW;
if( op->next->next) return EXP_ER_INVALARGCHIGH;
if((0 !=(status=exp_to_double( &op->param, &d1))) ||(0 !=(status=exp_to_double( &op->next->param, &d2)))){
return status;
}else{
if((d1==0) && (d2<=0)){
return EXP_ER_DIVBYZERO;
}else{
if(d1<0){
int64_t i2;
if(0==(status=exp_is_integer( &op->next->param, &i2))){
ret->value.real=pow( d1, i2);
ret->type=T_REAL;
return 0;
}else{
if( status==EXP_ER_NONINTEGER){
return EXP_ER_COMPLEX;
}else{
return status;
}
}
}else{
ret->value.real=pow( d1, d2);
ret->type=T_REAL;
return 0;
}
}
}
}
/**
* @page math_functions
* @section random random(a, b)
*
* Generate a pseudo-random number.
* The @c random() function uses implementation of @c random() from @c libm
* library. The function returns a pseudo-random number in the range [0, 1) if no
* arguments were supplied. The function returns a pseudo-random number in the
* range [0, a). If only one argument @a @c a was supplied. The function returns
* a pseudo-random number in the range [a, b). If two argument @a @c a and @a @c b
* were supplied.
*
* The function does not start a new sequence of pseudo-random integers when
* called. That is, a program code that uses @c libexpression library should
* call @c srand() standard C function when initialising libexpression.
*
* The function @c rand(a, b) is an alias to this function.
*/
static int call_random( token_t *op, value_t *ret){
if(op){
double rmin, rmax;
int status;
if( op->next){
//two arguments: low and high borders
if(op->next->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &rmin)) ||
0 !=(status=exp_to_double( &op->next->param, &rmax))){
return status;
}else{
ret->value.real=rmin+(double)rand()*(rmax-rmin)/(double)RAND_MAX;
ret->type=T_REAL;
return 0;
}
}else{
//one argument: high border
if(0 !=(status=exp_to_double( &op->param, &rmax))){
return status;
}else{
ret->value.real=(double)rand()*rmax/(double)RAND_MAX;
ret->type=T_REAL;
return 0;
}
}
}else{
ret->value.real=(double)rand()/(double)RAND_MAX;
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section round round(x)
*
* The @c round() function returns the closest integer to @a @c x.
*
*/
static int call_round( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if(d1<INT64_MIN || d1>INT64_MAX){
return EXP_ER_INTOVERFLOW;
}else{
ret->value.integer=(int64_t)round(d1);
ret->type=T_INTEGER;
return 0;
}
}
}
/**
* @page math_functions
* @section sin sin(a)
*
* The @c sin() function computes the sine (specified in radians) of @a @c a.
*
*/
static int call_sin( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=sin( d1);
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section sinh sinh(a)
*
* The @c sinh() function computes the hyperbolic sine (specified in radians) of
* @a @c a. A @c EXP_ER_TRIGONOMETRIC error occurs if the magnitude of @a @c a is too large.
*
*/
static int call_sinh( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
errno=0;
d1=sinh( d1);
if( errno !=0){
return EXP_ER_TRIGONOMETRIC;
}
ret->value.real=d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section sqr sqr(x)
*
* The @c sqr() function computes the square of an argument.
*
*/
static int call_sqr( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=d1 * d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section sqrt sqrt(x)
*
* The function computes the nonnegative square root of @a @c x.
* A @c EXP_ER_COMPLEX error occurs if the argument is negative.
*
*/
static int call_sqrt( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
if( d1<0){
return EXP_ER_COMPLEX;
}else{
ret->value.real=sqrt(d1);
ret->type=T_REAL;
return 0;
}
}
}
/**
* @page math_functions
* @section tan tan(a)
*
* The function computes the tangent (specified in radians) of @a @c a.
*
*/
static int call_tan( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=tan( d1);
ret->type=T_REAL;
return 0;
}
}
/**
* @page math_functions
* @section tanh tanh(a)
*
* These functions compute the hyperbolic tangent (specified in radians) of @a @c a.
*
*/
static int call_tanh( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
errno=0;
d1=tanh( d1);
if( errno !=0){
return EXP_ER_TRIGONOMETRIC;
}
ret->value.real=d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page conversion_functions Conversion Functions
*
* This section contains all embedded conversion functions implemented in
* @c libexpression that could be used in expressions.
*
*/
/**
* @page conversion_functions
* @section bin2dec bin2dec(s)
*
* The function converts a binary representation of a number to an integer.
* The argument supplied to the function should be a string.
*
* The easier way to use binary numbers in expression is to use a special binary
* notation to represent a number. Binary notation uses prefix @c 0b to represent
* the numbers, e.g. @code 0b101 @endcode stands for 5.
*/
static int call_bin2dec( token_t *op, value_t *ret){
uint64_t result;
char *s;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(op->param.type !=T_STRING) return EXP_ER_NONSTRING;
result=0;
if( op->param.value.string){
s=op->param.value.string;
while( *s){
if( *s=='0' || *s=='1'){
result=(result<<1) | (*s=='1'? 1 : 0);
if( result>INT64_MAX){
return EXP_ER_INTOVERFLOW;
}
}else{
return EXP_ER_INVALARGV;
}
s++;
}
}
ret->type=T_INTEGER;
ret->value.integer=(int64_t)result;
return 0;
}
/**
* @page conversion_functions
* @section boolean boolean(b)
*
* The function converts an argument to a boolean value.
* If the argument could not be represented as boolean, @c EXP_ER_NONBOOLEAN is
* returned.
*
* The function @c bool() is an alias to this function.
*/
static int call_boolean( token_t *op, value_t *ret){
int status;
int b1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_boolean( &op->param, &b1))){
return status;
}else{
ret->value.boolean=b1;
ret->type=T_BOOLEAN;
return 0;
}
}
/**
* @page conversion_functions
* @section dec2bin dec2bin(i)
*
* The function returns a string containing a binary representation of an argument.
*/
static int call_dec2bin( token_t *op, value_t *ret){
uint64_t i1, c;
int64_t i;
char s[128];
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if( 0!=exp_is_integer( &op->param, &i)) return EXP_ER_NONINTEGER;
i1=(uint64_t) i;
c=0;
do{
s[c++]=(i1 & 0x01? '1' : '0');
i1>>=1;
}while( i1);
s[c]=0;
if(NULL==( ret->value.string=malloc( c+1))) return EXP_ER_NOMEM;
ret->type=T_STRING;
for( i=0; i<=c; i++){
ret->value.string[i]=s[c-i-1];
}
ret->value.string[c]=0;
return 0;
}
/**
* @page conversion_functions
* @section dec2hex dec2hex(i)
*
* The function returns a string containing a hexademical representation of an argument.
*/
static int call_dec2hex( token_t *op, value_t *ret){
uint64_t i1, c;
int64_t i;
char s[128];
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if( 0!=exp_is_integer( &op->param, &i)) return EXP_ER_NONINTEGER;
i1=(uint64_t) i;
c=0;
do{
if((i1 & 0x0f) <10){
s[c++]=(i1 & 0x0f)+'0';
}else{
switch( i1 & 0x0f){
case 0x0a: s[c++]='a'; break;
case 0x0b: s[c++]='b'; break;
case 0x0c: s[c++]='c'; break;
case 0x0d: s[c++]='d'; break;
case 0x0e: s[c++]='e'; break;
case 0x0f: s[c++]='f'; break;
default: s[c++]='0'; break;
}
}
i1=i1>>4;
}while( i1);
s[c]=0;
if(NULL==( ret->value.string=malloc( c+1))) return EXP_ER_NOMEM;
ret->type=T_STRING;
for( i=0; i<=c; i++){
ret->value.string[i]=s[c-i-1];
}
ret->value.string[c]=0;
return 0;
}
/**
* @page conversion_functions
* @section dec2oct dec2oct(i)
*
* The function returns a string containing a octal representation of an argument.
*/
static int call_dec2oct( token_t *op, value_t *ret){
uint64_t i1, c;
int64_t i;
char s[128];
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if( 0!=exp_is_integer( &op->param, &i)) return EXP_ER_NONINTEGER;
i1=(uint64_t) i;
c=0;
do{
s[c++]=(i1 & 0x07)+'0';
i1=i1>>3;
}while( i1);
s[c]=0;
if(NULL==( ret->value.string=malloc( c+1))) return EXP_ER_NOMEM;
ret->type=T_STRING;
for( i=0; i<=c; i++){
ret->value.string[i]=s[c-i-1];
}
ret->value.string[c]=0;
return 0;
}
/**
* @page conversion_functions
* @section float float(f)
*
* The function converts an argument to a floating point number.
* If the argument could not be converted to float, @c EXP_ER_NONNUMERIC is
* returned.
*
* The function @c double() is an alias to this function.
*/
static int call_double( token_t *op, value_t *ret){
int status;
double d1;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(0 !=(status=exp_to_double( &op->param, &d1))){
return status;
}else{
ret->value.real=d1;
ret->type=T_REAL;
return 0;
}
}
/**
* @page conversion_functions
* @section hex2dec hex2dec(s)
*
* The function converts a string with hexademical representation of a number
* to a number.
*
* The easier way to use hexademical numbers in expression is to use a special hex
* notation to represent a number. Hex notation uses prefix @c 0x to represent
* the numbers, e.g. @code 0xff @endcode stands for 255.
*/
static int call_hex2dec( token_t *op, value_t *ret){
uint64_t result;
char *s;
int i;
if(NULL==op) return EXP_ER_INVALARGCLOW;
if( op->next) return EXP_ER_INVALARGCHIGH;
if(op->param.type !=T_STRING) return EXP_ER_NONSTRING;
result=0;