Changeset 3 for trunk/UBrainFuck.pas


Ignore:
Timestamp:
Feb 9, 2012, 1:25:43 PM (12 years ago)
Author:
chronos
Message:
  • Fixed: Now "Hello world" example is correctly interpretted.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11lib
         2LazFuckIDE.exe
  • trunk/UBrainFuck.pas

    r2 r3  
    2626    function ReadCode: Char;
    2727  public
    28     Source: TStrings;
     28    Source: string;
    2929    SourcePosition: Integer;
    3030    Memory: array of Byte;
     
    3232    Loop: array of Integer;
    3333    LoopCurrent: Integer;
    34     Output: TStrings;
    35     Input: TStrings;
     34    Output: string;
     35    Input: string;
    3636    InputPosition: Integer;
    3737    procedure Reset;
     
    4242  end;
    4343
     44
    4445implementation
     46
     47resourcestring
     48  SProgramLowerLimit = 'Program run over lower limit';
     49  SProgramUpperLimit = 'Program run over upper limit';
    4550
    4651{ TBrainFuckInterpreter }
     
    4853procedure TBrainFuckInterpreter.Write(Value: Byte);
    4954begin
    50   Output.Text := Output.Text + Char(Value);
     55  Output := Output + Char(Value);
    5156end;
    5257
     
    5560  Character: string;
    5661begin
    57   Character := Copy(Input.Text, InputPosition, 1);
     62  Character := Copy(Input, InputPosition, 1);
    5863  Result := Ord(Character[1]);
    5964  Inc(InputPosition);
     
    6469  Code: string;
    6570begin
    66   Code := Copy(Source.Text, SourcePosition, 1);
     71  Code := Copy(Source, SourcePosition + 1, 1);
    6772  Result := Code[1];
    6873end;
     
    7479  SourcePosition := 0;
    7580  InputPosition := 0;
    76   Output.Text := '';
     81  Output := '';
    7782  MemoryPosition := 0;
    7883  for I := 0 to Length(Memory) - 1 do
     
    8994  case ReadCode of
    9095    '>': Inc(MemoryPosition);
    91     '<': Dec(MemoryPosition);
     96    '<': if MemoryPosition > 0 then Dec(MemoryPosition)
     97      else raise Exception.Create(SProgramLowerLimit);
    9298    '+': Memory[MemoryPosition] := Memory[MemoryPosition] + 1;
    9399    '-': Memory[MemoryPosition] := Memory[MemoryPosition] - 1;
     
    97103      if Memory[MemoryPosition] = 0 then begin
    98104        C := 1;
    99         Inc(MemoryPosition);
     105        Inc(SourcePosition);
    100106        while C > 0 do begin
    101107          case ReadCode of
     
    103109            ']': Dec(C);
    104110          end;
    105           Inc(MemoryPosition);
     111          Inc(SourcePosition);
    106112        end;
    107         Dec(MemoryPosition);
     113        Dec(SourcePosition);
    108114      end;
    109115    end;
     
    111117      if Memory[MemoryPosition] > 0 then begin
    112118        C := 1;
    113         Dec(MemoryPosition);
     119        Dec(SourcePosition);
    114120        while C > 0 do begin
    115121          case ReadCode of
     
    117123            '[': Dec(C);
    118124          end;
    119           Dec(MemoryPosition);
     125          Dec(SourcePosition);
    120126        end;
    121127      end;
    122128    end;
    123129  end;
    124   Inc(MemoryPosition);
     130  Inc(SourcePosition);
    125131end;
    126132
     
    128134begin
    129135  Reset;
    130   while SourcePosition < Length(Source.Text) do
     136  while SourcePosition < Length(Source) do
    131137    SingleStep;
    132138end;
Note: See TracChangeset for help on using the changeset viewer.