BrainF*ck

 BrainF*ckインタプリタです。BrainF*ckについてはWikipediaを参照の事。多分、BrainF*ckの仕様に沿っているとは思いますが、間違いがあったら指摘して下さい。

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Memo_KeyPress(Sender: TObject; var Key: Char);
  private
    { Private 宣言 }
    gKey:Char;
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const
  Buf_Len = 30000;
var
  Buf:PChar;
  P1,P2:PChar;
  Dmy:String;
  BCnt:Integer;
begin
  Memo1.SetFocus;
  Memo1.OnKeyPress := nil;
  gKey := #$00;
  GetMem(Buf,Buf_Len);
  ZeroMemory(Buf,Buf_Len);
  P1 := Buf;
  Dmy := Memo1.Lines.Text;
  P2 := PChar(Dmy);
  try
    repeat
      case P2^ of
        '>':Inc(P1);
        '<':Dec(P1);
        '+':Inc(P1^);
        '-':Dec(P1^);
        '.':begin
              Memo1.Lines.Text := Memo1.Lines.Text + P1^;
              Application.ProcessMessages;
            end;
        ',':begin
              Memo1.OnKeyPress := Memo_KeyPress;
              try
                while gKey = #$00 do
                  Application.ProcessMessages;
                if gKey = #$1B then
                  Break;
                P1^ := gKey;
                gKey := #$00;
              finally
                Memo1.OnKeyPress := nil;
              end;
            end;
        '[':begin
              if P1^ = #$00 then
                begin
                  BCnt := 1;
                  repeat
                    Inc(P2);
                    if P2^ = '[' then
                      Inc(BCnt)
                    else if P2^ = ']' then
                      Dec(BCnt);
                  until (P2^ = ']'and (BCnt = 0);
                end;
            end;
        ']':begin
              BCnt := 1;
              repeat
                Dec(P2);
                if P2^ = ']' then
                  Inc(BCnt)
                else if P2^ = '[' then
                  Dec(BCnt);
              until (P2^ = '['and (BCnt = 0);
              Continue;
            end;
      end;
      Inc(P2);
    until(P2^ = #$00);
  finally
    FreeMem(Buf);
  end;
end;

procedure TForm1.Memo_KeyPress(Sender: TObject; var Key: Char);
begin
  gKey := Key;
  Key  := #$00;
end;

end.

 フォームにTMemo(Memo1)とTButton(Button1)を一つずつ貼り付けてTButtonのOnClickイベントにButton1Click()を割り当てます(MemoのOnKeyPressは割り当てる必要はありません)。