Problem z if

shoock12

Użytkownik
Dołączył
Styczeń 24, 2009
Posty
123
Mam taki kod:
Kod:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus;

type
  TForm1 = class(TForm)

    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    N1: TMenuItem;
    Save1: TMenuItem;
    N2: TMenuItem;
    Exit1: TMenuItem;
    Project1: TMenuItem;
    Run1: TMenuItem;
    Otwrz1: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure Open1Click(Sender: TObject);
    procedure Otwrz1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Run1Click(Sender: TObject);

  private
    { Private declarations }
    procedure stworz_okno(tytul : string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses unit2, unit4;

type
    TArray = array of string;

function explode(cDelimiter,  sValue : string; iCount : integer) : TArray;
var
s : string; i,p : integer;
begin

        s := sValue; i := 0;
        while length(s) > 0 do
        begin
                inc(i);
                SetLength(result, i);
                p := pos(cDelimiter,s);

                if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
                begin

                       
                        result[i - 1] := copy(s,0,p-1);

                        {updated, thanks Irfan}
                        s := copy(s,p + length(cDelimiter),length(s));
                end else
                begin result[i - 1] := s;
                        s :=  '';
                end;
        end;

end;

procedure TForm1.stworz_okno(tytul : string);
var
  Child : TForm2;
begin
    Child := TForm2.Create(Application);
    Child.Caption := tytul;
    if FileExists(tytul) then
        Child.memo1.Lines.LoadFromFile(tytul);
end;

procedure TForm1.Open1Click(Sender: TObject);
begin
stworz_okno('bez_nazwy'+inttostr(MDIChildCount + 1));
end;

procedure TForm1.Otwrz1Click(Sender: TObject);
begin
if opendialog1.execute then begin
    stworz_okno(opendialog1.filename);
    end;
end;

procedure TForm1.Save1Click(Sender: TObject);
begin
  if assigned(ActiveMDIChild) then begin
        savedialog1.FileName := (ActiveMDIChild as TForm2).caption;
    if savedialog1.execute then begin
            (ActiveMDIChild as TForm2).memo1.Lines.SaveToFile(savedialog1.filename);
        end;
    end;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
application.terminate;
end;

procedure TForm1.Run1Click(Sender: TObject);
var
i:integer;
tmp:string;
tmp2:string;
doif:TArray;
trybif:string;
test:integer;
begin

for i:=0 to (ActiveMDIChild as TForm2).Memo1.Lines.Count do begin

  tmp:=(ActiveMDIChild as TForm2).Memo1.Lines[i];

if(trybif='T') then
begin

  if(tmp='end') then
    trybif:='N';

   if Pos('write', tmp) > 0 then
    begin
      tmp2:=tmp;
     delete(tmp2, 1, 6);
      form4.memo1.Lines.Add(tmp2);
   end;

end;

    if Pos('write', tmp) > 0 then
    begin
      tmp2:=tmp;
     delete(tmp2, 1, 6);
      form4.memo1.Lines.Add(tmp2);
   end else if Pos('if', tmp) > 0 then
   begin
      delete(tmp2, 1, 3);
      doif:=explode('=', tmp2, 2);

      if(doif[0]=doif[1]) then
       trybif:='T';

  end;


end;

form4.Show;

end;

end.

Chodzi dokładnie o tą linijkę
Kod:
    if(doif[0]=doif[1]) then
       trybif:='T';

Program się kompiluje, ale (ten program to taki mały interpretator mojego języka skryptowego są tylko dwa polecenia) po uruchomieniu
i wpisaniu w odpowiednie miejsce instrukcji np.
Kod:
if a=a
write c
end
i po kliknięciu w moim programie, żeby zaczęło się wykonywać mam taki błąd:
bladprogramu.png

ale jak wpiszę tam np.
Kod:
write a
to uruchomi się normalnie i w moim program pokaże się okno z wynikiem czyli po prostu wypisane "a".
 

The

Użytkownik
Dołączył
Maj 16, 2008
Posty
285
W błędzie chodzi o to, że odnosisz się do czegoś czego nie ma. Przeanalizuj kod.
 

lol_ek

Użytkownik
Dołączył
Maj 30, 2003
Posty
306
<div class='quotetop'>CYTAT(The @ 27.04.2009, 18:24) <{POST_SNAPBACK}></div>
W błędzie chodzi o to, że odnosisz się do czegoś czego nie ma. Przeanalizuj kod.[/b]

Na uzupełnienie dodam byś zwrócił uwagę na poniższy fragment Twojego kodu.

Kod:
for i:=0 to (ActiveMDIChild as TForm2).Memo1.Lines.Count do begin

Linie do których się odwołujesz są indeksowane od 0, a więc wartość Memo1.Lines.Count nie będzie indeksem tej ostatniej.
 
Do góry Bottom