diff options
author | Li Jin <dragon-fly@qq.com> | 2023-07-28 11:53:04 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-07-28 11:53:04 +0800 |
commit | 5497775534d20ba06ab9c13bc4db1c5bee877513 (patch) | |
tree | be68d03cf0928efca4a579125e4ba15ef3ab9325 /spec/inputs/macro_teal.yue | |
parent | f415df9617d251abd802257d9750618ccc71ca93 (diff) | |
download | yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.tar.gz yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.tar.bz2 yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.zip |
fix xpcall usages in different Lua version.
Diffstat (limited to 'spec/inputs/macro_teal.yue')
-rw-r--r-- | spec/inputs/macro_teal.yue | 87 |
1 files changed, 87 insertions, 0 deletions
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 @@ | |||
1 | $ -> | ||
2 | import "yue" as {:options} | ||
3 | if options.tl_enabled | ||
4 | options.target_extension = "tl" | ||
5 | package.path ..= ";./spec/lib/?.lua" | ||
6 | |||
7 | macro to_lua = (code)-> | ||
8 | "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" | ||
9 | |||
10 | macro trim = (name)-> | ||
11 | "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" | ||
12 | |||
13 | export macro local = (decl, value = nil)-> | ||
14 | import "yue" as {options:{:tl_enabled}} | ||
15 | name, type = ($trim decl)\match "(.-):(.*)" | ||
16 | if not (name and type) | ||
17 | error "invalid local varaible declaration for \"#{decl}\"" | ||
18 | value = $to_lua(value)\gsub "^return ", "" | ||
19 | code = if tl_enabled | ||
20 | "local #{name}:#{$trim type} = #{value}" | ||
21 | else | ||
22 | "local #{name} = #{value}" | ||
23 | { | ||
24 | :code | ||
25 | type: "text" | ||
26 | locals: {name} | ||
27 | } | ||
28 | |||
29 | export macro function = (decl, value)-> | ||
30 | import "yue" as {options:{:tl_enabled}} | ||
31 | import "tl" | ||
32 | decl = $trim decl | ||
33 | name, type = decl\match "(.-)(%(.*)" | ||
34 | if not (name and type) | ||
35 | error "invalid function declaration for \"#{decl}\"" | ||
36 | tokens = tl.lex "function #{decl}" | ||
37 | _, node = tl.parse_program tokens,{},"macro-function" | ||
38 | args = table.concat [arg.tk for arg in *node[1].args],", " | ||
39 | value = "(#{args})#{value}" | ||
40 | code = if tl_enabled | ||
41 | value = $to_lua(value)\match "function%([^\n]*%)(.*)end" | ||
42 | "local function #{name}#{type}\n#{value}\nend" | ||
43 | else | ||
44 | value = $to_lua(value)\gsub "^return ", "" | ||
45 | "local #{name} = #{value}" | ||
46 | { | ||
47 | :code | ||
48 | type: "text" | ||
49 | locals: {name} | ||
50 | } | ||
51 | |||
52 | export macro record = (name, decl)-> | ||
53 | import "yue" as {options:{:tl_enabled}} | ||
54 | code = if tl_enabled | ||
55 | "local record #{name} | ||
56 | #{decl} | ||
57 | end" | ||
58 | else | ||
59 | "local #{name} = {}" | ||
60 | { | ||
61 | :code | ||
62 | type: "text" | ||
63 | locals: {name} | ||
64 | } | ||
65 | |||
66 | export macro method = (decl, value)-> | ||
67 | import "yue" as {options:{:tl_enabled}} | ||
68 | import "tl" | ||
69 | decl = $trim decl | ||
70 | tab, sym, func, type = decl\match "(.-)([%.:])(.-)(%(.*)" | ||
71 | if not (tab and sym and func and type) | ||
72 | error "invalid method declaration for \"#{decl}\"" | ||
73 | tokens = tl.lex "function #{decl}" | ||
74 | _, node = tl.parse_program tokens,{},"macro-function" | ||
75 | args = table.concat [arg.tk for arg in *node[1].args],", " | ||
76 | value = "(#{args})->#{value\match "[%-=]>(.*)"}" | ||
77 | code = if tl_enabled | ||
78 | value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" | ||
79 | "function #{tab}#{sym}#{func}#{type}\n#{value}\nend" | ||
80 | else | ||
81 | value = $to_lua(value)\gsub "^return ", "" | ||
82 | "#{tab}.#{func} = #{value}" | ||
83 | { | ||
84 | :code | ||
85 | type: "text" | ||
86 | } | ||
87 | |||