{$APPTYPE CONSOLE}
program Project1;
uses
  PAXCOMP_SYS,
  PaxCompiler, PaxProgram, PaxRegister, u;

// IMPORTANT! You have not include IMPORT_u into uses clause!

type
  TMapHandler = class
  public
    procedure DoMapTableVarAddress(Sender: TPaxProgram;
           const FullName: String; Global: Boolean; var Address: Pointer);
    procedure DoMapTableProcAddress(Sender: TPaxProgram;
           const FullName: String; OverCount: Byte;
           Global: Boolean; var Address: Pointer);
    procedure DoMapTableClassRef(Sender: TPaxProgram;
           const FullName: String;
           Global: Boolean; var ClassRef: TClass);
  end;

procedure TMapHandler.DoMapTableVarAddress(Sender: TPaxProgram;
       const FullName: String; Global: Boolean; var Address: Pointer);
begin
  if StrEql(FullName, 'u.I') then
    Address := @u.I;
end;

procedure TMapHandler.DoMapTableProcAddress(Sender: TPaxProgram;
       const FullName: String; OverCount: Byte;
       Global: Boolean; var Address: Pointer);
begin
  if StrEql(FullName, 'u.P') then
    Address := @u.P
  else if StrEql(FullName, 'u.TMyClass.Create') then
    Address := @u.TMyClass.Create
  else if StrEql(FullName, 'u.TMyClass.Proc') then
    Address := @u.TMyClass.Proc;
end;

procedure TMapHandler.DoMapTableClassRef(Sender: TPaxProgram;
       const FullName: String;
       Global: Boolean; var ClassRef: TClass);
begin
  if StrEql(FullName, 'u.TMyClass') then
    ClassRef := TMyClass;
end;

var
  PaxCompiler1: TPaxCompiler;
  PaxProgram1: TPaxProgram;
  PaxPascalLanguage1: TPaxPascalLanguage;
  MapHandler: TMapHandler;
begin
  LoadGlobalSymbolTableFromFile('g.bin');

// IMPORTANT! You have to call LoadGlobalSymbolTableFromFile BEFORE you
// create TPaxCompiler instance.

  PaxCompiler1 := TPaxCompiler.Create(nil);
  PaxProgram1 := TPaxProgram.Create(nil);
  PaxPascalLanguage1 := TPaxPascalLanguage.Create(nil);
  MapHandler := TMapHandler.Create;

  PaxProgram1.OnMapTableVarAddress := MapHandler.DoMapTableVarAddress;
  PaxProgram1.OnMapTableProcAddress := MapHandler.DoMapTableProcAddress;
  PaxProgram1.OnMapTableClassRef := MapHandler.DoMapTableClassref;

  try
    PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
    PaxCompiler1.AddModule('1', 'Pascal');
    PaxCompiler1.AddCodeFromFile('1', 'script.txt');
    if PaxCompiler1.Compile(PaxProgram1) then
    begin
      PaxProgram1.MapGlobal;
      PaxProgram1.Run;
    end
    else
      writeln(PaxCompiler1.ErrorMessage[0]);
  finally
    PaxCompiler1.Free;
    PaxProgram1.Free;
    PaxPascalLanguage1.Free;
    MapHandler.Free;
  end;
  writeln('Press any key...');
  Readln;
end.
