SRL Archive - Lape

Weird lape for loop


riwu

2014-10-09 07:02:06


var
i: Integer;
a: TIntegerArray;
begin
SetLength(a, 1);
for i:=0 to High(a) do
begin
writeln(i);
SetLength(a, Length(a) + 1);
end;
end.

This will result in an infinite loop in PascalScript because the loop's end condition – High(a) is rechecked every loop,
but it will terminate after 1 loop in lape, so i thought that perhaps lape's for loop has its end condition evaluated only once
at the start for efficiency purpose, but when i tested the following:


var
i, e: Integer;
a: TIntegerArray;
begin
SetLength(a, 1);
for i:=0 to e do
begin
writeln(i);
Inc(e);
end;
end.
This results in an infinite loop in lape too, so i'm wondering why in the first example it terminates after 1 loop?

@Nielsie

slacky

2014-10-09 07:39:02

This is as intended.
Expressions are computed on loop-initialization (and stored in a temporary variable). If the loop is simply given a variable Lape doesn't have to store a temporary variable, so it simply points to the existing variable, which means you can modify it in the loop. This is the same way as it is in FPC.


var
i,l: Integer;
a: TIntegerArray;
begin
SetLength(a, 1);
l := 0;
for i:=0 to l do
begin
writeln(i);
SetLength(a, Length(a) + 1);
l := High(a);
end;
end.
will result in a loop which will keep going until mem-usage limit is reached.

riwu

2014-10-09 10:00:52

From: slacky
This is as intended.
Expressions are computed on loop-initialization (and stored in a temporary variable). If the loop is simply given a variable Lape doesn't have to store a temporary variable, so it simply points to the existing variable, which means you can modify it in the loop. This is the same way as it is in FPC.


var
i,l: Integer;
a: TIntegerArray;
begin
SetLength(a, 1);
l := 0;
for i:=0 to l do
begin
writeln(i);
SetLength(a, Length(a) + 1);
l := High(a);
end;
end.
will result in a loop which will keep going until mem-usage limit is reached.

I see.. thanks for the explanation.
Is there any other difference b/w lape and pascalscript pertaining to execution mechanism that i should be aware of? Always wasted significant time debugging stuffs like this…

Gu3st...

2014-10-09 14:50:16

From: riwu
I see.. thanks for the explanation.
Is there any other difference b/w lape and pascalscript pertaining to execution mechanism that i should be aware of? Always wasted significant time debugging stuffs like this…
You can find a lot of information (including this) in the lape thread: https://villavu.com/forum/showthread.php?t=68613

riwu

2014-10-09 18:52:41

From: Zyt3x
You can find a lot of information (including this) in the lape thread: https://villavu.com/forum/showthread.php?t=68613

Already read that and Brandon's lape tutorial too, didnt mention stuffs like this :(