Changeset 114 for trunk/UBFTarget.pas


Ignore:
Timestamp:
May 18, 2019, 12:13:44 AM (6 years ago)
Author:
chronos
Message:
  • Modified: Improved stepping through source and target code. Each step has source, program and target index.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBFTarget.pas

    r112 r114  
    4444    FProgram: array of TMachineOperation;
    4545    FProgramIndex: Integer;
     46    function GetOperationText(Operation: TMachineOperation): string; virtual;
    4647    procedure LoadProgram; override;
    4748  public
     
    5556  end;
    5657
     58const
     59  BrainFuckCommandText: array[TMachineCommand] of Char = (
     60    ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*');
     61
    5762
    5863implementation
     
    98103  NewProgram: array of TMachineOperation;
    99104  NewProgramIndex: Integer;
     105  NewTargetPos: Integer;
     106  FirstIndex: Integer;
    100107begin
    101108  NewProgramIndex := 0;
     109  NewTargetPos := 0;
    102110  SetLength(NewProgram, Length(FProgram));
    103111
    104112  FProgramIndex := 0;
    105113  while (FProgramIndex < Length(FProgram)) do begin
     114    FirstIndex := FProgramIndex;
    106115    case FProgram[FProgramIndex].Command of
    107116      cmPointerInc: begin
     
    123132      else NewProgram[NewProgramIndex] := FProgram[FProgramIndex];
    124133    end;
    125     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     134    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTargetPos);
     135    Inc(NewTargetPos, Length(GetOperationText(NewProgram[NewProgramIndex])));
    126136    Inc(FProgramIndex);
    127137    Inc(NewProgramIndex);
     
    139149  NewProgramIndex: Integer;
    140150  PreviousCommand: TMachineCommand;
     151  FirstIndex: Integer;
     152  NewTextIndex: Integer;
    141153begin
    142154  // Merge together cmInc, cmDec, cmSet
     
    147159
    148160  FProgramIndex := 0;
     161  NewTextIndex := 0;
    149162  while (FProgramIndex < Length(FProgram)) do begin
     163    FirstIndex := FProgramIndex;
    150164    case FProgram[FProgramIndex].Command of
    151165      cmPointerInc: begin
     
    258272    end;
    259273    PreviousCommand := FProgram[FProgramIndex].Command;
    260     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     274    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     275    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    261276    Inc(FProgramIndex);
    262277    Inc(NewProgramIndex);
     
    281296  NewProgramIndex: Integer;
    282297  RelIndex: Integer;
     298  FirstIndex: Integer;
     299  NewTextIndex: Integer;
    283300begin
    284301  NewProgramIndex := 0;
     
    287304  RelIndex := 0;
    288305  FProgramIndex := 0;
     306  NewTextIndex := 0;
    289307  while (FProgramIndex < Length(FProgram)) do begin
     308    FirstIndex := FProgramIndex;
    290309    case FProgram[FProgramIndex].Command of
    291310      cmPointerInc: begin
     
    319338      else raise Exception.Create(Format('Unsupported command %d', [FProgram[FProgramIndex].Command]));
    320339    end;
    321     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     340    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     341    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    322342    Inc(FProgramIndex);
    323343    Inc(NewProgramIndex);
     
    340360  LoopStartIndex: Integer;
    341361  LoopStartIndexNew: Integer;
     362  FirstIndex: Integer;
     363  NewTextIndex: Integer;
    342364begin
    343365  NewProgramIndex := 0;
     
    347369  ProcessLoop := False;
    348370  FProgramIndex := 0;
     371  NewTextIndex := 0;
    349372  PointerChange := 0;
    350373  while (FProgramIndex < Length(FProgram)) do begin
     374    FirstIndex := FProgramIndex;
    351375    case FProgram[FProgramIndex].Command of
    352376      cmPointerInc: begin
     
    418442      else raise Exception.Create(Format('Unsupported command %d', [FProgram[FProgramIndex].Command]));
    419443    end;
    420     DebugSteps.UpdateTargetPos(FProgramIndex, NewProgramIndex);
     444    DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex);
     445    Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex])));
    421446    Inc(FProgramIndex);
    422447    Inc(NewProgramIndex);
     
    428453  Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) *
    429454    Length(NewProgram));
     455end;
     456
     457function TBFTarget.GetOperationText(Operation: TMachineOperation): string;
     458begin
     459  Result := BrainFuckCommandText[Operation.Command];
     460  if Operation.Command in [cmInc, cmDec, cmPointerInc, cmPointerDec,
     461    cmSet, cmMultipy] then begin
     462  if Operation.Parameter <> 1 then
     463    Result := Result + IntToStr(Operation.Parameter);
     464  end;
     465  if Operation.RelIndex <> 0 then
     466    Result := Result + 'R' + IntToStr(Operation.RelIndex);
    430467end;
    431468
Note: See TracChangeset for help on using the changeset viewer.