diff options
Diffstat (limited to 'spec/inputs/macro-teal.mp')
| -rw-r--r-- | spec/inputs/macro-teal.mp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/spec/inputs/macro-teal.mp b/spec/inputs/macro-teal.mp index 20444e1..9ce1bcd 100644 --- a/spec/inputs/macro-teal.mp +++ b/spec/inputs/macro-teal.mp | |||
| @@ -2,6 +2,7 @@ $ -> | |||
| 2 | import "moonp" as {:options} | 2 | import "moonp" as {:options} |
| 3 | if options.tl_enabled | 3 | if options.tl_enabled |
| 4 | options.target_extension = "tl" | 4 | options.target_extension = "tl" |
| 5 | package.path ..= "?.lua;./spec/lib/?.lua" | ||
| 5 | 6 | ||
| 6 | macro expr to_lua = (codes)-> | 7 | macro expr to_lua = (codes)-> |
| 7 | "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)" | 8 | "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)" |
| @@ -9,19 +10,31 @@ macro expr to_lua = (codes)-> | |||
| 9 | macro expr trim = (name)-> | 10 | macro expr trim = (name)-> |
| 10 | "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" | 11 | "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" |
| 11 | 12 | ||
| 12 | export macro text var = (name, type, value = nil)-> | 13 | export macro text local = (decl, value = nil)-> |
| 13 | import "moonp" as {options:{:tl_enabled}} | 14 | import "moonp" as {options:{:tl_enabled}} |
| 15 | name, type = ($trim decl)\match "(.-):(.*)" | ||
| 16 | if not (name and type) | ||
| 17 | error "invalid local varaible declaration for \"#{decl}\"" | ||
| 14 | value = $to_lua(value)\gsub "^return ", "" | 18 | value = $to_lua(value)\gsub "^return ", "" |
| 15 | if tl_enabled | 19 | if tl_enabled |
| 16 | "local #{name}:#{$trim type} = #{value}", {name} | 20 | "local #{name}:#{$trim type} = #{value}", {name} |
| 17 | else | 21 | else |
| 18 | "local #{name} = #{value}", {name} | 22 | "local #{name} = #{value}", {name} |
| 19 | 23 | ||
| 20 | export macro text def = (name, type, value)-> | 24 | export macro text function = (decl, value)-> |
| 21 | import "moonp" as {options:{:tl_enabled}} | 25 | import "moonp" as {options:{:tl_enabled}} |
| 26 | import "tl" | ||
| 27 | decl = $trim decl | ||
| 28 | name, type = decl\match "(.-)(%(.*)" | ||
| 29 | if not (name and type) | ||
| 30 | error "invalid function declaration for \"#{decl}\"" | ||
| 31 | tokens = tl.lex "function #{decl}" | ||
| 32 | _, node = tl.parse_program tokens,{},"macro-function" | ||
| 33 | args = table.concat [arg.tk for arg in *node[1].args],", " | ||
| 34 | value = "(#{args})#{value}" | ||
| 22 | if tl_enabled | 35 | if tl_enabled |
| 23 | value = $to_lua(value)\match "function%(.*%)(.*)end" | 36 | value = $to_lua(value)\match "function%([^\n]*%)(.*)end" |
| 24 | "local function #{name}#{$trim type}\n#{value}\nend", {name} | 37 | "local function #{name}#{type}\n#{value}\nend", {name} |
| 25 | else | 38 | else |
| 26 | value = $to_lua(value)\gsub "^return ", "" | 39 | value = $to_lua(value)\gsub "^return ", "" |
| 27 | "local #{name} = #{value}", {name} | 40 | "local #{name} = #{value}", {name} |
| @@ -35,11 +48,20 @@ end", {name} | |||
| 35 | else | 48 | else |
| 36 | "local #{name} = {}", {name} | 49 | "local #{name} = {}", {name} |
| 37 | 50 | ||
| 38 | export macro text field = (tab, sym, func, type, value)-> | 51 | export macro text method = (decl, value)-> |
| 39 | import "moonp" as {options:{:tl_enabled}} | 52 | import "moonp" as {options:{:tl_enabled}} |
| 53 | import "tl" | ||
| 54 | decl = $trim decl | ||
| 55 | tab, sym, func, type = decl\match "(.-)([%.:])(.-)(%(.*)" | ||
| 56 | if not (tab and sym and func and type) | ||
| 57 | error "invalid method declaration for \"#{decl}\"" | ||
| 58 | tokens = tl.lex "function #{decl}" | ||
| 59 | _, node = tl.parse_program tokens,{},"macro-function" | ||
| 60 | args = table.concat [arg.tk for arg in *node[1].args],", " | ||
| 61 | value = "(#{args})->#{value\match "[%-=]>(.*)"}" | ||
| 40 | if tl_enabled | 62 | if tl_enabled |
| 41 | value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" | 63 | value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" |
| 42 | "function #{tab}#{$trim sym}#{func}#{$trim type}\n#{value}\nend" | 64 | "function #{tab}#{sym}#{func}#{type}\n#{value}\nend" |
| 43 | else | 65 | else |
| 44 | value = $to_lua(value)\gsub "^return ", "" | 66 | value = $to_lua(value)\gsub "^return ", "" |
| 45 | "#{tab}.#{func} = #{value}" | 67 | "#{tab}.#{func} = #{value}" |
