diff options
author | Li Jin <dragon-fly@qq.com> | 2021-02-17 11:22:07 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2021-02-17 11:22:07 +0800 |
commit | 7066392d1c974065181d95d93274136dcd625d43 (patch) | |
tree | cf51eafc2c52cbc12246a306bca172d799193d30 /spec/inputs/macro.yue | |
parent | 90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff) | |
download | yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2 yuescript-7066392d1c974065181d95d93274136dcd625d43.zip |
stop reusing variables, rename project.
Diffstat (limited to 'spec/inputs/macro.yue')
-rw-r--r-- | spec/inputs/macro.yue | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue new file mode 100644 index 0000000..eb3cf75 --- /dev/null +++ b/spec/inputs/macro.yue | |||
@@ -0,0 +1,258 @@ | |||
1 | $ -> | ||
2 | package.yuepath = "?.yue;./spec/inputs/?.yue" | ||
3 | |||
4 | import "macro-export" as { | ||
5 | $, -- import all macros | ||
6 | $config:$myconfig, -- rename macro $config to $myconfig | ||
7 | } | ||
8 | |||
9 | $asserts item == nil | ||
10 | |||
11 | $myconfig false | ||
12 | |||
13 | v = $assert item == nil | ||
14 | |||
15 | macro and = (...)-> | ||
16 | values = [value for value in *{...}] | ||
17 | $showMacro "and", "#{ table.concat values, " and " }" | ||
18 | |||
19 | if $and f1! | ||
20 | print "OK" | ||
21 | |||
22 | if $and f1!, f2!, f3! | ||
23 | print "OK" | ||
24 | |||
25 | macro in = (target, ...)-> | ||
26 | values = [value for value in *{...}] | ||
27 | $showMacro "in", table.concat ["#{target} == #{item}" for item in *values], " or " | ||
28 | |||
29 | if x |> $in "Apple", "Pig", "Dog" | ||
30 | print "exist" | ||
31 | |||
32 | macro map = (items, action)-> | ||
33 | $showMacro "map", "[#{action} for _ in *#{items}]" | ||
34 | |||
35 | macro filter = (items, action)-> | ||
36 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | ||
37 | |||
38 | macro reduce = (items, def, action)-> | ||
39 | $showMacro "reduce", "if ##{items} == 0 | ||
40 | #{def} | ||
41 | else | ||
42 | _1 = #{def} | ||
43 | for _2 in *#{items} | ||
44 | _1 = #{action} | ||
45 | _1" | ||
46 | |||
47 | macro foreach = (items, action)-> | ||
48 | $showMacro "foreach", "for _ in *#{items} | ||
49 | #{action}" | ||
50 | |||
51 | macro pipe = (...)-> | ||
52 | switch select "#", ... | ||
53 | when 0 then return "" | ||
54 | when 1 then return ... | ||
55 | ops = {...} | ||
56 | last = ops[1] | ||
57 | stmts = for i = 2, #ops | ||
58 | stmt = "\tlocal _#{i} = #{last} |> #{ops[i]}" | ||
59 | last = "_#{i}" | ||
60 | stmt | ||
61 | res = "do | ||
62 | #{table.concat stmts, "\n"} | ||
63 | #{last}" | ||
64 | $showMacro "pipe", res | ||
65 | |||
66 | {1,2,3} |> $map(_ * 2) |> $filter(_ > 4) |> $foreach print _ | ||
67 | |||
68 | $foreach $filter($map({1,2,3}, _ * 2), _ > 4), print _ | ||
69 | |||
70 | val = $pipe( | ||
71 | {1, 2, 3} | ||
72 | $map(_ * 2) | ||
73 | $filter(_ > 4) | ||
74 | $reduce(0, _1 + _2) | ||
75 | ) | ||
76 | |||
77 | macro plus = (a, b)-> "#{a} + #{b}" | ||
78 | |||
79 | $plus(1,2)\call 123 | ||
80 | |||
81 | res = 1 |> $plus 2 | ||
82 | |||
83 | macro curry = (...)-> | ||
84 | args = {...} | ||
85 | len = #args | ||
86 | body = args[len] | ||
87 | def = table.concat ["(#{args[i]})->" for i = 1, len - 1] | ||
88 | "#{def}\n#{body\gsub "^do%s*\n",""}" | ||
89 | |||
90 | f = $curry x,y,z,do | ||
91 | print x,y,z | ||
92 | |||
93 | macro get_inner = (var)-> "do | ||
94 | a = 1 | ||
95 | a + 1" | ||
96 | |||
97 | macro get_inner_hygienic = (var)-> "(-> | ||
98 | local a = 1 | ||
99 | a + 1)!" | ||
100 | |||
101 | do | ||
102 | a = 8 | ||
103 | a = $get_inner! | ||
104 | a += $get_inner! | ||
105 | print a | ||
106 | |||
107 | do | ||
108 | a = 8 | ||
109 | a = $get_inner_hygienic! | ||
110 | a += $get_inner_hygienic! | ||
111 | print a | ||
112 | |||
113 | macro lua = (codes)-> { | ||
114 | :codes | ||
115 | type: "lua" | ||
116 | } | ||
117 | |||
118 | x = 0 | ||
119 | |||
120 | $lua [[ | ||
121 | local function f(a) | ||
122 | return a + 1 | ||
123 | end | ||
124 | x = x + f(3) | ||
125 | ]] | ||
126 | |||
127 | $lua[[ | ||
128 | function tb:func() | ||
129 | print(123) | ||
130 | end | ||
131 | ]] | ||
132 | |||
133 | print x | ||
134 | |||
135 | macro def = (fname, ...)-> | ||
136 | args = {...} | ||
137 | last = table.remove args | ||
138 | { | ||
139 | codes: $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) | ||
140 | #{last} | ||
141 | end" | ||
142 | type: "lua" | ||
143 | } | ||
144 | |||
145 | sel = (a, b, c)-> if a then b else c | ||
146 | |||
147 | $def sel, a, b, c, [[ | ||
148 | if a then | ||
149 | return b | ||
150 | else | ||
151 | return c | ||
152 | end | ||
153 | ]] | ||
154 | |||
155 | $def dummy,[[]] | ||
156 | |||
157 | macro insertComment = (text)-> { | ||
158 | codes: "-- #{text\match '[\'"](.*)[\'"]'}" | ||
159 | type: "lua" | ||
160 | } | ||
161 | |||
162 | $insertComment "a comment here" | ||
163 | |||
164 | import 'underscore' as _ | ||
165 | |||
166 | macro chain = (...)-> | ||
167 | callable = nil | ||
168 | for item in *{...} | ||
169 | callable = callable? and "(#{callable})\\#{item}" or item | ||
170 | $showMacro "chain", callable | ||
171 | |||
172 | a = $chain( | ||
173 | _{1, 2, 3, 4, -2, 3} | ||
174 | chain! | ||
175 | map => @ * 2 | ||
176 | filter => @ > 3 | ||
177 | value! | ||
178 | ) | ||
179 | |||
180 | $chain( | ||
181 | _{1, 2, 3, 4, -2, 3} | ||
182 | chain! | ||
183 | map => @ * 2 | ||
184 | filter => @ > 3 | ||
185 | each => print @ | ||
186 | ) | ||
187 | |||
188 | result = $chain( | ||
189 | origin.transform.root.gameObject\Parents! | ||
190 | Descendants! | ||
191 | SelectEnable! | ||
192 | SelectVisible! | ||
193 | TagEqual "fx" | ||
194 | Where (x) -> x.name\EndsWith "(Clone)" | ||
195 | Destroy! | ||
196 | ) | ||
197 | |||
198 | macro chainB = (...)-> | ||
199 | switch select "#", ... | ||
200 | when 0 then return "" | ||
201 | when 1 then return ... | ||
202 | items = {...} | ||
203 | last = nil | ||
204 | stmts = for i = 1,#items | ||
205 | stmt = if i == #items | ||
206 | lastStr = last and "#{last}\\" or "" | ||
207 | "\t#{lastStr}#{items[i]}" | ||
208 | else | ||
209 | lastStr = last and "#{last}\\" or "" | ||
210 | "\tlocal _#{i} = #{lastStr}#{items[i]}" | ||
211 | last = "_#{i}" | ||
212 | stmt | ||
213 | res = "do | ||
214 | #{table.concat stmts, '\n'} | ||
215 | " | ||
216 | $showMacro "chainB", res | ||
217 | |||
218 | $chainB( | ||
219 | origin.transform.root.gameObject\Parents! | ||
220 | Descendants! | ||
221 | SelectEnable! | ||
222 | SelectVisible! | ||
223 | TagEqual "fx" | ||
224 | Where (x) -> x.name\EndsWith "(Clone)" | ||
225 | Destroy! | ||
226 | ) | ||
227 | |||
228 | macro chainC = (...)-> | ||
229 | import "moonp" as {:to_lua} | ||
230 | callable = nil | ||
231 | config = { | ||
232 | implicit_return_root: false | ||
233 | reserve_line_number: false | ||
234 | } | ||
235 | for item in *{...} | ||
236 | if callable? | ||
237 | callable = "#{callable}:#{to_lua(item,config)\gsub '%s*$',''}" | ||
238 | else | ||
239 | callable = to_lua(item,config)\gsub '%s*$','' | ||
240 | { | ||
241 | codes: $showMacro "chainC", callable | ||
242 | type: "lua" | ||
243 | } | ||
244 | |||
245 | $chainC( | ||
246 | origin.transform.root.gameObject\Parents! | ||
247 | Descendants! | ||
248 | SelectEnable! | ||
249 | SelectVisible! | ||
250 | TagEqual "fx" | ||
251 | Where (x) -> x.name\EndsWith "(Clone)" | ||
252 | Destroy! | ||
253 | ) | ||
254 | |||
255 | macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123" | ||
256 | |||
257 | $implicitReturnMacroIsAllowed! | ||
258 | |||