Ignore:
Timestamp:
Jul 26, 2012, 3:11:08 PM (12 years ago)
Author:
chronos
Message:
  • Modified: Optimization functions moved to shared place in TTarget.
  • Modified: Text source is loaded to program source array of brainfuck commands for better processing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTarget.pas

    r50 r52  
    6666  end;
    6767
     68  TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec,
     69    cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug);
     70
    6871  { TTarget }
    6972
    7073  TTarget = class
    71   private
     74  protected
    7275    FCompiled: Boolean;
     76    function SourceReadNext: Char;
     77    function IsOpcode(Opcode: Char): Boolean;
     78    function CheckClear: Boolean;
     79    function CheckOccurence(C: TBrainFuckCommand): Integer;
    7380  protected
    7481    FSourceCode: string;
     82    FProgram: array of TBrainFuckCommand;
     83    FProgramIndex: Integer;
    7584    FTargetCode: string;
     85    FTargetIndex: Integer;
    7686    Indent: Integer;
    7787    FState: TRunState;
    7888    FOnChangeState: TNotifyEvent;
     89    procedure LoadProgram;
    7990    procedure SetSourceCode(AValue: string); virtual;
    8091    function GetTargetCode: string; virtual;
     
    119130    property Compiled: Boolean read FCompiled write FCompiled;
    120131    property ExecutionPosition: Integer read GetExecutionPosition;
     132    property ProgramIndex: Integer read FProgramIndex;
    121133  end;
    122134
     
    377389procedure TTarget.Compile;
    378390begin
     391  LoadProgram;
    379392  Compiled := True;
    380393end;
     
    489502end;
    490503
     504function TTarget.CheckOccurence(C: TBrainFuckCommand): Integer;
     505begin
     506  Result := 1;
     507  if Optimization = coNormal then
     508  while ((FProgramIndex + 1) <= Length(FProgram)) and (FProgram[FProgramIndex + 1] = C) do begin
     509    Inc(Result);
     510    Inc(FProgramIndex);
     511  end;
     512end;
     513
     514procedure TTarget.LoadProgram;
     515var
     516  I: Integer;
     517begin
     518  inherited;
     519  DebugSteps.Clear;
     520  SetLength(FProgram, Length(FSourceCode));
     521  FProgramIndex := 0;
     522  for I := 1 to Length(FSourceCode) do begin
     523    case FSourceCode[I] of
     524      '+': begin
     525        FProgram[FProgramIndex] := cmInc;
     526        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     527      end;
     528      '-': begin
     529        FProgram[FProgramIndex] := cmDec;
     530        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     531      end;
     532      '>': begin
     533        FProgram[FProgramIndex] := cmPointerInc;
     534        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     535      end;
     536      '<': begin
     537        FProgram[FProgramIndex] := cmPointerDec;
     538        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     539      end;
     540      ',': begin
     541        FProgram[FProgramIndex] := cmInput;
     542        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     543      end;
     544      '.': begin
     545        FProgram[FProgramIndex] := cmOutput;
     546        DebugSteps.AddStep(I - 1, FProgramIndex, soNormal);
     547      end;
     548      '[': begin
     549        FProgram[FProgramIndex] := cmLoopStart;
     550        DebugSteps.AddStep(I - 1, FProgramIndex, soStepIn);
     551      end;
     552      ']': begin
     553        FProgram[FProgramIndex] := cmLoopEnd;
     554        DebugSteps.AddStep(I - 1, FProgramIndex, soStepOut);
     555      end
     556      else Dec(FProgramIndex);
     557    end;
     558    Inc(FProgramIndex);
     559  end;
     560  SetLength(FProgram, FProgramIndex);
     561end;
     562
     563function TTarget.SourceReadNext: Char;
     564begin
     565//  while FProgramIndex;
     566end;
     567
     568function TTarget.IsOpcode(Opcode: Char): Boolean;
     569begin
     570  Result := (Opcode = '+') or (Opcode = '-') or (Opcode = '<') or (Opcode = '>') or
     571    (Opcode = '[') or (Opcode = ']') or (Opcode = ',') or (Opcode = '.');
     572end;
     573
     574function TTarget.CheckClear: Boolean;
     575begin
     576  Result := (FProgram[FProgramIndex] = cmLoopStart) and (Length(FProgram) >= FProgramIndex + 2) and
     577    (FProgram[FProgramIndex + 1] = cmDec) and (FProgram[FProgramIndex + 2] = cmLoopEnd);
     578end;
     579
    491580end.
    492581
Note: See TracChangeset for help on using the changeset viewer.