-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcpp.c
985 lines (915 loc) · 17.3 KB
/
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
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
/* neatcc preprocessor */
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ncc.h"
static char *buf;
static long len;
static long cur;
static struct macro {
char name[NAMELEN]; /* macro name */
char def[MDEFLEN]; /* macro definition */
char args[NARGS][NAMELEN];
int nargs; /* number of arguments */
int isfunc; /* macro is a function */
int undef; /* macro is removed */
} macros[NDEFS];
static int mcount = 1; /* number of macros */
static int mhead[256]; /* macro hash table heads */
static int mnext[NDEFS]; /* macro hash table next entries */
#define BUF_FILE 0
#define BUF_MACRO 1
#define BUF_ARG 2
#define BUF_EVAL 3
#define BUF_TEMP 4
/* preprocessing input buffers for files, macros and macro arguments */
static struct buf {
char *buf;
long len;
long cur;
int type;
/* for BUF_FILE */
char path[NAMELEN];
/* for BUF_MACRO */
struct macro *macro;
char args[NARGS][MARGLEN]; /* arguments passed to a macro */
/* for BUF_ARG */
int arg_buf; /* the bufs index of the owning macro */
} bufs[NBUFS];
static int bufs_n;
static int bufs_limit = 0; /* cpp_read() limit; useful in cpp_eval() */
void die(char *fmt, ...)
{
va_list ap;
char msg[512];
va_start(ap, fmt);
vsprintf(msg, fmt, ap);
va_end(ap);
write(2, msg, strlen(msg));
exit(1);
}
static void buf_new(int type, char *dat, long dlen)
{
if (bufs_n) {
bufs[bufs_n - 1].buf = buf;
bufs[bufs_n - 1].cur = cur;
bufs[bufs_n - 1].len = len;
}
if (bufs_n >= NBUFS)
die("nomem: NBUFS reached!\n");
bufs_n++;
cur = 0;
buf = dat;
len = dlen;
bufs[bufs_n - 1].type = type;
}
static void buf_file(char *path, char *dat, int dlen)
{
buf_new(BUF_FILE, dat, dlen);
strcpy(bufs[bufs_n - 1].path, path ? path : "");
}
static int macro_arg(struct macro *m, char *arg);
static void buf_macro(struct macro *m)
{
struct mem mem;
char *s = m->def;
char arg[NAMELEN];
int len;
int quote = 0;
mem_init(&mem);
while (*s) {
int numsign = 0;
if (quote && s[0] == quote)
quote = 0;
else if (!quote && s[0] == '"')
quote = s[0];
else if (!quote && s[0] == '\'')
quote = s[0];
if (!quote && s[0] == '#')
numsign = s[1] == '#' ? 2 : 1;
if (numsign && s[numsign]) {
struct buf *mbuf = &bufs[bufs_n];
char *r = s + numsign;
char *d = arg;
while (*r && d - arg < sizeof(arg) - 1 &&
(isalnum((unsigned char) *r) || *r == '_'))
*d++ = *r++;
*d++ = '\0';
if (macro_arg(m, arg) >= 0) {
char *def = mbuf->args[macro_arg(m, arg)];
if (def && numsign == 1) {
mem_putc(&mem, '\"');
while (*def) {
if (*def == '\"')
mem_putc(&mem, '\\');
mem_putc(&mem, (unsigned char) *def++);
}
mem_putc(&mem, '\"');
s = r;
continue;
}
if (def && numsign == 2) {
while (*def)
mem_putc(&mem, (unsigned char) *def++);
s = r;
continue;
}
}
}
if (quote && s[0] == '\\')
mem_putc(&mem, (unsigned char) *s++);
if (s[0])
mem_putc(&mem, (unsigned char) *s++);
}
len = mem_len(&mem);
buf_new(BUF_MACRO, mem_get(&mem), len);
mem_done(&mem);
bufs[bufs_n - 1].macro = m;
}
static void buf_arg(char *arg, int mbuf)
{
buf_new(BUF_ARG, arg, strlen(arg));
bufs[bufs_n - 1].arg_buf = mbuf;
}
static void buf_pop(void)
{
bufs_n--;
if (bufs[bufs_n].type == BUF_FILE || bufs[bufs_n].type == BUF_MACRO)
free(buf);
if (bufs_n) {
cur = bufs[bufs_n - 1].cur;
len = bufs[bufs_n - 1].len;
buf = bufs[bufs_n - 1].buf;
}
}
static int buf_iseval(void)
{
int i;
for (i = bufs_n - 1; i >= 0; i--)
if (bufs[i].type == BUF_EVAL)
return 1;
return 0;
}
static size_t file_size(int fd)
{
struct stat st;
if (!fstat(fd, &st))
return st.st_size;
return 0;
}
static int include_file(char *path)
{
int fd = open(path, O_RDONLY);
int n = 0, nr = 0;
char *dat;
int size;
if (fd == -1)
return -1;
size = file_size(fd) + 1;
dat = malloc(size);
while ((n = read(fd, dat + nr, size - nr)) > 0)
nr += n;
close(fd);
dat[nr] = '\0';
buf_file(path, dat, nr);
return 0;
}
int cpp_init(char *path)
{
return include_file(path);
}
static int jumpws(void)
{
int old = cur;
while (cur < len && isspace(buf[cur]))
cur++;
return cur == old;
}
static void read_word(char *dst)
{
jumpws();
while (cur < len && (isalnum(buf[cur]) || buf[cur] == '_'))
*dst++ = buf[cur++];
*dst = '\0';
}
static int jumpcomment(void)
{
if (buf[cur] == '/' && buf[cur + 1] == '*') {
while (++cur < len) {
if (buf[cur] == '*' && buf[cur + 1] == '/') {
cur += 2;
return 0;
}
}
}
if (buf[cur] == '/' && buf[cur + 1] == '/') {
while (++cur < len && buf[cur] != '\n')
if (buf[cur] == '\\')
cur++;
return 0;
}
return 1;
}
static int jumpstr(void)
{
if (buf[cur] == '\'') {
while (++cur < len && buf[cur] != '\'')
if (buf[cur] == '\\')
cur++;
cur++;
return 0;
}
if (buf[cur] == '"') {
while (++cur < len && buf[cur] != '"')
if (buf[cur] == '\\')
cur++;
cur++;
return 0;
}
return 1;
}
static void read_tilleol(char *dst)
{
while (cur < len && isspace(buf[cur]) && buf[cur] != '\n')
cur++;
while (cur < len && buf[cur] != '\n') {
int last = cur;
if (buf[cur] == '\\' && buf[cur + 1] == '\n') {
cur += 2;
continue;
}
if (!jumpstr()) {
memcpy(dst, buf + last, cur - last);
dst += cur - last;
continue;
}
if (!jumpcomment())
continue;
*dst++ = buf[cur++];
}
*dst = '\0';
}
static char *locs[NLOCS] = {};
static int nlocs = 0;
/* header directory */
void cpp_path(char *s)
{
locs[nlocs++] = s;
}
static int include_find(char *name, int std)
{
int i;
for (i = std ? nlocs - 1 : nlocs; i >= 0; i--) {
char path[1 << 10];
if (locs[i])
sprintf(path, "%s/%s", locs[i], name);
else
strcpy(path, name);
if (!include_file(path))
return 0;
}
return -1;
}
static void readarg(char *s)
{
int depth = 0;
int beg = cur;
while (cur < len && (depth || (buf[cur] != ',' && buf[cur] != ')'))) {
if (!jumpstr() || !jumpcomment())
continue;
switch (buf[cur++]) {
case '(':
case '[':
case '{':
depth++;
break;
case ')':
case ']':
case '}':
depth--;
break;
}
}
if (s) {
memcpy(s, buf + beg, cur - beg);
s[cur - beg] = '\0';
}
}
/* find a macro; if undef is nonzero, search #undef-ed macros too */
static int macro_find(char *name, int undef)
{
int i = mhead[(unsigned char) name[0]];
while (i > 0) {
if (!strcmp(name, macros[i].name))
if (!macros[i].undef || undef)
return i;
i = mnext[i];
}
return -1;
}
static void macro_undef(char *name)
{
int i = macro_find(name, 0);
if (i >= 0)
macros[i].undef = 1;
}
static int macro_new(char *name)
{
int i = macro_find(name, 1);
if (i >= 0)
return i;
if (mcount >= NDEFS)
die("nomem: NDEFS reached!\n");
i = mcount++;
strcpy(macros[i].name, name);
mnext[i] = mhead[(unsigned char) name[0]];
mhead[(unsigned char) name[0]] = i;
return i;
}
static void macro_define(void)
{
char name[NAMELEN];
struct macro *d;
read_word(name);
d = ¯os[macro_new(name)];
d->isfunc = 0;
d->nargs = 0;
d->undef = 0;
if (buf[cur] == '(') {
cur++;
jumpws();
while (cur < len && buf[cur] != ')') {
readarg(d->args[d->nargs++]);
jumpws();
if (buf[cur] != ',')
break;
cur++;
jumpws();
}
cur++;
d->isfunc = 1;
}
read_tilleol(d->def);
}
static char ebuf[MARGLEN];
static int elen;
static int ecur;
static long evalexpr(void);
static long cpp_eval(void)
{
char evalbuf[MARGLEN];
int old_limit;
long ret, clen;
char *cbuf;
read_tilleol(evalbuf);
buf_new(BUF_EVAL, evalbuf, strlen(evalbuf));
elen = 0;
ecur = 0;
old_limit = bufs_limit;
bufs_limit = bufs_n;
while (!cpp_read(&cbuf, &clen)) {
memcpy(ebuf + elen, cbuf, clen);
elen += clen;
}
bufs_limit = old_limit;
ret = evalexpr();
buf_pop();
return ret;
}
static void jumpifs(int jumpelse)
{
int depth = 0;
while (cur < len) {
if (buf[cur] == '#') {
char cmd[NAMELEN];
cur++;
read_word(cmd);
if (!strcmp("else", cmd))
if (!depth && !jumpelse)
break;
if (!strcmp("elif", cmd))
if (!depth && !jumpelse && cpp_eval())
break;
if (!strcmp("endif", cmd)) {
if (!depth)
break;
else
depth--;
}
if (!strcmp("ifdef", cmd) || !strcmp("ifndef", cmd) ||
!strcmp("if", cmd))
depth++;
continue;
}
if (!jumpcomment())
continue;
if (!jumpstr())
continue;
cur++;
}
}
static int cpp_cmd(void)
{
char cmd[NAMELEN];
cur++;
read_word(cmd);
if (!strcmp("define", cmd)) {
macro_define();
return 0;
}
if (!strcmp("undef", cmd)) {
char name[NAMELEN];
read_word(name);
macro_undef(name);
return 0;
}
if (!strcmp("ifdef", cmd) || !strcmp("ifndef", cmd) ||
!strcmp("if", cmd)) {
char name[NAMELEN];
int matched = 0;
if (cmd[2]) {
int not = cmd[2] == 'n';
read_word(name);
matched = not ? macro_find(name, 0) < 0 :
macro_find(name, 0) >= 0;
} else {
matched = cpp_eval();
}
if (!matched)
jumpifs(0);
return 0;
}
if (!strcmp("else", cmd) || !strcmp("elif", cmd)) {
jumpifs(1);
return 0;
}
if (!strcmp("endif", cmd))
return 0;
if (!strcmp("include", cmd)) {
char file[NAMELEN];
char *s, *e;
jumpws();
s = buf + cur + 1;
e = strchr(buf + cur + 1, buf[cur] == '"' ? '"' : '>');
memcpy(file, s, e - s);
file[e - s] = '\0';
cur += e - s + 2;
if (include_find(file, *e == '>') == -1)
err("cannot include <%s>\n", file);
return 0;
}
err("unknown directive <%s>\n", cmd);
return 1;
}
static int macro_arg(struct macro *m, char *arg)
{
int i;
for (i = 0; i < m->nargs; i++)
if (!strcmp(arg, m->args[i]))
return i;
return -1;
}
static int buf_arg_find(char *name)
{
int i;
for (i = bufs_n - 1; i >= 0; i--) {
struct buf *mbuf = &bufs[i];
struct macro *m = mbuf->macro;
if (mbuf->type == BUF_MACRO && macro_arg(m, name) >= 0)
return i;
if (mbuf->type == BUF_ARG)
i = mbuf->arg_buf;
}
return -1;
}
static void macro_expand(char *name)
{
struct macro *m;
int mbuf;
if ((mbuf = buf_arg_find(name)) >= 0) {
int arg = macro_arg(bufs[mbuf].macro, name);
char *dat = bufs[mbuf].args[arg];
buf_arg(dat, mbuf);
return;
}
m = ¯os[macro_find(name, 0)];
if (!m->isfunc) {
buf_macro(m);
return;
}
jumpws();
if (buf[cur] == '(') {
int i = 0;
struct buf *mbuf = &bufs[bufs_n];
cur++;
jumpws();
while (cur < len && buf[cur] != ')') {
readarg(mbuf->args[i++]);
jumpws();
if (buf[cur] != ',')
break;
cur++;
jumpws();
}
while (i < m->nargs)
mbuf->args[i++][0] = '\0';
cur++;
buf_macro(m);
}
}
static int buf_expanding(char *macro)
{
int i;
for (i = bufs_n - 1; i >= 0; i--) {
if (bufs[i].type == BUF_ARG)
return 0;
if (bufs[i].type == BUF_MACRO &&
!strcmp(macro, bufs[i].macro->name))
return 1;
}
return 0;
}
/* return 1 for plain macros and arguments and 2 for function macros */
static int expandable(char *word)
{
int i;
if (buf_arg_find(word) >= 0)
return 1;
if (buf_expanding(word))
return 0;
i = macro_find(word, 0);
return i >= 0 ? macros[i].isfunc + 1 : 0;
}
void cpp_define(char *name, char *def)
{
char tmp_buf[MDEFLEN];
sprintf(tmp_buf, "%s\t%s", name, def);
buf_new(BUF_TEMP, tmp_buf, strlen(tmp_buf));
macro_define();
buf_pop();
}
static int seen_macro; /* seen a macro; 2 if a function macro */
static char seen_name[NAMELEN]; /* the name of the last macro */
static int hunk_off;
static int hunk_len;
int cpp_read(char **obuf, long *olen)
{
int old, end;
int jump_name = 0;
*olen = 0;
*obuf = "";
if (seen_macro == 1) {
macro_expand(seen_name);
seen_macro = 0;
}
if (cur == len) {
if (bufs_n < bufs_limit + 1)
return 1;
buf_pop();
}
old = cur;
if (cur < len && buf[cur] == '#')
if (!cpp_cmd())
return 0;
while (cur < len) {
if (!jumpws())
continue;
if (buf[cur] == '#')
break;
if (!jumpcomment())
continue;
if (seen_macro == 2) {
if (buf[cur] == '(')
macro_expand(seen_name);
seen_macro = 0;
old = cur;
continue;
}
if (!jumpstr())
continue;
if (isalnum(buf[cur]) || buf[cur] == '_') {
char word[NAMELEN];
read_word(word);
seen_macro = expandable(word);
if (seen_macro) {
strcpy(seen_name, word);
jump_name = 1;
break;
}
if (buf_iseval() && !strcmp("defined", word)) {
int parens = 0;
jumpws();
if (buf[cur] == '(') {
parens = 1;
cur++;
}
read_word(word);
if (parens) {
jumpws();
cur++;
}
}
continue;
}
cur++;
}
/* macros are expanded later; ignoring their names */
end = jump_name ? cur - strlen(seen_name) : cur;
if (!buf_iseval()) {
hunk_off += hunk_len;
hunk_len = end - old;
}
*obuf = buf + old;
*olen = end - old;
return 0;
}
/* preprocessor constant expression evaluation */
#define TOK2(a) ((a)[0] << 16 | (a)[1] << 8)
#define TOK_NAME 256
#define TOK_NUM 257
#define TOK_EOF -1
static char etok[NAMELEN];
static int enext;
static char *tok2[] = {
"<<", ">>", "&&", "||", "==", "!=", "<=", ">="
};
static int eval_tok(void)
{
char *s = etok;
int i;
while (ecur < elen) {
while (ecur < elen && isspace(ebuf[ecur]))
ecur++;
if (ebuf[ecur] == '/' && ebuf[ecur + 1] == '*') {
while (ecur < elen && (ebuf[ecur - 2] != '*' ||
ebuf[ecur - 1] != '/'))
ecur++;
continue;
}
break;
}
if (ecur >= elen)
return TOK_EOF;
if (isalpha(ebuf[ecur]) || ebuf[ecur] == '_') {
while (isalnum(ebuf[ecur]) || ebuf[ecur] == '_')
*s++ = ebuf[ecur++];
*s = '\0';
return TOK_NAME;
}
if (isdigit(ebuf[ecur])) {
while (isdigit(ebuf[ecur]))
*s++ = ebuf[ecur++];
while (tolower(ebuf[ecur]) == 'u' || tolower(ebuf[ecur]) == 'l')
ecur++;
*s = '\0';
return TOK_NUM;
}
for (i = 0; i < LEN(tok2); i++)
if (TOK2(tok2[i]) == TOK2(ebuf + ecur)) {
int ret = TOK2(tok2[i]);
ecur += 2;
return ret;
}
return ebuf[ecur++];
}
static int eval_see(void)
{
if (enext == -1)
enext = eval_tok();
return enext;
}
static int eval_get(void)
{
if (enext != -1) {
int ret = enext;
enext = -1;
return ret;
}
return eval_tok();
}
static long eval_num(void)
{
return atol(etok);
}
static int eval_jmp(int tok)
{
if (eval_see() == tok) {
eval_get();
return 0;
}
return 1;
}
static void eval_expect(int tok)
{
eval_jmp(tok);
}
static char *eval_id(void)
{
return etok;
}
static long evalcexpr(void);
static long evalatom(void)
{
if (!eval_jmp(TOK_NUM))
return eval_num();
if (!eval_jmp(TOK_NAME)) {
int parens = !eval_jmp('(');
long ret;
eval_expect(TOK_NAME);
ret = macro_find(eval_id(), 0) >= 0;
if (parens)
eval_expect(')');
return ret;
}
if (!eval_jmp('(')) {
long ret = evalcexpr();
eval_expect(')');
return ret;
}
return -1;
}
static long evalpre(void)
{
if (!eval_jmp('!'))
return !evalpre();
if (!eval_jmp('-'))
return -evalpre();
if (!eval_jmp('~'))
return ~evalpre();
return evalatom();
}
static long evalmul(void)
{
long ret = evalpre();
while (1) {
if (!eval_jmp('*')) {
ret *= evalpre();
continue;
}
if (!eval_jmp('/')) {
ret /= evalpre();
continue;
}
if (!eval_jmp('%')) {
ret %= evalpre();
continue;
}
break;
}
return ret;
}
static long evaladd(void)
{
long ret = evalmul();
while (1) {
if (!eval_jmp('+')) {
ret += evalmul();
continue;
}
if (!eval_jmp('-')) {
ret -= evalmul();
continue;
}
break;
}
return ret;
}
static long evalshift(void)
{
long ret = evaladd();
while (1) {
if (!eval_jmp(TOK2("<<"))) {
ret <<= evaladd();
continue;
}
if (!eval_jmp(TOK2(">>"))) {
ret >>= evaladd();
continue;
}
break;
}
return ret;
}
static long evalcmp(void)
{
long ret = evalshift();
while (1) {
if (!eval_jmp('<')) {
ret = ret < evalshift();
continue;
}
if (!eval_jmp('>')) {
ret = ret > evalshift();
continue;
}
if (!eval_jmp(TOK2("<="))) {
ret = ret <= evalshift();
continue;
}
if (!eval_jmp(TOK2(">="))) {
ret = ret >= evalshift();
continue;
}
break;
}
return ret;
}
static long evaleq(void)
{
long ret = evalcmp();
while (1) {
if (!eval_jmp(TOK2("=="))) {
ret = ret == evalcmp();
continue;
}
if (!eval_jmp(TOK2("!="))) {
ret = ret != evalcmp();
continue;
}
break;
}
return ret;
}
static long evalbitand(void)
{
long ret = evaleq();
while (!eval_jmp('&'))
ret &= evaleq();
return ret;
}
static long evalxor(void)
{
long ret = evalbitand();
while (!eval_jmp('^'))
ret ^= evalbitand();
return ret;
}
static long evalbitor(void)
{
long ret = evalxor();
while (!eval_jmp('|'))
ret |= evalxor();
return ret;
}
static long evaland(void)
{
long ret = evalbitor();
while (!eval_jmp(TOK2("&&")))
ret = ret && evalbitor();
return ret;
}
static long evalor(void)
{
long ret = evaland();
while (!eval_jmp(TOK2("||")))
ret = ret || evaland();
return ret;
}
static long evalcexpr(void)
{
long ret = evalor();
if (eval_jmp('?'))
return ret;
if (ret)
return evalor();
while (eval_get() != ':')
;
return evalor();
}
static long evalexpr(void)
{
enext = -1;
return evalcexpr();
}
static int buf_loc(char *s, int off)
{
char *e = s + off;
int n = 1;
while ((s = strchr(s, '\n')) && s < e) {
n++;
s++;
}
return n;
}
char *cpp_loc(long addr)
{
static char loc[256];
int line = -1;
int i;
for (i = bufs_n - 1; i > 0; i--)
if (bufs[i].type == BUF_FILE)
break;
if (addr >= hunk_off && i == bufs_n - 1)
line = buf_loc(buf, (cur - hunk_len) + (addr - hunk_off));
else
line = buf_loc(bufs[i].buf, bufs[i].cur);
sprintf(loc, "%s:%d", bufs[i].path, line);
return loc;
}