宏替换帮助请求 [英] Macro Substitution Help Request

查看:66
本文介绍了宏替换帮助请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下内容失败,并且在宏观数量上存在分歧

参数使用Imagecraft ICCAVR进行编译时。有没有人得到任何

的想法 - 它不是至关重要但会使代码更好看:


#define SET(x,y)(x | =(1<< y))

#define PIEZO PORTB,5

然后在代码中它失败了:


SET(PIEZO);


当我这样做时,SET宏工作:


SET(PORTB,5);


任何帮助表示赞赏。我已经搜索了新闻组,但找不到我要找的




马尔科姆。

解决方案

" Malcolm" <毫安********** @ gmail.com>写道:





#我有以下内容因宏观数量不一致而失败

#arguments"使用Imagecraft ICCAVR进行编译时。有没有人得到任何

#想法 - 它不是至关重要但会让代码更好看:



#define SET (x,y)(x | =(1 <<< y))

#define PIEZO PORTB,5





#然后在代码中我失败了:



#SET(PIEZO);


预处理器将其分块为

define-name:SET

open-paren:(

参数:PIEZO
define-name:PIEZO

参数列表:空

close-paren:)

然后进行替换

define-name:SET

open-paren :(

参数:PORTB,5

define-value: PORTB,5

close-paren:)


一些预处理器在进行替换后会重新出现,但是你的

显然没有(也不是必须的)。因此,它将SET的整个第一个参数x设置为PORTB,5,并将其设置为PORTB,5。并且找不到

的第二个参数。


#SET宏在我这样做时起作用:



#SET(PORTB,5);


这次预处理器将其分块为

define-name:SET

open-paren :(

参数:PORTB

逗号:,

参数:5

关闭-paren:)


CPP不是一个特别强大的宏处理器;其他人可以使用

将更好地处理这个问题。你可能有类似M5或sed的东西,

或者你可以自己做预处理器。


-

SM瑞安 http://www.rawbw.com/~wyrmwif/

我希望对你来说感觉很好。没有什么比这更令人兴奋地指出别人的缺点了,是吗?


感谢您的回复。深思熟虑..


" Malcolm" <毫安********** @ gmail.com>写道:



我有以下内容因与宏的
参数数量不一致而失败。使用Imagecraft ICCAVR进行编译时。有没有人有任何想法 - 它不重要但会使代码更好阅读:

#define SET(x,y)(x | =(1<< ))
#define PIEZO PORTB,5

然后在代码中我失败了:

SET(PIEZO);

SET宏在我这样做时起作用:

SET(PORTB,5);

任何帮助表示赞赏。我已经搜索过新闻组,但找不到我正在寻找的东西。




这里可能会有类似的内容

你想要的:


#define SET(x,y)((x)| = 1u<<(y))

#define PIEZO(PORTB,5)

#define INVOKE(x)x


INVOKE(SET PIEZO);


它不是特别漂亮,但它似乎允许使用PIEZO作为SET的参数来使用




顺便提一下,SET宏的定义对于某些样式改进,

略有改写。

一般情况下宏参数应该在宏定义的括号中包含



Hi,

I have the following which fails with "disagreement in number of macro
arguments" when compiling with Imagecraft ICCAVR. Has anyone got any
ideas - its not vital but would make the code a lot nicer to read:

#define SET(x,y) (x |=(1<<y))
#define PIEZO PORTB,5
then in code it fails when I do:

SET(PIEZO);

The SET macro works when I do:

SET(PORTB,5);

Any help appreciated. I have trawled the newsgroups but cannot find
what I''m looking for.

Malcolm.

解决方案

"Malcolm" <ma**********@gmail.com> wrote:
# Hi,
#
# I have the following which fails with "disagreement in number of macro
# arguments" when compiling with Imagecraft ICCAVR. Has anyone got any
# ideas - its not vital but would make the code a lot nicer to read:
#
# #define SET(x,y) (x |=(1<<y))
# #define PIEZO PORTB,5
#
#
# then in code it fails when I do:
#
# SET(PIEZO);

The preprocessor is chunking it up as
define-name: SET
open-paren: (
argument: PIEZO
define-name: PIEZO
argument-list: empty
close-paren: )
and then doing the substitutions
define-name: SET
open-paren: (
argument: PORTB,5
define-value: PORTB,5
close-paren: )

Some preprocessors will rechunk after doing substitutions, but yours
apparently does not (nor is it required to). So it is setting the
entire first argument x of SET to "PORTB,5" and cannot find the
second argument.

# The SET macro works when I do:
#
# SET(PORTB,5);

This time the preprocessor is chunking it up as
define-name: SET
open-paren: (
argument: PORTB
comma: ,
argument: 5
close-paren: )

CPP is not a particularly powerful macro processor; others are available that
will handle this better. You might have something like M5 or sed available,
or you can do your own preprocessor.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There''s nothing more
exhilarating pointing out the shortcomings of others, is there?


Thanks for the reply. Food for thought..


"Malcolm" <ma**********@gmail.com> writes:

Hi,

I have the following which fails with "disagreement in number of macro
arguments" when compiling with Imagecraft ICCAVR. Has anyone got any
ideas - its not vital but would make the code a lot nicer to read:

#define SET(x,y) (x |=(1<<y))
#define PIEZO PORTB,5
then in code it fails when I do:

SET(PIEZO);

The SET macro works when I do:

SET(PORTB,5);

Any help appreciated. I have trawled the newsgroups but cannot find
what I''m looking for.



Here is something that may do something like what
you want:

#define SET(x,y) ((x) |= 1u<<(y))
#define PIEZO ( PORTB, 5 )
#define INVOKE(x) x

INVOKE( SET PIEZO );

It''s not especially pretty, but it seems to permit
using PIEZO as an argument to SET.

Incidentally, the definition of the SET macro was
rewritten slightly for some style improvements.
Generally macro parameters should be enclosed
in parentheses inside of macro definitions.


这篇关于宏替换帮助请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆