aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-11-19 13:23:11 +0800
committerLi Jin <dragon-fly@qq.com>2021-11-19 13:23:11 +0800
commit2ff18b4fb66d25d22e5a25fb386fe171853e0b06 (patch)
treed6e6f1671f74a4430b24869c74767aaf0ee2e128 /spec
parenta8e5aaf64969792741f3a094fe0070ddb5e3bc7d (diff)
downloadyuescript-2ff18b4fb66d25d22e5a25fb386fe171853e0b06.tar.gz
yuescript-2ff18b4fb66d25d22e5a25fb386fe171853e0b06.tar.bz2
yuescript-2ff18b4fb66d25d22e5a25fb386fe171853e0b06.zip
try to fix issue #69 with new macro functions. add builtin macro $MODULE and $LINE.
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/macro-teal.yue20
-rw-r--r--spec/inputs/macro.yue38
-rw-r--r--spec/outputs/macro.lua31
3 files changed, 70 insertions, 19 deletions
diff --git a/spec/inputs/macro-teal.yue b/spec/inputs/macro-teal.yue
index 3a9bb2b..951e882 100644
--- a/spec/inputs/macro-teal.yue
+++ b/spec/inputs/macro-teal.yue
@@ -4,8 +4,8 @@ $ ->
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 to_lua = (codes)-> 7macro to_lua = (code)->
8 "require('yue').to_lua(#{codes}, reserve_line_number:false, same_module:true)" 8 "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)"
9 9
10macro trim = (name)-> 10macro trim = (name)->
11 "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" 11 "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}"
@@ -16,12 +16,12 @@ export macro local = (decl, value = nil)->
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 codes = if tl_enabled 19 code = if tl_enabled
20 "local #{name}:#{$trim type} = #{value}" 20 "local #{name}:#{$trim type} = #{value}"
21 else 21 else
22 "local #{name} = #{value}" 22 "local #{name} = #{value}"
23 { 23 {
24 :codes 24 :code
25 type: "text" 25 type: "text"
26 locals: {name} 26 locals: {name}
27 } 27 }
@@ -37,28 +37,28 @@ export macro function = (decl, value)->
37 _, node = tl.parse_program tokens,{},"macro-function" 37 _, node = tl.parse_program tokens,{},"macro-function"
38 args = table.concat [arg.tk for arg in *node[1].args],", " 38 args = table.concat [arg.tk for arg in *node[1].args],", "
39 value = "(#{args})#{value}" 39 value = "(#{args})#{value}"
40 codes = if tl_enabled 40 code = if tl_enabled
41 value = $to_lua(value)\match "function%([^\n]*%)(.*)end" 41 value = $to_lua(value)\match "function%([^\n]*%)(.*)end"
42 "local function #{name}#{type}\n#{value}\nend" 42 "local function #{name}#{type}\n#{value}\nend"
43 else 43 else
44 value = $to_lua(value)\gsub "^return ", "" 44 value = $to_lua(value)\gsub "^return ", ""
45 "local #{name} = #{value}" 45 "local #{name} = #{value}"
46 { 46 {
47 :codes 47 :code
48 type: "text" 48 type: "text"
49 locals: {name} 49 locals: {name}
50 } 50 }
51 51
52export macro record = (name, decl)-> 52export macro record = (name, decl)->
53 import "yue" as {options:{:tl_enabled}} 53 import "yue" as {options:{:tl_enabled}}
54 codes = if tl_enabled 54 code = if tl_enabled
55 "local record #{name} 55 "local record #{name}
56 #{decl} 56 #{decl}
57end" 57end"
58 else 58 else
59 "local #{name} = {}" 59 "local #{name} = {}"
60 { 60 {
61 :codes 61 :code
62 type: "text" 62 type: "text"
63 locals: {name} 63 locals: {name}
64 } 64 }
@@ -74,14 +74,14 @@ export macro method = (decl, value)->
74 _, node = tl.parse_program tokens,{},"macro-function" 74 _, node = tl.parse_program tokens,{},"macro-function"
75 args = table.concat [arg.tk for arg in *node[1].args],", " 75 args = table.concat [arg.tk for arg in *node[1].args],", "
76 value = "(#{args})->#{value\match "[%-=]>(.*)"}" 76 value = "(#{args})->#{value\match "[%-=]>(.*)"}"
77 codes = if tl_enabled 77 code = if tl_enabled
78 value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" 78 value = $to_lua(value)\match "^return function%(.-%)\n(.*)end"
79 "function #{tab}#{sym}#{func}#{type}\n#{value}\nend" 79 "function #{tab}#{sym}#{func}#{type}\n#{value}\nend"
80 else 80 else
81 value = $to_lua(value)\gsub "^return ", "" 81 value = $to_lua(value)\gsub "^return ", ""
82 "#{tab}.#{func} = #{value}" 82 "#{tab}.#{func} = #{value}"
83 { 83 {
84 :codes 84 :code
85 type: "text" 85 type: "text"
86 } 86 }
87 87
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue
index 2dd15ac..366a3d9 100644
--- a/spec/inputs/macro.yue
+++ b/spec/inputs/macro.yue
@@ -69,9 +69,9 @@ $foreach $filter($map({1,2,3}, _ * 2), _ > 4), print _
69 69
70val = $pipe( 70val = $pipe(
71 {1, 2, 3} 71 {1, 2, 3}
72 $map(_ * 2) 72 [[$map(_ * 2)]]
73 $filter(_ > 4) 73 [[$filter(_ > 4)]]
74 $reduce(0, _1 + _2) 74 [[$reduce(0, _1 + _2)]]
75) 75)
76 76
77macro plus = (a, b)-> "#{a} + #{b}" 77macro plus = (a, b)-> "#{a} + #{b}"
@@ -110,8 +110,8 @@ do
110 a += $get_inner_hygienic! 110 a += $get_inner_hygienic!
111 print a 111 print a
112 112
113macro lua = (codes)-> { 113macro lua = (code)-> {
114 :codes 114 :code
115 type: "lua" 115 type: "lua"
116} 116}
117 117
@@ -136,7 +136,7 @@ macro def = (fname, ...)->
136 args = {...} 136 args = {...}
137 last = table.remove args 137 last = table.remove args
138 { 138 {
139 codes: $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) 139 code: $showMacro "def", "local function #{fname}(#{table.concat args, ', '})
140 #{last} 140 #{last}
141end" 141end"
142 type: "lua" 142 type: "lua"
@@ -155,7 +155,7 @@ $def sel, a, b, c, [[
155$def dummy,[[]] 155$def dummy,[[]]
156 156
157macro insertComment = (text)-> { 157macro insertComment = (text)-> {
158 codes: "-- #{text\match '[\'"](.*)[\'"]'}" 158 code: "-- #{text\match '[\'"](.*)[\'"]'}"
159 type: "lua" 159 type: "lua"
160} 160}
161 161
@@ -239,7 +239,7 @@ macro chainC = (...)->
239 else 239 else
240 callable = itemCodes 240 callable = itemCodes
241 { 241 {
242 codes: $showMacro "chainC", callable 242 code: $showMacro "chainC", callable
243 type: "lua" 243 type: "lua"
244 } 244 }
245 245
@@ -253,7 +253,27 @@ $chainC(
253 Destroy! 253 Destroy!
254) 254)
255 255
256macro tb = -> "{'abc', a:123, call#:=> 998}"
257print $tb[1], $tb.a, ($tb)!, $tb!
258
259print "current line: #{ $LINE }"
260
261macro todoInner = (module, line, msg)->
262 print "TODO#{msg and ': ' .. msg or ''} in file #{module}, at line #{line}"
263 {
264 code: "-- TODO#{msg and ': ' .. msg or ''}"
265 type: "lua"
266 }
267
268macro todo = (msg)->
269 if msg
270 "$todoInner $MODULE, $LINE, #{msg}"
271 else
272 "$todoInner $MODULE, $LINE"
273
274$todo
275
256macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123" 276macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123"
257 277
258$implicitReturnMacroIsAllowed! 278$implicitReturnMacroIsAllowed
259 279
diff --git a/spec/outputs/macro.lua b/spec/outputs/macro.lua
index a2430a2..7812182 100644
--- a/spec/outputs/macro.lua
+++ b/spec/outputs/macro.lua
@@ -229,5 +229,36 @@ end
229origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x) 229origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x)
230 return x.name:EndsWith("(Clone)") 230 return x.name:EndsWith("(Clone)")
231end):Destroy() 231end):Destroy()
232print((setmetatable({
233 'abc',
234 a = 123,
235}, {
236 __call = function(self)
237 return 998
238 end
239}))[1], (setmetatable({
240 'abc',
241 a = 123,
242}, {
243 __call = function(self)
244 return 998
245 end
246})).a, (setmetatable({
247 'abc',
248 a = 123,
249}, {
250 __call = function(self)
251 return 998
252 end
253}))(), setmetatable({
254 'abc',
255 a = 123,
256}, {
257 __call = function(self)
258 return 998
259 end
260}))
261print("current line: " .. tostring(259))
262-- TODO
232print('abc') 263print('abc')
233return 123 264return 123