Changeset 14 for trunk/UBrainFuck.pas


Ignore:
Timestamp:
Feb 11, 2012, 6:27:10 PM (13 years ago)
Author:
chronos
Message:
  • Added: Support for memory cell up to Integer size. Default 256.
  • Fixed: Application now can be closed if running program is waiting to new key press.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBrainFuck.pas

    r13 r14  
    4343  TBrainFuckInterpretter = class
    4444  private
     45    FCellSize: Integer;
    4546    FOnChangeState: TNotifyEvent;
    4647    FState: TRunState;
     
    4849    FThread: TBrainFuckInterpretterThread;
    4950    FStepCount: Integer;
     51    function GetMemorySize: Integer;
    5052    function GetSource: string;
     53    procedure SetMemorySize(AValue: Integer);
    5154    procedure SetSource(AValue: string);
    5255    procedure SetState(AValue: TRunState);
    5356    procedure Write(Value: Byte);
    5457    function Read: Byte;
     58    function TryRead(Output: Byte): Boolean;
    5559    function ReadCode: Char;
    5660    procedure SetThread(State: Boolean);
     
    6165    SourcePosition: Integer;
    6266    SourceBreakpoint: array of Boolean;
    63     Memory: array of Byte;
     67    Memory: array of Integer;
    6468    MemoryPosition: Integer;
    6569    Output: string;
     
    7781    property StepCount: Integer read FStepCount;
    7882    property Source: string read GetSource write SetSource;
     83    property MemorySize: Integer read GetMemorySize write SetMemorySize;
     84    property CellSize: Integer read FCellSize write FCellSize;
    7985  end;
    8086
     
    8894  SJumpTableInsistent = 'Jump table is inconsistent';
    8995  SJumpTableColision = 'Jump table colision';
     96  SMemoryCellOutOfRange = 'Memory cell %s value out of range';
    9097
    9198{ TBrainFuckInterpretterThread }
     
    128135end;
    129136
     137function TBrainFuckInterpretter.GetMemorySize: Integer;
     138begin
     139  Result := Length(Memory);
     140end;
     141
     142procedure TBrainFuckInterpretter.SetMemorySize(AValue: Integer);
     143begin
     144  SetLength(Memory, AValue);
     145end;
     146
    130147procedure TBrainFuckInterpretter.SetSource(AValue: string);
    131148var
     
    140157function TBrainFuckInterpretter.Read: Byte;
    141158begin
    142   while InputPosition >= Length(Input) do begin
     159  while (InputPosition >= Length(Input)) and (FState <> rsStopped) do begin
    143160    Sleep(1);
    144161  end;
    145   Result := Ord(Input[InputPosition + 1]);
    146   Inc(InputPosition);
     162  if InputPosition < Length(Input) then begin
     163    Result := Ord(Input[InputPosition + 1]);
     164    Inc(InputPosition);
     165  end else Result := 0;
     166end;
     167
     168function TBrainFuckInterpretter.TryRead(Output: Byte): Boolean;
     169begin
     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;
    147176end;
    148177
     
    217246begin
    218247  case ReadCode of
    219     '>': if MemoryPosition < Length(Memory) then Inc(MemoryPosition)
     248    '>': if MemoryPosition < MemorySize then Inc(MemoryPosition)
    220249      else raise Exception.Create(SProgramUpperLimit);
    221250    '<': if MemoryPosition > 0 then Dec(MemoryPosition)
    222251      else raise Exception.Create(SProgramLowerLimit);
    223     '+': Memory[MemoryPosition] := Memory[MemoryPosition] + 1;
    224     '-': Memory[MemoryPosition] := Memory[MemoryPosition] - 1;
     252    '+': Memory[MemoryPosition] := ((Memory[MemoryPosition] + 1) mod CellSize);
     253    '-': Memory[MemoryPosition] := ((Memory[MemoryPosition] - 1) mod CellSize);
    225254    '.': Write(Memory[MemoryPosition]);
    226255    ',': Memory[MemoryPosition] := Read;
     
    282311constructor TBrainFuckInterpretter.Create;
    283312begin
    284   SetLength(Memory, 32000);
     313  MemorySize := 30000;
     314  CellSize := 256;
    285315end;
    286316
    287317destructor TBrainFuckInterpretter.Destroy;
    288318begin
     319  FState := rsStopped;
    289320  SetThread(False);
    290321  inherited Destroy;
Note: See TracChangeset for help on using the changeset viewer.