From 2ff18b4fb66d25d22e5a25fb386fe171853e0b06 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 19 Nov 2021 13:23:11 +0800 Subject: try to fix issue #69 with new macro functions. add builtin macro $MODULE and $LINE. --- spec/inputs/macro-teal.yue | 20 ++++++++++---------- spec/inputs/macro.yue | 38 +++++++++++++++++++++++++++++--------- spec/outputs/macro.lua | 31 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 19 deletions(-) (limited to 'spec') 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 @@ $ -> options.target_extension = "tl" package.path ..= "?.lua;./spec/lib/?.lua" -macro to_lua = (codes)-> - "require('yue').to_lua(#{codes}, reserve_line_number:false, same_module:true)" +macro to_lua = (code)-> + "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" macro trim = (name)-> "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" @@ -16,12 +16,12 @@ export macro local = (decl, value = nil)-> if not (name and type) error "invalid local varaible declaration for \"#{decl}\"" value = $to_lua(value)\gsub "^return ", "" - codes = if tl_enabled + code = if tl_enabled "local #{name}:#{$trim type} = #{value}" else "local #{name} = #{value}" { - :codes + :code type: "text" locals: {name} } @@ -37,28 +37,28 @@ export macro function = (decl, value)-> _, node = tl.parse_program tokens,{},"macro-function" args = table.concat [arg.tk for arg in *node[1].args],", " value = "(#{args})#{value}" - codes = if tl_enabled + code = if tl_enabled value = $to_lua(value)\match "function%([^\n]*%)(.*)end" "local function #{name}#{type}\n#{value}\nend" else value = $to_lua(value)\gsub "^return ", "" "local #{name} = #{value}" { - :codes + :code type: "text" locals: {name} } export macro record = (name, decl)-> import "yue" as {options:{:tl_enabled}} - codes = if tl_enabled + code = if tl_enabled "local record #{name} #{decl} end" else "local #{name} = {}" { - :codes + :code type: "text" locals: {name} } @@ -74,14 +74,14 @@ export macro method = (decl, value)-> _, node = tl.parse_program tokens,{},"macro-function" args = table.concat [arg.tk for arg in *node[1].args],", " value = "(#{args})->#{value\match "[%-=]>(.*)"}" - codes = if tl_enabled + code = if tl_enabled value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" "function #{tab}#{sym}#{func}#{type}\n#{value}\nend" else value = $to_lua(value)\gsub "^return ", "" "#{tab}.#{func} = #{value}" { - :codes + :code type: "text" } 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 _ val = $pipe( {1, 2, 3} - $map(_ * 2) - $filter(_ > 4) - $reduce(0, _1 + _2) + [[$map(_ * 2)]] + [[$filter(_ > 4)]] + [[$reduce(0, _1 + _2)]] ) macro plus = (a, b)-> "#{a} + #{b}" @@ -110,8 +110,8 @@ do a += $get_inner_hygienic! print a -macro lua = (codes)-> { - :codes +macro lua = (code)-> { + :code type: "lua" } @@ -136,7 +136,7 @@ macro def = (fname, ...)-> args = {...} last = table.remove args { - codes: $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) + code: $showMacro "def", "local function #{fname}(#{table.concat args, ', '}) #{last} end" type: "lua" @@ -155,7 +155,7 @@ $def sel, a, b, c, [[ $def dummy,[[]] macro insertComment = (text)-> { - codes: "-- #{text\match '[\'"](.*)[\'"]'}" + code: "-- #{text\match '[\'"](.*)[\'"]'}" type: "lua" } @@ -239,7 +239,7 @@ macro chainC = (...)-> else callable = itemCodes { - codes: $showMacro "chainC", callable + code: $showMacro "chainC", callable type: "lua" } @@ -253,7 +253,27 @@ $chainC( Destroy! ) +macro tb = -> "{'abc', a:123, call#:=> 998}" +print $tb[1], $tb.a, ($tb)!, $tb! + +print "current line: #{ $LINE }" + +macro todoInner = (module, line, msg)-> + print "TODO#{msg and ': ' .. msg or ''} in file #{module}, at line #{line}" + { + code: "-- TODO#{msg and ': ' .. msg or ''}" + type: "lua" + } + +macro todo = (msg)-> + if msg + "$todoInner $MODULE, $LINE, #{msg}" + else + "$todoInner $MODULE, $LINE" + +$todo + macro implicitReturnMacroIsAllowed = -> "print 'abc'\n123" -$implicitReturnMacroIsAllowed! +$implicitReturnMacroIsAllowed 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 origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x) return x.name:EndsWith("(Clone)") end):Destroy() +print((setmetatable({ + 'abc', + a = 123, +}, { + __call = function(self) + return 998 + end +}))[1], (setmetatable({ + 'abc', + a = 123, +}, { + __call = function(self) + return 998 + end +})).a, (setmetatable({ + 'abc', + a = 123, +}, { + __call = function(self) + return 998 + end +}))(), setmetatable({ + 'abc', + a = 123, +}, { + __call = function(self) + return 998 + end +})) +print("current line: " .. tostring(259)) +-- TODO print('abc') return 123 -- cgit v1.2.3-55-g6feb