From cf0a0f37bf07eb8e9435febe96e3adfe45004f91 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 23 May 2025 12:22:21 +0800 Subject: Added break with value to with syntax. --- spec/inputs/with.yue | 9 +++++++++ spec/outputs/codes_from_doc.lua | 33 +++++++++++++++++++++++++++++++++ spec/outputs/codes_from_doc_zh.lua | 33 +++++++++++++++++++++++++++++++++ spec/outputs/with.lua | 27 +++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) (limited to 'spec') diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue index 19b7be1..3fee48e 100644 --- a/spec/inputs/with.yue +++ b/spec/inputs/with.yue @@ -152,4 +152,13 @@ do return with {} return [123] +do + f with item + if .id > 0 + break .content + + a = with tb + if .v + break .a + nil diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua index 177c33d..3ee20bb 100644 --- a/spec/outputs/codes_from_doc.lua +++ b/spec/outputs/codes_from_doc.lua @@ -694,6 +694,7 @@ local some_string = "Here is a string\n that has a line break in it." print("I am " .. tostring(math.random() * 100) .. "% sure.") local integer = 1000000 local hex = 0xEFBBBF +local binary = 19 local my_function my_function = function() end my_function() @@ -2228,6 +2229,38 @@ local inventory = { } } } +local map +map = function(arr, action) + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #arr do + local item = arr[_index_0] + _accum_0[_len_0] = action(item) + _len_0 = _len_0 + 1 + end + return _accum_0 +end +local filter +filter = function(arr, cond) + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #arr do + local item = arr[_index_0] + if cond(item) then + _accum_0[_len_0] = item + _len_0 = _len_0 + 1 + end + end + return _accum_0 +end +local reduce +reduce = function(arr, init, action) + for _index_0 = 1, #arr do + local item = arr[_index_0] + init = action(init, item) + end + return init +end print(reduce(filter(map({ 1, 2, diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index 6349010..52204b7 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua @@ -694,6 +694,7 @@ local some_string = "这是一个字符串\n 并包括一个换行。" print("我有" .. tostring(math.random() * 100) .. "%的把握。") local integer = 1000000 local hex = 0xEFBBBF +local binary = 19 local my_function my_function = function() end my_function() @@ -2222,6 +2223,38 @@ local inventory = { } } } +local map +map = function(arr, action) + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #arr do + local item = arr[_index_0] + _accum_0[_len_0] = action(item) + _len_0 = _len_0 + 1 + end + return _accum_0 +end +local filter +filter = function(arr, cond) + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #arr do + local item = arr[_index_0] + if cond(item) then + _accum_0[_len_0] = item + _len_0 = _len_0 + 1 + end + end + return _accum_0 +end +local reduce +reduce = function(arr, init, action) + for _index_0 = 1, #arr do + local item = arr[_index_0] + init = action(init, item) + end + return init +end print(reduce(filter(map({ 1, 2, diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index 1a795c1..20c5d44 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua @@ -187,4 +187,31 @@ do return _with_0[123] end end +do + f((function() + local _with_0 = item + do + local _accum_0 + while true do + if _with_0.id > 0 then + _accum_0 = _with_0.content + break + end + end + _with_0 = _accum_0 + end + return _with_0 + end)()) + local a + local _with_0 = tb + local _accum_0 + while true do + if _with_0.v then + _accum_0 = _with_0.a + break + end + end + _with_0 = _accum_0 + a = _with_0 +end return nil -- cgit v1.2.3-55-g6feb