paxCompiler for Delphi. Access to script-defined variables.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, PaxCompiler, PaxProgram;
type
  TForm1 = class(TForm)
    PaxCompiler1: TPaxCompiler;
    PaxPascalLanguage1: TPaxPascalLanguage;
    PaxProgram1: TPaxProgram;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  H_X: Integer;
  P: Pointer;
  I: Integer;
begin
 {$O-}
  PaxCompiler1.Reset;
  PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);

  PaxCompiler1.RegisterHeader(0, 'procedure ShowMessage(const Msg: string);', @ShowMessage);
  PaxCompiler1.RegisterHeader(0, 'function IntToStr(Value: Integer): string;', @IntToStr);

  PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);
  PaxCompiler1.AddCode('1', 'var x: Integer = 5;');
  PaxCompiler1.AddCode('1', 'begin');
  PaxCompiler1.AddCode('1', '  ShowMessage(''script:'' + IntToStr(x));');
  PaxCompiler1.AddCode('1', 'end.');

  if PaxCompiler1.Compile(PaxProgram1) then
  begin
    H_X := PaxCompiler1.GetHandle(0, 'x', true);
    PaxProgram1.Run; // the first run
    if H_X <> 0 then
    begin
      P := PaxProgram1.GetAddress(H_X);
      ShowMessage('host:' + IntToStr(Integer(P^))); // show script-defined var
    end;
    Integer(P^) := 30; // change script-defind variable
    PaxProgram1.Run; // the second run
  end
  else
    for I:=0 to PaxCompiler1.ErrorCount do
      ShowMessage(PaxCompiler1.ErrorMessage[I]);
end;

end.


Copyright © 2006-2017 Alexander Baranovsky. All rights reserved.