diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/inputs/attrib.moon | 37 | ||||
-rw-r--r-- | spec/inputs/backcall.moon | 6 | ||||
-rw-r--r-- | spec/inputs/macro.moon | 59 |
3 files changed, 95 insertions, 7 deletions
diff --git a/spec/inputs/attrib.moon b/spec/inputs/attrib.moon new file mode 100644 index 0000000..a5fa376 --- /dev/null +++ b/spec/inputs/attrib.moon | |||
@@ -0,0 +1,37 @@ | |||
1 | do | ||
2 | close a = setmetatable {},__close:=> print "closed" | ||
3 | const a = 123 | ||
4 | |||
5 | do | ||
6 | close v = if flag | ||
7 | func! | ||
8 | else | ||
9 | setmetatable {},__close:=> | ||
10 | |||
11 | close f = with io.open "file.txt" | ||
12 | \write "Hello" | ||
13 | |||
14 | macro block defer = (item)-> "close _ = #{item}" | ||
15 | macro block defer_f = (func)-> "close _ = setmetatable {},__close:#{func}" | ||
16 | |||
17 | do | ||
18 | $defer with io.open "file.txt" | ||
19 | \write "Hello" | ||
20 | |||
21 | $defer setmetatable {},__close:=> print "second" | ||
22 | |||
23 | $defer_f -> print "first" | ||
24 | |||
25 | _defers = setmetatable {},__close:=> | ||
26 | @[#@]! | ||
27 | @[#@] = nil | ||
28 | |||
29 | macro block defer_i = (item)-> " | ||
30 | _defers[#_defers + 1] = #{item} | ||
31 | close _ = _defers" | ||
32 | |||
33 | do | ||
34 | $defer_i -> print 3 | ||
35 | $defer_i -> print 2 | ||
36 | $defer_i -> print 1 | ||
37 | |||
diff --git a/spec/inputs/backcall.moon b/spec/inputs/backcall.moon index b6fe24f..86b0ba3 100644 --- a/spec/inputs/backcall.moon +++ b/spec/inputs/backcall.moon | |||
@@ -79,7 +79,7 @@ do | |||
79 | f7! | 79 | f7! |
80 | 80 | ||
81 | do | 81 | do |
82 | {:result,:msg} = do | 82 | :result,:msg = do |
83 | (data)<- receiveAsync "filename.txt" | 83 | (data)<- receiveAsync "filename.txt" |
84 | print data | 84 | print data |
85 | (info)<- processAsync data | 85 | (info)<- processAsync data |
@@ -107,6 +107,10 @@ alert "hi" | |||
107 | 107 | ||
108 | x = 123 |> a |> b or 456 |> c |> d or a.if\then("abc") or a?.b\c?(123) or x\y | 108 | x = 123 |> a |> b or 456 |> c |> d or a.if\then("abc") or a?.b\c?(123) or x\y |
109 | 109 | ||
110 | x1 = 3 * -4 |> f | ||
111 | |||
112 | x2 = 3 * -2 ^ 2 |> f | ||
113 | |||
110 | y = 1 + not # 2 |> (a ^ c) |> b(3,_) * 4 ^ -123 |> f |> f1 or 123 | 114 | y = 1 + not # 2 |> (a ^ c) |> b(3,_) * 4 ^ -123 |> f |> f1 or 123 |
111 | 115 | ||
112 | nil | 116 | nil |
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 | ||
20 | if $and f1 | 20 | if $and f1! |
21 | print "OK" | 21 | print "OK" |
22 | 22 | ||
23 | if $and f1,f2,f3 | 23 | if $and f1!, f2!, f3! |
24 | print "OK" | 24 | print "OK" |
25 | 25 | ||
26 | macro expr map = (items,action)-> | 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)-> | ||
27 | $showMacro "map", "[#{action} for _ in *#{items}]" | 34 | $showMacro "map", "[#{action} for _ in *#{items}]" |
28 | 35 | ||
29 | macro expr filter = (items,action)-> | 36 | macro expr filter = (items, action)-> |
30 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | 37 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" |
31 | 38 | ||
32 | macro expr reduce = (items,def,action)-> | 39 | macro expr reduce = (items, def, action)-> |
33 | $showMacro "reduce", "if ##{items} == 0 | 40 | $showMacro "reduce", "if ##{items} == 0 |
34 | #{def} | 41 | #{def} |
35 | else | 42 | else |
@@ -38,7 +45,7 @@ else | |||
38 | _1 = #{action} | 45 | _1 = #{action} |
39 | _1" | 46 | _1" |
40 | 47 | ||
41 | macro block foreach = (items,action)-> | 48 | macro 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 | ||
82 | res = 1 |> $plus 2 | ||
83 | |||
75 | macro expr curry = (...)-> | 84 | macro 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 | ||
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 | "\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 | |||
138 | macro block implicitReturnblockMacroIsAllowed = -> "123" | 185 | macro block implicitReturnblockMacroIsAllowed = -> "123" |
139 | 186 | ||
140 | $implicitReturnblockMacroIsAllowed! | 187 | $implicitReturnblockMacroIsAllowed! |