unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    PaxCompiler1: TPaxCompiler;
    PaxPascalLanguage1: TPaxPascalLanguage;
    PaxProgram1: TPaxProgram;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure PaxProgram1Exception(Sender: TPaxProgram; E: Exception;
      const ModuleName: String; SourceLineNumber: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  IMPORT_SysUtils;

procedure _Writeln;
begin
  Form1.Memo2.Text := Form1.Memo2.Text + #13#10;
end;

procedure _WriteString(const value: String; L: Integer);
var
  S: String;
begin
  if L = 0 then
    Form1.Memo2.Text := Form1.Memo2.Text + value
  else
  begin
    S := value;
    while Length(S) < L do
      S := ' ' + S;
    Form1.Memo2.Text := Form1.Memo2.Text + S;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PaxCompiler1.Reset;
  PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
  PaxCompiler1.AddModule('main', 'Pascal');
  PaxCompiler1.AddCode('main', Memo1.Lines.Text);
  if PaxCompiler1.Compile(PaxProgram1) then
  begin
    PaxProgram1.SetAddress(H_Writeln, @_Writeln);
    PaxProgram1.SetAddress(H_WriteString, @_WriteString);

    PaxProgram1.Run;
  end
  else
    ShowMessage(PaxCompiler1.ErrorMessage[0]);
end;

procedure TForm1.PaxProgram1Exception(Sender: TPaxProgram; E: Exception;
  const ModuleName: String; SourceLineNumber: Integer);
begin
  Form1.Memo2.Text := Form1.Memo2.Text + #13#10 +
      'Exception (' + E.Message +
      ') raised at line ' + IntToStr(SourceLineNumber) + ':' +
      PaxCompiler1.Modules[ModuleName][SourceLineNumber] + #13#10;
end;

end.
