#define (from C) in Python

Rhodri James rhodri at wildebst.demon.co.uk
Fri Nov 13 21:30:13 EST 2009


On Fri, 13 Nov 2009 08:43:14 -0000, Ulrich Eckhardt  
<eckhardt at satorlaser.com> wrote:

> Santiago Romero wrote:
>> Well, In the above concrete example, that would work, but I was
>> talking for multiple code lines, like:
>>
>>
>> #define LD_r_n(reg) (reg) = Z80ReadMem(r_PC++)
>>
>> #define LD_rr_nn(reg)   r_opl = Z80ReadMem(r_PC); r_PC++; \
>>                         r_oph = Z80ReadMem(r_PC); r_PC++; \
>>                         reg = r_op
>>
>> #define LOAD_r(dreg, saddreg)   (dreg)=Z80ReadMem((saddreg))
>>
>> #define LOAD_rr_nn(dreg)   r_opl = Z80ReadMem(r_PC); r_PC++; \
>>                            r_oph = Z80ReadMem(r_PC); r_PC++; \
>>                            r_tmpl = Z80ReadMem(r_op); \
>>                            r_tmph = Z80ReadMem((r_op)+1); \
>>                            dreg=r_tmp
>>
>> #define STORE_nn_rr(dreg) \
>>                         r_opl = Z80ReadMem(r_PC); r_PC++;\
>>                         r_oph = Z80ReadMem(r_PC); r_PC++; \
>>                         r_tmp = dreg; \
>>                         Z80WriteMem((r_op),r_tmpl, regs); \
>>                         Z80WriteMem((r_op+1),r_tmph, regs)
>
> Someone writing such code and calling it C should be taken behind the  
> barn
> and shot.

Having spent all day working on C code with *much* more complex
#defines than that, I beg to disagree.  Particularly in embedded
applications, there are occasions when you absolutely must have
bits of code inlined rather than as function calls.  In those
cases, declaring a function 'inline' is insufficient, as the
compiler can choose to ignore you.

In Python, however, the point is moot; you don't get to play
those games without using a separate preprocessor.  I've never
particularly felt the urge to try, either.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list