[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why 'while' and 'repeat' loop need double block
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 9 Jan 2013 14:18:06 -0200
> When parsing 'while' and 'repeat' statement, lua uses double block
> to encapsulate the inner statement(one for 'loop').
If I remember correctly, the inner block controls the scope of variables,
while the outer block controls where to jump from a break. Consider
this code:
  while 1 do
    local a
    x = function () return a end
  end
and its opcodes:
	1	[2]	LOADNIL  	0 0
	2	[3]	CLOSURE  	1 0	; 0x9f18fa0
	3	[3]	MOVE     	0 0
	4	[3]	SETGLOBAL	1 -1	; x
	5	[3]	CLOSE    	0
	6	[3]	JMP      	-6	; to 1
	7	[4]	RETURN   	0 1
Roughly, the inner block ends on opcode 5, where it creates the CLOSE
instruction. The outer block ends on opcode 7, the destination of any
'break' inside the loop.
-- Roberto