aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/macro-teal.mp
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs/macro-teal.mp')
-rw-r--r--spec/inputs/macro-teal.mp46
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/inputs/macro-teal.mp b/spec/inputs/macro-teal.mp
new file mode 100644
index 0000000..20444e1
--- /dev/null
+++ b/spec/inputs/macro-teal.mp
@@ -0,0 +1,46 @@
1$ ->
2 import "moonp" as {:options}
3 if options.tl_enabled
4 options.target_extension = "tl"
5
6macro expr to_lua = (codes)->
7 "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)"
8
9macro expr trim = (name)->
10 "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}"
11
12export macro text var = (name, type, value = nil)->
13 import "moonp" as {options:{:tl_enabled}}
14 value = $to_lua(value)\gsub "^return ", ""
15 if tl_enabled
16 "local #{name}:#{$trim type} = #{value}", {name}
17 else
18 "local #{name} = #{value}", {name}
19
20export macro text def = (name, type, value)->
21 import "moonp" as {options:{:tl_enabled}}
22 if tl_enabled
23 value = $to_lua(value)\match "function%(.*%)(.*)end"
24 "local function #{name}#{$trim type}\n#{value}\nend", {name}
25 else
26 value = $to_lua(value)\gsub "^return ", ""
27 "local #{name} = #{value}", {name}
28
29export macro text record = (name, decl)->
30 import "moonp" as {options:{:tl_enabled}}
31 if tl_enabled
32 "local record #{name}
33 #{decl}
34end", {name}
35 else
36 "local #{name} = {}", {name}
37
38export macro text field = (tab, sym, func, type, value)->
39 import "moonp" as {options:{:tl_enabled}}
40 if tl_enabled
41 value = $to_lua(value)\match "^return function%(.-%)\n(.*)end"
42 "function #{tab}#{$trim sym}#{func}#{$trim type}\n#{value}\nend"
43 else
44 value = $to_lua(value)\gsub "^return ", ""
45 "#{tab}.#{func} = #{value}"
46