FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gnuplot/src/parse.c at master · gnuplot/gnuplot · GitHub
gnuplot
gnuplot
Repository navigation
Code
Pull requests
2
(2)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
gnuplot
/
src
/
parse.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1898 lines (1645 loc) · 50.6 KB
Breadcrumbs
gnuplot
/
src
/
parse.c
Copy path
File metadata and controls
1898 lines (1645 loc) · 50.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* GNUPLOT - parse.c */
/*[
* Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the complete modified source code. Modifications are to
* be distributed as patches to the released version. Permission to
* distribute binaries produced by compiling modified sources is granted,
* provided you
* 1. distribute the corresponding source modifications from the
* released version in the form of a patch file along with the binaries,
* 2. add special version identification to distinguish your version
* in addition to the base release version number,
* 3. provide your name and address as the primary contact for the
* support of your modified version, and
* 4. retain our contact information in regard to use of the base
* software.
* Permission to distribute the released version of the source code along
* with corresponding source modifications in the form of a patch file is
* granted with same provisions 2 through 4 for binary distributions.
*
* This software is provided "as is" without express or implied warranty
* to the extent permitted by applicable law.
]*/
#include
"parse.h"
#include
"alloc.h"
#include
"command.h"
#include
"datablock.h"
#include
"eval.h"
#include
"util.h"
/* Protection mechanism for trying to parse a string followed by a + or - sign.
* Also suppresses an undefined variable message if an unrecognized token
* is encountered during try_to_get_string().
*/
TBOOLEAN
string_result_only
=
FALSE;
static
int
parse_recursion_level
;
/* Exported globals: the current 'dummy' variable names */
char
c_dummy_var
[
MAX_NUM_VAR
][
MAX_ID_LEN
+
1
];
char
set_dummy_var
[
MAX_NUM_VAR
][
MAX_ID_LEN
+
1
]
=
{
"x"
,
"y"
};
int
fit_dummy_var
[
MAX_NUM_VAR
];
TBOOLEAN
scanning_range_in_progress
=
FALSE;
/* This is used by plot_option_using() */
int
at_highest_column_used
=
-1
;
/* This is checked by df_readascii() */
TBOOLEAN
parse_1st_row_as_headers
=
FALSE;
/* This is used by df_open() and df_readascii() */
udvt_entry
*
df_array
=
NULL
;
/* Iteration structures used for bookkeeping */
t_iterator
*
plot_iterator
=
NULL
;
t_iterator
*
set_iterator
=
NULL
;
t_iterator
*
print_iterator
=
NULL
;
/* Internal prototypes: */
static
void
convert
(
struct
value
*
,
int
);
static
void
extend_at
(
void
);
static
union
argument
*
add_action
(
enum
operators
sf_index
);
static
void
parse_expression
(
void
);
static
void
accept_logical_OR_expression
(
void
);
static
void
accept_logical_AND_expression
(
void
);
static
void
accept_inclusive_OR_expression
(
void
);
static
void
accept_exclusive_OR_expression
(
void
);
static
void
accept_AND_expression
(
void
);
static
void
accept_equality_expression
(
void
);
static
void
accept_relational_expression
(
void
);
static
void
accept_bitshift_expression
(
void
);
static
void
accept_additive_expression
(
void
);
static
void
accept_multiplicative_expression
(
void
);
static
void
parse_primary_expression
(
void
);
static
void
parse_conditional_expression
(
void
);
static
void
parse_logical_OR_expression
(
void
);
static
void
parse_logical_AND_expression
(
void
);
static
void
parse_inclusive_OR_expression
(
void
);
static
void
parse_exclusive_OR_expression
(
void
);
static
void
parse_AND_expression
(
void
);
static
void
parse_equality_expression
(
void
);
static
void
parse_relational_expression
(
void
);
static
void
parse_bitshift_expression
(
void
);
static
void
parse_additive_expression
(
void
);
static
void
parse_multiplicative_expression
(
void
);
static
void
parse_unary_expression
(
void
);
static
void
parse_sum_expression
(
void
);
static
int
parse_assignment_expression
(
void
);
static
int
parse_array_assignment_expression
(
void
);
static
void
parse_function_block
(
void
);
static
void
set_up_columnheader_parsing
(
struct
at_entry
*
previous
);
static
TBOOLEAN
no_iteration
(
t_iterator
*
);
static
void
reevaluate_iteration_limits
(
t_iterator
*
iter
);
static
void
reset_iteration
(
t_iterator
*
iter
);
/* Internal variables: */
static
struct
at_type
*
at
=
NULL
;
static
int
at_size
=
0
;
static
void
convert
(
struct
value
*
val_ptr
,
int
t_num
)
{
*
val_ptr
=
token
[
t_num
].
l_val
;
}
intgr_t
int_expression
()
{
return
(
intgr_t
)
real_expression
();
}
double
real_expression
()
{
double
result
;
struct
value
a
;
result
=
real
(
const_express
(
&
a
));
free_value
(
&
a
);
return
result
;
}
void
parse_reset_after_error
()
{
string_result_only
=
FALSE;
parse_recursion_level
=
0
;
}
/* JW 20051126:
* Wrapper around const_express() called by try_to_get_string().
* Disallows top level + and - operators.
* This enables things like set xtics ('-\pi' -pi, '-\pi/2' -pi/2.)
*/
struct
value
*
const_string_express
(
struct
value
*
valptr
)
{
string_result_only
=
TRUE;
const_express
(
valptr
);
string_result_only
=
FALSE;
return
(
valptr
);
}
/*
* const_express() may return a value of any type.
* NB: If the returned type is ARRAY, the caller must check for
* the TEMP_ARRAY flag and either free the structure after
* immediate use or call make_array_permanent().
*/
struct
value
*
const_express
(
struct
value
*
valptr
)
{
int
tkn
=
c_token
;
if
(
END_OF_COMMAND
)
int_error
(
c_token
,
"constant expression required"
);
/* div - no dummy variables in a constant expression */
dummy_func
=
NULL
;
evaluate_at
(
temp_at
(),
valptr
);
/* run it and send answer back */
if
(
undefined
) {
int_error
(
tkn
,
"undefined value"
);
}
return
(
valptr
);
}
/* Used by plot2d/plot3d/stats/fit:
* Parse an expression that may return a filename string, a datablock name,
* a constant, or a dummy function using dummy variables x, y, ...
* If any dummy variables are present, set (*atptr) to point to an action table
* corresponding to the parsed expression, and return NULL.
* Otherwise evaluate the expression and return a string if there is one.
* The return value "str" and "*atptr" both point to locally-managed memory,
* which must not be freed by the caller!
*/
char
*
string_or_express
(
struct
at_type
*
*
atptr
)
{
int
i
;
TBOOLEAN
has_dummies
;
static
char
*
array_placeholder
=
"@@"
;
static
char
*
str
=
NULL
;
free
(
str
);
str
=
NULL
;
df_array
=
NULL
;
if
(
atptr
)
*
atptr
=
NULL
;
if
(
END_OF_COMMAND
)
int_error
(
c_token
,
"expression expected"
);
/* distinguish data blocks from function blocks */
if
(
equals
(
c_token
,
"$"
)) {
int
save_token
=
c_token
;
char
*
name
=
parse_datablock_name
();
udvt_entry
*
udv
=
get_udv_by_name
(
name
);
if
(
udv
&&
(
udv
->
udv_value
.
type
==
FUNCTIONBLOCK
))
c_token
=
save_token
;
else
return
name
;
}
/* special keywords */
if
(
equals
(
c_token
,
"keyentry"
))
return
NULL
;
if
(
isstring
(
c_token
)
&&
(
str
=
try_to_get_string
()))
return
str
;
/* If this is a bare array name for an existing array, store a pointer */
/* for df_open() to use. "@@" is a magic pseudo-filename passed to */
/* df_open() that tells it to use the stored pointer. */
if
(
type_udv
(
c_token
)
==
ARRAY
&&
!
equals
(
c_token
+
1
,
"["
)) {
df_array
=
add_udv
(
c_token
++
);
return
array_placeholder
;
}
/* parse expression */
temp_at
();
/* check if any dummy variables are used */
has_dummies
=
FALSE;
for
(
i
=
0
;
i
<
at
->
a_count
;
i
++
) {
enum
operators
op_index
=
at
->
actions
[
i
].
index
;
if
(
op_index
==
PUSHD1
||
op_index
==
PUSHD2
||
op_index
==
PUSHD
||
op_index
==
SUM
) {
has_dummies
=
TRUE;
break
;
}
}
if
(!
has_dummies
) {
/* no dummy variables: evaluate expression */
struct
value
val
;
evaluate_at
(
at
,
&
val
);
if
(!
undefined
&&
val
.
type
==
STRING
) {
/* prevent empty string variable from treated as special file '' or "" */
if
(
*
val
.
v
.
string_val
==
'\0'
) {
free
(
val
.
v
.
string_val
);
str
=
strdup
(
" "
);
}
else
{
str
=
val
.
v
.
string_val
;
}
}
if
(!
undefined
&&
val
.
type
==
ARRAY
) {
/* This must be an array slice or a function returning an array,
* because otherwise we would have caught it above.
*/
df_array
=
add_udv_by_name
(
"GPVAL_PLOT_ARRAY"
);
free_value
(
&
(
df_array
->
udv_value
));
make_array_permanent
(
&
val
);
df_array
->
udv_value
=
val
;
return
array_placeholder
;
}
}
/* The function for a function plot will be stored in the plot header. */
if
(
atptr
&&
!
str
) {
size_t
len
=
sizeof
(
struct
at_type
)
+
(
at
->
a_count
-
MAX_AT_LEN
)
*
sizeof
(
struct
at_entry
);
*
atptr
=
(
struct
at_type
*
)
realloc
(
at
,
len
);
at
=
NULL
;
}
return
str
;
}
/* build an action table and return its pointer, but keep a pointer in at
* so that we can free it later if the caller hasn't taken over management
* of this table.
*/
struct
at_type
*
temp_at
()
{
if
(
at
!=
NULL
)
free_at
(
at
);
at
=
(
struct
at_type
*
)
gp_alloc
(
sizeof
(
struct
at_type
),
"action table"
);
memset
(
at
,
0
,
sizeof
(
*
at
));
/* reset action table !!! */
at_size
=
MAX_AT_LEN
;
parse_recursion_level
=
0
;
parse_expression
();
return
(
at
);
}
/* build an action table, put it in dynamic memory, and return its pointer */
struct
at_type
*
perm_at
()
{
struct
at_type
*
at_ptr
;
size_t
len
;
(
void
)
temp_at
();
len
=
sizeof
(
struct
at_type
)
+
(
at
->
a_count
-
MAX_AT_LEN
)
*
sizeof
(
struct
at_entry
);
at_ptr
=
(
struct
at_type
*
)
gp_realloc
(
at
,
len
,
"perm_at"
);
at
=
NULL
;
/* invalidate at pointer */
return
(
at_ptr
);
}
/* Create an action table that describes a call to column("string"). */
/* This is used by plot_option_using() to handle 'plot ... using "string"' */
struct
at_type
*
create_call_column_at
(
char
*
string
)
{
struct
at_type
*
at
=
gp_alloc
(
sizeof
(
int
)
+
2
*
sizeof
(
struct
at_entry
),
""
);
at
->
a_count
=
2
;
at
->
actions
[
0
].
index
=
PUSHC
;
at
->
actions
[
0
].
arg
.
j_arg
=
0
;
at
->
actions
[
0
].
arg
.
v_arg
.
type
=
STRING
;
at
->
actions
[
0
].
arg
.
v_arg
.
v
.
string_val
=
string
;
at
->
actions
[
1
].
index
=
COLUMN
;
at
->
actions
[
1
].
arg
.
j_arg
=
0
;
return
(
at
);
}
/* Create an action table that describes a call to columnhead(-1). */
/* This is substituted for the bare keyword "colummhead" */
struct
at_type
*
create_call_columnhead
()
{
struct
at_type
*
at
=
gp_alloc
(
sizeof
(
int
)
+
2
*
sizeof
(
struct
at_entry
),
""
);
at
->
a_count
=
2
;
at
->
actions
[
0
].
index
=
PUSHC
;
at
->
actions
[
0
].
arg
.
j_arg
=
0
;
at
->
actions
[
0
].
arg
.
v_arg
.
type
=
INTGR
;
at
->
actions
[
0
].
arg
.
v_arg
.
v
.
int_val
=
-1
;
at
->
actions
[
1
].
index
=
COLUMNHEAD
;
at
->
actions
[
1
].
arg
.
j_arg
=
0
;
return
(
at
);
}
static
void
extend_at
()
{
size_t
newsize
=
sizeof
(
struct
at_type
)
+
at_size
*
sizeof
(
struct
at_entry
);
at
=
gp_realloc
(
at
,
newsize
,
"extend_at"
);
at_size
+=
MAX_AT_LEN
;
FPRINTF
((
stderr
,
"Extending at size to %d\n"
,
at_size
));
}
#ifdef
USE_FUNCTIONBLOCKS
/* The f_eval operation supporting function blocks restarts command parsing
* inside an existing evaluation stack.
* In order to not corrupt that existing stack, we must save its action table,
* hide it from the parser, and allow f_eval to restore it afterwards.
*/
void
cache_at
(
struct
at_type
*
*
shadow_at
,
int
*
shadow_at_size
)
{
*
shadow_at
=
at
;
*
shadow_at_size
=
at_size
;
at
=
NULL
;
}
void
uncache_at
(
struct
at_type
*
shadow_at
,
int
shadow_at_size
)
{
free_at
(
at
);
at
=
shadow_at
;
at_size
=
shadow_at_size
;
}
#endif
/* USE_FUNCTIONBLOCKS */
/* Add function number <sf_index> to the current action table */
static
union
argument
*
add_action
(
enum
operators
sf_index
)
{
if
(
at
->
a_count
>=
at_size
) {
extend_at
();
}
at
->
actions
[
at
->
a_count
].
index
=
sf_index
;
return
(
&
(
at
->
actions
[
at
->
a_count
++
].
arg
));
}
/* For external calls to parse_expressions()
* parse_recursion_level is expected to be 0 */
static
void
parse_expression
()
{
/* full expressions */
if
(
parse_assignment_expression
())
return
;
parse_recursion_level
++
;
accept_logical_OR_expression
();
parse_conditional_expression
();
parse_recursion_level
--
;
}
static
void
accept_logical_OR_expression
()
{
/* ? : expressions */
accept_logical_AND_expression
();
parse_logical_OR_expression
();
}
static
void
accept_logical_AND_expression
()
{
accept_inclusive_OR_expression
();
parse_logical_AND_expression
();
}
static
void
accept_inclusive_OR_expression
()
{
accept_exclusive_OR_expression
();
parse_inclusive_OR_expression
();
}
static
void
accept_exclusive_OR_expression
()
{
accept_AND_expression
();
parse_exclusive_OR_expression
();
}
static
void
accept_AND_expression
()
{
accept_equality_expression
();
parse_AND_expression
();
}
static
void
accept_equality_expression
()
{
accept_relational_expression
();
parse_equality_expression
();
}
static
void
accept_relational_expression
()
{
accept_bitshift_expression
();
parse_relational_expression
();
}
static
void
accept_bitshift_expression
()
{
accept_additive_expression
();
parse_bitshift_expression
();
}
static
void
accept_additive_expression
()
{
accept_multiplicative_expression
();
parse_additive_expression
();
}
static
void
accept_multiplicative_expression
()
{
parse_unary_expression
();
/* - things */
parse_multiplicative_expression
();
/* * / % */
}
static
int
parse_assignment_expression
()
{
/* Check for assignment operator Var = <expr> */
if
(
isletter
(
c_token
)
&&
equals
(
c_token
+
1
,
"="
)) {
union
argument
*
foo
;
char
*
varname
=
NULL
;
/* We're going to push the name of the variable receiving a new value,
* but if it's a dummy variable in a function definition that can't work.
*/
if
(
dummy_func
) {
int
i
;
for
(
i
=
0
;
i
<
MAX_NUM_VAR
;
i
++
) {
if
(
equals
(
c_token
,
c_dummy_var
[
i
]))
int_error
(
c_token
,
"Cannot assign to a dummy variable"
);
}
}
/* Push the name of the variable */
foo
=
add_action
(
PUSHC
);
m_capture
(
&
varname
,
c_token
,
c_token
);
foo
->
v_arg
.
type
=
STRING
;
foo
->
v_arg
.
v
.
string_val
=
varname
;
/* push the expression whose value it will get */
c_token
+=
2
;
parse_expression
();
/* push the actual assignment operation */
foo
=
add_action
(
ASSIGN
);
foo
->
v_arg
.
type
=
0
;
/* could be anything but ARRAY */
return
1
;
}
/* Check for assignment to an array element Array[<expr>] = <expr> */
if
(
isletter
(
c_token
)
&&
equals
(
c_token
+
1
,
"["
)) {
if
(
parse_array_assignment_expression
())
return
1
;
}
return
0
;
}
/*
* If an array assignment is the first thing on a command line it is handled by
* the separate routine is_array_assignment().
* Here we catch assignments that are embedded in an expression.
* Examples:
* print A[2] = foo
* A[1] = A[2] = A[3] = 0
*/
static
int
parse_array_assignment_expression
()
{
/* Check for assignment to an array element Array[<expr>] = <expr> */
if
(
equals
(
c_token
+
1
,
"["
)) {
char
*
varname
=
NULL
;
union
argument
*
foo
;
int
save_action
,
save_token
;
TBOOLEAN
standard_at
;
int
i
;
/* Quick checks for the most common false positives */
/* i.e. other constructs that begin with "name[" */
/* FIXME: quicker than the full test below, but do we care? */
if
(
equals
(
c_token
+
3
,
"]"
)
&&
!
equals
(
c_token
+
4
,
"="
))
return
0
;
if
(
equals
(
c_token
+
3
,
":"
))
/* substring s[foo:baz] */
return
0
;
for
(
i
=
c_token
;
i
<
num_tokens
;
i
++
) {
if
(
equals
(
i
,
"]"
)
&&
equals
(
i
+
1
,
"="
))
break
;
}
if
(
i
==
num_tokens
)
return
0
;
/* Save state of the action table and the command line */
save_action
=
at
->
a_count
;
save_token
=
c_token
;
/* Get the array name */
m_capture
(
&
varname
,
c_token
,
c_token
);
/* push the index */
c_token
+=
2
;
parse_expression
();
/* Now it gets tricky. If the name we just saw is a dummy parameter
* rather than a true variable name we can't use the standard action table
* PUSH index; PUSH "name"; PUSH <value>; ASSIGN
* we must either treat this as an error or invent a new sequence
* PUSH index; PUSHDn; PUSH <value>; ASSIGN
* with corresponding code in f_assign() that recognizes it must replace
* the entry via a pointer to it in the array stored in dummy_var[].
*/
standard_at
=
TRUE;
if
(
dummy_func
) {
for
(
i
=
0
;
i
<
MAX_NUM_VAR
;
i
++
) {
if
(
equals
(
save_token
,
c_dummy_var
[
i
])) {
foo
=
add_action
(
PUSHC
);
foo
->
v_arg
.
type
=
INTGR
;
foo
->
v_arg
.
v
.
int_val
=
i
;
add_action
(
PUSHD
)
->
udf_arg
=
dummy_func
;
standard_at
=
FALSE;
break
;
}
}
}
if
(
standard_at
) {
/* push the array name */
foo
=
add_action
(
PUSHC
);
foo
->
v_arg
.
type
=
STRING
;
foo
->
v_arg
.
v
.
string_val
=
varname
;
}
else
{
free
(
varname
);
}
/* If this wasn't really an array element assignment, back out. */
if
(!
equals
(
c_token
,
"]"
)
||
!
equals
(
c_token
+
1
,
"="
)) {
for
(
i
=
save_action
;
i
<
at
->
a_count
;
i
++
) {
struct
at_entry
*
a
=
&
(
at
->
actions
[
i
]);
free_action_entry
(
a
);
}
c_token
=
save_token
;
at
->
a_count
=
save_action
;
return
0
;
}
/* Now we evaluate the expression whose value it will get */
c_token
+=
2
;
parse_expression
();
/* push the actual assignment operation */
foo
=
add_action
(
ASSIGN
);
/* This is a flag to indicate to f_assign that the assignment is
* to an element of an array, rather than to a named array variable.
* The ASSIGN action->v_arg is not itself an array so make sure
* no one will ever try to dereference it.
*/
foo
->
v_arg
.
type
=
ARRAY
;
foo
->
v_arg
.
v
.
value_array
=
NULL
;
return
1
;
}
return
0
;
}
/* add action table entries for primary expressions, i.e. either a
* parenthesized expression, a variable name, a numeric constant, a
* function evaluation, a power operator or postfix '!' (factorial)
* expression.
* Sep 2016 cardinality expression |Array| */
static
void
parse_primary_expression
()
{
if
(
equals
(
c_token
,
"("
)) {
c_token
++
;
parse_expression
();
/* Expressions may be separated by a comma */
while
(
equals
(
c_token
,
","
)) {
c_token
++
;
(
void
)
add_action
(
POP
);
parse_expression
();
}
if
(!
equals
(
c_token
,
")"
))
int_error
(
c_token
,
"')' expected"
);
c_token
++
;
}
else
if
(
equals
(
c_token
,
"$"
)
&&
equals
(
c_token
+
2
,
"("
)) {
parse_function_block
();
}
else
if
(
equals
(
c_token
,
"$"
)) {
struct
value
a
;
c_token
++
;
if
(!
isanumber
(
c_token
)) {
if
(
equals
(
c_token
+
1
,
"["
)) {
struct
udvt_entry
*
datablock_udv
;
c_token
--
;
datablock_udv
=
get_udv_by_name
(
parse_datablock_name
());
if
(!
datablock_udv
)
int_error
(
c_token
-
2
,
"No such datablock"
);
add_action
(
PUSH
)
->
udv_arg
=
datablock_udv
;
}
else
int_error
(
c_token
,
"Column number or datablock line expected"
);
}
else
{
convert
(
&
a
,
c_token
++
);
if
(
a
.
type
!=
INTGR
||
a
.
v
.
int_val
<
0
)
int_error
(
c_token
,
"Positive integer expected"
);
if
(
at_highest_column_used
<
a
.
v
.
int_val
)
at_highest_column_used
=
a
.
v
.
int_val
;
add_action
(
DOLLARS
)
->
v_arg
=
a
;
}
}
else
if
(
equals
(
c_token
,
"$#"
)) {
struct
value
a
=
{
INTGR
, {
DOLLAR_NCOLUMNS
}};
c_token
++
;
add_action
(
DOLLARS
)
->
v_arg
=
a
;
}
else
if
(
equals
(
c_token
,
"|"
)) {
struct
udvt_entry
*
udv
;
c_token
++
;
if
(
equals
(
c_token
,
"$"
)) {
udv
=
get_udv_by_name
(
parse_datablock_name
());
if
(!
udv
)
int_error
(
c_token
-
1
,
"no such datablock"
);
add_action
(
PUSH
)
->
udv_arg
=
udv
;
}
else
{
/* Allow array name as a dummy variable */
/* Give an error during evaluation if it isn't really an array */
parse_primary_expression
();
}
if
(!
equals
(
c_token
,
"|"
))
int_error
(
c_token
,
"'|' expected"
);
c_token
++
;
add_action
(
CARDINALITY
);
}
else
if
(
isanumber
(
c_token
)) {
union
argument
*
foo
=
add_action
(
PUSHC
);
convert
(
&
(
foo
->
v_arg
),
c_token
);
c_token
++
;
}
else
if
(
isletter
(
c_token
)) {
/* Found an identifier --- check whether its a function or a
* variable by looking for the parentheses of a function
* argument list */
if
(
equals
(
c_token
+
1
,
"("
)) {
enum
operators
whichfunc
=
is_builtin_function
(
c_token
);
struct
value
num_params
;
num_params
.
type
=
INTGR
;
if
(
whichfunc
) {
c_token
+=
2
;
/* skip fnc name and '(' */
parse_expression
();
/* parse fnc argument */
num_params
.
v
.
int_val
=
1
;
while
(
equals
(
c_token
,
","
)) {
c_token
++
;
num_params
.
v
.
int_val
++
;
parse_expression
();
}
if
(!
equals
(
c_token
,
")"
))
int_error
(
c_token
,
"')' expected"
);
c_token
++
;
/* The sprintf built-in function has a variable number of arguments */
if
(!
strcmp
(
ft
[
whichfunc
].
f_name
,
"sprintf"
))
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
/* v4 timecolumn only had 1 param; v5 has 2. Accept either */
if
(!
strcmp
(
ft
[
whichfunc
].
f_name
,
"timecolumn"
))
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
/* These functions have an optional 3rd parameter */
if
(!
strncmp
(
ft
[
whichfunc
].
f_name
,
"weekdate_"
,
9
))
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
/* The column() function has side effects requiring special handling */
if
(!
strcmp
(
ft
[
whichfunc
].
f_name
,
"column"
)) {
set_up_columnheader_parsing
(
&
(
at
->
actions
[
at
->
a_count
-
1
]) );
}
/* split( "string" {, "sep"} ) has an optional 2nd parameter */
if
(!
strcmp
(
ft
[
whichfunc
].
f_name
,
"split"
))
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
(
void
)
add_action
(
whichfunc
);
}
else
{
/* it's a call to a user-defined function */
enum
operators
call_type
=
(
int
)
CALL
;
int
tok
=
c_token
;
c_token
+=
2
;
/* skip func name and '(' */
parse_expression
();
if
(
equals
(
c_token
,
","
)) {
/* more than 1 argument? */
num_params
.
v
.
int_val
=
1
;
while
(
equals
(
c_token
,
","
)) {
num_params
.
v
.
int_val
+=
1
;
c_token
+=
1
;
parse_expression
();
}
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
call_type
=
(
int
)
CALLN
;
}
if
(!
equals
(
c_token
,
")"
))
int_error
(
c_token
,
"')' expected"
);
c_token
++
;
add_action
(
call_type
)
->
udf_arg
=
add_udf
(
tok
);
}
}
else
if
(
equals
(
c_token
,
"sum"
)
&&
equals
(
c_token
+
1
,
"["
)) {
parse_sum_expression
();
/* dummy_func==NULL is a flag to say no dummy variables active */
}
else
if
(
dummy_func
) {
if
(
equals
(
c_token
,
c_dummy_var
[
0
])) {
c_token
++
;
add_action
(
PUSHD1
)
->
udf_arg
=
dummy_func
;
fit_dummy_var
[
0
]
++
;
}
else
if
(
equals
(
c_token
,
c_dummy_var
[
1
])) {
c_token
++
;
add_action
(
PUSHD2
)
->
udf_arg
=
dummy_func
;
fit_dummy_var
[
1
]
++
;
}
else
{
int
i
,
param
=
0
;
for
(
i
=
2
;
i
<
MAX_NUM_VAR
;
i
++
) {
if
(
equals
(
c_token
,
c_dummy_var
[
i
])) {
struct
value
num_params
;
num_params
.
type
=
INTGR
;
num_params
.
v
.
int_val
=
i
;
param
=
1
;
c_token
++
;
add_action
(
PUSHC
)
->
v_arg
=
num_params
;
add_action
(
PUSHD
)
->
udf_arg
=
dummy_func
;
fit_dummy_var
[
i
]
++
;
break
;
}
}
if
(!
param
) {
/* defined variable */
add_action
(
PUSH
)
->
udv_arg
=
add_udv
(
c_token
);
c_token
++
;
}
}
/* its a variable, with no dummies active - div */
}
else
{
add_action
(
PUSH
)
->
udv_arg
=
add_udv
(
c_token
);
c_token
++
;
}
}
/* end if letter */
/* Maybe it's a string constant */
else
if
(
isstring
(
c_token
)) {
union
argument
*
foo
=
add_action
(
PUSHC
);
foo
->
v_arg
.
type
=
STRING
;
foo
->
v_arg
.
v
.
string_val
=
NULL
;
/* this dynamically allocated string will be freed by free_at() */
m_quote_capture
(
&
(
foo
->
v_arg
.
v
.
string_val
),
c_token
,
c_token
);
c_token
++
;
}
else
{
int_error
(
c_token
,
"invalid expression "
);
}
/* The remaining operators are postfixes and can be stacked, e.g. */
/* Array[i]**2, so we may have to loop to catch all of them. */
while
(TRUE) {
/* add action code for ! (factorial) operator */
if
(
equals
(
c_token
,
"!"
)) {
c_token
++
;
(
void
)
add_action
(
FACTORIAL
);
}
/* add action code for ** operator */
else
if
(
equals
(
c_token
,
"**"
)) {
c_token
++
;
parse_unary_expression
();
(
void
)
add_action
(
POWER
);
}
/* Parse and add actions for range specifier applying to previous entity.
* Currently the [beg:end] form is used to generate substrings, but could
* also be used to extract vector slices. The [i] form is used to index
* arrays, but could also be a shorthand for extracting a single-character
* substring.
*/
else
if
(
equals
(
c_token
,
"["
)
&&
!
isanumber
(
c_token
-
1
)) {
/* handle '*' or empty start of range */
if
(
equals
(
++
c_token
,
"*"
)
||
equals
(
c_token
,
":"
)) {
union
argument
*
empty
=
add_action
(
PUSHC
);
empty
->
v_arg
.
type
=
INTGR
;
empty
->
v_arg
.
v
.
int_val
=
1
;
if
(
equals
(
c_token
,
"*"
))
c_token
++
;
}
else
parse_expression
();
/* handle array indexing (single value in square brackets) */
if
(
equals
(
c_token
,
"]"
)) {
c_token
++
;
(
void
)
add_action
(
INDEX
);
continue
;
}
if
(!
equals
(
c_token
,
":"
))
int_error
(
c_token
,
"':' expected"
);
/* handle '*' or empty end of range */
if
(
equals
(
++
c_token
,
"*"
)
||
equals
(
c_token
,
"]"
)) {
union
argument
*
empty
=
add_action
(
PUSHC
);
empty
->
v_arg
.
type
=
INTGR
;
empty
->
v_arg
.
v
.
int_val
=
65535
;
/* should be INT_MAX */
if
(
equals
(
c_token
,
"*"
))
c_token
++
;
}
else
parse_expression
();
if
(!
equals
(
c_token
,
"]"
))
int_error
(
c_token
,
"']' expected"
);
c_token
++
;
(
void
)
add_action
(
RANGE
);
/* Whatever this is, it isn't another postfix operator */
}
else
{
break
;
}
}
}
/* HBB 20010309: Here and below: can't store pointers into the middle
* of at->actions[]. That array may be realloc()ed by add_action() or
* express() calls!. Access via index savepc1/savepc2, instead.
*/
static
void
parse_conditional_expression
()
{
/* create action code for ? : expressions */
if
(
equals
(
c_token
,
"?"
)) {
int
savepc1
,
savepc2
;
/* Fake same recursion level for alternatives */
parse_recursion_level
--
;
c_token
++
;
savepc1
=
at
->
a_count
;
add_action
(
JTERN
);
parse_expression
();
if
(!
equals
(
c_token
,
":"
))
int_error
(
c_token
,
"expecting ':'"
);
c_token
++
;
savepc2
=
at
->
a_count
;
add_action
(
JUMP
);
at
->
actions
[
savepc1
].
arg
.
j_arg
=
at
->
a_count
-
savepc1
;
parse_expression
();
at
->
actions
[
savepc2
].
arg
.
j_arg
=
at
->
a_count
-
savepc2
;
add_action
(
NOP
);
parse_recursion_level
++
;
}
}
static
void
parse_logical_OR_expression
()
{
/* create action codes for || operator */
while
(
equals
(
c_token
,
"||"
)) {
int
savepc
;
c_token
++
;
savepc
=
at
->
a_count
;
add_action
(
JUMPNZ
);
/* short-circuit if already TRUE */
accept_logical_AND_expression
();
/* offset for jump */
at
->
actions
[
savepc
].
arg
.
j_arg
=
at
->
a_count
-
savepc
;
(
void
)
add_action
(
BOOLE
);
}
}
static
void
parse_logical_AND_expression
()
{
/* create action code for && operator */
while
(
equals
(
c_token
,
"&&"
)) {
int
savepc
;
c_token
++
;
savepc
=
at
->
a_count
;
add_action
(
JUMPZ
);
/* short-circuit if already FALSE */
accept_inclusive_OR_expression
();
at
->
actions
[
savepc
].
arg
.
j_arg
=
at
->
a_count
-
savepc
;
/* offset for jump */
(
void
)
add_action
(
BOOLE
);
}
}
static
void
parse_inclusive_OR_expression
()
{
/* create action code for | operator */
while
(
equals
(
c_token
,
"|"
)) {
c_token
++
;
accept_exclusive_OR_expression
();
(
void
)
add_action
(
BOR
);
}
}
static
void
parse_exclusive_OR_expression
()
{
/* create action code for ^ operator */
while
(
equals
(
c_token
,
"^"
)) {
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL