From 5497775534d20ba06ab9c13bc4db1c5bee877513 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 28 Jul 2023 11:53:04 +0800 Subject: fix xpcall usages in different Lua version. --- spec/inputs/macro-export.yue | 50 ------------------------- spec/inputs/macro-teal.yue | 87 -------------------------------------------- spec/inputs/macro-todo.yue | 13 ------- spec/inputs/macro.yue | 4 +- spec/inputs/macro_export.yue | 50 +++++++++++++++++++++++++ spec/inputs/macro_teal.yue | 87 ++++++++++++++++++++++++++++++++++++++++++++ spec/inputs/macro_todo.yue | 13 +++++++ spec/inputs/teal-lang.yue | 36 ------------------ spec/inputs/teal_lang.yue | 36 ++++++++++++++++++ spec/inputs/try-catch.yue | 54 --------------------------- spec/inputs/try_catch.yue | 54 +++++++++++++++++++++++++++ 11 files changed, 242 insertions(+), 242 deletions(-) delete mode 100644 spec/inputs/macro-export.yue delete mode 100644 spec/inputs/macro-teal.yue delete mode 100644 spec/inputs/macro-todo.yue create mode 100644 spec/inputs/macro_export.yue create mode 100644 spec/inputs/macro_teal.yue create mode 100644 spec/inputs/macro_todo.yue delete mode 100644 spec/inputs/teal-lang.yue create mode 100644 spec/inputs/teal_lang.yue delete mode 100644 spec/inputs/try-catch.yue create mode 100644 spec/inputs/try_catch.yue (limited to 'spec/inputs') diff --git a/spec/inputs/macro-export.yue b/spec/inputs/macro-export.yue deleted file mode 100644 index ff0d273..0000000 --- a/spec/inputs/macro-export.yue +++ /dev/null @@ -1,50 +0,0 @@ -import "macro-todo" as $ - -import "macro-todo" as {$, :$todo} - -export macro config = (debugging = true)-> - global debugMode = debugging == "true" - global debugMacro = true - "" - -export macro showMacro = (name, res)-> - if debugMacro then " -do - txt = #{res} - print '[macro ' .. #{name} .. ']' - print txt - txt -" - else - res - -export macro asserts = (cond)-> - if debugMode - $showMacro "assert", "assert #{cond}" - else - "" - -export macro assert = (cond)-> - if debugMode - $showMacro "assert", "assert #{cond}" - else - "#{cond}" - -export macro copy = (src, dst, ...)-> - assert( - src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" - "copy targets can not be _src_ or _dst_" - ) - " -do - local _src_, _dst_ - with _dst_ = #{dst} - with _src_ = #{src} -#{table.concat for field in *{...} do " - _dst_.#{field} = _src_.#{field} -"}" - -$ -> - global debugMode = true - global debugMacro = true - diff --git a/spec/inputs/macro-teal.yue b/spec/inputs/macro-teal.yue deleted file mode 100644 index a443614..0000000 --- a/spec/inputs/macro-teal.yue +++ /dev/null @@ -1,87 +0,0 @@ -$ -> - import "yue" as {:options} - if options.tl_enabled - options.target_extension = "tl" - package.path ..= ";./spec/lib/?.lua" - -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}" - -export macro local = (decl, value = nil)-> - import "yue" as {options:{:tl_enabled}} - name, type = ($trim decl)\match "(.-):(.*)" - if not (name and type) - error "invalid local varaible declaration for \"#{decl}\"" - value = $to_lua(value)\gsub "^return ", "" - code = if tl_enabled - "local #{name}:#{$trim type} = #{value}" - else - "local #{name} = #{value}" - { - :code - type: "text" - locals: {name} - } - -export macro function = (decl, value)-> - import "yue" as {options:{:tl_enabled}} - import "tl" - decl = $trim decl - name, type = decl\match "(.-)(%(.*)" - if not (name and type) - error "invalid function declaration for \"#{decl}\"" - tokens = tl.lex "function #{decl}" - _, node = tl.parse_program tokens,{},"macro-function" - args = table.concat [arg.tk for arg in *node[1].args],", " - value = "(#{args})#{value}" - 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}" - { - :code - type: "text" - locals: {name} - } - -export macro record = (name, decl)-> - import "yue" as {options:{:tl_enabled}} - code = if tl_enabled - "local record #{name} - #{decl} -end" - else - "local #{name} = {}" - { - :code - type: "text" - locals: {name} - } - -export macro method = (decl, value)-> - import "yue" as {options:{:tl_enabled}} - import "tl" - decl = $trim decl - tab, sym, func, type = decl\match "(.-)([%.:])(.-)(%(.*)" - if not (tab and sym and func and type) - error "invalid method declaration for \"#{decl}\"" - tokens = tl.lex "function #{decl}" - _, node = tl.parse_program tokens,{},"macro-function" - args = table.concat [arg.tk for arg in *node[1].args],", " - value = "(#{args})->#{value\match "[%-=]>(.*)"}" - 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}" - { - :code - type: "text" - } - diff --git a/spec/inputs/macro-todo.yue b/spec/inputs/macro-todo.yue deleted file mode 100644 index 752c9cb..0000000 --- a/spec/inputs/macro-todo.yue +++ /dev/null @@ -1,13 +0,0 @@ -export 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" - } - -export macro todo = (msg)-> - if msg - "$todoInner $FILE, $LINE, #{msg}" - else - "$todoInner $FILE, $LINE" - diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue index ac51d85..ae14f53 100644 --- a/spec/inputs/macro.yue +++ b/spec/inputs/macro.yue @@ -1,9 +1,9 @@ -import "macro-export" as { +import "macro_export" as { $, -- import all macros $config: $myconfig, -- rename macro $config to $myconfig } -import "macro-todo" as $ +import "macro_todo" as $ $asserts item == nil diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue new file mode 100644 index 0000000..eec5848 --- /dev/null +++ b/spec/inputs/macro_export.yue @@ -0,0 +1,50 @@ +import "macro_todo" as $ + +import "macro_todo" as {$, :$todo} + +export macro config = (debugging = true)-> + global debugMode = debugging == "true" + global debugMacro = true + "" + +export macro showMacro = (name, res)-> + if debugMacro then " +do + txt = #{res} + print '[macro ' .. #{name} .. ']' + print txt + txt +" + else + res + +export macro asserts = (cond)-> + if debugMode + $showMacro "assert", "assert #{cond}" + else + "" + +export macro assert = (cond)-> + if debugMode + $showMacro "assert", "assert #{cond}" + else + "#{cond}" + +export macro copy = (src, dst, ...)-> + assert( + src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" + "copy targets can not be _src_ or _dst_" + ) + " +do + local _src_, _dst_ + with _dst_ = #{dst} + with _src_ = #{src} +#{table.concat for field in *{...} do " + _dst_.#{field} = _src_.#{field} +"}" + +$ -> + global debugMode = true + global debugMacro = true + diff --git a/spec/inputs/macro_teal.yue b/spec/inputs/macro_teal.yue new file mode 100644 index 0000000..a443614 --- /dev/null +++ b/spec/inputs/macro_teal.yue @@ -0,0 +1,87 @@ +$ -> + import "yue" as {:options} + if options.tl_enabled + options.target_extension = "tl" + package.path ..= ";./spec/lib/?.lua" + +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}" + +export macro local = (decl, value = nil)-> + import "yue" as {options:{:tl_enabled}} + name, type = ($trim decl)\match "(.-):(.*)" + if not (name and type) + error "invalid local varaible declaration for \"#{decl}\"" + value = $to_lua(value)\gsub "^return ", "" + code = if tl_enabled + "local #{name}:#{$trim type} = #{value}" + else + "local #{name} = #{value}" + { + :code + type: "text" + locals: {name} + } + +export macro function = (decl, value)-> + import "yue" as {options:{:tl_enabled}} + import "tl" + decl = $trim decl + name, type = decl\match "(.-)(%(.*)" + if not (name and type) + error "invalid function declaration for \"#{decl}\"" + tokens = tl.lex "function #{decl}" + _, node = tl.parse_program tokens,{},"macro-function" + args = table.concat [arg.tk for arg in *node[1].args],", " + value = "(#{args})#{value}" + 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}" + { + :code + type: "text" + locals: {name} + } + +export macro record = (name, decl)-> + import "yue" as {options:{:tl_enabled}} + code = if tl_enabled + "local record #{name} + #{decl} +end" + else + "local #{name} = {}" + { + :code + type: "text" + locals: {name} + } + +export macro method = (decl, value)-> + import "yue" as {options:{:tl_enabled}} + import "tl" + decl = $trim decl + tab, sym, func, type = decl\match "(.-)([%.:])(.-)(%(.*)" + if not (tab and sym and func and type) + error "invalid method declaration for \"#{decl}\"" + tokens = tl.lex "function #{decl}" + _, node = tl.parse_program tokens,{},"macro-function" + args = table.concat [arg.tk for arg in *node[1].args],", " + value = "(#{args})->#{value\match "[%-=]>(.*)"}" + 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}" + { + :code + type: "text" + } + diff --git a/spec/inputs/macro_todo.yue b/spec/inputs/macro_todo.yue new file mode 100644 index 0000000..752c9cb --- /dev/null +++ b/spec/inputs/macro_todo.yue @@ -0,0 +1,13 @@ +export 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" + } + +export macro todo = (msg)-> + if msg + "$todoInner $FILE, $LINE, #{msg}" + else + "$todoInner $FILE, $LINE" + diff --git a/spec/inputs/teal-lang.yue b/spec/inputs/teal-lang.yue deleted file mode 100644 index b28c915..0000000 --- a/spec/inputs/teal-lang.yue +++ /dev/null @@ -1,36 +0,0 @@ -import "macro-teal" as $ - -$local "a:{string:number}", {value:123} -$local "b:number", a.value - -$function "add(a:number, b:number):number", -> a + b - -s = add(a.value, b) -print(s) - -$record Point, - x: number - y: number - -$method "Point.new(x:number, y:number):Point", -> - $local "point:Point", setmetatable {}, __index: Point - point.x = x or 0 - point.y = y or 0 - point - -$method "Point:move(dx:number, dy:number)", -> - @x += dx - @y += dy - -$local "p:Point", Point.new 100, 100 - -p\move 50, 50 - -$function "filter(tab:{string}, handler:function(item:string):boolean):{string}", -> - [item for item in *tab when handler item] - -$function "cond(item:string):boolean", -> item ~= "a" - -res = filter {"a", "b", "c", "a"}, cond -for s in *res - print s diff --git a/spec/inputs/teal_lang.yue b/spec/inputs/teal_lang.yue new file mode 100644 index 0000000..e01682f --- /dev/null +++ b/spec/inputs/teal_lang.yue @@ -0,0 +1,36 @@ +import "macro_teal" as $ + +$local "a:{string:number}", {value:123} +$local "b:number", a.value + +$function "add(a:number, b:number):number", -> a + b + +s = add(a.value, b) +print(s) + +$record Point, + x: number + y: number + +$method "Point.new(x:number, y:number):Point", -> + $local "point:Point", setmetatable {}, __index: Point + point.x = x or 0 + point.y = y or 0 + point + +$method "Point:move(dx:number, dy:number)", -> + @x += dx + @y += dy + +$local "p:Point", Point.new 100, 100 + +p\move 50, 50 + +$function "filter(tab:{string}, handler:function(item:string):boolean):{string}", -> + [item for item in *tab when handler item] + +$function "cond(item:string):boolean", -> item ~= "a" + +res = filter {"a", "b", "c", "a"}, cond +for s in *res + print s diff --git a/spec/inputs/try-catch.yue b/spec/inputs/try-catch.yue deleted file mode 100644 index e38cbef..0000000 --- a/spec/inputs/try-catch.yue +++ /dev/null @@ -1,54 +0,0 @@ -try - func 1, 2, 3 -catch err - print err - -try func 1, 2, 3 -catch err - print err - -try - print "trying" - func 1, 2, 3 - -do - success, result = try - func 1, 2, 3 - catch err - print err - - success, result = try func 1, 2, 3 - -try tb.func -try tb.func! -try tb.func() -try (tb.func!) -try (tb\func(1, 2, 3)) - -try tb.func 1 -try tb.func(1) - -if (try func 1 -catch err - print err) - print "OK" - -if try (func 1) -catch err - print err - print "OK" - -do - if success, result = try func "abc", 123 - print result - - success, result = try func "abc", 123 - catch err - print err - - print result if success, result = try func "abc", 123 - catch err - print err - -nil - diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue new file mode 100644 index 0000000..e38cbef --- /dev/null +++ b/spec/inputs/try_catch.yue @@ -0,0 +1,54 @@ +try + func 1, 2, 3 +catch err + print err + +try func 1, 2, 3 +catch err + print err + +try + print "trying" + func 1, 2, 3 + +do + success, result = try + func 1, 2, 3 + catch err + print err + + success, result = try func 1, 2, 3 + +try tb.func +try tb.func! +try tb.func() +try (tb.func!) +try (tb\func(1, 2, 3)) + +try tb.func 1 +try tb.func(1) + +if (try func 1 +catch err + print err) + print "OK" + +if try (func 1) +catch err + print err + print "OK" + +do + if success, result = try func "abc", 123 + print result + + success, result = try func "abc", 123 + catch err + print err + + print result if success, result = try func "abc", 123 + catch err + print err + +nil + -- cgit v1.2.3-55-g6feb