-
Notifications
You must be signed in to change notification settings - Fork 7
/
ComGenerate.cpp
2908 lines (2652 loc) · 85.3 KB
/
ComGenerate.cpp
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 <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
#include "ComGlobal.h"
#include "ComGenerate.h"
#if defined(LRSTAR)
# include "PGGenerate.h"
#elif defined(DFASTAR)
# include "LGGenerate.h"
#endif
int Generate::num_char;
int Generate::num_uchar;
int Generate::num_short;
int Generate::num_ushort;
int Generate::num_int;
int Generate::num_uint;
int Generate::num_charp;
STAKTYPE Generate::STAK[MAXTOP];
int Generate::staktop;
int Generate::maxtop;
const char* Generate::group_start;
int Generate::skip_code;
int Generate::g_size;
char Generate::in_group;
char Generate::format[50];
int Generate::width;
int Generate::bytes_out;
int Generate::first_err;
int Generate::last_sep_l;
int Generate::linenumb;
int Generate::fd;
char Generate::skl_fid [MAX_PATH];
char Generate::out_fid [MAX_PATH];
int Generate::count;
int Generate::mult;
int Generate::plus;
char Generate::middle[2000];
char Generate::sep[2000];
char Generate::end[2000];
char Generate::arg4[30];
char Generate::arg5[30];
char Generate::arg6[30];
int Generate::middle_l;
int Generate::sep_l;
int Generate::end_l;
int Generate::n_out;
const char* Generate::skel;
const char* Generate::skelbeg;
const char* Generate::skelend;
char* Generate::buffer;
char* Generate::buffptr;
char* Generate::buffend;
int Generate::operation_flag;
int Generate::n_origlines;
int Generate::n_addedlines;
int Generate::max_outbuff;
///////////////////////////////////////////////////////////////////////////////////////////////////
// //
int N_targs;
int N_pargs;
int N_nargs;
#ifdef LRSTAR
enum DefCon
{
ACCP_STA, // Accept state (final state).
ARG_NUMB, // Argument numbers.
ARG_TEXT, // Argument text strings.
BMAT_COL, // B matrix column index.
BMAT_MASK, // B matrix mask.
BMAT_NUMB, // B matrix (0's and 1's).
BMAT_ROW, // B matrix row index.
DEF_CONS, // Defined constants.
EOF_NUMB, // EOF terminal number.
EOL_NUMB, // EOL terminal number.
ERR_USED, // <error> symbol used in grammar (0 or 1).
GRM_FILE, // Grammar filename (e.g. C.grm).
GRM_NAME, // Grammar name (e.g. C).
GRM_TEXT, // Grammar text (complete file contents).
HEAD_PROD, // Head production (first production).
HEAD_SYMB, // Head symbols.
KEY_NUMB, // <keyword> symbol number.
MAKE_AST, // Make AST switch (0 or 1).
NACT_ARG, // Node action arguments.
NACT_FUNC, // Node fuction pointers.
NACT_NUMB, // Node action number for each production.
ND_ACTION, // Nondeterministic action.
ND_START, // Nondeterministic starting point in list (n+1 of these).
ND_TERM, // Nondeterministic terminal.
NMAT_COL, // N matrix column index.
NMAT_NUMB, // N matrix numbers.
NMAT_ROW, // N matrix row index.
NODE_NAME, // Node names.
NODE_NUMB, // Node numbers for productions.
NUMB_HEAD, // Number of head symbols (nonterminals)
NUMB_NACT, // Number of node actions.
NUMB_NODE, // Number of nodes.
NUMB_PACT, // Number of parse actions (tact + ract).
NUMB_PROD, // Number of productions.
NUMB_STA, // Number of states.
NUMB_TACT, // Number of token actions.
NUMB_TERM, // Number of terminals.
OPTN_AST, // Abstract-syntax tree (0,1).
OPTN_BM, // Boolean matrix (1,2),
OPTN_CLR, // Canonical LR(1) parser tables (0,1),
OPTN_DEBUG, // Debug parser option (0,1,2,3),
OPTN_DR, // Default reductions (0,1),
OPTN_EXP, // Expecting list option (0,1,2),
OPTN_LR, // Minimal LR(1) parser tables (0,1),
OPTN_NA, // Number of arguments required for a node (0,1,2,3).
OPTN_ND, // Nondeterministic parsing in effect (0,1).
OPTN_SRA, // Shift-reduce actions (0,1).
OPTN_X, // x - defined by user (?).
OPTN_Y, // y - defined by user (?).
OPTN_Z, // z - defined by user (?).
OUT_FILE, // Output filename.
PACT_ARG, // Parsing-action argument pointer (first argument also used by make-node code).
PACT_FUNC, // Parsing-action function names.
PACT_NUMB, // Parsing-action numbers (one for each production).
PROD_HEAD, // Production head symbol.
PROD_LENG, // Production length
PROD_REVS, // Production reverse code (0 or 1).
PROD_TAIL, // Production tail (first tail number).
PROGRAM, // Program name (LRSTAR).
RESTORE, // Restore parser stack code.
RMAT_COL, // R matrix column index.
RMAT_NUMB, // R matrix numbers.
RMAT_ROW, // R matrix row index.
SKL_FILE, // Skeleton filename.
STA_ACCS, // State accessor.
STA_DOT, // State dot.
STA_ITEM, // State items.
STA_PROD, // State productions.
TACT_ARG, // Token action argument numbers.
TACT_FUNC, // Token action function names.
TACT_NUMB, // Token action numbers.
TAIL_NUMB, // Tail numbers.
TERM_SYMB, // Terminal symbols (all terminals)
TMAT_COL, // T matrix column index.
TMAT_NUMB, // T matrix numbers.
TMAT_ROW, // T matrix row index.
VERSION, // Version of program.
};
#endif
#ifdef DFASTAR
enum DefCon
{
LG_ACTION_CODE, // Action code.
LG_BMAT_COL, // B matrix column index.
LG_BMAT_NUMB, // B matrix (0's and 1's).
LG_BMAT_ROW, // B matrix row index.
LG_DIRECT_CODE, // Direct code.
LG_DEF_CONS, // Defined constants.
LG_ERR_TOKEN, // Error token.
LG_GRM_FILE, // Grammar filename.
LG_GRM_NAME, // Grammar name.
LG_GRM_TEXT, // Grammar text (complete file contents).
LG_NUMB_STA, // Number of states.
LG_NUMB_TERM, // Number of terminals.
LG_OPTN_CODE, // Direct-code lexer.
LG_OPTN_COL, // Column number in lexer.
LG_OPTN_DEBUG, // Debug option.
LG_OPTN_LARGE, // Large table-driven lexer.
LG_OPTN_LINE, // Line number in lexer.
LG_OPTN_MEDIUM, // Medium table-driven lexer.
LG_OPTN_SMALL, // Small table-driven lexer.
LG_OUT_FILE, // Output filename.
LG_PROGRAM, // Program name (DFASTAR).
LG_SKL_FILE, // Skeleton filename.
LG_STRINGS, // String return values.
LG_TERM_NUMB, // Terminal number for lexer tables.
LG_TMAT_COL, // T matrix column index.
LG_TMAT_NUMB, // T matrix numbers.
LG_TMAT_ROW, // T matrix row index.
LG_VERSION // Version of program.
};
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
// //
CODETABLE* code_table[200];
#ifdef LRSTAR
CODETABLE PGCodeTable[] =
{
"accp_sta", "dt", ACCP_STA, "Accept (final) state number",
"arg_numb", "dt", ARG_NUMB, "Argument numbers",
"arg_text", "dt", ARG_TEXT, "Argument text strings",
"bmat_col", "dt", BMAT_COL, "B matrix column index",
"bmat_mask", "dt", BMAT_MASK, "B matrix mask",
"bmat_numb", "dt", BMAT_NUMB, "B matrix numbers",
"bmat_row", "dt", BMAT_ROW, "B matrix row index",
"def_cons", "dt", DEF_CONS, "Defined constants",
"eof_numb", "dt", EOF_NUMB, "End of file terminal number",
"eol_numb", "dt", EOL_NUMB, "End of line terminal number",
"err_used", "dt", ERR_USED, "<error> symbol used in grammar (0 or 1)",
"grm", "s ", GRM_NAME, "Grammar name (same as grm_name)",
"grm_file", "s ", GRM_FILE, "Grammar filename",
"grm_name", "s ", GRM_NAME, "Grammar name",
"grm_text", "s ", GRM_TEXT, "Grammar text (whole file contents)",
"head_prod", "dt", HEAD_PROD, "Head symbol first production (rule)",
"head_symb", "dt", HEAD_SYMB, "Head symbol for a production (rule)",
"key_numb", "dt", KEY_NUMB, "<keyword> symbol number",
"make_ast", "dt", MAKE_AST, "Make AST switch (0 or 1)",
"nact_arg", "dt", NACT_ARG, "Node-action arguments index",
"nact_func", "dt", NACT_FUNC, "Node-action fuction names",
"nact_numb", "dt", NACT_NUMB, "Node-action number for a production",
"nd_action", "dt", ND_ACTION, "Nondeterministic action list (goto or reduce)",
"nd_start", "dt", ND_START, "Nondeterministic start point in action list",
"nd_term", "dt", ND_TERM, "Nondeterministic terminal symbol list",
"nmat_col", "dt", NMAT_COL, "N matrix column index",
"nmat_numb", "dt", NMAT_NUMB, "N matrix numbers",
"nmat_row", "dt", NMAT_ROW, "N matrix row index",
"node_name", "dt", NODE_NAME, "Node names",
"node_numb", "dt", NODE_NUMB, "Node number for a production",
"numb_head", "dt", NUMB_HEAD, "Number of head symbols (nonterminals)",
"numb_nact", "dt", NUMB_NACT, "Number of node actions",
"numb_node", "dt", NUMB_NODE, "Number of nodes",
"numb_pact", "dt", NUMB_PACT, "Number of parse actions",
"numb_prod", "dt", NUMB_PROD, "Number of productions (rules)",
"numb_sta", "dt", NUMB_STA, "Number of states",
"numb_tact", "dt", NUMB_TACT, "Number of token actions",
"numb_term", "dt", NUMB_TERM, "Number of terminal symbols",
"optn_ast", "dt", OPTN_AST, "Abstract-Syntax Tree construction (0,1)",
"optn_bm", "dt", OPTN_BM, "Boolean matrix (1=bytes,2=bits)",
"optn_clr", "dt", OPTN_CLR, "Canonical LR(1) parser tables (0,1)",
"optn_debug", "dt", OPTN_DEBUG, "Debug parser option (0,1,2,3)",
"optn_dr", "dt", OPTN_DR, "Default reductions (0,1)",
"optn_exp", "dt", OPTN_EXP, "Expecting list option (0,1,2)",
"optn_lr", "dt", OPTN_LR, "Minimal LR(1) parser tables (0,1)",
"optn_na", "dt", OPTN_NA, "Number of arguments required for a node (0,1,2,3)",
"optn_nd", "dt", OPTN_ND, "Nondeterministic parsing in effect (0,1)",
"optn_sra", "dt", OPTN_SRA, "Shirt-reduce actions in parser (0,1)",
"optn_x", "dt", OPTN_X, "x - defined by user (?)",
"optn_y", "dt", OPTN_Y, "y - defined by user (?)",
"optn_z", "dt", OPTN_Z, "z - defined by user (?)",
"out_file", "s ", OUT_FILE, "Output filename (e.g. parser.cpp)",
"pact_arg", "dt", PACT_ARG, "Parsing-action arguments index",
"pact_func", "dt", PACT_FUNC, "Parsing-action function names",
"pact_numb", "dt", PACT_NUMB, "Parsing-action numbers (one for each production)",
"prod_head", "dt", PROD_HEAD, "Production head symbol (one for each rule)",
"prod_leng", "dt", PROD_LENG, "Production length",
"prod_revs", "dt", PROD_REVS, "Production reverse code (0 or 1)",
"prod_tail", "dt", PROD_TAIL, "Production tail (first tail number)",
"program", "s ", PROGRAM, "Program name (LRSTAR)",
"restore", "dt", RESTORE, "Restore parser stack code (0 or 1)",
"rmat_col", "dt", RMAT_COL, "R matrix column index",
"rmat_numb", "dt", RMAT_NUMB, "R matrix numbers",
"rmat_row", "dt", RMAT_ROW, "R matrix row index",
"skl_file", "s ", SKL_FILE, "Skeleton filename (e.g. parser.cpp.skl)",
"sta_accs", "dt", STA_ACCS, "State accessor symbols",
"sta_dot", "dt", STA_DOT, "State dot (in production)",
"sta_item", "dt", STA_ITEM, "State items",
"sta_prod", "dt", STA_PROD, "State productions",
"tact_arg", "dt", TACT_ARG, "Terminal action index into argument numbers list",
"tact_func", "dt", TACT_FUNC, "Terminal action function names",
"tact_numb", "dt", TACT_NUMB, "Terminal action numbers (one for each terminal)",
"tail_numb", "dt", TAIL_NUMB, "Tail numbers",
"term_symb", "dt", TERM_SYMB, "Terminal symbols (all terminals)",
"tmat_col", "dt", TMAT_COL, "T matrix column index",
"tmat_numb", "dt", TMAT_NUMB, "T matrix numbers",
"tmat_row", "dt", TMAT_ROW, "T matrix row index",
"version", "s ", VERSION, "Version of program (e.g. 3.0.100)",
"~", "", 0, "Terminates this list"
};
#endif
#ifdef DFASTAR
CODETABLE LGCodeTable[] =
{
"action_code", "dt", LG_ACTION_CODE, "Action code",
"bmat_col", "dt", LG_BMAT_COL, "B matrix column index",
"bmat_numb", "dt", LG_BMAT_NUMB, "B matrix (0's and 1's)",
"bmat_row", "dt", LG_BMAT_ROW, "B matrix row index",
"def_cons", "dt", LG_DEF_CONS, "Defined constants",
"direct_code", "s", LG_DIRECT_CODE, "Direct code output",
"err_token", "dt", LG_ERR_TOKEN, "Error token",
"grm", "s ", LG_GRM_NAME, "Grammar name (same as grm_name)",
"grm_file", "s ", LG_GRM_FILE, "Grammar filename",
"grm_name", "s ", LG_GRM_NAME, "Grammar name",
"grm_text", "s ", LG_GRM_TEXT, "Grammar text (complete file contents)",
"numb_sta", "dt", LG_NUMB_STA, "Number of states",
"numb_term", "dt", LG_NUMB_TERM, "Number of terminals/characters(256)",
"optn_code", "dt", LG_OPTN_CODE, "Direct code lexers",
"optn_col", "dt", LG_OPTN_COL, "Column number in lexer",
"optn_debug", "dt", LG_OPTN_DEBUG, "Debug option",
"optn_large", "dt", LG_OPTN_LARGE, "Large table-driven lexer",
"optn_line", "dt", LG_OPTN_LINE, "Line number in lexer",
"optn_medium", "dt", LG_OPTN_MEDIUM, "Medium table-driven lexer",
"optn_small", "dt", LG_OPTN_SMALL, "Small table-driven lexer",
"out_file", "s ", LG_OUT_FILE, "Output filename",
"program", "s ", LG_PROGRAM, "Program name (DFASTAR)",
"skl_file", "s ", LG_SKL_FILE, "Skeleton filename",
"strings", "dt", LG_STRINGS, "String return values",
"term_numb", "dt", LG_TERM_NUMB, "Terminal number for lexer tables",
"tmat_col", "dt", LG_TMAT_COL, "T matrix column index",
"tmat_numb", "dt", LG_TMAT_NUMB, "T matrix numbers",
"tmat_row", "dt", LG_TMAT_ROW, "T matrix row index",
"version", "s ", LG_VERSION, "Version of program",
"~", "", 0, "Terminates this list"
};
#endif
///////////////////////////////////////////////////////////////////////////////
// //
bool Generate::GenerateCode(const char* sklfid, const char* outfid, int verbose)
{
static bool first = true;
if (first) {
first = false;
#ifdef LRSTAR
for (int k = 0;; k++)
{
code_table[k] = &(PGCodeTable[k]);
if (PGCodeTable[k].keyword[0] == '~') break;
}
#endif
#ifdef DFASTAR
for (int k = 0;; k++)
{
code_table[k] = &(LGCodeTable[k]);
if (LGCodeTable[k].keyword[0] == '~') break;
}
#endif
initialize();
}
first_err = 1;
n_origlines = 0;
n_addedlines = 0;
staktop = 0;
count = 1;
mult = 1;
plus = 0;
maxtop = MAXTOP;
group_start = NULL;
operation_flag = 0;
strcpy (skl_fid, sklfid);
strcpy (out_fid, outfid);
// Load skeleton file ...
std::vector<char> skeldata;
std::ifstream skel_stm(skl_fid, std::ios_base::binary);
if (!skel_stm)
return false;
skel_stm.unsetf(std::ios_base::skipws);
skel_stm.seekg(0, std::ios_base::end);
skeldata.reserve(static_cast<size_t>(skel_stm.tellg()));
skel_stm.seekg(0, std::ios_base::beg);
std::copy(std::istream_iterator<char>(skel_stm), std::istream_iterator<char>(), std::back_inserter(skeldata));
if (skeldata.empty()) {
if (++n_errors == 1)
prt_log ("\n");
prt_log ("Skeleton: %s is empty!\n", skl_fid);
return false;
}
if (verbose > 1)
prt_log("Skeleton: %s\n", skl_fid);
else
prt_logonly("Skeleton: %s\n", skl_fid);
// TODO: elliminate those globals
skel = skeldata.data();
skelend = skel + skeldata.size();
if (chmod(out_fid, S_IWRITE) == 0) // File can be written ?
{
if (unlink (out_fid) != 0) // Delete it?
{
if (++n_errors == 1) prt_log ("\n");
prt_log ("Output: %s cannot be rewritten!\n", out_fid);
return false;
}
}
#ifdef DFASTAR
if (optn[LG_DIRECTCODE])
{
if (!open_code (out_fid))
return false;
}
else
#endif
{
fd = open (out_fid, O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
if (fd < 0)
{
if (++n_errors == 1) prt_log ("\n");
prt_log ("Output: %s cannot be created!\n", out_fid);
return false;
}
}
INIT_VARS ();
EMIT_ALL (verbose);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// //
#ifdef LRSTAR
void PGGenerate::initialize()
{
static bool firsttime = true;
if (firsttime)
firsttime = false;
else
return;
if (N_args == 0) {
N_args = 1;
Arg_numb[0] = 0;
}
if (N_strings == 0) {
N_strings = 1;
char* nullstr;
nullstr = (char*)malloc(4);
strcpy (nullstr, "\"\"");
Str_start[0] = nullstr;
}
// AST construction turned off ?
if (optn[PG_ASTCONST] == 0) {
N_nodes = 0;
N_nacts = 0;
for (int i = 0; i < N_prods; i++)
Node_numb[i] = -1;
}
// No token actions ?
if (N_tacts == 0) {
N_targs = 1;
F_targ[0] = 0;
} else
N_targs = N_terms;
// No parse actions and no nodes ?
if (N_pacts == 0 && N_nodes == 0) {
N_pargs = 1;
F_parg[0] = 0;
} else
N_pargs = N_prods;
// No node actions ?
if (N_nacts == 0) {
N_nargs = 1;
F_narg[0] = 0;
} else
N_nargs = N_prods;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// //
void Generate::INIT_VARS()
{
str_char = "char";
str_uchar = "unsigned char";
str_short = "short";
str_ushort = "unsigned short";
str_int = "int";
str_uint = "unsigned int";
str_charp = "const char*";
// looks like sizeof(type) on x86_32 probably have to be elliminated
num_char = 1;
num_uchar = 1;
num_short = 2;
num_ushort = 2;
num_int = 4;
num_uint = 4;
num_charp = 4;
}
void Generate::EMIT_ALL (int verbose)
{
g_size = 0;
in_group = 0;
linenumb = 1;
bytes_out = 0;
skip_code = 0;
max_outbuff = 1000000; // 1 MB
ALLOC (buffer, max_outbuff);
buffptr = buffer;
buffend = buffer + max_outbuff - max_outbuff/4; // 25% overflow.
skelbeg = skel;
SCAN ();
DUMP_SKEL(skelbeg, skel, nullptr);
FREE (buffer, max_outbuff);
char num[12] = " ";
number (n_origlines+n_addedlines, num);
#ifdef DFASTAR
if (optn[LG_DIRECTCODE])
{
close_code();
}
else
#endif
{
close (fd);
}
if (verbose > 1) prt_log ("Output: %s (%s lines)\n", out_fid, num);
else prt_logonly ("Output: %s (%s lines)\n", out_fid, num);
chmod(out_fid, S_IREAD); // Make output file read-only.
}
void Generate::SCAN ()
{
while (skel < skelend)
{
if (*skel == '"') // String ?
{
const char *p;
Scan: p = skel++;
while (*skel != '"' && *skel != '\n' && *skel != '\\' && *skel != '@') skel++;
if (*skel == '"') // End of string?
{
/* Nothing */
}
else if (*skel == '\n') // End of line?
{
PRT_ERR (p, linenumb);
prt_log ("%s(%04d) : String has no ending '\"' on this line.\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
else if (*skel == '\\') // \ escape.
{
if (*(skel+1) == '@') // \@ found?
{
DUMP_SKEL (skelbeg, skel, skel+1);
}
goto Scan;
}
else if (*skel == '@')
{
if (alpha[*(skel+1)]) // @[a-zA-Z] inside of string?
{
DUMP_SKEL (skelbeg, skel, skel);
skelbeg = skel = READ_CODE(skel);
goto Scan;
}
else
{
PRT_ERR (skel+1, linenumb);
prt_log ("%s(%04d) : Expecting <name> after '@'.\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
}
}
else if (*skel == '@') // Skeleton code ?
{
if (*(skel-1) == '\\') // \@ escape.
{
DUMP_SKEL (skelbeg, skel-1, skel);
goto Incr;
}
if (*(skel+1) == '(') // \@ escape.
{
const char* p = skel;
READ_VARS (skel+1);
DUMP_SKEL (skelbeg, p, skel);
goto Cont;
}
if (*(skel+1) == '/' && *(skel+2) == '/') // @// line comment.
{
const char* p = skel+3;
DUMP_SKEL (skelbeg, skel, skel);
skelbeg = skel = skip_rest_of_line(p);
goto Cont;
}
if (*(skel+1) == '/' && *(skel+2) == '*') // @/* block comment.
{
const char* p = skel;
DUMP_SKEL (skelbeg, skel, skel);
skelbeg = skel = skip_rest_of_comment (p);
goto Cont;
}
if (alpha[*(skel+1)]) // @[a-zA-Z]?
{
DUMP_SKEL (skelbeg, skel, skel);
skelbeg = skel = READ_CODE(skel);
goto Cont;
}
if (*(skel+1) == '@') // '@@' skeleton group terminator.
{
// printf ("%5d @@\n", linenumb);
if (in_group == 0) // Already outside of group?
{
PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Group ending '@@' found outside of a group.\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
if (skip_code)
{
while (*skel++ != '\n'); // Skip rest of line.
linenumb++;
DUMP_SKEL (skelbeg, skel, skel);
}
else
{
DUMP_SKEL (skelbeg, skel, skel);
while (*skel++ != '\n'); // Skip rest of line.
linenumb++;
skelbeg = skel;
}
UNSTAKCOND();
goto Cont;
}
else // Not recognized, crash.
{
PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : After '@' expecting: '//', '/*', <name>, or '@'\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
}
Incr: if (*skel++ == '\n') linenumb++;
Cont: continue;
}
// printf ("in_group = %d\n", in_group);
if (in_group)
{
while (skel > group_start) if (*--skel == '\n') linenumb--;
PRT_ERR (group_start, linenumb);
prt_log ("%s(%04d) : End of file reached, but no group ending '@@' was found for this group.\n\n", skl_fid, linenumb);
unlink (out_fid); // Delete output file.
Terminate (n_errors);
}
}
void Generate::STAKCOND() // Stack current conditional status.
{
in_group = 1;
if (staktop == maxtop)
{
PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : More than 10 nested groups causes a stack overflow.\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
STAK[staktop].skipcode = skip_code;
STAK[staktop].groupstart = group_start;
const char* p = skel;
while (*p != '\n') p--; // Find beginning of line.
p++; // p = beginning of line.
while (*p != '@') p++;
group_start = p;
staktop++;
}
void Generate::UNSTAKCOND() // Unstack conditional status.
{
if (staktop > 0)
{
staktop--;
skip_code = STAK[staktop].skipcode;
group_start = STAK[staktop].groupstart;
if (staktop == 0)
{
in_group = 0; // Out of block now.
}
}
else
{
in_group = 0;
skip_code = 0;
group_start = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
// //
const char* Generate::READ_CODE(const char* sk) // sk -> @
{
const char* p;
char c;
int x, result = 1, oper = 1, val, vx;
skel = sk;
p = GETCODENUM (++skel, x); // If skeleton code name is bad, this terminates.
switch (c = GET_OPER (p, x))
{
case '?':
Quest: val = value(x);
if (val != 0) val = 1;
if (oper == 0) result |= val;
else result &= val;
goto Check;
case '!':
Excl: val = value(x);
if (val == 0) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
Check: if (*++skel == ';')
{
Ret: if (result) ERASE ();
else SkipRestOfLineOrBlock (skel+1);
return (skel);
}
Or: if (strncmp (skel, ".or.", 4) == 0)
{
oper = 0; // or
skel += 4;
if (*skel == '@') goto Name;
goto Err1;
}
else if (strncmp (skel, ".and.", 5) == 0)
{
oper = 1; // and
skel += 5;
if (*skel == '@') goto Name;
goto Err1;
}
goto Err2;
Name: p = GETCODENUM (++skel, x); // If skeleton code name is bad, this terminates.
switch (c = GET_OPER (p, x))
{
case '?': goto Quest;
case '!': goto Excl;
case '.': goto Eq;
}
goto Err3;
case '.':
Eq: vx = value(x);
if (strncmp (skel, ".eq.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx == atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
else if (strncmp (skel, ".ne.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx != atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
else if (strncmp (skel, ".gt.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx > atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
else if (strncmp (skel, ".ge.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx >= atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
else if (strncmp (skel, ".lt.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx < atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
else if (strncmp (skel, ".le.", 4) == 0)
{
skel += 4;
const char* num = skel;
while (numeric[*skel]) skel++;
if (skel == num) goto Err4; // No numeric characters?
if (vx <= atoi(num)) val = 1;
else val = 0;
if (oper == 0) result |= val;
else result &= val;
if (*skel == '.') goto Or;
if (*skel == ';') goto Ret;
goto Err2;
}
goto Err5;
case 'd':
skel++;
EMIT_NUM (value(x));
return (skel+1);
case 's':
skel++;
emitStr(x);
return (skel+1);
case 't':
skel++;
emitType(x);
if (*format == 0) strcpy (format, "%t");
SPRINT (format, 0, (int*)string, (int*)string);
return (skel+1);
case '|': // operator was a <number>
skel++;
GET_STRINGS (skel);
emit(x);
return (skel);
case ';':
skel++;
if (code_table[x]->operators[0] == 's')
emitStr(x);
else {
skel--;
PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Default string type not valid for skeleton name \"%s\"\n\n",
skl_fid, linenumb, code_table[x]->keyword);
Terminate (n_errors);
}
return (skel);
}
Err1: PRT_ERR (skel+1, linenumb);
prt_log ("%s(%04d) : Expecting another skeleton code name starting with '@'\n\n", skl_fid, linenumb);
Terminate (n_errors);
Err2: PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Expecting '.or.', '.and.', or ';'\n\n", skl_fid, linenumb);
Terminate (n_errors);
Err3: PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Expecting '?' or '!'\n\n", skl_fid, linenumb);
Terminate (n_errors);
Err4: PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Expecting an <integer>\n\n", skl_fid, linenumb);
Terminate (n_errors);
Err5: PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Expecting '.eq.', '.ne.', '.ge.', or '.lt.'\n\n", skl_fid, linenumb);
Terminate (n_errors);
return (0);
}
///////////////////////////////////////////////////////////////////////////////
// //
const char* Generate::GETCODENUM(const char* keyword, int& x)
{
static constexpr size_t max_keyword= 32;
const char *p;
for (p = keyword; alpha[*p] != 0 && p < keyword + max_keyword; ++p);
if (p == keyword) // Check for no skeleton code name @;.
{
PRT_ERR (keyword, linenumb);
prt_log ("%s(%04d) : No skeleton code name was specified.\n\n", skl_fid, linenumb);
Terminate (n_errors);
}
for (int i = 0;; i++)
{
if (code_table[i]->keyword[0] < keyword[0]) continue; // Sorted table!
const int a = memcmp(code_table[i]->keyword, keyword, std::distance(keyword, p));
if (a < 0) continue;
if (a > 0) goto Err;
x = i; // Set code number.
return (p);
}
Err:
char tmp[max_keyword + 1];
*(std::copy(keyword, p, std::begin(tmp))) = 0;
PRT_ERR (tmp, linenumb);
prt_log ("%s(%04d) : \"%s\" was not found among skeleton code names.\n\n", skl_fid, linenumb, tmp);
prt_log ("Expecting one of the following:\n\n");
for (int i = 0;; i++)
{
if (code_table[i]->keyword[0] == '~') break;
prt_log (" %-12s - %s\n", code_table[i]->keyword, code_table[i]->description);
}
prt_log ("\n");
Terminate (n_errors);
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// //
char Generate::GET_OPER(const char* p, int x)
{
int n = 0;
skel = p;
p = NULL;
n_out = 0;
count = 1;
width = 1;
format[0] = 0;
switch (*skel)
{
case '.':
skel++;
break;
case '?':
case '!':
return *skel; // Return operator character.
case ';':
return *skel; // Return operator character.
default: {
const char buf[] = {*skel, 0};
PRT_ERR (buf, linenumb);
if (*skel == '\n') skel = "\\n";
else if (*skel == '\t') skel = "\\t";
else if (*skel == 26 ) skel = "\\z";
prt_log ("%s(%04d) : Error at '%s', expecting '.', '?', '!', or ';'\n\n", skl_fid, linenumb, buf);
Terminate (n_errors);
}
}
Top: switch (*skel)
{
case '\0':
case '\t':
case ' ':
skel++;
goto Top;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
p = skel;
n = GETN (skel);
goto Top;
case 'e': // First part of .eq. ?
case 'g': // First part of .ge. ?
case 'l': // First part of .lt. ?
case 'n': // First part of .ne. ?
skel--; // point at '.'
return *skel; // return '.'
case 'd':
case 's':
case 't':
if (code_table[x]->operators[0] == *skel || code_table[x]->operators[1] == *skel);
else
{
PRT_ERR (skel, linenumb);
prt_log ("%s(%04d) : Operator '%c' not valid for this skeleton name\n\n", skl_fid, linenumb, *skel);
Terminate (n_errors);
}
if (*(skel+1) != ';')
{
PRT_ERR (skel+1, linenumb);