Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - How do I build an array of const?

I am implementing an interpreter, and one of the functions my interpreter will support is like Delphi's Format. In fact, I'm implementing my function using SysUtils.Format. However, I'm having trouble building the second parameter to the function, the array of TVarRec.

Suppose I have the following code. For now, I just assume which Delphi variables the interpreted code will need access to (iVar1 and iVar2), but I still don't know how to put them into the structure that Format requires (arFormatArgs).

type TFormatArgs = array of TVarRec;

procedure RunInterpretedFormatFunction;
var
  iMyAge: integer;
  iMyIQ: integer;
  sCode: string;
  sText: string;
begin
  iMyAge := 5;
  iMyIQ := -5;
  sCode := 'Format(''My age is %d and my IQ is %d'', [iMyAge, iMyIQ])';
  sText := FormatThis(sCode, iMyAge, iMyIQ);
end;

function FormatThis(sFormatCode: string; iVar1: integer; iVar2: integer): string;
var
  sFormatString: string;
  arFormatArgs: TFormatArgs;
begin
  sFormatString := GetFormatString(sFormatCode); // I can implement this function
  arFormatArgs := ConstructFormatArgs(iVar1, iVar2); // NEED HELP HERE!
  result := SysUtils.Format(sFormatString, arFormatArgs);
end;

How can I implement my ConstructFormatArgs function in Delphi (not Assembly)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The array of const gives you the freedom to add strings, integers, floats and so on and having these formatted into a string. And there is no limit to how many items you can add.

The way Delphi deals with this issue is that the array of const really is a array of TVarRec's.

A TVarRec is a record of the following type:

TVarRec = record
  case Byte of
    vtInteger:    (VInteger: Integer; VType: Byte);
    vtBoolean:    (VBoolean: Boolean);
    vtChar:       (VChar: Char);
    vtExtended:   (VExtended: PExtended);
    vtString:     (VString: PShortString);
    vtPointer:    (VPointer: Pointer);
    vtPChar:      (VPChar: PChar);
    vtObject:     (VObject: TObject);
    vtClass:      (VClass: TClass);
    vtWideChar:   (VWideChar: WideChar);
    vtPWideChar:  (VPWideChar: PWideChar);
    vtAnsiString: (VAnsiString: Pointer);
    vtCurrency:   (VCurrency: PCurrency);
    vtVariant:    (VVariant: PVariant);

The type of the value inside the TVarRec is determined by the VType value.

This gives you the flexibility to add either type you wish to the array of const, like in the Format() function:

Format( '%s is a string, %d is an integer', ['string',10] );

Using the array of const in your own procedure is no big deal. Take a look at this example:

 procedure VarArraySample( AVarArray : array of const );
  var
    i : integer;
  begin
    for i := 0 to High(AVarArray) do
      do_something;
  end;

The function High() returns the last index of the array.

You can also convert the contents of the TVarRec. This example is taken from the Delphi on-line help and revamped a bit. The function converts a TVarRec to a string:

function VarRecToStr( AVarRec : TVarRec ) : string;
  const
    Bool : array[Boolean] of string = ('False', 'True');
  begin
    case AVarRec.VType of
      vtInteger:    Result := IntToStr(AVarRec.VInteger);
      vtBoolean:    Result := Bool[AVarRec.VBoolean];
      vtChar:       Result := AVarRec.VChar;
      vtExtended:   Result := FloatToStr(AVarRec.VExtended^);
      vtString:     Result := AVarRec.VString^;
      vtPChar:      Result := AVarRec.VPChar;
      vtObject:     Result := AVarRec.VObject.ClassName;
      vtClass:      Result := AVarRec.VClass.ClassName;
      vtAnsiString: Result := string(AVarRec.VAnsiString);
      vtCurrency:   Result := CurrToStr(AVarRec.VCurrency^);
      vtVariant:    Result := string(AVarRec.VVariant^);
    else
      result := '';
    end;
  end;

You can combine the two functions above to one function that converts all elements in the array of const into one string:

function VarArrayToStr( AVarArray : array of const ) : string;
  var
    i : integer;
  begin
    result := '';
    for i := 0 to High(AVarArray) do
      result := result + VarRecToStr( AVarArray[i] );
  end;

you will now be able to create your own Format() function. The Format() function scans for %'s and replaces the %something with the value in the array of const, depending on the format specifiers and precision specifiers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...