Changeset 17


Ignore:
Timestamp:
Feb 11, 2012, 7:41:10 PM (12 years ago)
Author:
chronos
Message:
  • Modified: Optimized brainfuck command Write.
  • Added: All nonusable characters are removed before starting interpretter program.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBrainFuck.pas

    r14 r17  
    5656    procedure Write(Value: Byte);
    5757    function Read: Byte;
    58     function TryRead(Output: Byte): Boolean;
    59     function ReadCode: Char;
    6058    procedure SetThread(State: Boolean);
    6159    procedure PrepareJumpTable;
     60    procedure RemoveBlankCharacters;
    6261  public
    6362    FSource: array of Char;
     
    6867    MemoryPosition: Integer;
    6968    Output: string;
     69    OutputPosition: Integer;
    7070    Input: string;
    7171    InputPosition: Integer;
     
    115115procedure TBrainFuckInterpretter.Write(Value: Byte);
    116116begin
    117   Output := Output + Char(Value);
     117  if OutputPosition > Length(Output) then
     118    SetLength(Output, Length(Output) + 1 + Length(Output) div 4);
     119  Output[OutputPosition] := Char(Value);
     120  Inc(OutputPosition);
    118121end;
    119122
     
    157160function TBrainFuckInterpretter.Read: Byte;
    158161begin
    159   while (InputPosition >= Length(Input)) and (FState <> rsStopped) do begin
     162  while (InputPosition > Length(Input)) and (FState <> rsStopped) do begin
    160163    Sleep(1);
    161164  end;
    162   if InputPosition < Length(Input) then begin
    163     Result := Ord(Input[InputPosition + 1]);
     165  if InputPosition <= Length(Input) then begin
     166    Result := Ord(Input[InputPosition]);
    164167    Inc(InputPosition);
    165168  end else Result := 0;
    166 end;
    167 
    168 function TBrainFuckInterpretter.TryRead(Output: Byte): Boolean;
    169 begin
    170   if InputPosition >= Length(Input) then Result := False
    171   else begin
    172     Output := Ord(Input[InputPosition + 1]);
    173     Inc(InputPosition);
    174     Result := True;
    175   end;
    176 end;
    177 
    178 function TBrainFuckInterpretter.ReadCode: Char;
    179 begin
    180   Result := FSource[SourcePosition];
    181169end;
    182170
     
    224212end;
    225213
     214procedure TBrainFuckInterpretter.RemoveBlankCharacters;
     215var
     216  I: Integer;
     217  LastChar: Integer;
     218begin
     219  LastChar := 0;
     220  for I := 0 to Length(FSource) - 1 do
     221    if FSource[I] in ['+','-','>','<','.',',','[',']'] then begin
     222      FSource[LastChar] := FSource[I];
     223      Inc(LastChar);
     224    end;
     225  SetLength(FSource, LastChar);
     226end;
     227
    226228procedure TBrainFuckInterpretter.Reset;
    227229var
    228230  I: Integer;
    229231begin
     232  RemoveBlankCharacters;
    230233  PrepareJumpTable;
    231234  SourcePosition := 0;
    232   InputPosition := 0;
     235  InputPosition := 1;
    233236  Output := '';
     237  OutputPosition := 1;
    234238  MemoryPosition := 0;
    235239  //FillChar(Pointer(Memory)^, Length(Memory), 0);
     
    245249  NewPos: Integer;
    246250begin
    247   case ReadCode of
     251  case FSource[SourcePosition] of
    248252    '>': if MemoryPosition < MemorySize then Inc(MemoryPosition)
    249253      else raise Exception.Create(SProgramUpperLimit);
Note: See TracChangeset for help on using the changeset viewer.