Changeset 12 for trunk


Ignore:
Timestamp:
Feb 11, 2012, 4:32:27 PM (12 years ago)
Author:
chronos
Message:
  • Added: Display source code caret position in statusbar.
  • Modified: Optimized jump speed using precreated direct jump table.
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        33LazFuckIDE.lps
        44backup
         5LazFuckIDE
  • trunk/LazFuckIDE.lpi

    r9 r12  
    77      <SessionStorage Value="InProjectDir"/>
    88      <MainUnit Value="0"/>
    9       <Title Value="LazFuck"/>
     9      <Title Value="LazFuck IDE"/>
    1010      <ResourceType Value="res"/>
    1111      <UseXPManifest Value="True"/>
     
    2929      <local>
    3030        <FormatVersion Value="1"/>
     31        <LaunchingApplication PathPlusParams="\usr\bin\xterm -T 'Lazarus Run Output' -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/>
    3132      </local>
    3233    </RunParams>
     
    8889      <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
    8990    </SearchPaths>
     91    <CodeGeneration>
     92      <Checks>
     93        <IOChecks Value="True"/>
     94        <RangeChecks Value="True"/>
     95        <OverflowChecks Value="True"/>
     96        <StackChecks Value="True"/>
     97      </Checks>
     98      <VerifyObjMethodCallValidity Value="True"/>
     99    </CodeGeneration>
    90100    <Linking>
    91101      <Options>
  • trunk/LazFuckIDE.lpr

    r9 r12  
    44
    55uses
    6   {$IFDEF UNIX}{$IFDEF UseCThreads}
     6  {$IFDEF UNIX}//{$IFDEF UseCThreads}
    77  cthreads,
    8   {$ENDIF}{$ENDIF}
     8  //{$ENDIF}
     9  {$ENDIF}
    910  Interfaces, // this includes the LCL widgetset
    1011  Forms, UMainForm, UBrainFuck, UInterpreterForm, UApplicationInfo,
     
    1516
    1617begin
    17   Application.Title:='LazFuck';
     18  Application.Title:='LazFuck IDE';
    1819  RequireDerivedFormResource := True;
    1920  Application.Initialize;
  • trunk/UBrainFuck.pas

    r10 r12  
    1212
    1313  TCompilerTarget = (ctDelphi);
     14  TCompilerOptimization = (coNone, coNormal);
     15
    1416  { TBrainFuckCompiler }
    1517
     
    2325    Output: string;
    2426    Target: TCompilerTarget;
     27    Optimization: TCompilerOptimization;
     28    procedure OptimizeSource;
    2529    procedure Compile;
    2630  end;
     
    4347    FThreadState: Boolean;
    4448    FThread: TBrainFuckInterpretterThread;
     49    FStepCount: Integer;
    4550    procedure SetState(AValue: TRunState);
    4651    procedure Write(Value: Byte);
     
    4853    function ReadCode: Char;
    4954    procedure SetThread(State: Boolean);
     55    procedure PrepareJumpTable;
    5056  public
    51     Source: string;
     57    Source: array of Char;
     58    SourceJump: array of Integer;
    5259    SourcePosition: Integer;
    5360    Memory: array of Byte;
    5461    MemoryPosition: Integer;
    55     Loop: array of Integer;
    56     LoopCurrent: Integer;
    5762    Output: string;
    5863    Input: string;
    5964    InputPosition: Integer;
    60     StepCount: Integer;
    6165    procedure Reset;
    6266    procedure SingleStep;
     
    6872    property State: TRunState read FState;
    6973    property OnChangeState: TNotifyEvent read FOnChangeState write FOnChangeState;
     74    property StepCount: Integer read FStepCount;
    7075  end;
    7176
     
    7782  SProgramUpperLimit = 'Program run over upper limit';
    7883  SReadInputError = 'Read input error';
     84  SJumpTableInsistent = 'Jump table is inconsistent';
     85  SJumpTableColision = 'Jump table colision';
    7986
    8087{ TBrainFuckInterpretterThread }
     
    118125function TBrainFuckInterpretter.ReadCode: Char;
    119126begin
    120   Result := Source[SourcePosition + 1]
     127  Result := Source[SourcePosition];
    121128end;
    122129
     
    135142end;
    136143
     144procedure TBrainFuckInterpretter.PrepareJumpTable;
     145var
     146  Loop: array of Integer;
     147  LoopCurrent: Integer;
     148  I: Integer;
     149begin
     150  SetLength(SourceJump, Length(Source));
     151  FillChar(Pointer(SourceJump)^, Length(SourceJump), 0);
     152  SetLength(Loop, 0);
     153  for I := 0 to Length(Source) - 1 do begin
     154    case Source[I] of
     155      '[': begin
     156        SetLength(Loop, Length(Loop) + 1);
     157        Loop[High(Loop)] := I;
     158      end;
     159      ']': begin
     160        if SourceJump[I] > 0 then raise Exception.Create(SJumpTableColision);
     161        SourceJump[I] := Loop[High(Loop)];
     162        if SourceJump[Loop[High(Loop)]] > 0 then raise Exception.Create(SJumpTableColision);
     163        SourceJump[Loop[High(Loop)]] := I;
     164        SetLength(Loop, Length(Loop) - 1);
     165      end;
     166    end;
     167  end;
     168  if Length(Loop) > 0 then raise Exception.Create(SJumpTableInsistent);
     169end;
     170
    137171procedure TBrainFuckInterpretter.Reset;
    138 var
    139   I: Integer;
    140 begin
     172begin
     173  PrepareJumpTable;
    141174  SourcePosition := 0;
    142175  InputPosition := 0;
    143176  Output := '';
    144177  MemoryPosition := 0;
    145   for I := 0 to Length(Memory) - 1 do
    146     Memory[I] := 0;
    147   SetLength(Loop, 0);
    148   StepCount := 0;
     178  FillChar(Pointer(Memory)^, Length(Memory), 0);
     179  FStepCount := 0;
    149180end;
    150181
     
    152183var
    153184  CodeText: string;
    154   Code: Char;
    155185  C: Integer;
     186  NewPos: Integer;
    156187begin
    157188  case ReadCode of
     
    166197    '[': begin
    167198      if Memory[MemoryPosition] = 0 then begin
    168         C := 1;
     199        SourcePosition := SourceJump[SourcePosition];
     200        (*C := 1;
    169201        Inc(SourcePosition);
    170202        while C > 0 do begin
     
    175207          Inc(SourcePosition);
    176208        end;
    177         Dec(SourcePosition);
     209        Dec(SourcePosition);*)
     210        //if NewPos <> SourcePosition then raise Exception.Create('Wrong pos: ' + IntToStr(SourcePosition) + ' ' + IntToStr(NewPos));
    178211      end;
    179212    end;
    180213    ']': begin
    181214      if Memory[MemoryPosition] > 0 then begin
    182         C := 1;
     215        SourcePosition := SourceJump[SourcePosition] - 1;
     216        (*C := 1;
    183217        Dec(SourcePosition);
    184218        while C > 0 do begin
     
    189223          Dec(SourcePosition);
    190224        end;
     225        if NewPos <> SourcePosition then raise Exception.Create('Wrong pos: ' + IntToStr(SourcePosition) + ' ' + IntToStr(NewPos));
     226        *)
    191227      end;
    192228    end;
    193229  end;
    194230  Inc(SourcePosition);
    195   Inc(StepCount);
     231  Inc(FStepCount);
    196232end;
    197233
     
    230266begin
    231267  Output := Output + DupeString('  ', Indent) + Text + LineEnding;
     268end;
     269
     270procedure TBrainFuckCompiler.OptimizeSource;
     271begin
     272  // Remove redundand code
     273
    232274end;
    233275
  • trunk/UInterpreterForm.lfm

    r11 r12  
    4343      object Label1: TLabel
    4444        Left = 8
    45         Height = 14
     45        Height = 18
    4646        Top = 8
    47         Width = 31
     47        Width = 38
    4848        Caption = 'Input:'
    4949        ParentColor = False
     
    7171      object Label2: TLabel
    7272        Left = 8
    73         Height = 14
     73        Height = 18
    7474        Top = 6
    75         Width = 39
     75        Width = 49
    7676        Caption = 'Output:'
    7777        ParentColor = False
     
    109109    object Label3: TLabel
    110110      Left = 1
    111       Height = 14
     111      Height = 18
    112112      Top = 8
    113       Width = 82
     113      Width = 110
    114114      Caption = 'Program pointer:'
    115115      ParentColor = False
     
    117117    object LabelProgramPointer: TLabel
    118118      Left = 112
    119       Height = 14
     119      Height = 18
    120120      Top = 8
    121       Width = 10
     121      Width = 13
    122122      Caption = '   '
    123123      ParentColor = False
     
    125125    object Label5: TLabel
    126126      Left = 1
    127       Height = 14
     127      Height = 18
    128128      Top = 24
    129       Width = 80
     129      Width = 108
    130130      Caption = 'Memory pointer:'
    131131      ParentColor = False
     
    133133    object LabelMemoryPointer: TLabel
    134134      Left = 112
    135       Height = 14
     135      Height = 18
    136136      Top = 24
    137       Width = 10
     137      Width = 13
    138138      Caption = '   '
    139139      ParentColor = False
     
    141141    object Label4: TLabel
    142142      Left = 1
    143       Height = 14
     143      Height = 18
    144144      Top = 42
    145       Width = 67
     145      Width = 87
    146146      Caption = 'Step counter:'
    147147      ParentColor = False
     
    149149    object LabelStepCounter: TLabel
    150150      Left = 112
    151       Height = 14
     151      Height = 18
    152152      Top = 42
    153       Width = 10
     153      Width = 13
    154154      Caption = '   '
    155155      ParentColor = False
     
    168168        item
    169169          Caption = 'Data'
    170           Width = 300
     170          Width = 330
    171171        end>
    172172      Font.Height = -11
     
    181181    object Label6: TLabel
    182182      Left = 8
    183       Height = 14
     183      Height = 18
    184184      Top = 104
    185       Width = 43
     185      Width = 57
    186186      Caption = 'Memory:'
    187187      ParentColor = False
     
    211211      Action = MainForm.AProgramStop
    212212    end
     213    object ToolButton4: TToolButton
     214      Left = 70
     215      Top = 2
     216      Action = MainForm.AInterpretterRunToCursor
     217    end
     218    object ToolButton5: TToolButton
     219      Left = 93
     220      Top = 2
     221      Action = MainForm.AInterpretterStepInto
     222    end
     223    object ToolButton6: TToolButton
     224      Left = 116
     225      Top = 2
     226      Action = MainForm.AInterpretterStepOut
     227    end
     228    object ToolButton7: TToolButton
     229      Left = 139
     230      Top = 2
     231      Action = MainForm.AInterpretterStopOver
     232    end
    213233  end
    214234  object Timer1: TTimer
  • trunk/UInterpreterForm.pas

    r6 r12  
    4040    ToolButton2: TToolButton;
    4141    ToolButton3: TToolButton;
     42    ToolButton4: TToolButton;
     43    ToolButton5: TToolButton;
     44    ToolButton6: TToolButton;
     45    ToolButton7: TToolButton;
    4246    procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
    4347    procedure MemoInputKeyPress(Sender: TObject; var Key: char);
  • trunk/UMainForm.lfm

    r11 r12  
    11object MainForm: TMainForm
    2   Left = 258
     2  Left = 286
    33  Height = 465
    4   Top = 121
     4  Top = 140
    55  Width = 643
    66  Caption = 'LazFuck'
    7   ClientHeight = 446
     7  ClientHeight = 440
    88  ClientWidth = 643
    99  Menu = MainMenu1
     
    1313  OnShow = FormShow
    1414  LCLVersion = '0.9.31'
     15  WindowState = wsMaximized
    1516  object MemoSource: TMemo
    1617    Left = 0
    17     Height = 370
     18    Height = 363
    1819    Top = 56
    1920    Width = 643
     
    2223    Font.Name = 'Courier New'
    2324    OnChange = MemoSourceChange
     25    OnKeyDown = MemoSourceKeyDown
     26    OnKeyPress = MemoSourceKeyPress
     27    OnKeyUp = MemoSourceKeyUp
     28    OnMouseDown = MemoSourceMouseDown
    2429    ParentFont = False
    2530    ScrollBars = ssAutoBoth
     
    2833  object StatusBar1: TStatusBar
    2934    Left = 0
    30     Height = 20
    31     Top = 426
     35    Height = 21
     36    Top = 419
    3237    Width = 643
    33     Panels = <>
     38    Panels = <   
     39      item
     40        Width = 70
     41      end   
     42      item
     43        Width = 70
     44      end   
     45      item
     46        Width = 80
     47      end>
     48    SimplePanel = False
    3449  end
    3550  object ToolBar1: TToolBar
     
    630645      Caption = 'Compiled'
    631646      OnExecute = AViewCompiledExecute
     647    end
     648    object AInterpretterStepInto: TAction
     649      Caption = 'Step into'
     650    end
     651    object AInterpretterStopOver: TAction
     652      Caption = 'Step over'
     653    end
     654    object AInterpretterRunToCursor: TAction
     655      Caption = 'Run to cursor'
     656    end
     657    object AInterpretterStepOut: TAction
     658      Caption = 'Step out'
     659    end
     660    object ABreakpointSet: TAction
     661      Caption = 'Breakpoint set'
     662    end
     663    object ABreakpointUnset: TAction
     664      Caption = 'Unset breakpoint'
    632665    end
    633666  end
  • trunk/UMainForm.pas

    r11 r12  
    1616    ACompile: TAction;
    1717    AAbout: TAction;
     18    ABreakpointSet: TAction;
     19    ABreakpointUnset: TAction;
     20    AInterpretterStepOut: TAction;
     21    AInterpretterRunToCursor: TAction;
     22    AInterpretterStopOver: TAction;
     23    AInterpretterStepInto: TAction;
    1824    AViewCompiled: TAction;
    1925    AViewInterpretter: TAction;
     
    8389    procedure FormShow(Sender: TObject);
    8490    procedure MemoSourceChange(Sender: TObject);
     91    procedure MemoSourceKeyDown(Sender: TObject; var Key: Word;
     92      Shift: TShiftState);
     93    procedure MemoSourceKeyPress(Sender: TObject; var Key: char);
     94    procedure MemoSourceKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
     95      );
     96    procedure MemoSourceMouseDown(Sender: TObject; Button: TMouseButton;
     97      Shift: TShiftState; X, Y: Integer);
    8598  private
    8699    procedure BrainFuckInterpreterChangeState(Sender: TObject);
     
    91104    BrainFuckInterpreter: TBrainFuckInterpretter;
    92105    procedure UpdateInterface;
     106    procedure UpdateStatusBar;
    93107  end;
    94108
     
    114128  Modified := True;
    115129  UpdateInterface;
     130end;
     131
     132procedure TMainForm.MemoSourceKeyDown(Sender: TObject; var Key: Word;
     133  Shift: TShiftState);
     134begin
     135end;
     136
     137procedure TMainForm.MemoSourceKeyPress(Sender: TObject; var Key: char);
     138begin
     139end;
     140
     141procedure TMainForm.MemoSourceKeyUp(Sender: TObject; var Key: Word;
     142  Shift: TShiftState);
     143begin
     144  UpdateStatusBar;
     145end;
     146
     147procedure TMainForm.MemoSourceMouseDown(Sender: TObject; Button: TMouseButton;
     148  Shift: TShiftState; X, Y: Integer);
     149begin
     150  UpdateStatusBar;
    116151end;
    117152
     
    136171  AProgramStop.Enabled := (ProjectFileName <> '') and (BrainFuckInterpreter.State <> rsStopped);
    137172  ACompile.Enabled := ProjectFileName <> '';
     173  UpdateStatusBar;
     174end;
     175
     176procedure TMainForm.UpdateStatusBar;
     177begin
     178  StatusBar1.Panels[0].Text := IntToStr(MemoSource.CaretPos.X) + ', ' + IntToStr(MemoSource.CaretPos.Y);
    138179end;
    139180
     
    152193
    153194procedure TMainForm.AProgramRunExecute(Sender: TObject);
     195var
     196  SourceText: string;
    154197begin
    155198  InterpreterForm.Show;
    156199  BrainFuckInterpreter.Input := InterpreterForm.MemoInput.Lines.Text;
    157   BrainFuckInterpreter.Source := MemoSource.Lines.Text;
     200  SourceText := MemoSource.Lines.Text;
     201  SetLength(BrainFuckInterpreter.Source, Length(SourceText));
     202  Move(Pointer(SourceText)^, Pointer(BrainFuckInterpreter.Source)^, Length(SourceText));
    158203  BrainFuckInterpreter.Run;
    159204end;
Note: See TracChangeset for help on using the changeset viewer.