Autor Beitrag
Maweki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 197

Ubuntu Linux
Lazarus
BeitragVerfasst: So 01.05.05 17:59 
Hallo.
Also, ich wollte einen kleinen Brainfuck-Interpreter programmieren.
Hier ist der Code:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
program MiniBf;

{$APPTYPE CONSOLE}


const
  maxArrayWidth = 2048; //in Byte
var
  Code: string;
  CurrentPositionInCode, CurrentPositionInArray,
  CurrentBracketDepth, TargetBracketDepth: Cardinal;
  ListenPointer: ^Byte;
  CodePointer: ^Char;
  Liste: Array of Byte;

procedure MoveLeft;
begin
  If CurrentPositionInArray <> 0 then
  begin
    Dec(ListenPointer);
    Dec(CurrentPositionInArray);
  end;
end;

procedure MoveRight;
var
  zwischen: Cardinal;
  i: Integer;
begin
  If CurrentPositionInArray < maxArrayWidth then
  begin
    Inc(CurrentPositionInArray);
    zwischen := High(Liste);
    If CurrentPositionInArray > zwischen then
    begin
      SetLength(Liste, zwischen + 2);
      Liste[High(Liste)] := 0;
    end;
    Inc(ListenPointer);
  end;
end;

procedure Increase;
begin
  If ListenPointer^ = 255 then
    ListenPointer^ := 0
  else
    Inc(ListenPointer^);
end;

procedure Decrease;
begin
  If ListenPointer^ = 0 then
    ListenPointer^ := 255
  else
    Dec(ListenPointer^);
end;

procedure GetInput;
var
  zwischen: string;
begin
  ReadLn(zwischen);
  if zwischen = '' then
    ListenPointer^ := Ord(#13)
  else
    ListenPointer^ := Ord(zwischen[1]);
end;

procedure Output; inline;
begin
  WriteLn(Chr(ListenPointer^));
end;

begin
  CurrentBracketDepth := 0;
  CurrentPositionInArray := 0;
  CurrentPositionInCode := 1;
  SetLength(Liste, 1);
  ListenPointer := @Liste[0];
  //Readln(Code);
  Code := '+[>+]';
  Writeln('Started');
  if Length(Code) = 0 then
    exit;
  CodePointer := @Code[1];
  repeat
    case Ord(CodePointer^) of
      43: Increase;
      45: Decrease;
      44: GetInput;
      46: Output;
      60: MoveLeft;
      62: MoveRight;
      91: begin
            Inc(CurrentBracketDepth);
            If ListenPointer^ = 0 then
            begin
              TargetBracketDepth := CurrentBracketDepth - 1;
              repeat
                Inc(CodePointer);
                Inc(CurrentPositionInCode);
                if CodePointer^ = Chr(91) then
                  Inc(CurrentBracketDepth);
                if CodePointer^ = Chr(93) then
                  Dec(CurrentBracketDepth);
              until CurrentBracketDepth = TargetBracketDepth;
            end;
          end;
      93: begin
            Dec(CurrentBracketDepth);
            If ListenPointer^ <> 0 then
            begin
              TargetBracketDepth := CurrentBracketDepth + 1;
              repeat
                Dec(CodePointer);
                Dec(CurrentPositionInCode);
                if CodePointer^ = Chr(91) then
                  Inc(CurrentBracketDepth);
                if CodePointer^ = Chr(93) then
                  Dec(CurrentBracketDepth);
              until CurrentBracketDepth = TargetBracketDepth;
          end;
        end;
      end;
    Inc(CodePointer);
    Inc(CurrentPositionInCode);
  until CurrentPositionInCode > Length(Code);
  Writeln('End');
  ReadLn;
end.

So, ich wollte, da ich die groesse des Dynamischen Arrays in einer Schleife erhoehe, mal das Ding komplett auslasten, zum Test. Wieviel Speicher wirklich verbraten wird (hatte bei DT/DS eine Formel gefunden).
Aber das Programm bricht einfach ab. Beim Debuggen sehe ich folgende Sachen (die Zahlen beziehen sich auf den Index auf dem Array):
Beim Durchlauf 15 ist das gerade ebend erstellte Feld 42
Beim Durchlauf 16 ist das gerade ebend erstellte Feld 92
Beim Durchlauf 16 ist das Schleifenschliessende [ verschwunden und durch ein / ersetzt worden.
Woran liegt das? Zum ersten werden die Felder nicht richtig initialisiert und zum zweiten veraendert sich der String. Das muss doch irgendeinen Grund haben...
Denn gerade letzteres kann ich nicht durch die Verwendung eines statischen Arrays loesen.
Maweki Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 197

Ubuntu Linux
Lazarus
BeitragVerfasst: So 01.05.05 19:26 
OK. Das Problem ist geloest. Die Pointer liefen irgendwann, fragt mich nicht warum, Asynchron zu den Countern und liefen auch mal aus dem String und auch aus der Liste raus.
Hier jetzt ein funktionierender BrainfuckInterpreter:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
program MiniBf;

{$APPTYPE CONSOLE}


const
  maxArrayWidth = 2048; //in Byte
var
  Code: string;
  CurrentPositionInCode, CurrentPositionInArray,
  CurrentBracketDepth, TargetBracketDepth, CodeLength: Cardinal;
  Liste: Array of Byte;
  foobar: string;

procedure MoveLeft;
begin
  If CurrentPositionInArray <> 0 then
  begin
    Dec(CurrentPositionInArray);
  end;
end;

procedure MoveRight;
var
  zwischen: Cardinal;
begin
  If CurrentPositionInArray < maxArrayWidth then
  begin
    Inc(CurrentPositionInArray);
    zwischen := High(Liste);
    If CurrentPositionInArray >= zwischen then
    begin
      SetLength(Liste, zwischen + 2);
      Liste[High(Liste)] := 0;
    end;
  end;
end;

procedure Increase;
begin
  If Liste[CurrentPositionInArray] = 255 then
    Liste[CurrentPositionInArray] := 0
  else
    Inc(Liste[CurrentPositionInArray]);
end;

procedure Decrease;
begin
  If Liste[CurrentPositionInArray] = 0 then
    Liste[CurrentPositionInArray] := 255
  else
    Dec(Liste[CurrentPositionInArray]);
end;

procedure GetInput;
var
  zwischen: string;
begin
  ReadLn(zwischen);
  if zwischen = '' then
    Liste[CurrentPositionInArray] := Ord(#13)
  else
    Liste[CurrentPositionInArray] := Ord(zwischen[1]);
end;

procedure Output; inline;
begin
  WriteLn(Chr(Liste[CurrentPositionInArray]));
end;

begin
  CurrentBracketDepth := 0;
  CurrentPositionInArray := 0;
  CurrentPositionInCode := 1;
  SetLength(Liste, 1);
  Readln(Code);
  Writeln('Started');
  CodeLength := Length(Code);
  if CodeLength = 0 then
    exit;
  repeat
    case Ord(Code[CurrentPositionInCode]) of
      43: Increase;
      45: Decrease;
      44: GetInput;
      46: Output;
      60: MoveLeft;
      62: MoveRight;
      91: begin
            Inc(CurrentBracketDepth);
            If Liste[CurrentPositionInArray] = 0 then
            begin
              TargetBracketDepth := CurrentBracketDepth - 1;
              repeat
                Inc(CurrentPositionInCode);
                if Code[CurrentPositionInCode] = Chr(91) then
                  Inc(CurrentBracketDepth);
                if Code[CurrentPositionInCode] = Chr(93) then
                  Dec(CurrentBracketDepth);
              until CurrentBracketDepth = TargetBracketDepth;
            end;
          end;
      93: begin
            Dec(CurrentBracketDepth);
            If Liste[CurrentPositionInArray] <> 0 then
            begin
              TargetBracketDepth := CurrentBracketDepth + 1;
              repeat
                Dec(CurrentPositionInCode);
                if Code[CurrentPositionInCode] = Chr(91) then
                  Inc(CurrentBracketDepth);
                if Code[CurrentPositionInCode] = Chr(93) then
                  Dec(CurrentBracketDepth);
              until CurrentBracketDepth = TargetBracketDepth;
          end;
        end;
      end;
    Inc(CurrentPositionInCode);
    SetLength(Code, CodeLength);
  until CurrentPositionInCode > CodeLength;
  Writeln('End');
  ReadLn(foobar);
end.