diff options
Diffstat (limited to 'spec/inputs/macro.mp')
-rw-r--r-- | spec/inputs/macro.mp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/spec/inputs/macro.mp b/spec/inputs/macro.mp new file mode 100644 index 0000000..c7ec79c --- /dev/null +++ b/spec/inputs/macro.mp | |||
@@ -0,0 +1,190 @@ | |||
1 | macro block init = -> | ||
2 | with require "moonp" | ||
3 | package.moonpath = "?.mp;./spec/inputs/?.mp" | ||
4 | "" | ||
5 | |||
6 | $init! | ||
7 | |||
8 | import "macro_export" as {$myconfig:$config, :$showMacro, :$asserts, :$assert} | ||
9 | |||
10 | $asserts item == nil | ||
11 | |||
12 | $myconfig false | ||
13 | |||
14 | v = $assert item == nil | ||
15 | |||
16 | macro expr and = (...)-> | ||
17 | values = [value for value in *{...}] | ||
18 | $showMacro "and", "#{ table.concat values, " and " }" | ||
19 | |||
20 | if $and f1! | ||
21 | print "OK" | ||
22 | |||
23 | if $and f1!, f2!, f3! | ||
24 | print "OK" | ||
25 | |||
26 | macro expr in = (target, ...)-> | ||
27 | values = [value for value in *{...}] | ||
28 | $showMacro "in", table.concat ["#{target} == #{item}" for item in *values], " or " | ||
29 | |||
30 | if x |> $in "Apple", "Pig", "Dog" | ||
31 | print "exist" | ||
32 | |||
33 | macro expr map = (items, action)-> | ||
34 | $showMacro "map", "[#{action} for _ in *#{items}]" | ||
35 | |||
36 | macro expr filter = (items, action)-> | ||
37 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | ||
38 | |||
39 | macro expr reduce = (items, def, action)-> | ||
40 | $showMacro "reduce", "if ##{items} == 0 | ||
41 | #{def} | ||
42 | else | ||
43 | _1 = #{def} | ||
44 | for _2 in *#{items} | ||
45 | _1 = #{action} | ||
46 | _1" | ||
47 | |||
48 | macro block foreach = (items, action)-> | ||
49 | $showMacro "foreach", "for _ in *#{items} | ||
50 | #{action}" | ||
51 | |||
52 | macro expr pipe = (...)-> | ||
53 | switch select "#", ... | ||
54 | when 0 then return "" | ||
55 | when 1 then return ... | ||
56 | ops = {...} | ||
57 | last = ops[1] | ||
58 | stmts = for i = 2, #ops | ||
59 | stmt = "\tlocal _#{i} = #{last} |> #{ops[i]}" | ||
60 | last = "_#{i}" | ||
61 | stmt | ||
62 | res = "do | ||
63 | #{table.concat stmts, "\n"} | ||
64 | #{last}" | ||
65 | $showMacro "pipe", res | ||
66 | |||
67 | {1,2,3} |> $map(_ * 2) |> $filter(_ > 4) |> $foreach print _ | ||
68 | |||
69 | $foreach $filter($map({1,2,3}, _ * 2), _ > 4), print _ | ||
70 | |||
71 | val = $pipe( | ||
72 | {1, 2, 3} | ||
73 | $map(_ * 2) | ||
74 | $filter(_ > 4) | ||
75 | $reduce(0, _1 + _2) | ||
76 | ) | ||
77 | |||
78 | macro expr plus = (a, b)-> "#{a} + #{b}" | ||
79 | |||
80 | $plus(1,2)\call 123 | ||
81 | |||
82 | res = 1 |> $plus 2 | ||
83 | |||
84 | macro expr curry = (...)-> | ||
85 | args = {...} | ||
86 | len = #args | ||
87 | body = args[len] | ||
88 | def = table.concat ["(#{args[i]})->" for i = 1, len - 1] | ||
89 | "#{def}\n#{body\gsub "^do%s*\n",""}" | ||
90 | |||
91 | f = $curry x,y,z,do | ||
92 | print x,y,z | ||
93 | |||
94 | macro expr get_inner = (var)-> "do | ||
95 | a = 1 | ||
96 | a + 1" | ||
97 | |||
98 | macro expr get_inner_hygienic = (var)-> "(-> | ||
99 | local a = 1 | ||
100 | a + 1)!" | ||
101 | |||
102 | do | ||
103 | a = 8 | ||
104 | a = $get_inner! | ||
105 | a += $get_inner! | ||
106 | print a | ||
107 | |||
108 | do | ||
109 | a = 8 | ||
110 | a = $get_inner_hygienic! | ||
111 | a += $get_inner_hygienic! | ||
112 | print a | ||
113 | |||
114 | macro lua lua = (codes)-> "#{codes}" | ||
115 | |||
116 | x = 0 | ||
117 | |||
118 | $lua [[ | ||
119 | local function f(a) | ||
120 | return a + 1 | ||
121 | end | ||
122 | x = x + f(3) | ||
123 | ]] | ||
124 | |||
125 | print x | ||
126 | |||
127 | macro lua def = (fname, ...)-> | ||
128 | args = {...} | ||
129 | last = table.remove args | ||
130 | $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) | ||
131 | #{last} | ||
132 | end" | ||
133 | |||
134 | sel = (a, b, c)-> if a then b else c | ||
135 | |||
136 | $def sel, a, b, c, [[ | ||
137 | if a then | ||
138 | return b | ||
139 | else | ||
140 | return c | ||
141 | end | ||
142 | ]] | ||
143 | |||
144 | $def dummy,[[ | ||
145 | ]] | ||
146 | |||
147 | import 'underscore' as _ | ||
148 | |||
149 | macro expr chain = (...)-> $showMacro "chain",table.concat {...}, "\\" | ||
150 | |||
151 | a = $chain( | ||
152 | _({1, 2, 3, 4, -2, 3}) | ||
153 | chain! | ||
154 | map(=> @ * 2) | ||
155 | filter(=> @ > 3) | ||
156 | value! | ||
157 | ) | ||
158 | |||
159 | macro 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 | lastStr = last and "#{last}\\" or "" | ||
168 | "\t#{lastStr}#{items[i]}" | ||
169 | else | ||
170 | lastStr = last and "#{last}\\" or "" | ||
171 | "\tlocal _#{i} = #{lastStr}#{items[i]}" | ||
172 | last = "_#{i}" | ||
173 | stmt | ||
174 | res = "do | ||
175 | #{table.concat stmts, '\n'} | ||
176 | " | ||
177 | $showMacro "chainB", res | ||
178 | |||
179 | $chainB( | ||
180 | _{1, 2, 3, 4, -2, 3} | ||
181 | chain! | ||
182 | map => @ * 2 | ||
183 | filter => @ > 3 | ||
184 | each => print @ | ||
185 | ) | ||
186 | |||
187 | macro block implicitReturnblockMacroIsAllowed = -> "123" | ||
188 | |||
189 | $implicitReturnblockMacroIsAllowed! | ||
190 | |||