Ignore:
Timestamp:
Feb 18, 2012, 11:08:44 PM (13 years ago)
Author:
chronos
Message:
  • Modified: CompiledForm replaced by compiled source tab in PageControl on MainForm.
  • Added: Function for switching position between source code and target code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTargetInterpretter.pas

    r32 r33  
    1818  end;
    1919
    20   TBrainFuckCommand = (cmNone, cmInc, cmDec, cmPointerInc, cmPointerDec,
    21     cmOutput, cmInput, cmLoopStart, cmLoopEnd);
     20  TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec,
     21    cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug);
    2222
    2323  TCommandHandler = procedure of object;
     
    4545    procedure CommandLoopStart;
    4646    procedure CommandLoopEnd;
     47    procedure SingleStep;
     48    procedure Reset;
    4749  protected
    48     procedure SetSource(AValue: string); override;
     50    function GetTargetCode: string; override;
    4951  public
    50     FSource: array of TBrainFuckCommand;
     52    FProgram: array of TBrainFuckCommand;
    5153    SourceJump: array of Integer;
    5254    SourcePosition: Integer;
     
    5860    Input: string;
    5961    InputPosition: Integer;
    60     procedure Reset;
    61     procedure SingleStep;
     62    procedure Compile; override;
    6263    procedure Run; override;
    6364    procedure Pause; override;
     
    7273  end;
    7374
     75const
     76  BrainFuckCommandText: array[TBrainFuckCommand] of Char = (
     77    ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@');
     78
    7479
    7580implementation
     
    8893begin
    8994  repeat
    90     while (Parent.SourcePosition < Length(Parent.FSource)) and (Parent.State <> rsStopped) do begin
     95    while (Parent.SourcePosition < Length(Parent.FProgram)) and (Parent.State <> rsStopped) do begin
    9196      Parent.SingleStep;
    9297      while Parent.State = rsPaused do begin
     
    117122end;
    118123
    119 procedure TTargetInterpretter.SetSource(AValue: string);
     124procedure TTargetInterpretter.Compile;
    120125var
    121126  I: Integer;
    122127  Pos: Integer;
    123128begin
    124   SetLength(FSource, Length(AValue));
     129  DebugSteps.Clear;
     130  SetLength(FProgram, Length(FSourceCode));
    125131  Pos := 0;
    126   for I := 1 to Length(AValue) do begin
    127     case AValue[I] of
    128       '+': FSource[Pos] := cmInc;
    129       '-': FSource[Pos] := cmDec;
    130       '>': FSource[Pos] := cmPointerInc;
    131       '<': FSource[Pos] := cmPointerDec;
    132       ',': FSource[Pos] := cmInput;
    133       '.': FSource[Pos] := cmOutput;
    134       '[': FSource[Pos] := cmLoopStart;
    135       ']': FSource[Pos] := cmLoopEnd;
     132  for I := 1 to Length(FSourceCode) do begin
     133    case FSourceCode[I] of
     134      '+': begin
     135        FProgram[Pos] := cmInc;
     136        DebugSteps.AddStep(I - 1, Pos, soNormal);
     137      end;
     138      '-': begin
     139        FProgram[Pos] := cmDec;
     140        DebugSteps.AddStep(I - 1, Pos, soNormal);
     141      end;
     142      '>': begin
     143        FProgram[Pos] := cmPointerInc;
     144        DebugSteps.AddStep(I - 1, Pos, soNormal);
     145      end;
     146      '<': begin
     147        FProgram[Pos] := cmPointerDec;
     148        DebugSteps.AddStep(I - 1, Pos, soNormal);
     149      end;
     150      ',': begin
     151        FProgram[Pos] := cmInput;
     152        DebugSteps.AddStep(I - 1, Pos, soNormal);
     153      end;
     154      '.': begin
     155        FProgram[Pos] := cmOutput;
     156        DebugSteps.AddStep(I - 1, Pos, soNormal);
     157      end;
     158      '[': begin
     159        FProgram[Pos] := cmLoopStart;
     160        DebugSteps.AddStep(I - 1, Pos, soStepIn);
     161      end;
     162      ']': begin
     163        FProgram[Pos] := cmLoopEnd;
     164        DebugSteps.AddStep(I - 1, Pos, soStepOut);
     165      end
    136166      else Dec(Pos);
    137167    end;
    138168    Inc(Pos);
    139169  end;
    140   SetLength(FSource, Pos);
     170  SetLength(FProgram, Pos);
    141171end;
    142172
     
    160190  I: Integer;
    161191begin
    162   SetLength(SourceJump, Length(FSource));
     192  SetLength(SourceJump, Length(FProgram));
    163193  //FillChar(Pointer(SourceJump)^, Length(SourceJump), 0);
    164   for I := 0 to Length(FSource) - 1 do
     194  for I := 0 to Length(FProgram) - 1 do
    165195    SourceJump[I] := 0;
    166196  SetLength(Loop, 0);
    167   for I := 0 to Length(FSource) - 1 do begin
    168     case FSource[I] of
     197  for I := 0 to Length(FProgram) - 1 do begin
     198    case FProgram[I] of
    169199      cmLoopStart: begin
    170200        SetLength(Loop, Length(Loop) + 1);
     
    252282end;
    253283
     284function TTargetInterpretter.GetTargetCode: string;
     285var
     286  I: Integer;
     287begin
     288  SetLength(Result, Length(FProgram));
     289  for I := 0 to Length(FProgram) - 1 do
     290    Result[I + 1] := BrainFuckCommandText[FProgram[I]];
     291end;
     292
    254293procedure TTargetInterpretter.SingleStep;
    255294begin
    256   FCommandTable[FSource[SourcePosition]];
     295  FCommandTable[FProgram[SourcePosition]];
    257296  Inc(SourcePosition);
    258297  Inc(FStepCount);
     
    281320  inherited;
    282321  Name := 'Interpretter';
    283   Capabilities := [tcRun, tcPause, tcStop];
     322  Capabilities := [tcRun, tcPause, tcStop, tcCompile, tcStepOut, tcStepInto,
     323    tcStepOver, tcRunToCursor];
    284324  MemorySize := 30000;
    285325  CellSize := 256;
Note: See TracChangeset for help on using the changeset viewer.