我正在尝试用pascal(freepascal)定义一些东西。 与在c ++中一样,您可以像这样传递变量定义宏:
#define REP(i,k) for(int i=0; i<k; i++)
你怎么能在pascal中做到这一点?
我在第一行添加了{$MACRO ON}
命令,因此它可以运行普通定义而不会出现像{$define lala:='hello world'}
这样的错误。
但是当我尝试{define lala(i):=i}
时程序出错了。
如何在Pascal定义中传递变量参数?
答案 0 :(得分:3)
使用外部预处理器,并在构建系统中执行它,然后在结果上使用Pascal编译器。
FPC宏系统不是用于元编程,而是用于调用头中的约定宏和仅用于压缩启用/禁用逻辑(如home-brewn assert和其他调试日志代码),并且不支持参数化。
非卫生宏与Pascal单元系统根本不兼容,卫生宏由内联功能/程序涵盖。
答案 1 :(得分:-1)
您可以使用宏的组合,并包括一些类似于令牌粘贴的内容...
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;
尽管此示例使用的代码比其保存的代码多,但显然您可以看到包含文件可能是相当复杂的,重复的代码,需要多次自定义。
UPDATE-使用命名/可选参数的更复杂的宏
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;