1515import os
1616import re
1717import argparse
18+ from typing import NamedTuple , List
1819
1920from .zxbpplex import tokens # noqa
2021from . import zxbpplex , zxbasmpplex
4546INCLUDEPATH = ('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
5461precedence = (
@@ -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
495501def 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
506515def 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
519527def 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
540551def 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
550563def 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
560575def p_expr (p ):
561576 """ expr : macrocall
0 commit comments