unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, PaxCompiler, StdCtrls, PaxProgram;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    PaxCompiler1: TPaxCompiler;
    PaxPascalLanguage1: TPaxPascalLanguage;
    PaxProgram1: TPaxProgram;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  t: Integer;
begin
  PaxCompiler1.Reset;
  PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);

  PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);

  PaxCompiler1.AddCode('1', 'function Fact(N: Integer): Integer;');
  PaxCompiler1.AddCode('1', 'begin');
  PaxCompiler1.AddCode('1', '  if N = 1 then');
  PaxCompiler1.AddCode('1', '    result := 1');
  PaxCompiler1.AddCode('1', '  else');
  PaxCompiler1.AddCode('1', '    result := N * Fact(N - 1);');
  PaxCompiler1.AddCode('1', 'end;');
  PaxCompiler1.AddCode('1', 'var');
  PaxCompiler1.AddCode('1', '  I: Integer;');
  PaxCompiler1.AddCode('1', 'begin');
  PaxCompiler1.AddCode('1', '  for I:=1 to 1000000 do');
  PaxCompiler1.AddCode('1', '    Fact(10);');
  PaxCompiler1.AddCode('1', 'end.');

  if PaxCompiler1.Compile(PaxProgram1) then
  begin
    t := GetTickCount;
    PaxProgram1.Run;
    ShowMessage(IntToStr(GetTickCount - t));
  end
  else
    for I:=0 to PaxCompiler1.ErrorCount - 1 do
      ShowMessage(PaxCompiler1.ErrorMessage[I]);
end;

function Fact(N: Integer): Integer;
begin
  if N = 1 then
    result := 1
  else
    result := N * Fact(N - 1);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I, t: Integer;
begin
  t := GetTickCount;
  for I:=1 to 1000000 do
    Fact(10);
  ShowMessage(IntToStr(GetTickCount - t));
end;

end.
