Skip to content

Commit ae3e94d

Browse files
committed
Fix preprocessor
Refactorize preprocessor and fixes nested if else conditions.
1 parent 123ee30 commit ae3e94d

3 files changed

Lines changed: 51 additions & 19 deletions

File tree

tests/functional/prepro95.bi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#ifdef MACRO
3+
# define ANOTHER
4+
5+
# ifdef ANOTHER
6+
# error This should never happen
7+
# else
8+
# error This should never happen
9+
# endif
10+
#else
11+
OK
12+
#endif
13+

tests/functional/prepro95.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#line 1 "prepro95.bi"
2+
#line 11 "prepro95.bi"
3+
OK
4+
#line 13 "prepro95.bi"

zxbpp/zxbpp.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import re
1717
import argparse
18+
from typing import NamedTuple, List
1819

1920
from .zxbpplex import tokens # noqa
2021
from . import zxbpplex, zxbasmpplex
@@ -45,10 +46,16 @@
4546
INCLUDEPATH = ('library', 'library-asm')
4647

4748
# Enabled to FALSE if IFDEF failed
48-
ENABLED = True
49+
ENABLED: bool = True
50+
51+
52+
class IfDef(NamedTuple):
53+
enabled: bool
54+
line: int
55+
4956

5057
# IFDEFS array
51-
IFDEFS = [] # Push (Line, state here)
58+
IFDEFS: List[IfDef] = [] # Push (Line, state here)
5259

5360

5461
precedence = (
@@ -484,36 +491,37 @@ def p_ifdef(p):
484491

485492
if ENABLED:
486493
p[0] = [p[2]] + p[3]
487-
p[0] += ['#line %i "%s"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]
488494
else:
489-
p[0] = ['#line %i "%s"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]
495+
p[0] = []
490496

491-
ENABLED = IFDEFS[-1][0]
492-
IFDEFS.pop()
497+
p[0] += ['#line %i "%s"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]
498+
ENABLED = IFDEFS.pop().enabled
493499

494500

495501
def p_ifdef_else(p):
496502
""" ifdef : ifdefelsea ifdefelseb ENDIF
497503
"""
498504
global ENABLED
499505

500-
p[0] = p[1] + p[2]
506+
ENABLED = IFDEFS.pop().enabled
507+
if ENABLED:
508+
p[0] = p[1] + p[2]
509+
else:
510+
p[0] = []
511+
501512
p[0] += ['#line %i "%s"' % (p.lineno(3) + 1, CURRENT_FILE[-1])]
502-
ENABLED = IFDEFS[-1][0]
503-
IFDEFS.pop()
504513

505514

506515
def p_ifdef_else_a(p):
507516
""" ifdefelsea : if_header NEWLINE program
508517
"""
509518
global ENABLED
510519

511-
if ENABLED:
512-
p[0] = [p[2]] + p[3]
513-
else:
514-
p[0] = []
515-
516-
ENABLED = not ENABLED
520+
p[0] = []
521+
if IFDEFS[-1].enabled:
522+
if p[1]:
523+
p[0] = [p[2]] + p[3]
524+
ENABLED = not p[1]
517525

518526

519527
def p_ifdef_else_b(p):
@@ -533,29 +541,36 @@ def p_if_header(p):
533541
"""
534542
global ENABLED
535543

536-
IFDEFS.append((ENABLED, p.lineno(2)))
537-
ENABLED = ID_TABLE.defined(p[2])
544+
IFDEFS.append(IfDef(ENABLED, p.lineno(2)))
545+
if ENABLED:
546+
ENABLED = ID_TABLE.defined(p[2])
547+
548+
p[0] = ENABLED
538549

539550

540551
def p_ifn_header(p):
541552
""" if_header : IFNDEF ID
542553
"""
543554
global ENABLED
544555

545-
IFDEFS.append((ENABLED, p.lineno(2)))
556+
IFDEFS.append(IfDef(ENABLED, p.lineno(2)))
546557
if ENABLED:
547558
ENABLED = not ID_TABLE.defined(p[2])
548559

560+
p[0] = ENABLED
561+
549562

550563
def p_if_expr_header(p):
551564
""" if_header : IF expr
552565
"""
553566
global ENABLED
554567

555-
IFDEFS.append((ENABLED, p.lineno(1)))
568+
IFDEFS.append(IfDef(ENABLED, p.lineno(2)))
556569
if ENABLED:
557570
ENABLED = bool(int(p[2])) if p[2].isdigit() else ID_TABLE.defined(p[2])
558571

572+
p[0] = ENABLED
573+
559574

560575
def p_expr(p):
561576
""" expr : macrocall

0 commit comments

Comments
 (0)