1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
import "macro_export" as {
$, -- import all macros
$config: $myconfig, -- rename macro $config to $myconfig
}
import "macro_todo" as $
macro not_leak = -> "leak"
do
macro x = -> 123
print $x
do
macro x = -> math.pi * 2
print $x
macro x = -> 456
print $x
import "macro_todo" as $todo: $not_leak
$not_leak "todo in a do block"
$not_leak!!
macro WindowFlag = $enum(
NoNav
NoDecoration
NoTitleBar
NoResize
NoMove
NoScrollbar
NoScrollWithMouse
NoCollapse
AlwaysAutoResize
NoSavedSettings
NoInputs
MenuBar
HorizontalScrollbar
NoFocusOnAppearing
NoBringToFrontOnFocus
AlwaysVerticalScrollbar
AlwaysHorizontalScrollbar
NoNavInputs
NoNavFocus
UnsavedDocument
)
print $WindowFlag AlwaysAutoResize
print $WindowFlag(
NoNav
NoDecoration
NoTitleBar
NoResize
NoMove
NoScrollbar
)
macro NumAndStr = (num, str) ->
unless $is_ast(Num, num) and $is_ast SingleString, str
error "got unexpected token"
"[#{num}, #{str}]"
print $NumAndStr 123, 'xyz'
$asserts item == nil
$myconfig false
v = $assert item == nil
macro and = (...)->
values = [value for value in *{...}]
$showMacro "and", "#{ table.concat values, " and " }"
if $and f1!
print "OK"
if $and f1!, f2!, f3!
print "OK"
item = $copy(
@
{pos: {}, flags: flags::tonumber!}
id
connections
pos.x
pos.y
pos.z
)
macro 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 map = (items, action)->
$showMacro "map", "[#{action} for _ in *#{items}]"
macro filter = (items, action)->
$showMacro "filter", "[_ for _ in *#{items} when #{action}]"
macro reduce = (items, def, action)->
$showMacro "reduce", "if ##{items} == 0
#{def}
else
_1 = #{def}
for _2 in *#{items}
_1 = #{action}
_1"
macro foreach = (items, action)->
$showMacro "foreach", "for _ in *#{items}
#{action}"
macro pipe = (...)->
switch select "#", ...
when 0 then return ""
when 1 then return ...
ops = {...}
last = ops[1]
stmts = for i = 2, #ops
stmt = "\tlocal _#{i} = #{last} |> #{ops[i]}"
last = "_#{i}"
stmt
res = "do
#{table.concat stmts, "\n"}
#{last}"
$showMacro "pipe", res
{1,2,3} |> $map(_ * 2) |> $filter(_ > 4) |> $foreach print _
$foreach $filter($map({1,2,3}, _ * 2), _ > 4), print _
val = $pipe(
{1, 2, 3}
[[$map(_ * 2)]]
[[$filter(_ > 4)]]
[[$reduce(0, _1 + _2)]]
)
macro plus = (a, b)-> "#{a} + #{b}"
$plus(1,2)\call 123
res = 1 |> $plus 2
macro curry = (...)->
args = {...}
len = #args
body = args[len]
def = table.concat ["(#{args[i]})->" for i = 1, len - 1]
"#{def}\n#{body\gsub "^do%s*\n",""}"
f = $curry x,y,z,do
print x,y,z
macro get_inner = (var)-> "do
a = 1
a + 1"
macro get_inner_hygienic = (var)-> "(->
local a = 1
a + 1)!"
do
a = 8
a = $get_inner!
a += $get_inner!
print a
do
a = 8
a = $get_inner_hygienic!
a += $get_inner_hygienic!
print a
macro lua = (code)-> {
:code
type: "lua"
}
x = 0
$lua [[
local function f(a)
return a + 1
end
x = x + f(3)
]]
$lua[[
function tb:func()
print(123)
end
]]
print x
macro def = (fname, ...)->
args = {...}
last = table.remove args
{
code: $showMacro "def", "local function #{fname}(#{table.concat args, ', '})
#{last}
end"
type: "lua"
}
sel = (a, b, c)-> if a then b else c
$def sel, a, b, c, [[
if a then
return b
else
return c
end
]]
$def dummy,[[]]
macro insertComment = (text)-> {
code: "-- #{text\match '[\'"](.*)[\'"]'}"
type: "lua"
}
$insertComment "a comment here"
import 'underscore' as _
macro chain = (...)->
callable = nil
for item in *{...}
callable = callable? and "(#{callable})\\#{item}" or item
$showMacro "chain", callable
a = $chain(
_{1, 2, 3, 4, -2, 3}
chain!
map => @ * 2
filter => @ > 3
value!
)
$chain(
_{1, 2, 3, 4, -2, 3}
chain!
map => @ * 2
filter => @ > 3
each => print @
)
result = $chain(
origin.transform.root.gameObject\Parents!
Descendants!
SelectEnable!
SelectVisible!
TagEqual "fx"
Where (x) -> x.name\EndsWith "(Clone)"
Destroy!
)
macro chainB = (...)->
switch select "#", ...
when 0 then return ""
when 1 then return ...
items = {...}
last = nil
stmts = for i = 1,#items
stmt = if i == #items
lastStr = last and "#{last}\\" or ""
"\t#{lastStr}#{items[i]}"
else
lastStr = last and "#{last}\\" or ""
"\tlocal _#{i} = #{lastStr}#{items[i]}"
last = "_#{i}"
stmt
res = "do
#{table.concat stmts, '\n'}
"
$showMacro "chainB", res
$chainB(
origin.transform.root.gameObject\Parents!
Descendants!
SelectEnable!
SelectVisible!
TagEqual "fx"
Where (x) -> x.name\EndsWith "(Clone)"
Destroy!
)
macro chainC = (...)->
import "yue" as {:to_lua}
callable = nil
config = {
implicit_return_root: false
reserve_line_number: false
}
for item in *{...}
itemCodes = to_lua(item,config)\gsub '%s*$',''
if callable?
callable = "#{callable}:#{itemCodes}"
else
callable = itemCodes
{
code: $showMacro "chainC", callable
type: "lua"
}
$chainC(
origin.transform.root.gameObject\Parents!
Descendants!
SelectEnable!
SelectVisible!
TagEqual "fx"
Where (x) -> x.name\EndsWith "(Clone)"
Destroy!
)
macro tb = -> "{'abc', a:123, <call>:=> 998}"
print $tb[1], $tb.a, ($tb)!, $tb!
print "current line: #{ $LINE }"
$todo
macro skip = -> ""
do
print 1
<- $skip
print 2
print 3
macro skip = -> "while false do break"
_1 = ->
print 1
<- $skip
print 2
print 3
do
macro foo = -> code: "tb:func(123)", type: "lua"
f = ->
x = $foo\bar 456
$foo!
f1 = ->
$foo!
return
macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123"
$implicitReturnMacroIsAllowed
|