-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathc_cpp.c
776 lines (620 loc) · 12.4 KB
/
c_cpp.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
/* c_cpp.c
Mike's Enhanced Small C Compiler for Z80 & CP/M
Integrated preprocessor module.
Copyright (c) 1999-2021 Miguel I. Garcia Lopez, FloppySoftware.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Revisions:
16 Jan 2001 : Last revision.
16 Apr 2007 : GPL'd.
14 Aug 2015 : Added support for simple comments: // comment.
26 Oct 2015 : Added included files control (removed from c_iofile.c).
Removed cpp_end(). Cleaned.
20 Jul 2016 : Removed error message in p_macdel() if macro doesn't exists.
12 Aug 2016 : Support for indented #commands.
11 Oct 2016 : Documented and slightly optimized.
12 Oct 2016 : Change input '\t' to ' ' in cpp_read().
13 Oct 2016 : Now cpp_read() skip empty lines (both C and assembler) and comments (assembler).
04 Jan 2021 : Remove space on the right in p_prep().
*/
// Types of symbol preprocess
#define CPP_SYM_NO 0 // No preprocessing of symbols
#define CPP_SYM_YES 1 // Preprocessing of symbols
// Setup CPP
cpp_start()
{
/* NOT NEEDED: ALL INITIAL VALUES ARE 0 ****************
int i;
// Setup initial values
cppinasm =
cppincmt =
cppinign =
cppiflev =
cppifact =
cppmacx =
cppincs = 0;
for(i = 0; i < CPPHASHSIZ; ++i)
cpphash[i] = 0;
********************************************************/
}
// Execute a #command
p_cmd()
{
// Skip #
BfGet();
// Check if #asm is active
if(cppinasm)
{
// Check if it's #endasm
if(InSymEq("endasm"))
{
p_endasm();
BfKill();
return;
}
// Error: #endasm expected
errcont(ERASMWE);
cppinasm = 0;
}
// Following commands are executed always
if(InSymEq("if"))
p_if(0);
else if(InSymEq("else"))
p_else();
else if(InSymEq("endif"))
p_endif();
else if(InSymEq("ifdef"))
p_if(1);
else if(InSymEq("ifndef"))
p_if(2);
// Don't execute following commands if we are in a 'false' if or else
else if(cppinign)
{
BfKill(); return;
}
else if(InSymEq("define"))
p_define();
else if(InSymEq("undef"))
p_undef();
else if(InSymEq("include"))
p_include();
else if(InSymEq("asm"))
p_asm();
else if(InSymEq("endasm"))
p_endasm();
// Unknown #command
else
errcont(ERBADCM);
// Discard remain contents
BfKill();
}
// Define a macro: #define name value
p_define()
{
char s[NAME_SIZ];
// Skip blanks
BfBlanks();
// Get name
if(!symname(s))
{
errcont(ERSYMNM); return;
}
// Skip blanks
BfBlanks();
// Preprocess the value
p_prep(CPP_SYM_YES);
// Add macro
p_macput(s, line);
}
// Undefine a macro: #undef name
p_undef()
{
char s[NAME_SIZ];
// Skip blanks
BfBlanks();
// Get name
if(!symname(s))
{
errcont(ERSYMNM); return;
}
// Delete macro
p_macdel(s);
}
// Include file: #include <filename> | "filename"
p_include()
{
char chend;
// Skip blanks
BfBlanks();
// Preprocess the value
p_prep(CPP_SYM_YES);
// Print message
msgindent(); co_str("#include "); co_line(line);
// Get filename
if(*line=='<') // <filename>
chend='>';
else if(*line=='"') // "filename"
chend='"';
else
errcont(ERBADCM); // Bad syntax
// Skip < or "
BfGet();
// Get filename
while(BfNeq(chend))
{
if(!BfGet())
{
// Error: > or " missing
errcont(ERBADCM); return;
}
}
// Set end of filename
*(line + lptr) = 0;
// Open file
if(cppincs == CPPMAXINC)
errfile(ERTMINC,line+1);
cppfps[cppincs] = fi_fp;
cpplines[cppincs] = fi_line;
strcpy(cppfnames + cppincs * FILENAME_MAX, fi_name);
++cppincs;
fi_open(line + 1);
}
// End included file
p_endinc()
{
fi_close();
fi_fp = cppfps[--cppincs];
fi_line = cpplines[cppincs];
strcpy(fi_name, cppfnames + cppincs * FILENAME_MAX);
fi_eof = 0;
// Print message
msgindent(); co_line("#end include");
}
// Assembler code: #asm
p_asm()
{
// Print message on output file for the optimizer
a_opt(0);
// Set flag
cppinasm = 1;
}
// End assembler code
p_endasm()
{
// Check if #asm is active
if(cppinasm)
{
// Print message on output file for the optimizer
a_opt(1);
// Unset flag
cppinasm = 0;
}
else
errcont(EREASWA);
}
// Test command:
// iftype = 0 -> #if constantValue
// iftype = 1 -> #ifdef macroName
// iftype = 2 -> #ifndef macroName
p_if(iftype)
int iftype;
{
char s[NAME_SIZ];
int val;
// Check for too many active #ifs
if(cppiflev == CPPMAXIFS)
{
errcont(ERTMIFS); return;
}
// Check if we have to execute this #if
if(!(cppiflev == cppifact && cppinign == 0))
{
// Ignore it
++cppiflev; return;
}
// Skip blanks
BfBlanks();
// Add #if level
++cppiflev;
++cppifact;
// Execute it
if(iftype)
{
// Get macro name
if(!symname(s))
{
// Error: bad macro name
errcont(ERSYMNM);
cppinign = 1;
return;
}
// Check if the macro exists
cppinign = p_macfind(s) != -1 ? 0 : 1; // FIXME -- cppinign = p_macfind(s) == -1 ???
// Reverse result on #ifndef
if(iftype == 2)
cppinign = 1 - cppinign; // FIXME -- Would it be better 'cppinign = !cppinign'?
}
else
{
// Preprocess the value
p_prep(CPP_SYM_YES);
// Get value
val = 0; number(&val);
cppinign = val ? 0 : 1; // FIXME -- cppinign = !val ???
}
}
// Test commando: #else
p_else()
{
// Check if there are active #ifs
if(!cppiflev)
errcont(ERELSEW);
else if(cppiflev == cppifact)
{
// Toggle flag
cppinign = 1 - cppinign; // FIXME -- cppinign = !cppinign ???
}
}
// End test command: #endif
p_endif()
{
// Check if there are active #ifs
if(!cppiflev)
{
errcont(ERENDIF); return;
}
// Check if we have to execute this #endif
if(cppiflev == cppifact)
{
--cppifact;
cppinign = 0;
}
// Decrement #if level
--cppiflev;
}
// Compute hash for macro name
p_hash(c)
char c;
{
if(isupper(c))
return c - 'A';
if(islower(c))
return 26 + c - 'a';
return 52;
}
// Add macro
p_macput(name, def)
char *name, *def;
{
int ln, ld;
// Check if there is a macro with that name
if(p_macfind(name) == -1)
{
ln = strlen(name);
ld = strlen(def);
if((cppmacx + ln + ld + 2) <= CPPMACSIZ)
{
// Store macro name and value
cppmac[cppmacx++] = ln;
cppmac[cppmacx++] = ld;
memcpy(cppmac + cppmacx, name, ln);
cppmacx += ln;
memcpy(cppmac + cppmacx, def, ld);
cppmacx += ld;
// Increment hash counter
++cpphash[p_hash(*name)];
}
else
errexit(EROFMAC);
}
else
errcont(ERMACAD);
}
// Delete macro if exists
p_macdel(name)
char *name;
{
int i, size;
if((i = p_macfind(name)) != -1)
{
size = cppmac[i] + (cppmac[i + 1] & 0xFF) + 2;
memcpy(cppmac + i, cppmac + i + size, cppmacx - (i + size));
cppmacx -= size;
--cpphash[p_hash(*name)];
}
}
// Find macro
// out: index on success; else -1
p_macfind(name)
char *name;
{
int i, ln, size;
// Check for user defined macros
if(cpphash[p_hash(*name)])
{
ln = strlen(name);
for(i = 0; i < cppmacx; i += size)
{
if(ln == cppmac[i])
{
if(!memcmp(name, cppmac + i + 2, ln)) // FIXME -- Optimize this
return i;
}
size = cppmac[i] + (cppmac[i + 1] & 0xFF) + 2;
}
}
// Error: macro not found
return -1;
}
// Get line and preprocess it
// out: 0 on success; 1 on EOF
cpp_read()
{
int c, lastc;
while(1)
{
lptr = lastc = 0;
// Read a line
while(1)
{
while(lptr != LN_SIZ)
{
/**************************************************
c = fi_ch();
if(c == '\n' || c == EOF) // FIXME -- Optimize this
break;
***************************************************/
if((c = fi_ch()) == '\n')
break;
if(c == EOF)
break;
if(c == '\t')
c = ' ';
line[lptr++] = lastc = c;
}
// source \ <nl> -> join with next line
// source <nl> -> eol
// source <eof> -> eol
// too-long-source <nl> -> eol
// <nl> -> eol
if(lastc != '\\' || c != '\n' || cppinasm) // FIXME -- Optimize this
break;
--lptr;
++fi_line;
}
// Increment line number
++fi_line;
// Check empty line
if(!lptr)
{
// Check EOF
if(c == EOF)
{
// Check if there are pending included files
if(cppincs)
{
// Continue with previous included file
p_endinc(); continue;
}
// EOF
return 1;
}
// Skip empty lines
continue;
}
// End of string
if(lptr != LN_SIZ)
line[lptr] = '\0';
else
{
// Line too long
line[LN_MAX] = '\0';
errcont(ERLTLNG);
}
// Start from first character in the line
lptr = 0;
// Check for multi-line comments not closed yet
if(cppincmt)
{
while(BfWordNeq('*/'))
{
if(!BfGet())
break;
}
// EOL ?
if(!BfGet())
continue;
BfGet();
// Close multi-line comment
cppincmt = 0;
}
// Skip blanks
BfBlanks();
// Skip empty lines
if(!Bf())
continue;
// Check if it's a #command
if(BfEq('#'))
{
// Preprocess #command line and execute it
p_prep(CPP_SYM_NO);
p_cmd();
}
// Check if we are not in ignoring code (false if, etc.)
else if(!cppinign)
{
// Check if it's an assembler line
if(cppinasm)
{
// Skip assembler comments
if(Bf() != ';')
fo_line(line /* + lptr */);
}
// It's C source code
else {
if(ctext)
{
// Output C source code as assembler comment -- FIXME - This outputs // coments too!
comment(); fo_line(line /* + lptr */);
}
// Preprocess C source code
p_prep(CPP_SYM_YES);
// Success
return 0;
}
}
}
}
// Preprocess input line
// in: type of symbol preprocessing
p_prep(sym)
int sym;
{
int i, x;
char s[NAME_SIZ], *p;
cpptmpx = 0;
while(Bf())
{
if(BfEq(' ') /*|| BfEq('\t')*/) // Compact blanks to 1 space -- FIXME -- Optimize this
{
// Skip blanks
BfBlanks();
// Put 1 space
p_keepch(' ');
}
else if(BfEq('"')) // Check for double quoted strings
{
p_preps('"');
}
else if(BfEq('\'')) // Check for single quoted strings
{
p_preps('\'');
}
else if(BfWordEq('/*')) // Check for multi-line comments
{
BfGet();
BfGet();
// Check for end of multi-line comment
while(BfWordNeq('*/'))
{
if(!BfGet())
{
// Unclosed multi-line comment
cppincmt = 1;
break;
}
}
// Close multi-line comment
BfGet();
BfGet();
}
else if(BfWordEq('//')) // Check for single-line comments
{
break;
}
else if(sym && issym1st(Bf()))
{
// Check for symbol name -- FIXME -- Optimize this (but note that 'i' is used later in p_keepstr!)
i = 0;
do
{
if(i < NAME_MAX)
s[i++] = BfGet();
else
BfGet();
}
while(issym(Bf()));
s[i] = '\0';
// Search for a macro name
if((x = p_macfind(s)) == -1)
{
// Not found: keep as it is
p_keepstr(s, i);
}
else
{
// Found: substitute it by its value
p_keepstr(cppmac + x + 2 + cppmac[x], cppmac[x + 1] & 0xFF);
}
}
else
{
// Keep character as it is
p_keepch(BfGet());
}
}
// Remove space on the right
if(cpptmpx) {
if(cpptmp[cpptmpx - 1] == ' ') {
--cpptmpx;
}
}
// Check for line buffer overlow
if(cpptmpx == LN_SIZ)
{
errcont(ERLTLNG);
--cpptmpx;
}
// EOS
p_keepch(0);
// Copy temporary buffer to line buffer -- FIXME -- Try to optimize this
memcpy(line, cpptmp, cpptmpx);
lptr = 0;
}
// Preprocess quoted string
p_preps(c)
int c;
{
// Get first quote
p_keepch(BfGet());
while(BfNeq(c))
{
// Get \?, including \" and \'
if(BfEq('\\'))
p_keepch(BfGet());
// Get character
if(Bf())
p_keepch(BfGet());
else
break;
}
// Get last quote if any -- FIXME -- What if the line has not a trailing quote?
if(BfGet())
p_keepch(c);
}
// Keep string
p_keepstr(s, len)
char *s; int len;
{
if(cpptmpx + len > LN_SIZ)
len = LN_SIZ - cpptmpx;
memcpy(cpptmp + cpptmpx, s, len);
cpptmpx += len;
}
// Keep character
p_keepch(c)
int c;
{
if(cpptmpx != LN_SIZ)
cpptmp[cpptmpx++] = c;
}
// Indent message according to the number of active #includes
msgindent()
{
int i;
for(i = 0; i < cppincs; ++i)
co_str(" ");
}
// Undefine internal macros
#undef CPP_SYM_NO
#undef CPP_SYM_YES