66from .exceptions import PreprocError
77from src .api .debug import __DEBUG__
88
9+ import src .libzxbpp .prepro as prepro
10+
911
1012DEBUG_LEVEL = 3 # Which -d level is required to show debug info
1113
@@ -15,24 +17,26 @@ class MacroCall:
1517 Every time the macro() is called, the macro returns
1618 it value.
1719 """
18- def __init__ (self , lineno , table , id_ , args = None ):
20+ __slots__ = 'table' , 'id_' , 'callargs' , 'lineno'
21+
22+ def __init__ (self , lineno : int , table : 'prepro.DefinesTable' , id_ : str , args = None ):
1923 """ Initializes the object with the ID table, the ID name and
2024 optionally, the passed args.
2125 """
22- self .table = table
23- self .id_ = id_
26+ self .table : 'prepro.DefinesTable' = table
27+ self .id_ : str = id_
2428 self .callargs = args
25- self .lineno = lineno
29+ self .lineno : int = lineno
2630
2731 @staticmethod
28- def eval (arg ):
32+ def eval (arg ) -> str :
2933 """ Evaluates a given argument. The token will be returned by default
3034 "as is", except if it's a macrocall. In such case it will be evaluated
3135 recursively.
3236 """
3337 return str (arg ()) # Evaluate the arg (could be a macrocall)
3438
35- def __call__ (self , symbolTable = None ):
39+ def __call__ (self , symbolTable : 'prepro.DefinesTable' = None ) -> str :
3640 """ Execute the macro call using LAZY evaluation
3741 """
3842 __DEBUG__ ("evaluating '%s'" % self .id_ , DEBUG_LEVEL )
@@ -50,47 +54,49 @@ def __call__(self, symbolTable=None):
5054
5155 # The macro is defined
5256 __DEBUG__ ("macro '%s' defined" % self .id_ , DEBUG_LEVEL )
53- TABLE = copy .deepcopy (symbolTable )
54- ID = TABLE [self .id_ ] # Get the defined macro
55- if ID .hasArgs and self .callargs is None :
57+ table = copy .deepcopy (symbolTable )
58+ id_ = table [self .id_ ] # Get the defined macro
59+ assert isinstance (id_ , prepro .ID )
60+ if id_ .hasArgs and self .callargs is None :
5661 return self .id_ # If no args passed, returned as is
5762
63+ args = []
5864 if self .callargs : # has args. Evaluate them removing spaces
5965 __DEBUG__ ("'%s' has args defined" % self .id_ , DEBUG_LEVEL )
6066 __DEBUG__ ("evaluating %i arg(s) for '%s'" %
6167 (len (self .callargs ), self .id_ ), DEBUG_LEVEL )
62- args = [x (TABLE ).strip () for x in self .callargs ]
68+ args = [x (table ).strip () for x in self .callargs ]
6369 __DEBUG__ ("macro call: %s%s" %
6470 (self .id_ , '(' + ', ' .join (args ) + ')' ), DEBUG_LEVEL )
6571
66- if not ID .hasArgs : # The macro doesn't need args
72+ if not id_ .hasArgs : # The macro doesn't need args
6773 __DEBUG__ ("'%s' has no args defined" % self .id_ , DEBUG_LEVEL )
68- tmp = ID ( TABLE ) # If no args passed, returned as is
74+ tmp = id_ ( table ) # If no args passed, returned as is
6975 if self .callargs is not None :
7076 tmp += '(' + ', ' .join (args ) + ')'
7177
7278 __DEBUG__ ("evaluation result: %s" % tmp , DEBUG_LEVEL )
7379 return tmp
7480
7581 # Now ensure both args and callargs have the same length
76- if len (self .callargs ) != len (ID .args ):
82+ if len (self .callargs ) != len (id_ .args ):
7783 raise PreprocError ('Macro "%s" expected %i params, got %i' %
78- (str (self .id_ ), len (ID .args ),
84+ (str (self .id_ ), len (id_ .args ),
7985 len (self .callargs )), self .lineno )
8086
8187 # Carry out unification
8288 __DEBUG__ ('carrying out args unification' , DEBUG_LEVEL )
8389 for i in range (len (self .callargs )):
84- __DEBUG__ ("arg '%s' = '%s'" % (ID .args [i ].name , args [i ]), DEBUG_LEVEL )
85- TABLE .set (ID .args [i ].name , self .lineno , args [i ])
90+ __DEBUG__ ("arg '%s' = '%s'" % (id_ .args [i ].name , args [i ]), DEBUG_LEVEL )
91+ table .set (id_ .args [i ].name , self .lineno , args [i ])
8692
87- tmp = ID ( TABLE )
93+ tmp = id_ ( table )
8894 if '\n ' in tmp :
8995 tmp += '\n #line %i\n ' % self .lineno
9096
9197 return tmp
9298
93- def is_defined (self , symbolTable = None ):
99+ def is_defined (self , symbolTable : 'prepro.DefinesTable' = None ) -> bool :
94100 """ True if this macro has been defined
95101 """
96102 if symbolTable is None :
0 commit comments