Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Refactor continue with gotos

Open nikneym opened this issue 3 years ago • 3 comments
trafficstars

YueScript currently emulates continue with a combination of if check and repeat. I propose refactoring the generated code with goto. This might save us from if checks and repeat loop.

Yue input:

numbers = { 1, 3, 5, 7, 9 }
for number in *numbers
  if number > 3
    continue

  print number

current output:

local numbers = {
	1,
	3,
	5,
	7,
	9
}
for _index_0 = 1, #numbers do
	local number = numbers[_index_0]
	local _continue_0 = false
	repeat
		if number <= 3 then
			_continue_0 = true
			break
		end
		print(number)
		_continue_0 = true
	until true
	if not _continue_0 then
		break
	end
end

same logic with goto:

local numbers = {
	1,
	3,
	5,
	7,
	9
}
for _index_0 = 1, #numbers do
	local number = numbers[_index_0]
	if number <= 3 then
		goto _continue_0
	end
	print(number)
	::_continue_0::
end

nikneym avatar Mar 04 '22 12:03 nikneym

It makes sense to generate more cleaner codes. The problem is goto statement is only added since Lua 5.2. So we will need an extra Lua compatible option to generate different code for different Lua versions.

pigpigyyy avatar Mar 05 '22 05:03 pigpigyyy

The lua compatibility option has been added.

Le0Developer avatar Sep 24 '22 10:09 Le0Developer

And Lua version independent implementation for continue key word is now refactored.

pigpigyyy avatar Sep 30 '22 06:09 pigpigyyy