【问题标题】:Pass parameter in pascal define在帕斯卡定义中传递参数
【发布时间】:2017-04-09 11:48:08
【问题描述】:

我正在尝试用 pascal (freepascal) 定义一些东西。 与在 c++ 中一样,您可以像这样传递变量定义宏:

#define REP(i,k) for(int i=0; i<k; i++)

你怎么能用帕斯卡做到这一点?

我在第一行添加了{$MACRO ON} 命令,因此它可以正常运行define 而不会出现{$define lala:='hello world'} 这样的错误。

但是当我尝试{define lala(i):=i} 时,程序出错了。

如何在 Pascal 定义中传递可变参数?

【问题讨论】:

  • 你不想这样做。这在 C++ 中是一个可怕的想法。不要抄袭坏主意。
  • 来自程序员指南的第 1.2.48 节:“在 {$MACRO ON} 状态下,编译器允许使用 C 风格的(虽然没有那么复杂)宏。”请注意“不那么详细”部分。我认为不支持参数。
  • @DavidHeffernan tbh,c 和 c++ 中的宏是它的救命稻草。当然,它可能会被滥用,但要明智地使用,宏可以将复杂的操作转移到编译阶段,从而加快执行速度,尽管可能会增加代码大小。 anyhooo ... 有一种方法可以在 freepascal 中做到这一点 - 请参阅我的回答 stackoverflow.com/a/62401436/830899

标签: macros pascal freepascal preprocessor


【解决方案1】:

使用外部预处理器,并在构建系统中执行它,然后再对结果使用 Pascal 编译器。

FPC 宏系统并非用于元编程,而是仅用于调用标头中的约定宏和紧凑的启用/禁用逻辑(如自制断言和其他调试日志记录代码),并且不支持参数化。

非卫生宏与帕斯卡单位系统根本不兼容,卫生宏被内联函数/过程覆盖。

【讨论】:

  • 感谢您的回答。我只想用 Pascal 编写一些模板,比如 c++ 哈哈哈。那我就用函数和过程吧。
  • FPC 有泛型。
【解决方案2】:

您可以使用宏和包含的组合来实现类似于令牌粘贴的功能...

ACME_Integer.inc

const ACME_1 = ACME_3;
const ACME_2 = pred(1 shl ACME_1);
type  ACME_0 = 0..ACME_2;
//clear the passed in parameters so they can be reused in the next inclusion
{$undef ACME_0}{$undef ACME_1}{$undef ACME_2}{$undef ACME_3}           

somefile.pas

{$macro on}

{$define  ACME_0:=TAllocationPatchIndex}
{$define  ACME_1:=TAllocationPatchIndex_bits}
{$define  ACME_2:=TAllocationPatchIndex_high}
{$define  ACME_3:=16}
{$Include ACME_Integer.inc} 

{$define  ACME_0:=TAllocationId}
{$define  ACME_1:=TAllocationId_bits}
{$define  ACME_2:=TAllocationId_high}
{$define  ACME_3:=28}
{$Include ACME_Integer.inc} 

扩展到

const TAllocationPatchIndex_bits = 16;
const TAllocationPatchIndex_high = pred(1 shl TAllocationPatchIndex_bits);
type  TAllocationPatchIndex= 0..TAllocationPatchIndex_high;

const TAllocationId_bits = 28;
const TAllocationId_high = pred(1 shl TAllocationId_bits);
type  TAllocationId= 0..TAllocationId_high;

虽然此示例使用的代码多于节省的代码,但您显然可以看到包含文件可能是相当复杂的重复代码,需要多次自定义。

更新 - 使用命名/可选参数的更复杂的宏

ACME_Integer.inc

     {$ifdef ACME_packed_array}
         {$undef ACME_array}
         {$define ACME_array:=ACME_packed_array}

         {$undef ACME_array_decl}
         {$define ACME_array_decl:=packed array}
     {$else}

          {$ifdef ACME_bitpacked_array}

             {$undef ACME_array}
             {$define ACME_array:=ACME_bitpacked_array}

             {$undef ACME_array_decl}
             {$define ACME_array_decl:=bitpacked array}

           {$else}
               {$undef ACME_array_decl}
               {$define ACME_array_decl:=array}
           {$endif}
     {$endif}

     {$ifndef ACME_array_type}
         {$define ACME_array_type:=byte}
     {$endif}

     {$ifdef ACME_bitconst}
        // we want to keep the X_bits constant
        const ACME_bitconst = ACME_bits; //X_bits
        {$ifdef ACME_high}
        // we want to keep the X_high constant
        const ACME_high = pred(1 shl ACME_bitconst);    //X_high
        type  ACME_type = 0..ACME_high;             //X
        {$else}
        // we don't care about keeping the X_high constant
        type  ACME_type = 0..pred(1 shl ACME_bitconst); //X
        {$endif}
    {$else}
       {$ifdef ACME_high}
          // we don't care about keeping the X_bits constant
          const ACME_high = pred(1 shl ACME_bits); //X_high
          type  ACME_type = 0..ACME_high;          //X
       {$else}
          // we don't care about keeping the X_high or X_bits constants
          type  ACME_type = 0..pred(1 shl ACME_bits); //X
       {$endif}
    {$endif}

    {$ifdef ACME_array}



      type  ACME_array = ACME_array_decl [ACME_type] of ACME_array_type;

      {$ifdef ACME_array_length}
          const ACME_array_length = sizeof(ACME_array);
      {$endif}


      {$ifdef ACME_array_pointer}
        type  ACME_array_pointer=^ACME_array;
      {$endif}

      {$undef ACME_array}
    {$endif}

{$undef ACME_type}
{$undef ACME_bitconst}
{$undef ACME_high}
{$undef ACME_bits}
{$undef ACME_packed_array}
{$undef ACME_bitpacked_array}
{$undef ACME_array}
{$undef ACME_array_pointer}
{$undef ACME_array_type}
{$undef ACME_array_decl}
{$undef ACME_array_length}

somefile.pas

{$macro on}

{$define  ACME_type:=TSha256IndexSliceMagic}
{$define  ACME_bitconst:=TSha256IndexSliceMagic_bits}
{$define  ACME_high:=TSha256IndexSliceMagic_high}
{$define  ACME_bits:=17}
{$Include ACME_Integer.inc}

{$define  ACME_type:=TSha256DataBufferIndex}
{$define  ACME_bitconst:=TSha256DataBufferIndex_bits}
{$define  ACME_high:=TSha256DataBufferIndex_high}
{$define  ACME_bits:=9}
{$define  ACME_packed_array:=TSha256DataBuffer}
{$define  ACME_array_pointer:=PSha256DataBuffer}
{$Include ACME_Integer.inc}        

扩展到

const TSha256IndexSliceMagic_bits = 17;
const TSha256IndexSliceMagic_high = pred(1 shl TSha256IndexSliceMagic_bits);  
type  TSha256IndexSliceMagic= 0..TSha256IndexSliceMagic_high;


const TSha256DataBufferIndex_bits = 9;
const TSha256DataBufferIndex_high = pred(1 shl TSha256DataBufferIndex_bits);  
type  TSha256DataBufferIndex= 0..TSha256DataBufferIndex_high;
type  TSha256DataBuffer = packed array [TSha256DataBufferIndex] of byte;
type  PSha256DataBuffer = ^TSha256DataBuffer;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多