-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcexp.cc
More file actions
1369 lines (1241 loc) · 34.4 KB
/
cexp.cc
File metadata and controls
1369 lines (1241 loc) · 34.4 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
/*
* kate: default-dictionary en_US; tab-width 4; tab-indents true;
*
* C Expression Evaluator
*
* This program is distributed under the terms of the GPL v3.0 or later
* Download the GNU Public License (GPL) from <https://www.gnu.org>
*
* Copyright(C) 2025, Free Software Foundation, Inc.
* Written by Nicholas Christopoulos <mailto:netnic@proton.me>
*/
#ifndef CEXP_H
#define CEXP_H 0x10000
#include <cstring>
#include <cstdarg>
#include <string>
#include <vector>
#include <stack>
#include <unordered_map>
#ifndef CEXP_USE_PROPS
#define CEXP_USE_PROPS
#endif
/*
* Default data types
*/
#ifndef CEXP_DATA_T
#define CEXP_DATA_T
typedef ssize_t int_t;
typedef size_t uint_t;
typedef double real_t;
#endif
#include "cvar.cc" // variant variables
/*
* Function definitions
*/
typedef struct cexp_func_s {
int n_args; // number of arguments, -1 for unknown
real_t (*std_math_1)(real_t); // pointer to standard math library routine
real_t (*std_math_2)(real_t,real_t); // pointer to standard math library routine
int (*custom_N)(const std::vector<CVar>&, CVar *); // pointer to function with variable number of arguments
} cexp_func_t;
// error codes, defined by developer
// user must only use E_SUCCESS or !E_SUCCESS
typedef enum { E_SUCCESS,
E_DIV_ZERO,
E_SYNTAX, E_TYPE_MM, E_NO_OP, E_OP_FOUND,
E_STACK_UNDERFLOW_V, E_STACK_UNDERFLOW_OP, E_OUT_OF_INDEX,
E_FUNC_NOT_FOUND, E_FUNC_ERROR, E_EXCEPTION = 666
} cexp_error_t;
/*
* Evaluator - main class
*/
class CExp {
public:
int verbose; // print debug messages - ignore it
cexp_error_t error_code; // error code
std::string error_message; // error message
protected:
std::unordered_map<std::string,cexp_func_t> funcs; // functions
std::unordered_map<std::string,CVar> vars; // variables
public:
CExp();
virtual ~CExp() { }
// -- functions management ---
// add a standard math function
void add_func(const char *name, real_t (*f)(real_t));
// add a standard math function with two double parameters
void add_func(const char *name, real_t (*f)(real_t,real_t));
// add a function with `n` number of variant parameters
void add_func(const char *name, int n, int (*f)(const std::vector<CVar>& args, CVar *result));
// add a function with unlimited number of variant parameters
void add_func(const char *name, int (*f)(const std::vector<CVar>& args, CVar *result))
{ add_func(name, -1, f); }
// --- variables management ---
// get variant, on error exception will be thrown
const CVar& getvar(const char *name, const char *prop = NULL) const;
CVar& getvar(const char *name, const char *prop = NULL);
// get variable pointer or NULL if not found
const CVar *getvar_p(const char *name, const char *prop = NULL) const;
CVar *getvar_p(const char *name, const char *prop = NULL);
// set the value of the variable
void setvar(const char *name, const CVar& data);
void setvar(const char *name, real_t n);
void setvar(const char *name, int_t n);
void setvar(const char *name, const char *s);
// remove the variable
bool unsetvar(const char *name);
// --- main routine ---
// execute the expression `source` and get the result in the
// `result` variable.
// On success returns zero; otherwise returns the error code.
cexp_error_t evaluate(const char *source, CVar *result);
// execute the expressions in `source` and get the multiple results
// in the `result` vector<>.
// On success returns zero; otherwise returns the error code.
cexp_error_t evaluate(const char *source, std::vector<CVar> *result);
protected:
// set error
void set_error(cexp_error_t code, const char *fmt, ...);
// string utils
int is_opchar(int c) const;
int is_keyword(int c) const;
int c_esc_char(const char *s, size_t *count) const;
char *remove_spaces(const char *src) const;
// operator utilities
int find_opcode(int op) const;
int find_assign_opcode(int op) const;
int find_op(const char *src, bool is_assign_op) const;
int find_unary(const char *src) const;
int precedence(int op) const;
// evaluator utilities
static int eval(const std::vector<CVar>& args, CVar *result);
CVar apply_op(const CVar& a, const CVar& b, int op) const;
CVar apply_unary(const CVar& v, int op) const;
const char *parse_num(const char *p, real_t *val) const;
const char *parse_function(const char *p, const char *name, CVar *r);
const char *parse_string(const char *p, CVar *r) const;
const char *parse_keyword(const char *p, char *buf, size_t len) const;
// create variable / property if does not exist
CVar *mkvar_p(const char *name, const char *prop);
// assign value to variable and/or property based on operator code `op`
void var_assign(int op, const char *name, const char *prop, CVar& value);
const char *evaluate_p(const char *tokens, CVar *result,
const char *xset, std::vector<CVar> *rstk = NULL);
};
#endif
// --- implamentation --------------------------------------------------------- */
#ifdef CEXP_IMPL
#include <cassert>
#include <cstdint>
#include <climits>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <ctime>
#include <cmath>
#include <stdexcept>
using namespace std;
// matrices
#include "cmatrix.cc"
// variant variables
#define CVAR_IMPL // include the implamentation
#include "cvar.cc"
// additional math functions
#include "cexp-lib.cc"
#define M_PHI 1.61803398874989484820
#define UNARY_BIT 0x80000000
#define UNARY_MASK 0x7fffffff
#if !defined(MAX)
#define MAX(a,b) ((a)>(b) ? (a) : (b))
#endif
#define OP2(a,b) (((b)<<8)|(a))
#define OP3(a,b,c) (((c)<<16)|((b)<<8)|(a))
#define OPSIZE(x) ( (((x) & 0xff0000) ? 3 : (((x) & 0xff00) ? 2 : (((x) & 0xff) ? 1 : 0) ) ) )
// returns true if 'c' is an octal-digit character
static inline int isoctal(int c)
{ return ((c >= '0') && (c <= '7')); }
// returns the value of hexadecimal-digit 'c'
static inline int hex_digit_val(int c) {
if ( c >= 'a' ) return (c - 'a') + 10;
else if ( c >= 'A' ) return (c - 'A') + 10;
return (c - '0');
}
/*
* returns the ASCII value of escaped string 's'
* in 'count' returned the bytes of 's' that used
*/
int CExp::c_esc_char(const char *s, size_t *count) const {
const char *p = s;
int c = *p, n, i, cnt;
*count = 1;
switch ( *p ) {
case '\n': return ' '; // error, continue to the next line
case 'a': return 7;
case 'b': return '\b';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case 'f': return '\f';
case 'e': return '\033';
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
n = 0;
n |= (*p - '0') << 6;
if ( !isoctal(* ++p) ) return *p;
n |= (*p - '0') << 3; (*count) ++;
if ( !isoctal(* ++p) ) return *p;
n |= (*p - '0'); (*count) ++;
return n;
case 'x': // hexadecimal
cnt = 2; goto hexinp;
case 'u': // unicode 16bit
cnt = 4; goto hexinp;
case 'U': // unicode 32bit
cnt = 8;
hexinp:
n = 0;
for ( i = 0; i < cnt; i ++ ) {
if ( !isxdigit(* ++p) ) return *p;
(*count) ++;
n = (n << 4) | hex_digit_val(*p);
}
return n;
}
return c;
}
/* -------------------------------------------------------------------------------- */
// utility function, removes all spaces from string src
// returns a newly allocated string with the results
char *CExp::remove_spaces(const char *src) const {
char *base = strdup(src);
char *p = base;
char *d, *dest = (char *) malloc(strlen(base)+1);
int dq = 0, sq = 0;
d = dest;
while ( *p ) {
if ( *p == '\'' ) {
// toggle single quotes block
sq = !sq;
*d ++ = *p ++;
continue;
}
if ( sq ) {
// inside single quotes block
*d ++ = *p ++;
continue;
}
if ( *p == '\\' ) {
// escape character
size_t count;
p ++;
*d ++ = c_esc_char(p, &count);
p += count;
continue;
}
if ( *p == '$' ) {
// environment variable or shell command
char buf[1024], *t = buf, *e = NULL;
FILE *fp;
p ++;
if ( *p == '{' ) {
p ++;
while ( *p && *p != '}' && (t - buf) < 1023 )
*t ++ = *p ++;
*t = '\0';
if ( *p ) p ++;
if ( getenv(buf) )
e = strdup(getenv(buf));
}
else if ( *p == '(' ) {
p ++;
while ( *p && *p != ')' && (t - buf) < 1023 )
*t ++ = *p ++;
*t = '\0';
if ( *p ) p ++;
// run command
if ( (fp = popen(buf, "r")) != NULL ) {
char line[1024];
while ( fgets(line, 1024, fp) ) {
if ( e ) {
e = (char *) realloc(e, strlen(e) + strlen(line) + 1);
strcat(e, line);
}
else {
e = (char *) malloc(strlen(line) + 1);
strcpy(e, line);
}
}
if ( e ) {
size_t len = strlen(e);
while ( *e && e[-- len] == '\n' )
e[len] = '\0';
}
pclose(fp);
}
}
else if ( isalpha(*p) || *p == '_' ) {
while ( (isalnum(*p) || *p == '_') && (t - buf) < 1023 )
*t ++ = *p ++;
*t = '\0';
if ( getenv(buf) )
e = strdup(getenv(buf));
}
else {
// itsnt valid
p --; // back, point to '$'
goto noenv_var;
}
// copy the result
if ( e ) {
/* copy to destination buffer
if ( strlen(buf) < strlen(e) ) {
size_t pos = d - dest;
*d = '\0';
dest = (char *) realloc(dest, strlen(dest) + strlen(e) + 1);
d = dest + pos;
}
while ( *e )
*d ++ = *e ++; */
/* copy to source buffer */
size_t ppos = p - base;
base = (char *) realloc(base, strlen(base) + strlen(e) + 1);
p = base + ppos;
memcpy(base+ppos+strlen(e), p, strlen(p)+1);
memcpy(base+ppos, e, strlen(e));
p = base + ppos;
// resize destination
size_t dpos = d - dest;
*d = '\0';
dest = (char *) realloc(dest, strlen(dest) + strlen(p) + 1);
d = dest + dpos;
// free $$ results buffer
free(e);
}
continue;
}
noenv_var:
if ( *p == '"' ) {
// toggle real_t quotes block
dq = !dq;
*d ++ = *p ++;
continue;
}
if ( dq ) {
// inside real_t quotes block
*d ++ = *p ++;
continue;
}
if ( isspace(*p) )
p ++;
else
*d ++ = *p ++;
}
*d = '\0';
free(base);
return dest;
}
// --------------------------------------------------------------------------------
/* static */ int CExp::eval(const vector<CVar>& args, CVar *result) {
CExp cexp;
if ( args.size() != 1 ) throw runtime_error("eval(): wrong number of parameters");
if ( args[0].type != v_str ) throw runtime_error("eval(): parameter is not string");
try { return cexp.evaluate(args[0].gets().c_str(), result); }
catch ( ... ) { throw; }
}
CExp::CExp() {
CVar r;
verbose = 0;
error_code = E_SUCCESS;
srand(clock() % RAND_MAX);
// add constants
setvar("pi", M_PI);
setvar("e", M_E);
setvar("phi", M_PHI);
setvar("log2e", M_LOG2E);
setvar("log10e", M_LOG10E);
setvar("ln2", M_LN2);
setvar("ln10", M_LN10);
// add default functions
add_func("abs", fabs);
add_func("dim", fdim);
add_func("sin", sin);
add_func("sinh", sinh);
add_func("cos", cos);
add_func("cosh", cosh);
add_func("tan", tan);
add_func("tanh", tanh);
add_func("log10", log10);
add_func("log", log);
add_func("exp", exp);
add_func("log2", log2);
add_func("exp2", exp2);
add_func("asin", asin);
add_func("asinh", asinh);
add_func("acos", acos);
add_func("acosh", acosh);
add_func("atan", atan);
add_func("atanh", atanh);
add_func("atan2", atan2);
add_func("pow", pow);
add_func("sqrt", sqrt);
add_func("cbrt", cbrt);
add_func("rint", rint);
add_func("round", round);
add_func("trunc", trunc);
add_func("floor", floor);
add_func("ceil", ceil);
add_func("mod", fmod);
add_func("rem", remainder);
add_func("hypot", hypot);
add_func("deg", deg);
add_func("rad", rad);
add_func("sgn", sgn);
add_func("rnd", rnd);
add_func("inv", 1, inv);
add_func("t", 1, transp);
add_func("i", 1, identity);
add_func("det", 1, det);
add_func("eval", 1, eval);
add_func("min", -1, vmin);
add_func("max", -1, vmax);
add_func("pf", -1, pf);
add_func("if", 3, iff);
}
void CExp::add_func(const char *name, real_t (*f)(real_t)) {
cexp_func_t p = {};
p.n_args = 1;
p.std_math_1 = f;
funcs.insert_or_assign(name, p);
}
void CExp::add_func(const char *name, real_t (*f)(real_t,real_t)) {
cexp_func_t p = {};
p.n_args = 2;
p.std_math_2 = f;
funcs.insert_or_assign(name, p);
}
void CExp::add_func(const char *name, int n, int (*f)(const vector<CVar>& args, CVar *result)) {
cexp_func_t p = {};
p.n_args = n;
p.custom_N = f;
funcs.insert_or_assign(string(name), p);
}
// returns pointer to the variable `name`
const CVar *CExp::getvar_p(const char *name, const char *prop) const {
auto it_var = vars.find(name);
if ( it_var != vars.end() ) {
const CVar *v = &(it_var->second);
#ifdef CEXP_USE_PROPS
if ( prop ) {
auto it = v->props.find(prop);
if ( it != v->props.end() )
return &(it->second);
}
else
#endif
return v;
}
return NULL;
}
CVar *CExp::getvar_p(const char *name, const char *prop) {
auto it_var = vars.find(name);
if ( it_var != vars.end() ) {
CVar *v = &(it_var->second);
#ifdef CEXP_USE_PROPS
if ( prop ) {
auto it = v->props.find(prop);
if ( it != v->props.end() )
return &(it->second);
}
else
#endif
return v;
}
return NULL;
}
CVar *CExp::mkvar_p(const char *name, const char *prop) {
auto it_var = vars.find(name);
if ( it_var == vars.end() ) {
if ( vars.insert_or_assign(name, CVar()).second )
it_var = vars.find(name);
}
#ifdef CEXP_USE_PROPS
if ( prop ) {
CVar *v = &(it_var->second);
auto it_pro = v->props.find(prop);
if ( it_pro == v->props.end() ) {
if ( v->props.insert_or_assign(prop, CVar()).second )
it_pro = v->props.find(prop);
}
return &(it_pro->second);
}
#endif
return &(it_var->second);
}
const CVar& CExp::getvar(const char *name, const char *prop) const {
auto it_var = vars.find(name);
if ( it_var != vars.end() ) {
#ifdef CEXP_USE_PROPS
const CVar *v = &(it_var->second);
if ( prop ) {
auto it = v->props.find(prop);
if ( it != v->props.end() )
return it->second;
throw runtime_error("property not found"); // error
}
#endif
return it_var->second;
}
throw runtime_error("variable not found"); // error
}
CVar& CExp::getvar(const char *name, const char *prop) {
auto it_var = vars.find(name);
if ( it_var != vars.end() ) {
#ifdef CEXP_USE_PROPS
CVar *v = &(it_var->second);
if ( prop ) {
auto it = v->props.find(prop);
if ( it != v->props.end() )
return it->second;
throw runtime_error("property not found"); // error
}
#endif
return it_var->second;
}
throw runtime_error("variable not found"); // error
}
// creates a new variable with `name` and the values of `v` and store it
// in the engine. returns pointer to the newly created variable.
void CExp::setvar(const char *name, const CVar& data)
{ vars.insert_or_assign(name, data); }
void CExp::setvar(const char *name, real_t n)
{ vars.insert_or_assign(name, CVar(n)); }
void CExp::setvar(const char *name, int_t n)
{ vars.insert_or_assign(name, CVar(n)); }
void CExp::setvar(const char *name, const char *s)
{ vars.insert_or_assign(name, CVar(s)); }
// removes the variable `name` from the engine's memory.
bool CExp::unsetvar(const char *name) {
auto it = vars.find(name);
if ( it != vars.end() ) {
vars.erase(it);
return true;
}
return false;
}
// handle errors
void CExp::set_error(cexp_error_t code, const char *fmt, ...) {
int n;
size_t size;
char *buf;
va_list ap;
error_code = code;
va_start(ap, fmt);
n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
assert(n > 0);
if ( n < 0 ) return;
size = (size_t) n + 1;
buf = (char *) malloc(size);
assert(buf);
va_start(ap, fmt);
n = vsnprintf(buf, size, fmt, ap);
va_end(ap);
assert(n > 0);
error_message = string(buf);
free(buf);
}
// returns true if `c` belongs to operators class
int CExp::is_opchar(int c) const {
if ( isgraph(c) && !isalnum(c)
&& c != '_' && c < 127
&& c != ',' && c != '"' && c != '\'' )
return 1;
return 0;
}
// returns true if `c` belongs to keyword class
int CExp::is_keyword(int c) const {
if ( isalnum(c) || (c == '_') )
return 1;
return 0;
}
// find precedence of operators.
int CExp::precedence(int op) const {
switch ( op ) {
case OP2('|','|'):
return 2;
case OP2('&','&'):
return 3;
case '|':
return 4;
case '#':
return 5;
case '&':
return 6;
case OP2('=','='):
case OP2('!','='):
return 7;
case OP2('>','='):
case OP2('<','='):
case '>': case '<':
return 8;
case OP2('<','<'):
case OP2('>','>'):
return 9;
case '+': case '-':
return 10;
case '*': case '/': case '%':
return 11;
case '^':
case OP2('*','*'):
return 12;
case '[': case '(': case '{':
case ']': case ')': case '}': case ',':
return 200;
default:
if ( op & UNARY_BIT )
return 100;
}
return 0;
}
// perform arithmetic operations.
CVar CExp::apply_op(const CVar& a, const CVar& b, int op) const {
if ( verbose )
printf("eval: apply operator %g %d %g\n", a.getf(), op, b.getf());
try {
switch ( op ) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '%': return a % b;
case '^': case OP2('*','*'):
if ( a.type == v_matrix )
return (a.m.power(b.geti()));
if ( a.type <= v_str && b.type <= v_str )
return pow(a.getf(), b.getf());
throw runtime_error("Type mismatch");
case OP2('<','<'): return (a.geti() << b.geti());
case OP2('>','>'): return (a.geti() >> b.geti());
case OP2('=','='): return ((a.getf() == b.getf()) ? 1 : 0);
case OP2('!','='): return ((a.getf() != b.getf()) ? 1 : 0);
case OP2('>','='): return ((a.getf() >= b.getf()) ? 1 : 0);
case OP2('<','='): return ((a.getf() <= b.getf()) ? 1 : 0);
case '>': return ((a.getf() > b.getf()) ? 1 : 0);
case '<': return ((a.getf() < b.getf()) ? 1 : 0);
case '&': return (a.geti() & b.geti());
case '|': return (a.geti() | b.geti());
case '#': return (a.geti() ^ b.geti());
case OP2('&','&'):
if ( a.getf() && b.getf() ) return 1;
return 0;
case OP2('|','|'):
if ( a.getf() || b.getf() ) return 1;
return 0;
}
}
catch(...) { throw; };
throw runtime_error("Missing operator (contact to creator)");
}
// part of find_op
int CExp::find_opcode(int op) const {
switch ( op ) {
case OP2('*','*'):
case OP2('=','='): case OP2('!','='):
case OP2('>','='): case OP2('<','='):
case OP2('<','<'): case OP2('>','>'):
case OP2('&','&'): case OP2('|','|'):
case '+': case '-':
case '*': case '/': case '%': case '^':
case '>': case '<':
case '&': case '|': case '#':
return op;
}
return 0;
}
// part of find_op; assignment operators
int CExp::find_assign_opcode(int op) const {
switch ( op ) {
// case OP3('<','<','='): case OP3('>','>','='):
case OP2('+','='): case OP2('-','='):
case OP2('*','='): case OP2('/','='): case OP2('%','='):
// case OP2('&','='): case OP2('|','='): case OP2('#','='):
case OP2(':','='): case '=':
return op;
}
return 0;
}
// returns the operator if src points to operator (1-3 chars)
// otherwise returns 0
int CExp::find_op(const char *src, bool is_assign_op) const {
int op, r = 0;
if ( src[0] && is_opchar(src[1]) && is_opchar(src[2]) ) {
op = OP3(src[0], src[1], src[2]);
r = ( is_assign_op ) ? find_assign_opcode(op) : find_opcode(op);
}
if ( r == 0 && src[0] && is_opchar(src[1]) ) {
op = OP2(src[0], src[1]);
r = ( is_assign_op ) ? find_assign_opcode(op) : find_opcode(op);
}
if ( r == 0 && src[0] ) {
op = src[0];
r = ( is_assign_op ) ? find_assign_opcode(op) : find_opcode(op);
}
return r;
}
// perform unary operations.
CVar CExp::apply_unary(const CVar& v, int op) const {
op &= UNARY_MASK;
switch ( op ) {
case '+':
return v;
case '-':
if ( v.type == v_int )
return -v.geti();
if ( v.type <= v_str )
return -v.getf();
if ( v.type == v_matrix )
return -v.m;
break;
case '!':
if ( v.type == v_int )
return ( v.geti() ) ? 0 : -1;
if ( v.type <= v_str )
return ( v.getf() ) ? 0 : -1;
break;
case '~':
if ( v.type <= v_str )
return ~v.geti();
break;
}
throw runtime_error("Type mismatch");
}
// returns the prefix [unary] operator if src points to operator (1-3 chars)
// otherwise returns 0
int CExp::find_unary(const char *src) const {
int op = src[0];
switch ( op ) {
case '+': case '-':
case '!': case '~':
return op;
}
return 0;
}
// parse number and return it to `val`
// on input `p` must points the first digit.
const char *CExp::parse_num(const char *p, real_t *val) const {
*val = 0.0;
if ( *p == '0' ) {
if ( *(p+1) == '.' ) { // decimal / float
p += 2;
real_t l = 1;
while ( isdigit(*p) ) {
*val += (*p - '0') * (l *= 0.1);
p ++;
}
// exponent format
if ( toupper(p[0]) == 'E' && (p[1] == '+' || p[1] == '-') ) {
char buf[64], *t = buf;
p ++; // E
*t ++ = *p ++; // sign
if ( *p == '0' ) p ++; // at least two digit, the first may be zero
while ( isdigit(*p) && (t - buf) < 63 )
*t ++ = *p ++;
*t = '\0';
*val = *val * pow(10, atoi(buf));
}
}
else if ( *(p+1) == 'x' ) { // hexadecimal
p += 2;
while ( isxdigit(*p) ) {
if ( *p >= 'A' )
*val = (*val * 16) + ((toupper(*p) - 'A') + 10);
else
*val = (*val * 16) + (*p - '0');
p ++;
}
}
else { // octal
p ++;
while ( isoctal(*p) ) {
*val = (*val * 8) + (*p - '0');
p ++;
}
}
}
else { // decimal
int intpart = 1;
real_t l = 1;
while ( isdigit(*p) || *p == '.' ) {
if ( *p == '.' )
intpart = 0;
else if ( intpart )
*val = (*val * 10) + (*p - '0');
else
*val += (*p - '0') * (l *= 0.1);
p ++;
}
// exponent format
if ( toupper(p[0]) == 'E' && (p[1] == '+' || p[1] == '-') ) {
char buf[64], *t = buf;
p ++; // E
*t ++ = *p ++; // sign
if ( *p == '0' ) p ++; // at least two digit, the first may be zero
while ( isdigit(*p) && (t - buf) < 63 )
*t ++ = *p ++;
*t = '\0';
*val = *val * pow(10, atoi(buf));
}
}
return p;
}
// parse function's parameters, execute and return the result.
// on input `p` must points to '('
const char *CExp::parse_function(const char *p, const char *name, CVar *r) {
cexp_func_t *f;
p ++;
auto it = funcs.find(name);
if ( it != funcs.end() ) {
vector<CVar> args;
f = &(it->second);
if ( f->n_args >= 0 ) {
for ( int i = 0; i < f->n_args; i ++ ) {
p = evaluate_p(p, r, ((i == f->n_args - 1) ? ")" : ","));
args.push_back(*r);
}
}
else { // unlimited mode
do {
p = evaluate_p(p, r, ",)");
args.push_back(*r);
} while ( *p && *(p-1) != ')' );
}
if ( f->std_math_1 )
r->setf(f->std_math_1(args[0].getf()));
else if ( f->std_math_2 )
r->setf(f->std_math_2(args[0].getf(), args[1].getf()));
else if ( f->custom_N ) {
int e;
if ( (e = f->custom_N(args, r)) != 0 )
set_error(E_FUNC_ERROR, "Custom function `%s` error: `%d`", name, e);
}
}
else
set_error(E_FUNC_NOT_FOUND, "Function `%s` not found", name);
return p;
}
// parse string
const char *CExp::parse_string(const char *p, CVar *val) const {
char *dest = (char *) malloc(strlen(p) + 1);
char *d = dest;
char q = *p ++;
while ( *p ) {
if ( *p == q )
break;
*d ++ = *p ++;
}
*d = '\0';
if ( *p ) p ++;
val->sets(dest);
free(dest);
return p;
}
// store the keyword to buf of len
const char *CExp::parse_keyword(const char *p, char *kwbuf, size_t len) const {
char *t = kwbuf;
len --;
while ( is_keyword(*p) && (size_t)(t - kwbuf) < len )
*t ++ = *p ++;
*t = '\0';
return p;
}
// assign to value to variable and/or property
// op is the assignment operator ('=', '+='. etc)
// returns the value of the variable/property
void CExp::var_assign(int op, const char *name, const char *prop, CVar &v) {
CVar *data = NULL;
if ( (data = getvar_p(name, prop)) == NULL )
data = mkvar_p(name, prop);
//
switch ( op ) {
case '=':
case OP2(':','='):
break;
case OP2('+','='):
if ( data->type == v_int && v.type == v_int )
v.seti(data->geti() + v.geti());
else if ( data->type == v_matrix && v.type <= v_str )
v.setm(data->m + v.getf());
else if ( data->type == v_matrix && v.type == v_matrix )
v.setm(data->m + v.m);
else if ( data->type == v_str && v.type == v_str )
v.sets(data->s + v.s);
else if ( v.type <= v_str )
v.setf(data->getf() + v.getf());
else
set_error(E_TYPE_MM, "Type mismatch");
break;
case OP2('-','='):
if ( data->type == v_int && v.type == v_int )
v.setf(data->getf() - v.getf());
else if ( data->type == v_matrix && v.type <= v_str )
v.setm(data->m - v.getf());
else if ( data->type == v_matrix && v.type == v_matrix )
v.setm(data->m - v.m);
else if ( v.type <= v_str )
v.setf(-v.getf());
else
set_error(E_TYPE_MM, "Type mismatch");
break;
case OP2('*','='):
if ( data->type == v_int && v.type == v_int )
v.seti(data->geti() * v.geti());
else if ( data->type == v_matrix && v.type <= v_str )
v.setm(data->m * v.getf());
else if ( data->type == v_matrix && v.type == v_matrix )
v.setm(data->m * v.m);
else if ( data->type <= v_str && v.type <= v_str )
v.setf(data->getf() * v.getf());
else
set_error(E_TYPE_MM, "Type mismatch");
break;
case OP2('/','='):
if ( v.getf() ) {
if ( data->type <= v_str && v.type <= v_str )
v.setf(data->getf() / v.getf());
else if ( data->type == v_matrix && v.type <= v_str )
v.setm(data->m / v.getf());
else
set_error(E_TYPE_MM, "Type mismatch");
}
else
set_error(E_DIV_ZERO, "Division by zero");