From 5aa41b436b3fdf29f5a0046c68cb60b16fa09eb2 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 30 Sep 2022 11:29:41 +0800 Subject: fix issue #81, refactor continue with gotos. --- spec/inputs/goto.yue | 4 +- spec/inputs/loops.yue | 8 +- spec/inputs/test/loops_spec.yue | 6 +- spec/outputs/5.1/loops.lua | 381 +++++++++++++++++++++++++++++++++++ spec/outputs/5.1/test/loops_spec.lua | 69 +++++++ spec/outputs/goto.lua | 4 +- spec/outputs/loops.lua | 204 ++++++------------- spec/outputs/test/loops_spec.lua | 51 ++--- 8 files changed, 537 insertions(+), 190 deletions(-) create mode 100644 spec/outputs/5.1/loops.lua create mode 100644 spec/outputs/5.1/test/loops_spec.lua (limited to 'spec') diff --git a/spec/inputs/goto.yue b/spec/inputs/goto.yue index 1197251..61584ca 100644 --- a/spec/inputs/goto.yue +++ b/spec/inputs/goto.yue @@ -10,8 +10,8 @@ do for z = 1, 10 do for y = 1, 10 do for x = 1, 10 if x^2 + y^2 == z^2 print 'found a Pythagorean triple:', x, y, z - goto done - ::done:: + goto ok + ::ok:: do for z = 1, 10 diff --git a/spec/inputs/loops.yue b/spec/inputs/loops.yue index d03e661..d997c65 100644 --- a/spec/inputs/loops.yue +++ b/spec/inputs/loops.yue @@ -111,10 +111,10 @@ until a == 10 x = 0 repeat - x += 1 - y = x - continue if x < 5 - print y + x += 1 + y = x + continue if x < 5 + print y until y == 10 a = 3 diff --git a/spec/inputs/test/loops_spec.yue b/spec/inputs/test/loops_spec.yue index 817919f..a115581 100644 --- a/spec/inputs/test/loops_spec.yue +++ b/spec/inputs/test/loops_spec.yue @@ -5,8 +5,8 @@ describe "loops", -> continue if x % 2 == 1 x - assert.same output, { 2,4,6 } - + assert.same { 2,4,6 }, output + it "continue in repeat", -> output = {} a = 0 @@ -19,5 +19,5 @@ describe "loops", -> output[] = a until a == 8 - assert.same output, { 1,2,4 } + assert.same { 1,2,4 }, output diff --git a/spec/outputs/5.1/loops.lua b/spec/outputs/5.1/loops.lua new file mode 100644 index 0000000..a3b9548 --- /dev/null +++ b/spec/outputs/5.1/loops.lua @@ -0,0 +1,381 @@ +for x = 1, 10 do + print("yeah") +end +for x = 1, #something do + print("yeah") +end +for y = 100, 60, -3 do + print("count down", y) +end +for a = 1, 10 do + print("okay") +end +for a = 1, 10 do + for b = 2, 43 do + print(a, b) + end +end +for i in iter do + for j in yeah do + local x = 343 + i + j + print(i, j) + end +end +local _list_0 = something +for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + print(x) +end +for k, v in pairs(hello) do + print(k, v) +end +for x in y, z do + print(x) +end +for x in y, z, k do + print(x) +end +local _list_1 = modules +for _index_0 = 1, #_list_1 do + local name, members = _list_1[_index_0] + print(name, member) +end +local x +x = function() + for x in y do + local _ = y + end +end +local hello = { + 1, + 2, + 3, + 4, + 5 +} +do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #hello do + local y = hello[_index_0] + if y % 2 == 0 then + _accum_0[_len_0] = y + end + _len_0 = _len_0 + 1 + end + x = _accum_0 +end +x = function() + for _index_0 = 1, #hello do + local x = hello[_index_0] + local _ = y + end +end +local t +do + local _accum_0 = { } + local _len_0 = 1 + for i = 10, 20 do + _accum_0[_len_0] = i * 2 + _len_0 = _len_0 + 1 + end + t = _accum_0 +end +local hmm = 0 +local y +do + local _accum_0 = { } + local _len_0 = 1 + for j = 3, 30, 8 do + hmm = hmm + 1 + _accum_0[_len_0] = j * hmm + _len_0 = _len_0 + 1 + end + y = _accum_0 +end +local _ +_ = function() + for k = 10, 40 do + _ = "okay" + end +end +_ = function() + local _accum_0 = { } + local _len_0 = 1 + for k = 10, 40 do + _accum_0[_len_0] = "okay" + _len_0 = _len_0 + 1 + end + return _accum_0 +end +while true do + print("name") +end +while 5 + 5 do + print("okay world") + working(man) +end +while also do + i(work(too)) + _ = "okay" +end +local i = 0 +do + local _accum_0 = { } + local _len_0 = 1 + while i < 10 do + i = i + 1 + _len_0 = _len_0 + 1 + end + x = _accum_0 +end +do + local _accum_0 = { } + local _len_0 = 1 + local _list_2 = 3 + for _index_0 = 1, #_list_2 do + local thing = _list_2[_index_0] + y = "hello" + _len_0 = _len_0 + 1 + end + x = _accum_0 +end +do + local _accum_0 = { } + local _len_0 = 1 + for x = 1, 2 do + y = "hello" + _len_0 = _len_0 + 1 + end + x = _accum_0 +end +while true do + local _continue_0 = false + repeat + if false then + _continue_0 = true + break + end + print("yes") + if true then + break + end + print("no") + _continue_0 = true + until true + if not _continue_0 then + break + end +end +for i = 1, 10 do + while true do + local _continue_0 = false + repeat + do + if not true then + _continue_0 = true + break + end + break + end + _continue_0 = true + until true + if not _continue_0 then + break + end + end +end +local a = 1 +repeat + local _cond_0 = false + local _continue_0 = false + repeat + a = a + 1 + if a == 5 then + _cond_0 = a == 10 + _continue_0 = true + break + end + if a == 6 then + break + end + print(a) + _cond_0 = a == 10 + _continue_0 = true + until true + if not _continue_0 then + break + end +until _cond_0 +x = 0 +repeat + local _cond_0 = false + local _continue_0 = false + repeat + x = x + 1 + y = x + if x < 5 then + _cond_0 = y == 10 + _continue_0 = true + break + end + print(y) + _cond_0 = y == 10 + _continue_0 = true + until true + if not _continue_0 then + break + end +until _cond_0 +a = 3 +while not (a == 0) do + a = a - 1 +end +local done = false +while not done do + done = true +end +repeat + print("hello") +until true +while not done do + x = 10 + repeat + x = x - 1 + until x == 0 +end +while not cond do + print("okay") +end +for x = 1, 10 do + local _continue_0 = false + repeat + if x > 3 and x < 7 then + _continue_0 = true + break + end + print(x) + _continue_0 = true + until true + if not _continue_0 then + break + end +end +local list +do + local _accum_0 = { } + local _len_0 = 1 + for x = 1, 10 do + local _continue_0 = false + repeat + if x > 3 and x < 7 then + _continue_0 = true + break + end + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + list = _accum_0 +end +local _list_2 = { + 1, + 2, + 3, + 4, + 5, + 6 +} +for _index_0 = 1, #_list_2 do + local a = _list_2[_index_0] + local _continue_0 = false + repeat + if a == 1 then + _continue_0 = true + break + end + if a == 3 then + _continue_0 = true + break + end + print(a) + _continue_0 = true + until true + if not _continue_0 then + break + end +end +for x = 1, 10 do + local _continue_0 = false + repeat + if x % 2 == 0 then + _continue_0 = true + break + end + for y = 2, 12 do + local _continue_1 = false + repeat + if y % 3 == 0 then + _continue_1 = true + break + end + _continue_1 = true + until true + if not _continue_1 then + break + end + end + _continue_0 = true + until true + if not _continue_0 then + break + end +end +while true do + local _continue_0 = false + repeat + do + if false then + _continue_0 = true + break + end + break + end + _continue_0 = true + until true + if not _continue_0 then + break + end +end +while true do + local _continue_0 = false + repeat + if false then + _continue_0 = true + break + end + do + return 22 + end + _continue_0 = true + until true + if not _continue_0 then + break + end +end +do + local xxx = { + 1, + 2, + 3, + 4 + } + for _index_0 = 1, #xxx do + local thing = xxx[_index_0] + print(thing) + end +end diff --git a/spec/outputs/5.1/test/loops_spec.lua b/spec/outputs/5.1/test/loops_spec.lua new file mode 100644 index 0000000..bad17a6 --- /dev/null +++ b/spec/outputs/5.1/test/loops_spec.lua @@ -0,0 +1,69 @@ +return describe("loops", function() + it("should continue", function() + local input = { + 1, + 2, + 3, + 4, + 5, + 6 + } + local output + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #input do + local x = input[_index_0] + local _continue_0 = false + repeat + if x % 2 == 1 then + _continue_0 = true + break + end + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + output = _accum_0 + end + return assert.same({ + 2, + 4, + 6 + }, output) + end) + return it("continue in repeat", function() + local output = { } + local a = 0 + repeat + local _cond_0 = false + local _continue_0 = false + repeat + a = a + 1 + if a == 3 then + _cond_0 = a == 8 + _continue_0 = true + break + end + if a == 5 then + break + end + output[#output + 1] = a + _cond_0 = a == 8 + _continue_0 = true + until true + if not _continue_0 then + break + end + until _cond_0 + return assert.same({ + 1, + 2, + 4 + }, output) + end) +end) diff --git a/spec/outputs/goto.lua b/spec/outputs/goto.lua index d1f7b77..421508a 100644 --- a/spec/outputs/goto.lua +++ b/spec/outputs/goto.lua @@ -14,12 +14,12 @@ do for x = 1, 10 do if x ^ 2 + y ^ 2 == z ^ 2 then print('found a Pythagorean triple:', x, y, z) - goto done + goto ok end end end end - ::done:: + ::ok:: end do for z = 1, 10 do diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua index e5c189e..9c16ee4 100644 --- a/spec/outputs/loops.lua +++ b/spec/outputs/loops.lua @@ -150,83 +150,47 @@ do x = _accum_0 end while true do - local _continue_0 = false - repeat - if false then - _continue_0 = true - break - end - print("yes") - if true then - break - end - print("no") - _continue_0 = true - until true - if not _continue_0 then + if false then + goto _continue_0 + end + print("yes") + if true then break end + print("no") + ::_continue_0:: end for i = 1, 10 do while true do - local _continue_0 = false - repeat - do - if not true then - _continue_0 = true - break - end - break - end - _continue_0 = true - until true - if not _continue_0 then - break + if not true then + goto _continue_1 end + break + ::_continue_1:: end end local a = 1 repeat - local _cond_0 = false - local _continue_0 = false - repeat - a = a + 1 - if a == 5 then - _cond_0 = a == 10 - _continue_0 = true - break - end - if a == 6 then - break - end - print(a) - _cond_0 = a == 10 - _continue_0 = true - until true - if not _continue_0 then + a = a + 1 + if a == 5 then + goto _continue_2 + end + if a == 6 then break end -until _cond_0 + print(a) + ::_continue_2:: +until a == 10 x = 0 repeat - local _cond_0 = false - local _continue_0 = false - repeat - x = x + 1 - y = x - if x < 5 then - _cond_0 = y == 10 - _continue_0 = true - break - end - print(y) - _cond_0 = y == 10 - _continue_0 = true - until true - if not _continue_0 then - break + x = x + 1 + y = x + if x < 5 then + goto _continue_3 end -until _cond_0 + print(y) + ::_continue_3:: +until y == 10 a = 3 while not (a == 0) do a = a - 1 @@ -248,37 +212,23 @@ while not cond do print("okay") end for x = 1, 10 do - local _continue_0 = false - repeat - if x > 3 and x < 7 then - _continue_0 = true - break - end - print(x) - _continue_0 = true - until true - if not _continue_0 then - break + if x > 3 and x < 7 then + goto _continue_4 end + print(x) + ::_continue_4:: end local list do local _accum_0 = { } local _len_0 = 1 for x = 1, 10 do - local _continue_0 = false - repeat - if x > 3 and x < 7 then - _continue_0 = true - break - end - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - _continue_0 = true - until true - if not _continue_0 then - break + if x > 3 and x < 7 then + goto _continue_5 end + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + ::_continue_5:: end list = _accum_0 end @@ -292,78 +242,42 @@ local _list_2 = { } for _index_0 = 1, #_list_2 do local a = _list_2[_index_0] - local _continue_0 = false - repeat - if a == 1 then - _continue_0 = true - break - end - if a == 3 then - _continue_0 = true - break - end - print(a) - _continue_0 = true - until true - if not _continue_0 then - break + if a == 1 then + goto _continue_6 end + if a == 3 then + goto _continue_6 + end + print(a) + ::_continue_6:: end for x = 1, 10 do - local _continue_0 = false - repeat - if x % 2 == 0 then - _continue_0 = true - break - end - for y = 2, 12 do - local _continue_1 = false - repeat - if y % 3 == 0 then - _continue_1 = true - break - end - _continue_1 = true - until true - if not _continue_1 then - break - end + if x % 2 == 0 then + goto _continue_7 + end + for y = 2, 12 do + if y % 3 == 0 then + goto _continue_8 end - _continue_0 = true - until true - if not _continue_0 then - break + ::_continue_8:: end + ::_continue_7:: end while true do - local _continue_0 = false - repeat - do - if false then - _continue_0 = true - break - end - break - end - _continue_0 = true - until true - if not _continue_0 then - break + if false then + goto _continue_9 end + break + ::_continue_9:: end while true do - local _continue_0 = false - repeat - if false then - _continue_0 = true - break - end + if false then + goto _continue_10 + end + do return 22 - _continue_0 = true - until true - if not _continue_0 then - break end + ::_continue_10:: end do local xxx = { diff --git a/spec/outputs/test/loops_spec.lua b/spec/outputs/test/loops_spec.lua index 34f2e9c..9c85e1a 100644 --- a/spec/outputs/test/loops_spec.lua +++ b/spec/outputs/test/loops_spec.lua @@ -14,56 +14,39 @@ return describe("loops", function() local _len_0 = 1 for _index_0 = 1, #input do local x = input[_index_0] - local _continue_0 = false - repeat - if x % 2 == 1 then - _continue_0 = true - break - end - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - _continue_0 = true - until true - if not _continue_0 then - break + if x % 2 == 1 then + goto _continue_0 end + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + ::_continue_0:: end output = _accum_0 end - return assert.same(output, { + return assert.same({ 2, 4, 6 - }) + }, output) end) return it("continue in repeat", function() local output = { } local a = 0 repeat - local _cond_0 = false - local _continue_0 = false - repeat - a = a + 1 - if a == 3 then - _cond_0 = a == 8 - _continue_0 = true - break - end - if a == 5 then - break - end - output[#output + 1] = a - _cond_0 = a == 8 - _continue_0 = true - until true - if not _continue_0 then + a = a + 1 + if a == 3 then + goto _continue_0 + end + if a == 5 then break end - until _cond_0 - return assert.same(output, { + output[#output + 1] = a + ::_continue_0:: + until a == 8 + return assert.same({ 1, 2, 4 - }) + }, output) end) end) -- cgit v1.2.3-55-g6feb