aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/attrib.moon37
-rw-r--r--spec/inputs/backcall.moon6
-rw-r--r--spec/inputs/macro.moon59
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 @@
1do
2 close a = setmetatable {},__close:=> print "closed"
3 const a = 123
4
5do
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
14macro block defer = (item)-> "close _ = #{item}"
15macro block defer_f = (func)-> "close _ = setmetatable {},__close:#{func}"
16
17do
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
29macro block defer_i = (item)-> "
30_defers[#_defers + 1] = #{item}
31close _ = _defers"
32
33do
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
81do 81do
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
108x = 123 |> a |> b or 456 |> c |> d or a.if\then("abc") or a?.b\c?(123) or x\y 108x = 123 |> a |> b or 456 |> c |> d or a.if\then("abc") or a?.b\c?(123) or x\y
109 109
110x1 = 3 * -4 |> f
111
112x2 = 3 * -2 ^ 2 |> f
113
110y = 1 + not # 2 |> (a ^ c) |> b(3,_) * 4 ^ -123 |> f |> f1 or 123 114y = 1 + not # 2 |> (a ^ c) |> b(3,_) * 4 ^ -123 |> f |> f1 or 123
111 115
112nil 116nil
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
20if $and f1 20if $and f1!
21 print "OK" 21 print "OK"
22 22
23if $and f1,f2,f3 23if $and f1!, f2!, f3!
24 print "OK" 24 print "OK"
25 25
26macro expr map = (items,action)-> 26macro expr in = (target, ...)->
27 values = [value for value in *{...}]
28 $showMacro "in", "#{ table.concat ["#{target} == #{item}" for item in *values], " or " }"
29
30if x |> $in "Apple", "Pig", "Dog"
31 print "exist"
32
33macro expr map = (items, action)->
27 $showMacro "map", "[#{action} for _ in *#{items}]" 34 $showMacro "map", "[#{action} for _ in *#{items}]"
28 35
29macro expr filter = (items,action)-> 36macro expr filter = (items, action)->
30 $showMacro "filter", "[_ for _ in *#{items} when #{action}]" 37 $showMacro "filter", "[_ for _ in *#{items} when #{action}]"
31 38
32macro expr reduce = (items,def,action)-> 39macro expr reduce = (items, def, action)->
33 $showMacro "reduce", "if ##{items} == 0 40 $showMacro "reduce", "if ##{items} == 0
34 #{def} 41 #{def}
35else 42else
@@ -38,7 +45,7 @@ else
38 _1 = #{action} 45 _1 = #{action}
39 _1" 46 _1"
40 47
41macro block foreach = (items,action)-> 48macro 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
82res = 1 |> $plus 2
83
75macro expr curry = (...)-> 84macro 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
147import 'underscore' as _
148
149macro expr chain = (...)-> $showMacro "chain",table.concat {...}, "\\"
150
151a = $chain(
152 _({1, 2, 3, 4, -2, 3})
153 chain!
154 map(=> @ * 2)
155 filter(=> @ > 3)
156 value!
157)
158
159macro 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
138macro block implicitReturnblockMacroIsAllowed = -> "123" 185macro block implicitReturnblockMacroIsAllowed = -> "123"
139 186
140$implicitReturnblockMacroIsAllowed! 187$implicitReturnblockMacroIsAllowed!