-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.c
1738 lines (1616 loc) · 43.2 KB
/
create.c
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
#include "common.h"
#include "yacc_stuff.h"
#include "proto.h"
#include <math.h>
extern var_list *head_table;
extern var_values *head_variable_list;
loop_stack *Head_Loop_Stack = NULL;
int output_ifdef_directive;
int operand_is_string;
char *ptr_to_string_operand;
/* Copyright (C) 1996 Himanshu M. Thaker
This file is part of vpp.
Vpp is distributed in the hope that it will be useful,
but without any warranty. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.
Everyone is granted permission to copy, modify and redistribute
vpp, but only under the conditions described in the
document "vpp copying permission notice". An exact copy
of the document is supposed to have been given to you along with
vpp so that you can know how you may redistribute it all.
It should be in a file named COPYING. Among other things, the
copyright notice and this notice must be preserved on all copies. */
double
round_int(double i)
{
/* ANSI C library replacement for double rint(double). */
return floor(i + 0.5);
}
/**************************************************************************
*
* Function Name : ascii2int()
* Parameters :
* char *name - string to convert
* int *nmbr - pointer to where to store the converted number
* Return Value :
* int
* Purpose :
* Given a string, convert it to an integer. Note that if
* ANY non-digit is encountered, FALSE is returned, and *nmbr is
* set to zero. Otherwise, TRUE is returned and the convered number
* is stored in *nmbr.
*
**************************************************************************/
int
ascii2int(char *name, int *nmbr)
{
int i;
int val;
val = 0;
for (i=0; i<strlen(name); i++)
{
if (!isdigit(name[i])) {
*nmbr = 0;
return (FALSE);
}
val = val*10 + name[i] - '0';
}
*nmbr = val;
return(TRUE);
}
/**************************************************************************
*
* Function Name : ascii2double()
* Parameters :
* char *name - string to convert
* double *nmbr - pointer to where to store the converted number
* Return Value :
* int
* Purpose :
* Given a string, convert it to double precision floating point number.
* Note that if ANY non-digit is encountered, FALSE is returned, and *nmbr is
* set to zero. Otherwise, TRUE is returned and the convered number
* is stored in *nmbr.
*
**************************************************************************/
int
ascii2double(char *name, double *nmbr)
{
int i,j;
double val;
int len;
double div;
int is_neg;
val = 0.0;
is_neg = FALSE;
/*
* strip off leading blanks...
*/
j = 0;
len = strlen(name);
while (((j<len) && (name[j] == ' ')) || (name[j] == '\t')) j++;
if ((!isdigit(name[j])) && (name[j] != '.') && (name[j] != '-') &&
(name[j] != '+') ) {
*nmbr = 0.0;
return (FALSE);
}
/*
* The first non blank can be a "-" or a "+"
*/
if (name[j] == '-') {
is_neg = TRUE;
j++;
}
else if (name[j] == '+') {
j++;
}
for (i=j; i<len; i++)
{
if (!isdigit(name[i])) {
j = i;
break;
}
val = val*10.0 + (name[i] - '0');
}
if (name[j] == '.') {
div = 10.0;
j++;
for (i=j; i<len; i++) {
if (!isdigit(name[i])) {
/*
* now, strip off trailing blanks
*/
for (j=i; j<len; j++) {
if ((name[j] != ' ') && (name[j] != '\t')) {
*nmbr = 0.0;
return(FALSE);
}
}
break;
}
val = val + (name[i]-'0')/div;
div = div*10.0;
j = i;
}
}
if (is_neg) val = -1.0 * val;
*nmbr = val;
return(TRUE);
}
/**************************************************************************
*
* Function Name : double2ascii()
* Parameters :
* double nmbr - number to convert
* char *name - where to store the converted string
* Return Value :
* int
* Purpose :
* Given a double, convert it to the string equivalent. Note that only
* 5 digits after the period are stored. For memory conservation, the
* calling routine is responsible for ensuring that "name" has been freed
* before calling this function.
*
**************************************************************************/
char *
double2ascii(double nmbr)
{
char *name;
char tmp[MAXSTR];
char tmp2[MAXSTR];
double rem;
int value;
int is_negative;
int len;
int i,j;
int digit;
if (nmbr < 0.0) {
is_negative = TRUE;
nmbr = nmbr * -1.0;
}
else is_negative = FALSE;
value = (int) floor(nmbr);
i = 0;
if (value == 0) tmp2[i++] = '0';
while (value > 0) {
tmp2[i++] = ((value % 10) + '0');
value = value / 10;
}
tmp2[i++] = '\0'; /* null terminate the string */
if (is_negative) tmp[0] = '-';
len = strlen(tmp2);
for (i=0; i<len; i++) {
tmp[i+is_negative]=tmp2[len-i-1];
}
tmp[i+is_negative]='.';
i++;
rem = nmbr - floor(nmbr) + DELTA/1000.0;
for(j=0; j<6; j++) {
digit = (int) (rem*10.0);
tmp[i+is_negative] = digit + '0';
i++;
rem = (rem*10.0 - digit*1.0);
}
tmp[i+is_negative] = '\0';
/*
* finally, allocate space for the new string and return it
*/
name = NMALLOC(strlen(tmp)+1,char);
strcpy(name,tmp);
return (name);
}
/**************************************************************************
*
* Function Name :
* Parameters :
* Return Value :
* Purpose :
*
**************************************************************************/
int
is_an_integer(double value)
{
double abs_value;
if (value < 0.0) abs_value = value * -1.0;
else abs_value = value;
abs_value = round_int(abs_value) - abs_value;
if (abs_value < 0.0) abs_value = abs_value * -1.0;
if (abs_value <= DELTA) {
return (TRUE);
}
else {
return (FALSE);
}
}
/**************************************************************************
*
* Function Name : int check_for_if(ep, ll_type, active)
* Parameters :
* Return Value :
* int - indicates if the region is active or not
* Purpose :
*
**************************************************************************/
int
check_for_if(expr *ep, int ll_type, int active)
{
loop_stack *tmp_stack;
int region_active;
/*
* Determine if the current region is active or not... If we have
* no defs on the stack, then we are obviously active. If there is
* a def on the stack, check to see if that def is active. If it is,
* then we too are active.
*/
if (Head_Loop_Stack == NULL) region_active = active;
else region_active = Head_Loop_Stack->active;
/*
* First, create the stack template, and put it on the
* head of the stack.
*/
if (Head_Loop_Stack == NULL) { /* nothing on the stack */
tmp_stack = Head_Loop_Stack = MALLOC(loop_stack);
tmp_stack->next = NULL;
} /* of if Head_Loop_Stack is NULL */
else {
tmp_stack = MALLOC(loop_stack);
tmp_stack->next = Head_Loop_Stack;
Head_Loop_Stack = tmp_stack;
}
tmp_stack->ll_type = ll_type;
tmp_stack->mate = NULL;
tmp_stack->ep = ep;
tmp_stack->region_active = region_active;
if (region_active) {
if (ll_type == IF_STRUCTURE || !output_ifdef_directive) {
int tmp;
if (ll_type == IF_STRUCTURE) {
/* `if conditional can use arithmetic expressions. */
tmp = (int)evaluate_expression(ep);
}
else if (ep->ident == STRING) {
/* `ifdef/`ifndef conditionals use names only. */
double val;
tmp = get_define_value(ep->u.strg, &val);
if ((ll_type == IFDEF_STRUCTURE && tmp != -1) ||
(ll_type == IFNDEF_STRUCTURE && tmp == -1))
tmp = 1;
else
tmp = 0;
}
else {
/* Invalid token used in conditional. */
tmp = 0;
}
tmp_stack->active = tmp ? TRUE : FALSE;
}
else {
tmp_stack->active = TRUE;
if (ll_type == IFDEF_STRUCTURE) {
printf("`ifdef ");
traverse_expression(ep);
printf("\n");
}
if (ll_type == IFNDEF_STRUCTURE) {
printf("`ifdef ");
traverse_expression(ep);
printf("\n");
printf("`else\n");
}
}
}
else tmp_stack->active = FALSE;
return(tmp_stack->active);
}
/**************************************************************************
*
* Function Name : int check_for_else()
* Parameters :
* int - indicates if the region is now active
* Return Value :
* Purpose :
*
**************************************************************************/
int
check_for_else()
{
if (Head_Loop_Stack == NULL) {
fprintf(stderr,"ERROR : `else not within a conditional\n");
return(TRUE);
}
else {
if (Head_Loop_Stack->region_active) {
if (Head_Loop_Stack->ll_type == IF_STRUCTURE || !output_ifdef_directive) {
Head_Loop_Stack->active = !Head_Loop_Stack->active;
return(Head_Loop_Stack->active);
}
else {
if (Head_Loop_Stack->ll_type == IFNDEF_STRUCTURE) {
printf("`endif\n");
printf("`ifdef ");
traverse_expression(Head_Loop_Stack->ep);
printf("\n");
}
else printf("`else\n");
}
}
return (Head_Loop_Stack->region_active);
}
}
/**************************************************************************
*
* Function Name : int handle_endif()
* Parameters :
* int - indicates if the region is now active
* Return Value :
* Purpose :
*
**************************************************************************/
int
handle_endif()
{
int active = TRUE;
loop_stack *tmp_head;
if (Head_Loop_Stack == NULL) {
fprintf(stderr,"WARNING : unbalanced `endif\n");
}
else {
if (Head_Loop_Stack->region_active && output_ifdef_directive) {
if (Head_Loop_Stack->ll_type != IF_STRUCTURE) {
printf("`endif\n");
}
}
active = Head_Loop_Stack->region_active;
tmp_head = Head_Loop_Stack;
Head_Loop_Stack = Head_Loop_Stack->next;
free(tmp_head);
}
if (Head_Loop_Stack == NULL) return (active);
else return (Head_Loop_Stack->active);
}
/**************************************************************************
*
* Function Name : expr *create_identifier()
* Parameters :
* int ident - type of expression operation to create (NMBR or STRING)
* char *name - name of identifier to create
* Return Value :
* expr * - pointer to created identifier
* Purpose :
* Currently, store the name as a text... eventually can either
* lookup the name in a database and point to that, or return the
* string if not found. CURRENTLY ONLY THE STRING IS STORED!
*
**************************************************************************/
expr *
create_identifier(int ident, char *name)
{
expr *tmp_ep;
tmp_ep = MALLOC(expr);
tmp_ep->ident = ident;
tmp_ep->u.strg = NMALLOC(strlen(name)+1,char);
strcpy(tmp_ep->u.strg, name);
tmp_ep->ep1 = NULL;
tmp_ep->ep2 = NULL;
tmp_ep->ep3 = NULL;
tmp_ep->elp = NULL;
tmp_ep->vvp = NULL;
return(tmp_ep);
}
/**************************************************************************
*
* Function Name : expr *create_expression()
* Parameters :
* int ident - type of expression operation to create (e.g. ADD)
* expr *ep1, *ep2, *ep3 - pointers to the two expressions to operate on
* Return Value :
* expr * - pointer to created expession
* Purpose :
*
**************************************************************************/
expr *
create_expression(int ident, expr *ep1, expr *ep2, expr *ep3)
{
expr *tmp_ep;
tmp_ep = MALLOC(expr);
tmp_ep->ident = ident;
tmp_ep->ep1 = ep1;
tmp_ep->ep2 = ep2;
tmp_ep->ep3 = ep3;
tmp_ep->elp = NULL;
tmp_ep->vvp = NULL;
return(tmp_ep);
}
/**************************************************************************
*
* Function Name : expr *create_primary()
* Parameters :
* int ident
* char *name
* expr *ep1, *ep2
* expr_list *elp;
* Return Value :
* expr * - pointer to created expession
* Purpose :
*
**************************************************************************/
expr *
create_primary(int ident, char *name, expr *ep1, expr *ep2, expr_list *elp)
{
expr *tmp_ep;
tmp_ep = MALLOC(expr);
tmp_ep->vvp = NULL;
if (name != NULL) {
tmp_ep->u.strg = NMALLOC(strlen(name)+1,char);
strcpy(tmp_ep->u.strg, name);
tmp_ep->vvp = get_variable_ref(ident,name);
}
else tmp_ep->u.strg = NULL;
tmp_ep->ident = ident;
tmp_ep->ep1 = ep1;
tmp_ep->ep2 = ep2;
tmp_ep->ep3 = NULL;
tmp_ep->elp = elp;
return(tmp_ep);
}
void
traverse_expression(expr *ep)
{
if (ep != NULL) {
if (ep->ident == SUBSTR) {
printf("SUBSTR(");
traverse_expression(ep->ep1);
printf(",");
traverse_expression(ep->ep2);
printf(",");
traverse_expression(ep->ep3);
printf(")");
}
else if ((ep->ep1 != NULL) && (ep->ep2 != NULL)) {
printf("(");
traverse_expression(ep->ep1);
print_operand(ep);
traverse_expression(ep->ep2);
printf(")");
}
else if ((ep->ep1 != NULL) && (ep->ep2 == NULL)) {
printf("(");
print_operand(ep);
traverse_expression(ep->ep1);
printf(")");
}
else print_operand(ep);
}
}
void
print_operand(expr *ep)
{
if ((ep->ident == STRING) || (ep->ident == NMBR) ||
(ep->ident == REAL)) printf("%s",ep->u.strg);
else if (ep->ident == QUOTED_STRING) printf("%s",ep->u.strg);
else if (ep->ident == ADD) printf(" PLUS ");
else if (ep->ident == SUB) printf(" MINUS ");
else if (ep->ident == MULT) printf(" MULT ");
else if (ep->ident == DIV) printf(" DIV ");
else if (ep->ident == MOD) printf(" MOD ");
else if (ep->ident == EQ) printf(" EQ ");
else if (ep->ident == NEQ) printf(" NEQ ");
else if (ep->ident == CASEEQ) printf(" CASEEQ ");
else if (ep->ident == CASENEQ) printf(" CASENEQ ");
else if (ep->ident == AND) printf(" AND ");
else if (ep->ident == OR) printf(" OR ");
else if (ep->ident == LT) printf(" LT ");
else if (ep->ident == LEQ) printf(" LEQ ");
else if (ep->ident == GT) printf(" GT ");
else if (ep->ident == GEQ) printf(" GEQ ");
else if (ep->ident == BITAND) printf(" BITAND ");
else if (ep->ident == BITOR) printf(" BITOR ");
else if (ep->ident == BITXOR) printf(" BITXOR ");
else if (ep->ident == BITXNOR) printf(" BITXNOR ");
else if (ep->ident == SHIFTR) printf(" SHIFTR ");
else if (ep->ident == SHIFTL) printf(" SHIFTL ");
else if (ep->ident == UPLUS) printf(" UPLUS ");
else if (ep->ident == UMINUS) printf(" UMINUS ");
else if (ep->ident == UNOT) printf(" UNOT ");
else if (ep->ident == UTILDA) printf(" UTILDA ");
else if (ep->ident == UAND) printf(" UAND ");
else if (ep->ident == UNAND) printf(" UNAND ");
else if (ep->ident == UNOR) printf(" UNOR ");
else if (ep->ident == UPIPE) printf(" UPIPE ");
else if (ep->ident == UCARAT) printf(" UCARAT ");
else if (ep->ident == UXNOR) printf(" UXNOR ");
else if (ep->ident == POWER) printf(" ** ");
else if (ep->ident == LOG2) printf(" LOG2 ");
else if (ep->ident == ROUND) printf(" ROUND ");
else if (ep->ident == CEIL) printf(" CEIL ");
else if (ep->ident == FLOOR) printf(" FLOOR ");
else if (ep->ident == MAX) printf(" MAX ");
else if (ep->ident == MIN) printf(" MIN ");
else if (ep->ident == ABS) printf(" ABS ");
else if (ep->ident == STOI) printf(" STOI ");
else if (ep->ident == ITOS) printf(" ITOS ");
else if (ep->ident == SUBSTR) printf(" SUBSTR ");
else if (ep->ident == SYSTEM) printf(" SYSTEM ");
else if (ep->ident == EVEN) printf(" EVEN ");
else if (ep->ident == ODD) printf(" ODD ");
else printf(" UNKNOWN ");
}
/**************************************************************************
*
* Function Name : expr_list *create_expression_list()
* Parameters :
* expr_list *head - pointer to head of list
* expr *ep - pointer to the expression to add to list
* Return Value :
* expr_list * - pointer to an expression list
* Purpose :
* Add to a linked list the given expression. Note that if
* a new list is being created, "head" should be null.
* SUGGESTIONS FOR SPEED IMPROVEMENT (SFSI) : send tail of linked
* list also... then don't have to search thru till end every time.
* We will first have to see how often this function is called, and
* if often enough, add the extra overhead of the tail.
*
**************************************************************************/
expr_list *
create_expression_list(expr_list *head, expr *ep)
{
expr_list *tmp_head;
if (head == NULL) {
head = MALLOC(expr_list);
head->ep = ep;
head->next = NULL;
}
else {
tmp_head = head;
while (tmp_head != NULL) {
if (tmp_head->next == NULL) {
tmp_head->next = MALLOC(expr_list);
tmp_head = tmp_head->next;
tmp_head->ep = ep;
tmp_head->next = NULL;
}
tmp_head = tmp_head->next;
} /* of while loop */
} /* of else of if head is null */
return (head);
}
/**************************************************************************
*
* Function Name : create_statement
* Parameters :
* Return Value : stmt * - pointer to a statement
* Purpose :
*
**************************************************************************/
stmt *
create_statement(int ident, expr *ep, stmt *sp1, stmt *sp2, t_ident_ref *lp)
{
stmt *tmp_sp;
tmp_sp = MALLOC(stmt);
tmp_sp->ident = ident;
tmp_sp->ep = ep;
tmp_sp->sp1 = sp1;
tmp_sp->sp2 = sp2;
tmp_sp->lp = lp;
return (tmp_sp);
}
/**************************************************************************
*
* Function Name : t_ident_ref *create_ident_ref()
* Parameters :
* int ident
* char *name
* expr *ep1, *ep2
* Return Value :
* t_ident_ref * - pointer to created lvalue
* Purpose :
*
**************************************************************************/
t_ident_ref *
create_ident_ref(int ident, char *name, expr *ep1, expr *ep2)
{
t_ident_ref *tmp_ptr;
tmp_ptr = MALLOC(t_ident_ref);
if (name != NULL) {
tmp_ptr->strg = NMALLOC(strlen(name)+1,char);
strcpy(tmp_ptr->strg, name);
}
else tmp_ptr->strg = NULL;
tmp_ptr->ident = ident;
tmp_ptr->ep1 = ep1;
tmp_ptr->ep2 = ep2;
return(tmp_ptr);
}
/**************************************************************************
*
* Function Name : create_variable_ref()
* Parameters :
* char *name - name of variable
* expr *ep - expression used to assign to variable
* int do_eval - whether or not to do evaluation
* ... this is important in cases like x=x-1
* Return Value :
* varp * - pointer to record created for variable
* Purpose :
* This function first creates a unique record that holds
* the name, expression to evaluate to assign to that variable,
* then inserts the variable into a table, and finally, evaluates
* the expression.
*
**************************************************************************/
varp *
create_variable_ref(char *name, expr *ep, int do_eval)
{
varp *tmp;
tmp = MALLOC(varp);
if (name != NULL) {
tmp->name = NMALLOC(strlen(name)+1, char);
strcpy(tmp->name, name);
}
else tmp->name = NULL;
tmp->vvp = get_variable_ref(STRING,name);
if (tmp->vvp == NULL) {
tmp->vvp = create_variable(name, evaluate_expression(ep));
if (operand_is_string) {
tmp->vvp->strg = NMALLOC(strlen(ptr_to_string_operand)+1,char);
strcpy(tmp->vvp->strg, ptr_to_string_operand);
}
}
else if (do_eval) {
tmp->vvp->strg = double2ascii(evaluate_expression(ep));
if (operand_is_string) {
free(tmp->vvp->strg);
tmp->vvp->strg = NMALLOC(strlen(ptr_to_string_operand)+1,char);
strcpy(tmp->vvp->strg, ptr_to_string_operand);
}
}
tmp->strg = NULL;
tmp->ep = ep;
return(tmp);
}
/**************************************************************************
*
* Function Name :
* Parameters :
* Return Value :
* var_values * - pointer to where variable is stored
* Purpose :
* This function creates a new variable. If one already exists,
* its value is overwritten with the new one.
*
**************************************************************************/
var_values *
create_variable(char *name, double dval)
{
var_values *tmp_head;
if (head_variable_list == NULL) {
head_variable_list = MALLOC(var_values);
tmp_head = head_variable_list;
}
else {
tmp_head = head_variable_list;
while (tmp_head != NULL) {
if (strcmp(name,tmp_head->name) == 0) {
tmp_head->value = (int)dval;
tmp_head->vtype = NMBR;
if (tmp_head->strg != NULL) free (tmp_head->strg);
tmp_head->strg = double2ascii(dval);
return(tmp_head);
}
else if (tmp_head->next == NULL) {
tmp_head->next = MALLOC(var_values);
tmp_head = tmp_head->next;
break;
}
tmp_head = tmp_head->next;
}
}
tmp_head->name = NMALLOC(strlen(name)+1,char);
strcpy(tmp_head->name,name);
tmp_head->value = (int)dval;
tmp_head->dval = 0.0;
tmp_head->vtype = NMBR;
tmp_head->strg = double2ascii(dval);
tmp_head->next = NULL;
return (tmp_head);
}
/**************************************************************************
*
* Function Name :
* Parameters :
* int ident - either NMBR, REAL or STRING
* char *name - name to get reference for
* Return Value :
* Purpose :
* If ident is NMBR, name is converted into an integer, a varp * pointer
* created, and returned. The user is responsible for destroying the returned
* pointer. If ident is STRING, the name is looked up. If found, a pointer
* to the reference is returned.
*
**************************************************************************/
var_values *
get_variable_ref(int ident, char *name)
{
var_values *tmp;
var_values *ttt;
double dval;
if ((ident == NMBR) || (ident == REAL)) {
ttt = MALLOC(var_values);
ttt->name = NULL;
ttt->next = NULL;
ascii2int(name, &(ttt->value));
ttt->dval = 0.0;
ascii2double(name,&dval);
ttt->strg = double2ascii(dval);
ttt->vtype = NMBR;
return (ttt);
}
else {
tmp = head_variable_list;
while (tmp != NULL) {
if (strcmp(name, tmp->name) == 0) return (tmp);
tmp = tmp->next;
}
}
return(NULL);
}
/**************************************************************************
*
* Function Name :
* Parameters :
* Return Value :
* Purpose :
*
**************************************************************************/
void
free_expr(expr *ep)
{
if (ep != NULL) {
free_expr(ep->ep1);
free_expr(ep->ep2);
free_expr(ep->ep3);
/*
* ++++ note note note!!! need to add code
* to properly free up elp and vp...
* we are ignoring it for now...
*/
free(ep);
}
}
/**************************************************************************
*
* Function Name :
* Parameters :
* Return Value :
* Purpose :
*
**************************************************************************/
double
evaluate_expression(expr *ep)
{
double tmp1, tmp2;
double val;
val = 0.0;
if (ep != NULL) {
if ((ep->ep1 != NULL) && (ep->ep2 != NULL)) {
tmp1 = evaluate_expression(ep->ep1);
tmp2 = evaluate_expression(ep->ep2);
val = perform_operand(2, tmp1, tmp2, ep);
}
else if ((ep->ep1 != NULL) && (ep->ep2 == NULL)) {
tmp1 = evaluate_expression(ep->ep1);
val = perform_operand(1, tmp1, 0.0, ep);
}
else {
val = perform_operand(0, 0.0, 0.0, ep);
}
} /* of if ep is not NULL */
return (val);
}
/**************************************************************************
*
* Function Name :
* Parameters :
* np - number of parameters
* tmp1 - parameter 1
* tmp2 - parameter 2
* ep - expression
* Return Value :
* Purpose :
*
**************************************************************************/
double
perform_operand(int np, double tmp1, double tmp2, expr *ep)
{
double val;
int int1,int2,intval;
char msg[MAXSTR];
val = 0.0;
operand_is_string = FALSE;
if ((ep->ident == NMBR) || (ep->ident == REAL) || (ep->ident == STRING)) {
if (ep->vvp == NULL) {
if (get_define_value(ep->u.strg, &val) != 0) {
sprintf(msg,"ERROR : variable `%s is undefined",ep->u.strg);
yyerror(msg);
}
}
else {
ascii2double(ep->vvp->strg, &val);
}
}
else if ((ep->ident == QUOTED_STRING)) {
operand_is_string = TRUE;
ptr_to_string_operand = ep->u.strg;
}
else if (ep->ident == ADD) {
if (np == 2) val = tmp1 + tmp2;
else val = tmp1;
}
else if (ep->ident == SUB) {
if (np == 2) val = tmp1 - tmp2;
else val = - tmp1;
}
else if (ep->ident == MULT) {
val = tmp1 * tmp2;
}
else if (ep->ident == DIV) {
if (tmp2 == 0) fprintf(stderr,"WARNING : divide by zero\n");
else val = tmp1 / tmp2;
}
else if (ep->ident == MOD) {
if (tmp2 == 0) fprintf(stderr,"WARNING : modulo by zero\n");
else val = (double) (((int) tmp1) % ((int) tmp2));
}
else if (ep->ident == POWER) {
val = pow(tmp1,tmp2);
}
else if (ep->ident == EQ) {
if (is_an_integer(tmp1) && is_an_integer(tmp2)) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 == int2);
val = intval * 1.0;
}
else {
val = (tmp1 == tmp2);
}
}
else if (ep->ident == NEQ) {
if (is_an_integer(tmp1) && is_an_integer(tmp2)) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 != int2);
val = intval * 1.0;
}
else {
val = (tmp1 != tmp2);
}
}
else if (ep->ident == AND) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 && int2);
val = intval * 1.0;
}
else if (ep->ident == OR) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 || int2);
val = intval * 1.0;
}
else if (ep->ident == LT) {
if (is_an_integer(tmp1) && is_an_integer(tmp2)) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 < int2);
val = intval * 1.0;
}
else {
val = (tmp1 < tmp2);
}
}
else if (ep->ident == LEQ) {
if (is_an_integer(tmp1) && is_an_integer(tmp2)) {
int1 = (int) round_int(tmp1);
int2 = (int) round_int(tmp2);
intval = (int1 <= int2);
val = intval * 1.0;
}
else {
val = (tmp1 <= tmp2);
}