aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-08 13:38:07 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-08 13:38:07 +0800
commit24dab43a753538dcf3b85bd09bf76c0a3c3c8b73 (patch)
tree51c59ddf8302657ae3cce5d28ba27c66737fc842 /spec
parent32651cccb380bf4682fa8f6d3e70d74952171f7b (diff)
downloadyuescript-24dab43a753538dcf3b85bd09bf76c0a3c3c8b73.tar.gz
yuescript-24dab43a753538dcf3b85bd09bf76c0a3c3c8b73.tar.bz2
yuescript-24dab43a753538dcf3b85bd09bf76c0a3c3c8b73.zip
simplify macro syntax. fix issue #38 again.
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/ambiguous.mp2
-rw-r--r--spec/inputs/attrib.mp6
-rw-r--r--spec/inputs/macro-export.mp9
-rw-r--r--spec/inputs/macro-teal.mp51
-rw-r--r--spec/inputs/macro.mp74
5 files changed, 99 insertions, 43 deletions
diff --git a/spec/inputs/ambiguous.mp b/spec/inputs/ambiguous.mp
index a5980db..d147e8e 100644
--- a/spec/inputs/ambiguous.mp
+++ b/spec/inputs/ambiguous.mp
@@ -10,7 +10,7 @@ import c from d
10a, b = c, d 10a, b = c, d
11(d a) c 11(d a) c
12 12
13macro block f = (func,arg)-> "(#{func}) #{arg}" 13macro f = (func,arg)-> "(#{func}) #{arg}"
14for i = 1, 10 14for i = 1, 10
15 a = -> 15 a = ->
16 $f print, 1 16 $f print, 1
diff --git a/spec/inputs/attrib.mp b/spec/inputs/attrib.mp
index bc1e67e..7e9a42c 100644
--- a/spec/inputs/attrib.mp
+++ b/spec/inputs/attrib.mp
@@ -14,8 +14,8 @@ do
14 close f = with io.open "file.txt" 14 close f = with io.open "file.txt"
15 \write "Hello" 15 \write "Hello"
16 16
17macro block defer = (item)-> "close _ = #{item}" 17macro defer = (item)-> "close _ = #{item}"
18macro block defer_f = (func)-> "close _ = setmetatable {},__close:#{func}" 18macro defer_f = (func)-> "close _ = setmetatable {},__close:#{func}"
19 19
20do 20do
21 $defer with io.open "file.txt" 21 $defer with io.open "file.txt"
@@ -29,7 +29,7 @@ _defers = setmetatable {},__close:=>
29 @[#@]! 29 @[#@]!
30 @[#@] = nil 30 @[#@] = nil
31 31
32macro block defer_i = (item)-> " 32macro defer_i = (item)-> "
33_defers[#_defers + 1] = #{item} 33_defers[#_defers + 1] = #{item}
34close _ = _defers" 34close _ = _defers"
35 35
diff --git a/spec/inputs/macro-export.mp b/spec/inputs/macro-export.mp
index b6079ca..d669975 100644
--- a/spec/inputs/macro-export.mp
+++ b/spec/inputs/macro-export.mp
@@ -1,9 +1,9 @@
1export macro block config = (debugging = true)-> 1export macro config = (debugging = true)->
2 global debugMode = debugging == "true" 2 global debugMode = debugging == "true"
3 global debugMacro = true 3 global debugMacro = true
4 "" 4 ""
5 5
6export macro expr showMacro = (name,res)-> 6export macro showMacro = (name,res)->
7 if debugMacro 7 if debugMacro
8 "do 8 "do
9 txt = #{res} 9 txt = #{res}
@@ -13,16 +13,17 @@ export macro expr showMacro = (name,res)->
13 else 13 else
14 res 14 res
15 15
16export macro block asserts = (cond)-> 16export macro asserts = (cond)->
17 if debugMode 17 if debugMode
18 $showMacro "assert", "assert #{cond}" 18 $showMacro "assert", "assert #{cond}"
19 else 19 else
20 "" 20 ""
21 21
22export macro expr assert = (cond)-> 22export macro assert = (cond)->
23 if debugMode 23 if debugMode
24 $showMacro "assert", "assert #{cond}" 24 $showMacro "assert", "assert #{cond}"
25 else 25 else
26 "#{cond}" 26 "#{cond}"
27 27
28$config! 28$config!
29
diff --git a/spec/inputs/macro-teal.mp b/spec/inputs/macro-teal.mp
index 9ce1bcd..37cde1c 100644
--- a/spec/inputs/macro-teal.mp
+++ b/spec/inputs/macro-teal.mp
@@ -4,24 +4,29 @@ $ ->
4 options.target_extension = "tl" 4 options.target_extension = "tl"
5 package.path ..= "?.lua;./spec/lib/?.lua" 5 package.path ..= "?.lua;./spec/lib/?.lua"
6 6
7macro expr to_lua = (codes)-> 7macro to_lua = (codes)->
8 "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)" 8 "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)"
9 9
10macro expr trim = (name)-> 10macro trim = (name)->
11 "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" 11 "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}"
12 12
13export macro text local = (decl, value = nil)-> 13export macro local = (decl, value = nil)->
14 import "moonp" as {options:{:tl_enabled}} 14 import "moonp" as {options:{:tl_enabled}}
15 name, type = ($trim decl)\match "(.-):(.*)" 15 name, type = ($trim decl)\match "(.-):(.*)"
16 if not (name and type) 16 if not (name and type)
17 error "invalid local varaible declaration for \"#{decl}\"" 17 error "invalid local varaible declaration for \"#{decl}\""
18 value = $to_lua(value)\gsub "^return ", "" 18 value = $to_lua(value)\gsub "^return ", ""
19 if tl_enabled 19 codes = if tl_enabled
20 "local #{name}:#{$trim type} = #{value}", {name} 20 "local #{name}:#{$trim type} = #{value}"
21 else 21 else
22 "local #{name} = #{value}", {name} 22 "local #{name} = #{value}"
23 {
24 :codes
25 type: "text"
26 locals: {name}
27 }
23 28
24export macro text function = (decl, value)-> 29export macro function = (decl, value)->
25 import "moonp" as {options:{:tl_enabled}} 30 import "moonp" as {options:{:tl_enabled}}
26 import "tl" 31 import "tl"
27 decl = $trim decl 32 decl = $trim decl
@@ -32,23 +37,33 @@ export macro text function = (decl, value)->
32 _, node = tl.parse_program tokens,{},"macro-function" 37 _, node = tl.parse_program tokens,{},"macro-function"
33 args = table.concat [arg.tk for arg in *node[1].args],", " 38 args = table.concat [arg.tk for arg in *node[1].args],", "
34 value = "(#{args})#{value}" 39 value = "(#{args})#{value}"
35 if tl_enabled 40 codes = if tl_enabled
36 value = $to_lua(value)\match "function%([^\n]*%)(.*)end" 41 value = $to_lua(value)\match "function%([^\n]*%)(.*)end"
37 "local function #{name}#{type}\n#{value}\nend", {name} 42 "local function #{name}#{type}\n#{value}\nend"
38 else 43 else
39 value = $to_lua(value)\gsub "^return ", "" 44 value = $to_lua(value)\gsub "^return ", ""
40 "local #{name} = #{value}", {name} 45 "local #{name} = #{value}"
46 {
47 :codes
48 type: "text"
49 locals: {name}
50 }
41 51
42export macro text record = (name, decl)-> 52export macro record = (name, decl)->
43 import "moonp" as {options:{:tl_enabled}} 53 import "moonp" as {options:{:tl_enabled}}
44 if tl_enabled 54 codes = if tl_enabled
45 "local record #{name} 55 "local record #{name}
46 #{decl} 56 #{decl}
47end", {name} 57end"
48 else 58 else
49 "local #{name} = {}", {name} 59 "local #{name} = {}"
60 {
61 :codes
62 type: "text"
63 locals: {name}
64 }
50 65
51export macro text method = (decl, value)-> 66export macro method = (decl, value)->
52 import "moonp" as {options:{:tl_enabled}} 67 import "moonp" as {options:{:tl_enabled}}
53 import "tl" 68 import "tl"
54 decl = $trim decl 69 decl = $trim decl
@@ -59,10 +74,14 @@ export macro text method = (decl, value)->
59 _, node = tl.parse_program tokens,{},"macro-function" 74 _, node = tl.parse_program tokens,{},"macro-function"
60 args = table.concat [arg.tk for arg in *node[1].args],", " 75 args = table.concat [arg.tk for arg in *node[1].args],", "
61 value = "(#{args})->#{value\match "[%-=]>(.*)"}" 76 value = "(#{args})->#{value\match "[%-=]>(.*)"}"
62 if tl_enabled 77 codes = if tl_enabled
63 value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" 78 value = $to_lua(value)\match "^return function%(.-%)\n(.*)end"
64 "function #{tab}#{sym}#{func}#{type}\n#{value}\nend" 79 "function #{tab}#{sym}#{func}#{type}\n#{value}\nend"
65 else 80 else
66 value = $to_lua(value)\gsub "^return ", "" 81 value = $to_lua(value)\gsub "^return ", ""
67 "#{tab}.#{func} = #{value}" 82 "#{tab}.#{func} = #{value}"
83 {
84 :codes
85 type: "text"
86 }
68 87
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
13v = $assert item == nil 13v = $assert item == nil
14 14
15macro expr and = (...)-> 15macro 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!
22if $and f1!, f2!, f3! 22if $and f1!, f2!, f3!
23 print "OK" 23 print "OK"
24 24
25macro expr in = (target, ...)-> 25macro 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
29if x |> $in "Apple", "Pig", "Dog" 29if x |> $in "Apple", "Pig", "Dog"
30 print "exist" 30 print "exist"
31 31
32macro expr map = (items, action)-> 32macro map = (items, action)->
33 $showMacro "map", "[#{action} for _ in *#{items}]" 33 $showMacro "map", "[#{action} for _ in *#{items}]"
34 34
35macro expr filter = (items, action)-> 35macro filter = (items, action)->
36 $showMacro "filter", "[_ for _ in *#{items} when #{action}]" 36 $showMacro "filter", "[_ for _ in *#{items} when #{action}]"
37 37
38macro expr reduce = (items, def, action)-> 38macro reduce = (items, def, action)->
39 $showMacro "reduce", "if ##{items} == 0 39 $showMacro "reduce", "if ##{items} == 0
40 #{def} 40 #{def}
41else 41else
@@ -44,11 +44,11 @@ else
44 _1 = #{action} 44 _1 = #{action}
45 _1" 45 _1"
46 46
47macro block foreach = (items, action)-> 47macro foreach = (items, action)->
48 $showMacro "foreach", "for _ in *#{items} 48 $showMacro "foreach", "for _ in *#{items}
49 #{action}" 49 #{action}"
50 50
51macro expr pipe = (...)-> 51macro 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
77macro expr plus = (a, b)-> "#{a} + #{b}" 77macro plus = (a, b)-> "#{a} + #{b}"
78 78
79$plus(1,2)\call 123 79$plus(1,2)\call 123
80 80
81res = 1 |> $plus 2 81res = 1 |> $plus 2
82 82
83macro expr curry = (...)-> 83macro 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 = (...)->
90f = $curry x,y,z,do 90f = $curry x,y,z,do
91 print x,y,z 91 print x,y,z
92 92
93macro expr get_inner = (var)-> "do 93macro get_inner = (var)-> "do
94 a = 1 94 a = 1
95 a + 1" 95 a + 1"
96 96
97macro expr get_inner_hygienic = (var)-> "(-> 97macro 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
113macro lua lua = (codes)-> codes 113macro lua = (codes)-> {
114 :codes
115 type: "lua"
116}
114 117
115x = 0 118x = 0
116 119
@@ -129,12 +132,15 @@ end
129 132
130print x 133print x
131 134
132macro lua def = (fname, ...)-> 135macro 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}
137end" 141end"
142 type: "lua"
143 }
138 144
139sel = (a, b, c)-> if a then b else c 145sel = (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
151macro lua insertComment = (text)-> "-- #{text\match '[\'"](.*)[\'"]'}" 157macro insertComment = (text)-> {
158 codes: "-- #{text\match '[\'"](.*)[\'"]'}"
159 type: "lua"
160}
152 161
153$insertComment "a comment here" 162$insertComment "a comment here"
154 163
155import 'underscore' as _ 164import 'underscore' as _
156 165
157macro expr chain = (...)-> 166macro 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
189macro block chainB = (...)-> 198macro 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
219macro block implicitReturnblockMacroIsAllowed = -> "123" 228macro 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
255macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123"
220 256
221$implicitReturnblockMacroIsAllowed! 257$implicitReturnMacroIsAllowed!
222 258