aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/macro.moon
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
committerLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
commitcd2b60b101a398cb9356d746364e70eaed1860f1 (patch)
treea1fe71b76faabc4883f16905a94164ce5c23e692 /spec/inputs/macro.moon
parent88c1052e700f38cf3d8ad82d469da4c487760b7e (diff)
downloadyuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'spec/inputs/macro.moon')
-rw-r--r--spec/inputs/macro.moon59
1 files changed, 53 insertions, 6 deletions
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 = (...)->
17 values = [value for value in *{...}] 17 values = [value for value in *{...}]
18 $showMacro "and", "#{ table.concat values, " and " }" 18 $showMacro "and", "#{ table.concat values, " and " }"
19 19
20if $and f1 20if $and f1!
21 print "OK" 21 print "OK"
22 22
23if $and f1,f2,f3 23if $and f1!, f2!, f3!
24 print "OK" 24 print "OK"
25 25
26macro expr map = (items,action)-> 26macro expr in = (target, ...)->
27 values = [value for value in *{...}]
28 $showMacro "in", "#{ table.concat ["#{target} == #{item}" for item in *values], " or " }"
29
30if x |> $in "Apple", "Pig", "Dog"
31 print "exist"
32
33macro expr map = (items, action)->
27 $showMacro "map", "[#{action} for _ in *#{items}]" 34 $showMacro "map", "[#{action} for _ in *#{items}]"
28 35
29macro expr filter = (items,action)-> 36macro expr filter = (items, action)->
30 $showMacro "filter", "[_ for _ in *#{items} when #{action}]" 37 $showMacro "filter", "[_ for _ in *#{items} when #{action}]"
31 38
32macro expr reduce = (items,def,action)-> 39macro expr reduce = (items, def, action)->
33 $showMacro "reduce", "if ##{items} == 0 40 $showMacro "reduce", "if ##{items} == 0
34 #{def} 41 #{def}
35else 42else
@@ -38,7 +45,7 @@ else
38 _1 = #{action} 45 _1 = #{action}
39 _1" 46 _1"
40 47
41macro block foreach = (items,action)-> 48macro block foreach = (items, action)->
42 $showMacro "foreach", "for _ in *#{items} 49 $showMacro "foreach", "for _ in *#{items}
43 #{action}" 50 #{action}"
44 51
@@ -72,6 +79,8 @@ macro expr plus = (a, b)-> "#{a} + #{b}"
72 79
73$plus(1,2)\call 123 80$plus(1,2)\call 123
74 81
82res = 1 |> $plus 2
83
75macro expr curry = (...)-> 84macro expr curry = (...)->
76 args = {...} 85 args = {...}
77 len = #args 86 len = #args
@@ -135,6 +144,44 @@ $def sel, a, b, c, [[
135$def dummy,[[ 144$def dummy,[[
136]] 145]]
137 146
147import 'underscore' as _
148
149macro expr chain = (...)-> $showMacro "chain",table.concat {...}, "\\"
150
151a = $chain(
152 _({1, 2, 3, 4, -2, 3})
153 chain!
154 map(=> @ * 2)
155 filter(=> @ > 3)
156 value!
157)
158
159macro block chainB = (...)->
160 switch select "#", ...
161 when 0 then return ""
162 when 1 then return ...
163 items = {...}
164 last = nil
165 stmts = for i = 1,#items
166 stmt = if i == #items
167 "\t#{last and "#{last}\\" or ""}#{items[i]}"
168 else
169 "\tlocal _#{i} = #{last and "#{last}\\" or ""}#{items[i]}"
170 last = "_#{i}"
171 stmt
172 res = "do
173#{table.concat stmts, "\n"}
174"
175 $showMacro "chainB",res
176
177$chainB(
178 _{1, 2, 3, 4, -2, 3}
179 chain!
180 map => @ * 2
181 filter => @ > 3
182 each => print @
183)
184
138macro block implicitReturnblockMacroIsAllowed = -> "123" 185macro block implicitReturnblockMacroIsAllowed = -> "123"
139 186
140$implicitReturnblockMacroIsAllowed! 187$implicitReturnblockMacroIsAllowed!