Metasyntax/Macros

Donald 'Paddy' McCarthy paddy3118 at blueyonder.co.ukNOTthisBIT
Wed May 28 17:38:24 EDT 2003


Donald 'Paddy' McCarthy wrote:

> Defining a macro:
> -----------------

> from __future__ import macros
> 
> macro IN_MACRO:
>   ''' - Verilog like input definition
>   in a,b,c   ==expands to==> a = In("a"); b = In("b"); c = In("c")
>   '''
>   # New syntax extension rules (productions) use equals sign.
>   # whitespace on RHS of = below can be any or no white
>   # -space separators found in source,
>   input_stament = 'in' __name__ ( ',' __name__ )*
>   # extend the predefined statements with the new input statement
>   # Can also be interpreted as stating where to look for the new macro
>   __statement__ = __statement__ | input_statement
>   # Save some typing :-)
>   i = input_statement
>   # Compute the expansion (I have used := for this )
>   expand := i[0] ' = In("'i[0]'")' \
>     ( __newline__ i[1][j] ' = In("'i[1][j]'")' )*[ for j in len(i[1])]
>   # macro definitions return their expansion
>   return expand
> 
> in x, y,  zed
> 
> IN_MACRO.disable()
> 
> in s
> Traceback (  File "<interactive input>", line 1
>     in s
>      ^
> SyntaxError: invalid syntax

Anyone for meta-macro programming ;-)





More information about the Python-list mailing list