diff options
Diffstat (limited to 'spec/inputs/macro.mp')
-rw-r--r-- | spec/inputs/macro.mp | 74 |
1 files changed, 55 insertions, 19 deletions
diff --git a/spec/inputs/macro.mp b/spec/inputs/macro.mp index da696bd..f0292c7 100644 --- a/spec/inputs/macro.mp +++ b/spec/inputs/macro.mp | |||
@@ -12,7 +12,7 @@ $myconfig false | |||
12 | 12 | ||
13 | v = $assert item == nil | 13 | v = $assert item == nil |
14 | 14 | ||
15 | macro expr and = (...)-> | 15 | macro and = (...)-> |
16 | values = [value for value in *{...}] | 16 | values = [value for value in *{...}] |
17 | $showMacro "and", "#{ table.concat values, " and " }" | 17 | $showMacro "and", "#{ table.concat values, " and " }" |
18 | 18 | ||
@@ -22,20 +22,20 @@ if $and f1! | |||
22 | if $and f1!, f2!, f3! | 22 | if $and f1!, f2!, f3! |
23 | print "OK" | 23 | print "OK" |
24 | 24 | ||
25 | macro expr in = (target, ...)-> | 25 | macro in = (target, ...)-> |
26 | values = [value for value in *{...}] | 26 | values = [value for value in *{...}] |
27 | $showMacro "in", table.concat ["#{target} == #{item}" for item in *values], " or " | 27 | $showMacro "in", table.concat ["#{target} == #{item}" for item in *values], " or " |
28 | 28 | ||
29 | if x |> $in "Apple", "Pig", "Dog" | 29 | if x |> $in "Apple", "Pig", "Dog" |
30 | print "exist" | 30 | print "exist" |
31 | 31 | ||
32 | macro expr map = (items, action)-> | 32 | macro map = (items, action)-> |
33 | $showMacro "map", "[#{action} for _ in *#{items}]" | 33 | $showMacro "map", "[#{action} for _ in *#{items}]" |
34 | 34 | ||
35 | macro expr filter = (items, action)-> | 35 | macro filter = (items, action)-> |
36 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | 36 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" |
37 | 37 | ||
38 | macro expr reduce = (items, def, action)-> | 38 | macro reduce = (items, def, action)-> |
39 | $showMacro "reduce", "if ##{items} == 0 | 39 | $showMacro "reduce", "if ##{items} == 0 |
40 | #{def} | 40 | #{def} |
41 | else | 41 | else |
@@ -44,11 +44,11 @@ else | |||
44 | _1 = #{action} | 44 | _1 = #{action} |
45 | _1" | 45 | _1" |
46 | 46 | ||
47 | macro block foreach = (items, action)-> | 47 | macro foreach = (items, action)-> |
48 | $showMacro "foreach", "for _ in *#{items} | 48 | $showMacro "foreach", "for _ in *#{items} |
49 | #{action}" | 49 | #{action}" |
50 | 50 | ||
51 | macro expr pipe = (...)-> | 51 | macro pipe = (...)-> |
52 | switch select "#", ... | 52 | switch select "#", ... |
53 | when 0 then return "" | 53 | when 0 then return "" |
54 | when 1 then return ... | 54 | when 1 then return ... |
@@ -74,13 +74,13 @@ val = $pipe( | |||
74 | $reduce(0, _1 + _2) | 74 | $reduce(0, _1 + _2) |
75 | ) | 75 | ) |
76 | 76 | ||
77 | macro expr plus = (a, b)-> "#{a} + #{b}" | 77 | macro plus = (a, b)-> "#{a} + #{b}" |
78 | 78 | ||
79 | $plus(1,2)\call 123 | 79 | $plus(1,2)\call 123 |
80 | 80 | ||
81 | res = 1 |> $plus 2 | 81 | res = 1 |> $plus 2 |
82 | 82 | ||
83 | macro expr curry = (...)-> | 83 | macro curry = (...)-> |
84 | args = {...} | 84 | args = {...} |
85 | len = #args | 85 | len = #args |
86 | body = args[len] | 86 | body = args[len] |
@@ -90,11 +90,11 @@ macro expr curry = (...)-> | |||
90 | f = $curry x,y,z,do | 90 | f = $curry x,y,z,do |
91 | print x,y,z | 91 | print x,y,z |
92 | 92 | ||
93 | macro expr get_inner = (var)-> "do | 93 | macro get_inner = (var)-> "do |
94 | a = 1 | 94 | a = 1 |
95 | a + 1" | 95 | a + 1" |
96 | 96 | ||
97 | macro expr get_inner_hygienic = (var)-> "(-> | 97 | macro get_inner_hygienic = (var)-> "(-> |
98 | local a = 1 | 98 | local a = 1 |
99 | a + 1)!" | 99 | a + 1)!" |
100 | 100 | ||
@@ -110,7 +110,10 @@ do | |||
110 | a += $get_inner_hygienic! | 110 | a += $get_inner_hygienic! |
111 | print a | 111 | print a |
112 | 112 | ||
113 | macro lua lua = (codes)-> codes | 113 | macro lua = (codes)-> { |
114 | :codes | ||
115 | type: "lua" | ||
116 | } | ||
114 | 117 | ||
115 | x = 0 | 118 | x = 0 |
116 | 119 | ||
@@ -129,12 +132,15 @@ end | |||
129 | 132 | ||
130 | print x | 133 | print x |
131 | 134 | ||
132 | macro lua def = (fname, ...)-> | 135 | macro def = (fname, ...)-> |
133 | args = {...} | 136 | args = {...} |
134 | last = table.remove args | 137 | last = table.remove args |
135 | $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) | 138 | { |
139 | codes: $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) | ||
136 | #{last} | 140 | #{last} |
137 | end" | 141 | end" |
142 | type: "lua" | ||
143 | } | ||
138 | 144 | ||
139 | sel = (a, b, c)-> if a then b else c | 145 | sel = (a, b, c)-> if a then b else c |
140 | 146 | ||
@@ -148,13 +154,16 @@ $def sel, a, b, c, [[ | |||
148 | 154 | ||
149 | $def dummy,[[]] | 155 | $def dummy,[[]] |
150 | 156 | ||
151 | macro lua insertComment = (text)-> "-- #{text\match '[\'"](.*)[\'"]'}" | 157 | macro insertComment = (text)-> { |
158 | codes: "-- #{text\match '[\'"](.*)[\'"]'}" | ||
159 | type: "lua" | ||
160 | } | ||
152 | 161 | ||
153 | $insertComment "a comment here" | 162 | $insertComment "a comment here" |
154 | 163 | ||
155 | import 'underscore' as _ | 164 | import 'underscore' as _ |
156 | 165 | ||
157 | macro expr chain = (...)-> | 166 | macro chain = (...)-> |
158 | callable = nil | 167 | callable = nil |
159 | for item in *{...} | 168 | for item in *{...} |
160 | callable = callable? and "(#{callable})\\#{item}" or item | 169 | callable = callable? and "(#{callable})\\#{item}" or item |
@@ -186,7 +195,7 @@ result = $chain( | |||
186 | Destroy! | 195 | Destroy! |
187 | ) | 196 | ) |
188 | 197 | ||
189 | macro block chainB = (...)-> | 198 | macro chainB = (...)-> |
190 | switch select "#", ... | 199 | switch select "#", ... |
191 | when 0 then return "" | 200 | when 0 then return "" |
192 | when 1 then return ... | 201 | when 1 then return ... |
@@ -216,7 +225,34 @@ $chainB( | |||
216 | Destroy! | 225 | Destroy! |
217 | ) | 226 | ) |
218 | 227 | ||
219 | macro block implicitReturnblockMacroIsAllowed = -> "123" | 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" | ||
220 | 256 | ||
221 | $implicitReturnblockMacroIsAllowed! | 257 | $implicitReturnMacroIsAllowed! |
222 | 258 | ||