Compiler FOR Schleife

05/09/2008 - 19:51 von Jens Kallup | Report spam
Hallo,

ich stehe gerade auf den Schlauch :/
wie bekomme ich eine Saubere FOR (tiefe) hin?

Danke schonmal
Jens

hier der jetzige Code:

-
for zaehler = 0 to 10
? "zaehler line: "
for uu = 0 to 5
?? "O "
endfor
endfor



mov dword [Lzahl_1], 0
.Llabel_1:
cmp dword [Lzahl_1], 10
je .Llabel_-1
push Lstr_0
call _printtext
add esp, 4
mov dword [Lzahl_2], 0
.Llabel_2:
cmp dword [Lzahl_2], 5
je .Llabel_0
push Lstr_1
call _printtext
add esp, 4
inc dword [Lzahl_1]
jmp .Llabel_2
.Llabel_1:
inc dword [Lzahl_0]
jmp .Llabel_1
.Llabel_0:
ret
 

Lesen sie die antworten

#1 Jens Kallup
05/09/2008 - 19:55 | Warnen spam
Das ganze habe ich natürlich in Delphi realsiert und schaut
in etwa so aus:

var for_tiefe: integer = 1;
procedure TParserClass.handle_for;
var
l,i: integer;
c: char;
s,s1,s2: String;
v1,v2: Extended;
begin
c := skip_white_space;
if c in ['A'..'Z','a'..'z'] then
begin
ident_str := c;
scan_ident;

c := skip_white_space;
if c = '=' then
begin
s1 := handle_number;
s1 := StringReplace(s1,',','.',[rfReplaceAll]);

if FKlammerZu > 0 then begin
s1 := s1 + ')';
FKlammerzu := 0;
end;

v1 := SetExpr(s1);
l := yylex;
if ident_str = 'to' then begin
s1 := handle_number;
s1 := StringReplace(s1,',','.',[rfReplaceAll]);

if FKlammerZu > 0 then begin
s1 := s1 + ')';
FKlammerzu := 0;
end;

v2 := SetExpr(s1);
s2 := FActiveClass+':'+FActiveObject;
i := FEventCodeOutput.IndexOf(s2);
if i > -1 then
begin
datacode := datacode + 'Lzahl_'+IntToStr(FStringLabelCounter+1) + ': dw ' + inttostr(round(int(v1)))+#10;

TStringList(FEventCodeOutput.Objects[i]).Add(#9+'mov dword [Lzahl_'+IntToStr(FStringLabelCounter+1)+'], '
+inttostr(round(int(v1))));
TStringList(FEventCodeOutput.Objects[i]).Add('.Llabel_'+IntToStr(for_tiefe)+':');
TStringList(FEventCodeOutput.Objects[i]).Add(#9+'cmp dword [Lzahl_'+IntToStr(FStringLabelCounter+1)+'], '+inttostr(round(int(v2))));
TStringList(FEventCodeOutput.Objects[i]).Add(#9+'je .Llabel_'+inttostr(for_tiefe-1));


repeat
showmessage('l: '+#10+inttostr(l)+'i: ' + ident_str);
l := yylex;
if l = TOK_ENDFOR then
begin
dec(for_tiefe);
break;
end else
if l = TOK_PRINT then
begin
handle_print
end else
if l = TOK_FOR then begin
inc(for_tiefe);
handle_for;
end else
handle_parser_code(l);
until l = TOK_ENDFOR;

TStringList(FEventCodeOutput.Objects[i]).Add(#9+'inc dword [Lzahl_'+IntToStr(for_tiefe)+']');
TStringList(FEventCodeOutput.Objects[i]).Add(#9+'jmp .Llabel_'+IntToStr(for_tiefe+1));
TStringList(FEventCodeOutput.Objects[i]).Add('.Llabel_'+inttostr(for_tiefe)+':');

inc(FStringLabelCounter,2);
end;
end;
end;
end;
end;

Ähnliche fragen