From cd2b60b101a398cb9356d746364e70eaed1860f1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 22 Jun 2020 16:50:40 +0800 Subject: add support for local variable declared with attribute 'close' and 'const' for Lua 5.4. --- spec/inputs/attrib.moon | 37 +++++++++++++++++++++++++++++ spec/inputs/backcall.moon | 6 ++++- spec/inputs/macro.moon | 59 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 spec/inputs/attrib.moon (limited to 'spec/inputs') diff --git a/spec/inputs/attrib.moon b/spec/inputs/attrib.moon new file mode 100644 index 0000000..a5fa376 --- /dev/null +++ b/spec/inputs/attrib.moon @@ -0,0 +1,37 @@ +do + close a = setmetatable {},__close:=> print "closed" + const a = 123 + +do + close v = if flag + func! + else + setmetatable {},__close:=> + + close f = with io.open "file.txt" + \write "Hello" + +macro block defer = (item)-> "close _ = #{item}" +macro block defer_f = (func)-> "close _ = setmetatable {},__close:#{func}" + +do + $defer with io.open "file.txt" + \write "Hello" + + $defer setmetatable {},__close:=> print "second" + + $defer_f -> print "first" + +_defers = setmetatable {},__close:=> + @[#@]! + @[#@] = nil + +macro block defer_i = (item)-> " +_defers[#_defers + 1] = #{item} +close _ = _defers" + +do + $defer_i -> print 3 + $defer_i -> print 2 + $defer_i -> print 1 + diff --git a/spec/inputs/backcall.moon b/spec/inputs/backcall.moon index b6fe24f..86b0ba3 100644 --- a/spec/inputs/backcall.moon +++ b/spec/inputs/backcall.moon @@ -79,7 +79,7 @@ do f7! do - {:result,:msg} = do + :result,:msg = do (data)<- receiveAsync "filename.txt" print data (info)<- processAsync data @@ -107,6 +107,10 @@ alert "hi" x = 123 |> a |> b or 456 |> c |> d or a.if\then("abc") or a?.b\c?(123) or x\y +x1 = 3 * -4 |> f + +x2 = 3 * -2 ^ 2 |> f + y = 1 + not # 2 |> (a ^ c) |> b(3,_) * 4 ^ -123 |> f |> f1 or 123 nil diff --git a/spec/inputs/macro.moon b/spec/inputs/macro.moon index 2677d15..aa8356c 100644 --- a/spec/inputs/macro.moon +++ b/spec/inputs/macro.moon @@ -17,19 +17,26 @@ macro expr and = (...)-> values = [value for value in *{...}] $showMacro "and", "#{ table.concat values, " and " }" -if $and f1 +if $and f1! print "OK" -if $and f1,f2,f3 +if $and f1!, f2!, f3! print "OK" -macro expr map = (items,action)-> +macro expr in = (target, ...)-> + values = [value for value in *{...}] + $showMacro "in", "#{ table.concat ["#{target} == #{item}" for item in *values], " or " }" + +if x |> $in "Apple", "Pig", "Dog" + print "exist" + +macro expr map = (items, action)-> $showMacro "map", "[#{action} for _ in *#{items}]" -macro expr filter = (items,action)-> +macro expr filter = (items, action)-> $showMacro "filter", "[_ for _ in *#{items} when #{action}]" -macro expr reduce = (items,def,action)-> +macro expr reduce = (items, def, action)-> $showMacro "reduce", "if ##{items} == 0 #{def} else @@ -38,7 +45,7 @@ else _1 = #{action} _1" -macro block foreach = (items,action)-> +macro block foreach = (items, action)-> $showMacro "foreach", "for _ in *#{items} #{action}" @@ -72,6 +79,8 @@ macro expr plus = (a, b)-> "#{a} + #{b}" $plus(1,2)\call 123 +res = 1 |> $plus 2 + macro expr curry = (...)-> args = {...} len = #args @@ -135,6 +144,44 @@ $def sel, a, b, c, [[ $def dummy,[[ ]] +import 'underscore' as _ + +macro expr chain = (...)-> $showMacro "chain",table.concat {...}, "\\" + +a = $chain( + _({1, 2, 3, 4, -2, 3}) + chain! + map(=> @ * 2) + filter(=> @ > 3) + value! +) + +macro block chainB = (...)-> + switch select "#", ... + when 0 then return "" + when 1 then return ... + items = {...} + last = nil + stmts = for i = 1,#items + stmt = if i == #items + "\t#{last and "#{last}\\" or ""}#{items[i]}" + else + "\tlocal _#{i} = #{last and "#{last}\\" or ""}#{items[i]}" + last = "_#{i}" + stmt + res = "do +#{table.concat stmts, "\n"} +" + $showMacro "chainB",res + +$chainB( + _{1, 2, 3, 4, -2, 3} + chain! + map => @ * 2 + filter => @ > 3 + each => print @ +) + macro block implicitReturnblockMacroIsAllowed = -> "123" $implicitReturnblockMacroIsAllowed! -- cgit v1.2.3-55-g6feb