-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparse.c
More file actions
1534 lines (1318 loc) · 32.4 KB
/
parse.c
File metadata and controls
1534 lines (1318 loc) · 32.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
#include <stdio.h>
#ifdef IBMPC
#include <stdlib.h>
#endif
#include "misc.h"
#include "defs.h"
#include "cvt.h"
#include "struct.h"
#include "cvt_id.h"
#include "tokens.h"
extern char *text_buffer, *text_ptr;
extern int line_count;
extern char *out_string;
/*
* Parse a procedure parameter list.
* Return head of linked list of parameters.
*/
void get_param_list(param_head)
PARAM_LIST **param_head;
{
PARAM_LIST *list_ptr, *param_ptr;
int token_class;
TOKEN sep_token;
*param_head = NULL;
list_ptr = NULL;
do {
get_param_ptr(¶m_ptr);
if (*param_head == NULL)
*param_head = param_ptr;
if (list_ptr)
list_ptr->next_param = param_ptr;
list_ptr = param_ptr;
token_class = get_token(¶m_ptr->param);
if (token_class != IDENTIFIER) {
parse_error("Identifier expected");
free_param_list(*param_head);
}
/* Get ',' or ')' */
token_class = get_token(&sep_token);
} while (token_class == COMMA);
if (token_class != RIGHT_PAREN) {
parse_error("')' expected");
free_param_list(*param_head);
}
return;
}
/*
* Parse parameter list.
* Parse DECLARE statements until all parameters in param_list
* have been found. Split declare statements into those used in
* param_list and those not. Return pointers to head of both
* DECL_MEMBER lists.
*/
void parse_param_list(param_list, decl_list, extra_decl_list)
PARAM_LIST *param_list;
DECL **decl_list, **extra_decl_list;
{
DECL *extra_decl, *extra_decl_ptr;
DECL *list, *list_ptr;
DECL_MEMBER *extra_list_ptr;
DECL_MEMBER *el_ptr, *last_el_ptr, *next_el_ptr;
DECL_MEMBER *extra_el_ptr;
int token_class;
TOKEN *decl_token;
DECL_ID *var_ptr, *last_var_ptr, *next_var_ptr;
DECL_ID *extra_last_var_ptr;
PARAM_LIST *param_ptr, *last_param;
*decl_list = NULL;
*extra_decl_list = NULL;
/* Pointer to next DECL_MEMBER in decl_ptr */
list_ptr = NULL;
/* Pointer to next DECL in extra_decl_list */
extra_decl_ptr = NULL;
while (param_list) {
/* Get declaration */
get_token_ptr(&decl_token);
token_class = get_token(decl_token);
if ((token_class != RESERVED) ||
(decl_token->token_type != DECLARE)) {
parse_error("DECLARE expected");
free((char *) decl_token);
return;
}
/* Get declaration list */
get_decl_ptr(&list);
get_decl_list(list);
list->decl_token = decl_token;
/* Points to start of extra declaration list */
extra_list_ptr = NULL;
/* Pointer to previous el_ptr */
last_el_ptr = NULL;
/* Check each element of the DECLARE statement */
el_ptr = list->decl_list;
while (el_ptr) {
/* Point to next member */
next_el_ptr = el_ptr->next_member;
/* Pointer to next DECL_MEMBER in extra_decl_ptr */
extra_el_ptr = NULL;
/* Points to last variable in variable list */
last_var_ptr = NULL;
/* Contains not found variables in name_list */
extra_last_var_ptr = NULL;
/* Check each variable in name list */
for (var_ptr = el_ptr->name_list; var_ptr; ) {
/* Point to following var_ptr */
next_var_ptr = var_ptr->next_var;
/* Is this variable in param list? */
last_param = NULL;
for (param_ptr = param_list; param_ptr;
param_ptr = param_ptr->next_param) {
if (!strcmp(param_ptr->param.token_name,
var_ptr->name->token_name))
break;
else
last_param = param_ptr;
}
if (param_ptr) {
/* Variable found */
/* Remove from parameter list */
if (last_param)
last_param->next_param = param_ptr->next_param;
else
param_list = param_ptr->next_param;
free((char *) param_ptr);
last_var_ptr = var_ptr;
} else {
/*
* Variable not found - Add to extra variable list
*/
if (extra_el_ptr == NULL) {
/*
* Create new element and copy DECLARE info
*/
get_element_ptr(&extra_el_ptr);
element_copy(el_ptr, extra_el_ptr);
extra_last_var_ptr = NULL;
/*
* Link new extra element into extra_list_ptr
*/
if (extra_list_ptr) {
extra_list_ptr->next_member = extra_el_ptr;
} else {
/*
* Create new extra declaration
*/
get_decl_ptr(&extra_decl);
/* Point to DECLARE token */
extra_decl->decl_token =
list->decl_token;
extra_decl->decl_list = extra_el_ptr;
/*
* Link new extra declaration into extra_decl_list
*/
if (extra_decl_ptr)
extra_decl_ptr->next_decl = extra_decl;
else
*extra_decl_list = extra_decl;
extra_decl_ptr = extra_decl;
}
extra_list_ptr = extra_el_ptr;
}
/* Add var_ptr to extra list */
if (extra_last_var_ptr)
extra_last_var_ptr->next_var = var_ptr;
else
extra_list_ptr->name_list = var_ptr;
extra_last_var_ptr = var_ptr;
/* Remove from DECLARE list */
if (last_var_ptr)
last_var_ptr->next_var = next_var_ptr;
else
el_ptr->name_list = next_var_ptr;
var_ptr->next_var = NULL;
}
var_ptr = next_var_ptr;
}
/*
* Check for empty name list
*/
if (el_ptr->name_list == NULL) {
/*
* Empty name list - unlink and discard element from declaration list
*/
if (last_el_ptr)
last_el_ptr->next_member = next_el_ptr;
else
list->decl_list = next_el_ptr;
el_ptr->next_member = NULL;
free((char *) el_ptr);
} else
last_el_ptr = el_ptr;
el_ptr = next_el_ptr;
}
/* Save found items in decl_list */
if (list->decl_list->name_list) {
if (*decl_list)
list_ptr->next_decl = list;
else
*decl_list = list;
list_ptr = list;
} else
free((char *) list);
}
return;
}
/*
* Parse until desired token type appears
*/
void parse_till(type, token)
int type;
TOKEN *token;
{
while (get_token(token) != type)
out_token(token);
return;
}
/*
* Parse until END statement
*/
void parse_till_end(token)
TOKEN *token;
{
int token_class;
while (1) {
token_class = get_token(token);
if (token_class == END_OF_FILE) {
parse_error("Premature end-of-file");
exit(1);
}
if ((token_class == RESERVED) && (token->token_type == END))
return;
parse_statement(token);
}
return;
}
/*
* Parse through END statement
*/
void parse_to_end()
{
TOKEN token;
parse_till_end(&token);
parse_statement(&token);
return;
}
/*
* Check for end of line (';')
*/
void check_eol()
{
TOKEN token;
if (get_token(&token) != END_OF_LINE)
parse_error("';' expected");
else
out_token(&token);
return;
}
/*
* Parse simple variable and return variable token and following token.
* Passed with initial identifier in token.
* Returns with next_token terminating variable.
* Handles [ .<identifier> ] ...
*/
int parse_simple_variable(token, next_token)
TOKEN *token, *next_token;
{
int token_class;
while (1) {
token_class = get_token(next_token);
if (token_class != PERIOD)
return token_class;
/* Process .<identifier> */
token_class = get_token(next_token);
if (token_class != IDENTIFIER) {
parse_error("Illegal identifier");
return ERROR;
}
/* Add .<identifier> to original token name */
(void) strcat(token->token_name, ".");
(void) strcat(token->token_name, next_token->token_name);
/* Parse for additional member */
return parse_simple_variable(token, next_token);
}
return 0;
}
/*
* Parse variable identifier. If pointer, output (*ident) else
* If variable has BASED attribute, output (*based_name).
* Otherwise, output ident.
*/
void out_ident(ident, decl, decl_id)
TOKEN *ident;
DECL_MEMBER *decl;
DECL_ID *decl_id;
{
if (decl->at_ptr || decl_id->is_ext_at) {
out_white_space(ident);
out_str("(*");
out_token_name(ident);
out_char(')');
} else
if (decl_id->based_name) {
out_white_space(ident);
out_str("(**");
out_token_name(decl_id->name);
out_char(')');
} else
out_token(ident);
return;
}
/*
* Parse variable name or structure element and output appropriate tokens.
* Passed with identifier in token, and declaration for token in decl.
* Returns with token terminating variable.
* Handles <member> { [ ( <expression> ) ] [ .<identifier> ] }
*/
int parse_member(token, decl, decl_id)
TOKEN *token;
DECL_MEMBER *decl;
DECL_ID *decl_id;
{
int token_class;
TOKEN member;
DECL_MEMBER *var_decl;
DECL_ID *var_decl_id;
/* Check for literal */
if (decl->literal) {
/* Yes - output case converted literal */
out_white_space(token);
out_cvt_name(token);
/* Return next token */
token_class = get_token(token);
return token_class;
}
token_copy(token, &member);
token_class = get_token(token);
/* Check for array subscript */
if (decl->array_bound) {
out_ident(&member, decl, decl_id);
if (token_class == LEFT_PAREN) {
/* Convert to open square bracket */
token->token_name[0] = '[';
out_token(token);
/* Parse expression to right parenthesis */
token_class = parse_expression(token);
if (token_class != RIGHT_PAREN) {
parse_error("')' expected");
return ERROR;
}
/* Convert to close square bracket */
token->token_name[0] = ']';
out_token(token);
token_class = get_token(token);
}
}
/* Check for .<identifier> */
if ((decl->type->token_type == STRUCTURE) && (token_class == PERIOD)) {
if (decl->array_bound)
/* Already printed identifier */
out_token(token);
else {
if (decl->at_ptr || decl_id->based_name) {
/*
* --- Note: Does not handle BASED AT variables!
*/
/* Print 'member->' */
out_token(&member);
out_str("->");
} else {
/* Print 'member.' */
out_ident(&member, decl, decl_id);
out_token(token);
}
}
token_class = get_token(token);
if (token_class != IDENTIFIER) {
parse_error("Illegal structure member");
return ERROR;
}
/* Find variable in list */
if (!find_list_symbol(token, decl->struct_list,
&var_decl, &var_decl_id)) {
parse_error("Undefined structure member");
return ERROR;
}
/* Parse this member now */
token_class = parse_member(token, var_decl, var_decl_id);
} else
if (decl->array_bound == NULL)
out_ident(&member, decl, decl_id);
return token_class;
}
/*
* Parse variable and output appropriate tokens.
* Passed with initial identifier in token.
* Returns with token terminating variable.
* Handles { [ ( <expression> ) ] [ .<identifier> ] } ...
*/
int parse_variable(token, var_decl, var_decl_id)
TOKEN *token;
DECL_MEMBER **var_decl;
DECL_ID **var_decl_id;
{
if (!find_symbol(token, var_decl, var_decl_id)) {
parse_error("Undefined variable");
return ERROR;
}
return parse_member(token, *var_decl, *var_decl_id);
}
/*
* See if token is in cvt_list.
* If found, return pointer to conversion string.
*/
int check_cvt_id(token, cvt_id, cvt_string)
TOKEN *token;
CVT_ID *cvt_id;
char **cvt_string;
{
/* Check each string in cvt_id */
while (*(cvt_id->id_name)) {
if (!strcmp(token->token_name, cvt_id->id_name)) {
/* Found match - return new string */
*cvt_string = cvt_id->new_id;
return 1;
}
cvt_id++;
}
return 0;
}
/*
* Parse function call
*/
parse_function(token)
TOKEN *token;
{
int token_class;
BOOLEAN left_shift, right_shift;
char *new_func;
DECL_MEMBER *decl_ptr;
DECL_ID *decl_id;
/* Function call - check for SHL or SHR */
out_white_space(token);
left_shift = !strcmp(token->token_name, "shl") ||
!strcmp(token->token_name, "SHL");
right_shift = !strcmp(token->token_name, "shr") ||
!strcmp(token->token_name, "SHR");
if (left_shift || right_shift) {
/* SHL(expr, expr) or SHR(expr, expr) */
/* Check for '(' */
token_class = get_token(token);
if (token_class != LEFT_PAREN) {
parse_error("'(' expected");
return ERROR;
}
out_token(token);
/* Output first expression */
out_char('(');
token_class = parse_expression(token);
if (token_class != COMMA) {
parse_error("',' expected");
return ERROR;
}
out_str(left_shift ? ") << (" : ") >> (");
/* Output second expression */
token_class = parse_expression(token);
if (token_class != RIGHT_PAREN) {
parse_error("Missing ')'");
return ERROR;
}
out_char(')');
out_token(token);
} else {
/* Check for a type cast function */
if (check_cvt_id(token, &cast_functions[0], &new_func)) {
/* Convert to a cast */
out_char('(');
out_str(new_func);
out_str(") ");
} else
/* Check for a function conversion */
if (check_cvt_id(token, &cvt_functions[0], &new_func)) {
/* Convert to desired function */
out_str(new_func);
} else {
/* Output function name */
out_token_name(token);
/* Check for parameter list */
if (find_symbol(token, &decl_ptr, &decl_id)) {
if (decl_ptr->type->token_type != PROCEDURE) {
parse_error("Illegal function call");
return ERROR;
}
if (decl_ptr->initialization != DATA) {
/* No param list */
token_class = get_token(token);
return token_class;
}
}
}
/* Check for parameter list */
token_class = get_token(token);
if (token_class != LEFT_PAREN) {
parse_warning("Parameter list expected");
return token_class;
}
out_token(token);
/* Parse to closing right paren */
do {
token_class = parse_expression(token);
out_token(token);
} while (token_class == COMMA);
if (token_class != RIGHT_PAREN) {
parse_error("Missing ')'");
return ERROR;
}
}
/* Return token following function */
token_class = get_token(token);
return token_class;
}
/*
* Parse expression and output appropriate tokens.
* Return token at end of expression.
*/
parse_expression(token)
TOKEN *token;
{
int token_class;
int i, last_class, temp_class;
DECL_MEMBER *id_type;
DECL_ID *id_id;
char *new_id;
char string_const[MAX_TOKEN_LENGTH], octal_const[5];
last_class = OPERATOR;
token_class = get_token(token);
while (1) {
switch (token_class) {
case LEFT_PAREN :
if (last_class != OPERATOR) {
parse_error("Missing operator");
return ERROR;
}
/* Sub-expression */
out_token(token);
/* Parse to closing right paren */
token_class = parse_expression(token);
if (token_class != RIGHT_PAREN) {
parse_error("Missing ')'");
return ERROR;
}
out_token(token);
break;
case RIGHT_PAREN :
return token_class;
case OPERATOR :
out_white_space(token);
if (token->token_type == EQUAL)
/* Make it a '==' */
out_str("==");
else
/* Check for address operator '@' or '.' */
if ((token->token_type == AT_OP) ||
(token->token_type == PERIOD)) {
token_class = get_token(token);
if (token_class == IDENTIFIER) {
/* Make it a '&' */
out_char('&');
/* See if it's a function reference */
if (find_symbol(token, &id_type, &id_id) &&
(id_type->type->token_type != PROCEDURE)) {
/* Variable - parse it */
temp_class = parse_member(token, id_type, id_id);
} else {
/* Function call - Check for */
/* a function conversion */
if (check_cvt_id(token, &cvt_functions[0], &new_id))
/* Convert to desired function */
out_str(new_id);
else
/* Function call - output name */
out_token_name(token);
temp_class = get_token(token);
}
} else
if (token_class == LEFT_PAREN) {
/* Constant list - convert to string */
out_char('"');
string_const[0] = '\0';
do {
token_class = get_token(token);
if (token_class == STRING)
(void) strcat(string_const, token->token_name);
else
if (token_class == NUMERIC) {
cvt_octal(token, octal_const);
(void) strcat(string_const, octal_const);
} else {
parse_error("Illegal constant");
return ERROR;
}
token_class = get_token(token);
} while (token_class == COMMA);
if (token_class != RIGHT_PAREN) {
parse_error("')' expected");
return ERROR;
}
i = strlen(string_const);
if ((i >= 4) &&
(!strcmp(string_const + i - 4, "\\000")))
/* Discard trailing null */
string_const[i - 4] = '\0';
out_str(string_const);
out_char('"');
} else {
parse_error("Illegal operator");
return ERROR;
}
} else
out_token_name(token);
break;
case IDENTIFIER :
/* Check for identifier conversion */
if (check_cvt_id(token, &cvt_identifiers[0], &new_id)) {
out_white_space(token);
out_str(new_id);
temp_class = get_token(token);
} else
/* See if variable in context */
if (find_symbol(token, &id_type, &id_id) &&
(id_type->type->token_type != PROCEDURE)) {
/* Variable - parse it */
temp_class = parse_member(token, id_type, id_id);
} else
/* Function call - parse it */
temp_class = parse_function(token);
break;
case NUMERIC :
out_token(token);
break;
case STRING :
out_white_space(token);
/* Convert to a numeric constant */
if (token->token_length > 4) {
parse_error("Illegal string constant");
return ERROR;
}
if (token->token_length > 1)
out_char('(');
out_str_const(token->token_name, token->token_length);
if (token->token_length > 1)
out_char(')');
break;
default :
/* Must not be part of an expression! */
return token_class;
}
last_class = token_class;
token_class = (last_class == IDENTIFIER) ?
temp_class : get_token(token);
}
}
/*
* DO statement
* Handles DO;, DO CASE, DO WHILE, and iterative DO
*/
void parse_do(first_token)
TOKEN *first_token;
{
TOKEN token;
int token_class;
int case_line;
char case_statement[MAX_TOKEN_LENGTH];
char case_output[MAX_CASE_STATEMENT_SIZE];
char var_string[MAX_TOKEN_LENGTH];
char *temp_out_string, *temp_out_string1;
DECL_MEMBER *var_decl;
DECL_ID *var_decl_id;
/* Create new context */
new_context(DO, (TOKEN *) NULL);
out_white_space(first_token);
/* Determine what kind of DO statement */
token_class = get_token(&token);
switch (token_class) {
case END_OF_LINE :
/* DO; */
out_white_space(&token);
out_char('{'); /* } for dumb vi */
parse_to_end();
break;
case IDENTIFIER :
/* DO counter = start TO limit BY step */
out_str("for");
out_must_white(&token);
out_char('(');
/* Put full variable in var_string */
var_string[0] = '\0';
temp_out_string = out_string;
out_string = var_string;
token_class = parse_variable(&token, &var_decl, &var_decl_id);
out_string = temp_out_string;
/* Check for '=' */
if ((token_class != OPERATOR) ||
(token.token_type != EQUAL)) {
parse_error("Missing '='");
pop_context();
return;
}
/* Send <ident> '=' <expr> */
out_str(var_string);
out_token(&token);
token_class = parse_expression(&token);
if ((token_class != RESERVED) ||
(token.token_type != TO)) {
parse_error("Missing TO");
pop_context();
return;
}
/* Send <ident> <= <limit> */
out_str("; ");
out_str(var_string);
out_str(" <=");
token_class = parse_expression(&token);
out_str("; ");
/* Parse increment */
if ((token_class == RESERVED) &&
(token.token_type == BY)) {
/* Send <ident> += <step> */
out_str(var_string);
out_str(" +=");
token_class = parse_expression(&token);
} else {
/* Send <ident>++ */
out_str(var_string);
out_str("++");
}
out_str(") {"); /* } for dumb vi */
out_white_space(&token);
if (token_class != END_OF_LINE) {
parse_error("BY or ';' expected");
pop_context();
return;
}
parse_to_end();
break;
case RESERVED :
switch (token.token_type) {
case CASE :
/* DO CASE <expr>; */
out_str("switch (");
if (parse_expression(&token) != END_OF_LINE) {
parse_error("';' expected");
pop_context();
return;
}
out_white_space(&token);
out_str(") {"); /* } for dumb vi */
case_line = 0;
while (1) {
/* Place case statement in out_string */
temp_out_string1 = out_string;
case_output[0] = '\0';
out_string = case_output;
(void) sprintf(case_statement, "case %d :",
case_line++);
token_class = parse_new_statement();
if (token_class == END_OF_FILE) {
parse_error("Premature end-of-file");
exit(1);
}
if (token_class == END) {
out_string = temp_out_string1;
out_str(case_output);
break;
}
out_string = temp_out_string1;
out_white_space(first_token);
out_str(case_statement);
out_str(case_output);
out_white_space(first_token);
out_str("break;\n");
}
break;
case WHILE :
/* DO WHILE <expr>; */
out_str("while (");
if (parse_expression(&token) != END_OF_LINE) {
parse_error("';' expected");
pop_context();
return;
}
out_white_space(&token);
out_str(") {"); /* } for dumb vi */
parse_to_end();
break;
default:
parse_error("Illegal DO clause");
pop_context();
return;
}
break;
}
/* End of context */
pop_context();
}
/*
* END statement
* Handles END [ <identifier> ] ;
*/
parse_end(first_token)
TOKEN *first_token;
{
TOKEN token;
int token_class;
out_white_space(first_token); /* { for dumb vi */
out_char('}');
/* Check for END <procedure name>; */
token_class = get_token(&token);
if (token_class == IDENTIFIER) {
/* END foo; where foo is a procedure */
out_white_space(&token);
out_str("/* ");
out_token_name(&token);
out_str(" */");
token_class = get_token(&token);
}
if (token_class == END_OF_LINE)
out_white_space(&token);
else
parse_error("';' expected");
}
/*
* IF statement
*/
parse_if(first_token)
TOKEN *first_token;
{
TOKEN token;
out_white_space(first_token);
out_str("if (");
if ((parse_expression(&token) != RESERVED) ||
(token.token_type != THEN))
parse_error("Missing THEN in IF statement");
else {
out_pre_line(&token);
out_char(')');
out_white_space(&token);
}
}
/*
* THEN statement
*/
parse_then()
{
parse_error("Illegal use of THEN");
}
/*
* ELSE statement
*/