lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


  I found that LUA is a very good language for computational tools
for Biology. Most biology textbook is so thick that people
do not have time to study perl or other computer stuff.
As found in mailing list archive, FOR loop support to LUA
would help them a lot.

  This is my idea of a FOR loop function with BREAK/CONTINUE.
With current LUA specification, BREAK/CONTINUE is only ok if they are
at the end of execution blocks of the while statement.
Otherwise, we have to use IF block that would be skipped on each condition
explicitly.  Also, a BREAK from nested loop requires BREAKs for all loops.

This function, forloop( from, to, stride) returns a table containing
counter, limit and functions.

-- sample program

dofile("forloop.lua")

mydata={0,10,21,15,0,-7,11,999,10,0}

one=forloop(1) while one.step() do
	write(" loop ",format("%2d",one.count)," : ")

	data=mydata[one.count]
	if( not data ) then
		one.break()
	elseif(data==0) then
		print("0")
		one.continue()
	elseif(data<0) then
		print("-")
		one.continue()
	else
			  -- bar graph length is data/2
		write(" ")
		two=forloop(2,data,2) while two.step() do
			write("*")
			if( two.count>50 ) then
				print(" test for nested break ")
				one.break()
				two.break()
				  -- need every loop explicitly 
			end
		end
	end

	  -- lua does not break an execution of current block
	  -- so we have a explicit IF condition 

	if(one.live) then
			  -- lets indicate odd values
		 	  -- our forloop keeps last valid count
		if(data-two.count>0) then
			write(".")
		end
		print()
	end
end

print(" end of loop one ",one.count)

$endinput

-----------------------------------
result is

 loop  1 : 0
 loop  2 :  *****
 loop  3 :  **********.
 loop  4 :  *******.
 loop  5 : 0
 loop  6 : -
 loop  7 :  *****.
 loop  8 :  ************************** test for nested break 
 end of loop one        8

-----------------------------------

:::forloop.lua:::

-- these two func may be implemented for real break and continue 

-- get block ID of current execution
function currentblock_() return 0 end

-- stop current execution block and back to specified
function resumeblock_(block) end -- n/a 


function forloop(from,to,stride)
	local looptable={live=nil,skip=1}
	looptable.count=from or 0
	looptable.to=to or 2^31           --- too bad
	looptable.stride=stride or 1

	looptable.step=function()
		local nextcount
		if(%looptable.live) then
			nextcount = %looptable.count+%looptable.stride 
			if(nextcount>%looptable.to) then
				%looptable.live=nil
				return nil
			end

			%looptable.count=nextcount
			return nextcount

		elseif(%looptable.skip) then
			%looptable.skip=nil
			%looptable.live=1
			return %looptable.count
		else
			return nil
		end
	end

	looptable.break=function()
		%looptable.live=nil
		-- we wish operator to kill current block and resume execution
		-- resumeblock_(%looptable.block)
	end

	looptable.continue=function()
		if( %looptable.step()) then
			%looptable.skip=1
			%looptable.live=nil
		end
		-- we wish a function to kill current block and resume 
		-- resumeblock_(%looptable.block)
	end

	-- we wish a function to save current block to resume execution
	-- looptable.block=currentblock_()
	return looptable
end

$endinput
--------------------

I guess, if we had special built-in functions,
currentblock_() and resumeblock_()
my code could provide real BREAK/CONTINUE functionalities.
It does not require any additional grammar or reserved word for LUA.

currentblock_() will return current execution block ID start from 0.
resumeblock_() stop current execution block and return to the specified
block ID. If the ID is already current one, it does nothing.
It looks like setjump/longjump but we do not need to restore stacks and
any variables.
LUA seems to dislike "statement not reached" conditions as described
in manual, "return" Section 4.5.3. However, I think this method
may provide more desirable flexibility maintaining current design.

I am afraid that I may not understand details of inside LUA.
Please let me know any correction or suggestions.
Thank you in advance.

------
上野 豊 電子技術総合研究所 知能情報部 遺伝子情報ラボ 〒305-8568
(0298) 54-5965   FAX:5938