Changeset 7 for trunk


Ignore:
Timestamp:
Feb 9, 2012, 3:37:38 PM (13 years ago)
Author:
chronos
Message:
  • Modified: Interpretter now run machine in separate thread. No thread safe so far.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11lib
        22LazFuckIDE.exe
         3LazFuckIDE.lps
  • trunk/UBrainFuck.pas

    r6 r7  
    99
    1010type
     11  TBrainFuckInterpretter = class;
    1112
    1213  { TBrainFuck }
     
    2021  TRunState = (rsStopped, rsPaused, rsRunning);
    2122
    22   { TBrainFuckInterpreter }
    23 
    24   TBrainFuckInterpreter = class
     23  { TBrainFuckInterpretterThread }
     24
     25  TBrainFuckInterpretterThread = class(TThread)
     26    Parent: TBrainFuckInterpretter;
     27    procedure Execute; override;
     28  end;
     29
     30  { TBrainFuckInterpretter }
     31
     32  TBrainFuckInterpretter = class
    2533  private
    2634    FOnChangeState: TNotifyEvent;
    2735    FState: TRunState;
     36    FThreadState: Boolean;
     37    FThread: TBrainFuckInterpretterThread;
    2838    procedure SetState(AValue: TRunState);
    2939    procedure Write(Value: Byte);
    3040    function Read: Byte;
    3141    function ReadCode: Char;
     42    procedure SetThread(State: Boolean);
    3243  public
    3344    Source: string;
     
    6071  SReadInputError = 'Read input error';
    6172
    62 { TBrainFuckInterpreter }
    63 
    64 procedure TBrainFuckInterpreter.Write(Value: Byte);
     73{ TBrainFuckInterpretterThread }
     74
     75procedure TBrainFuckInterpretterThread.Execute;
     76begin
     77  repeat
     78    while Parent.SourcePosition < Length(Parent.Source) do begin
     79      Parent.SingleStep;
     80      while Parent.State = rsPaused do begin
     81        Sleep(1);
     82      end;
     83    end;
     84    Parent.SetState(rsStopped);
     85  until Terminated;
     86end;
     87
     88{ TBrainFuckInterpretter }
     89
     90procedure TBrainFuckInterpretter.Write(Value: Byte);
    6591begin
    6692  Output := Output + Char(Value);
    6793end;
    6894
    69 procedure TBrainFuckInterpreter.SetState(AValue: TRunState);
     95procedure TBrainFuckInterpretter.SetState(AValue: TRunState);
    7096begin
    7197  if FState = AValue then Exit;
     
    74100end;
    75101
    76 function TBrainFuckInterpreter.Read: Byte;
     102function TBrainFuckInterpretter.Read: Byte;
    77103var
    78104  Character: string;
     
    87113end;
    88114
    89 function TBrainFuckInterpreter.ReadCode: Char;
     115function TBrainFuckInterpretter.ReadCode: Char;
    90116var
    91117  Code: string;
     
    95121end;
    96122
    97 procedure TBrainFuckInterpreter.Reset;
     123procedure TBrainFuckInterpretter.SetThread(State: Boolean);
     124begin
     125  if FThreadState = State then Exit;
     126  FThreadState := State;
     127  if State then begin
     128    FThread := TBrainFuckInterpretterThread.Create(True);
     129    FThread.Parent := Self;
     130    FThread.FreeOnTerminate := False;
     131    FThread.Start;
     132  end else begin
     133    FThread.Free;
     134  end;
     135end;
     136
     137procedure TBrainFuckInterpretter.Reset;
    98138var
    99139  I: Integer;
     
    109149end;
    110150
    111 procedure TBrainFuckInterpreter.SingleStep;
     151procedure TBrainFuckInterpretter.SingleStep;
    112152var
    113153  CodeText: string;
     
    155195end;
    156196
    157 procedure TBrainFuckInterpreter.Run;
     197procedure TBrainFuckInterpretter.Run;
    158198begin
    159199  SetState(rsRunning);
    160200  Reset;
    161   while SourcePosition < Length(Source) do begin
    162     SingleStep;
    163     Application.ProcessMessages;
    164     while State = rsPaused do begin
    165       Sleep(1);
    166       Application.ProcessMessages;
    167     end;
    168   end;
     201  SetThread(True);
     202end;
     203
     204procedure TBrainFuckInterpretter.Pause;
     205begin
     206  if State = rsRunning then SetState(rsPaused);
     207end;
     208
     209procedure TBrainFuckInterpretter.Stop;
     210begin
    169211  SetState(rsStopped);
    170212end;
    171213
    172 procedure TBrainFuckInterpreter.Pause;
    173 begin
    174   if State = rsRunning then SetState(rsPaused);
    175 end;
    176 
    177 procedure TBrainFuckInterpreter.Stop;
    178 begin
    179   SetState(rsStopped);
    180 end;
    181 
    182 constructor TBrainFuckInterpreter.Create;
     214constructor TBrainFuckInterpretter.Create;
    183215begin
    184216  SetLength(Memory, 32000);
    185217end;
    186218
    187 destructor TBrainFuckInterpreter.Destroy;
    188 begin
     219destructor TBrainFuckInterpretter.Destroy;
     220begin
     221  SetThread(False);
    189222  inherited Destroy;
    190223end;
  • trunk/UMainForm.pas

    r6 r7  
    7878    Modified: Boolean;
    7979    ProjectFileName: string;
    80     BrainFuckInterpreter: TBrainFuckInterpreter;
     80    BrainFuckInterpreter: TBrainFuckInterpretter;
    8181    procedure UpdateInterface;
    8282  end;
     
    129129procedure TMainForm.FormCreate(Sender: TObject);
    130130begin
    131   BrainFuckInterpreter := TBrainFuckInterpreter.Create;
     131  BrainFuckInterpreter := TBrainFuckInterpretter.Create;
    132132  BrainFuckInterpreter.OnChangeState := BrainFuckInterpreterChangeState;
    133133end;
Note: See TracChangeset for help on using the changeset viewer.