{$O-}
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Label3: TLabel;
    Button1: TButton;
    PaxCompiler1: TPaxCompiler;
    PaxPascalLanguage1: TPaxPascalLanguage;
    PaxProgram1: TPaxProgram;
    PaxCompilerDebugger1: TPaxCompilerDebugger;
    PaxCompilerExplorer1: TPaxCompilerExplorer;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Breakpoint: Integer;
begin
  Memo2.Lines.Clear;
  Breakpoint := StrToInt(Edit1.Text);

  PaxCompiler1.Reset;
  PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);

  PaxCompiler1.AddModule('1', PaxPascalLanguage1.LanguageName);
  PaxCompiler1.AddCode('1', Memo1.Lines.Text);

  PaxCompiler1.DebugMode := true;

  if PaxCompiler1.Compile(PaxProgram1) then
  begin
    PaxCompilerExplorer1.RegisterCompiler(PaxCompiler1);
    PaxCompilerDebugger1.RegisterCompiler(PaxCompiler1, PaxProgram1);
    if PaxCompilerExplorer1.IsExecutableLine('1', Breakpoint) then
      PaxCompilerDebugger1.AddBreakpoint('1', Breakpoint);

    PaxCompilerDebugger1.Run;
    while PaxCompilerDebugger1.IsPaused do
    begin
      ShowMessage('Program has been paused at breakpoint: ' +
         IntToStr(PaxCompilerDebugger1.SourceLineNumber));

      PaxProgram1.Run;
    end;
  end
  else
    for I:=0 to PaxCompiler1.ErrorCount - 1 do
      ShowMessage(PaxCompiler1.ErrorMessage[I]);
end;

procedure Print(C: Char);
begin
  Form1.Memo2.Lines.Add(C);
end;

initialization

RegisterHeader(0, 'procedure Print(C: Char);', @Print);

end.
