diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-10-21 23:44:50 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-10-21 23:44:50 +0800 |
| commit | b6725202f4a8cac5f829dac9a72a81f3ff73e787 (patch) | |
| tree | c173accb869b60cba14babc7685284864bc80426 /spec | |
| parent | 0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c (diff) | |
| download | yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.tar.gz yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.tar.bz2 yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.zip | |
extend macro feature to support compiling Moonscript to other Lua dialect like teal.
add examples for how to write MoonPlus codes that compile to teal.
fix C++ macro to build without MoonPlus macro feature or built-in Lua.
add support for passing arguments from command line to compiler that can be accessed or altered by "require('moonp').options".
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/inputs/macro-export.mp (renamed from spec/inputs/macro_export.mp) | 1 | ||||
| -rw-r--r-- | spec/inputs/macro-teal.mp | 46 | ||||
| -rw-r--r-- | spec/inputs/macro.mp | 23 | ||||
| -rw-r--r-- | spec/inputs/teal-lang.mp | 32 |
4 files changed, 92 insertions, 10 deletions
diff --git a/spec/inputs/macro_export.mp b/spec/inputs/macro-export.mp index 7208b2a..b6079ca 100644 --- a/spec/inputs/macro_export.mp +++ b/spec/inputs/macro-export.mp | |||
| @@ -26,4 +26,3 @@ export macro expr assert = (cond)-> | |||
| 26 | "#{cond}" | 26 | "#{cond}" |
| 27 | 27 | ||
| 28 | $config! | 28 | $config! |
| 29 | |||
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 | |||
| 6 | macro expr to_lua = (codes)-> | ||
| 7 | "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)" | ||
| 8 | |||
| 9 | macro expr trim = (name)-> | ||
| 10 | "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" | ||
| 11 | |||
| 12 | export 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 | |||
| 20 | export 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 | |||
| 29 | export macro text record = (name, decl)-> | ||
| 30 | import "moonp" as {options:{:tl_enabled}} | ||
| 31 | if tl_enabled | ||
| 32 | "local record #{name} | ||
| 33 | #{decl} | ||
| 34 | end", {name} | ||
| 35 | else | ||
| 36 | "local #{name} = {}", {name} | ||
| 37 | |||
| 38 | export 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 | |||
diff --git a/spec/inputs/macro.mp b/spec/inputs/macro.mp index 103df95..fafa522 100644 --- a/spec/inputs/macro.mp +++ b/spec/inputs/macro.mp | |||
| @@ -1,11 +1,7 @@ | |||
| 1 | macro block init = -> | 1 | $ -> |
| 2 | with require "moonp" | 2 | package.moonpath = "?.mp;./spec/inputs/?.mp" |
| 3 | package.moonpath = "?.mp;./spec/inputs/?.mp" | ||
| 4 | "" | ||
| 5 | 3 | ||
| 6 | $init! | 4 | import "macro-export" as { |
| 7 | |||
| 8 | import "macro_export" as { | ||
| 9 | $, -- import all macros | 5 | $, -- import all macros |
| 10 | $config:$myconfig, -- rename macro $config to $myconfig | 6 | $config:$myconfig, -- rename macro $config to $myconfig |
| 11 | } | 7 | } |
| @@ -125,6 +121,12 @@ end | |||
| 125 | x = x + f(3) | 121 | x = x + f(3) |
| 126 | ]] | 122 | ]] |
| 127 | 123 | ||
| 124 | $lua[[ | ||
| 125 | function tb:func() | ||
| 126 | print(123) | ||
| 127 | end | ||
| 128 | ]] | ||
| 129 | |||
| 128 | print x | 130 | print x |
| 129 | 131 | ||
| 130 | macro lua def = (fname, ...)-> | 132 | macro lua def = (fname, ...)-> |
| @@ -144,8 +146,11 @@ $def sel, a, b, c, [[ | |||
| 144 | end | 146 | end |
| 145 | ]] | 147 | ]] |
| 146 | 148 | ||
| 147 | $def dummy,[[ | 149 | $def dummy,[[]] |
| 148 | ]] | 150 | |
| 151 | macro lua insertComment = (text)-> "-- #{text\match '[\'"](.*)[\'"]'}" | ||
| 152 | |||
| 153 | $insertComment "a comment here" | ||
| 149 | 154 | ||
| 150 | import 'underscore' as _ | 155 | import 'underscore' as _ |
| 151 | 156 | ||
diff --git a/spec/inputs/teal-lang.mp b/spec/inputs/teal-lang.mp new file mode 100644 index 0000000..3c9c79b --- /dev/null +++ b/spec/inputs/teal-lang.mp | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | $ -> | ||
| 2 | package.moonpath = "?.mp;./spec/inputs/?.mp" | ||
| 3 | |||
| 4 | import "macro-teal" as {$} | ||
| 5 | |||
| 6 | $var a, "{string:number}", {value:123} | ||
| 7 | $var b, "number", a.value | ||
| 8 | |||
| 9 | $def add, "(a:number,b:number):number", (a, b)-> a + b | ||
| 10 | |||
| 11 | s = add(a.value, b) | ||
| 12 | print(s) | ||
| 13 | |||
| 14 | $record Point, [[ | ||
| 15 | x: number | ||
| 16 | y: number | ||
| 17 | ]] | ||
| 18 | |||
| 19 | $field Point, '.', new, "(x: number, y: number):Point", (x, y)-> | ||
| 20 | $var point, "Point", setmetatable {}, __index: Point | ||
| 21 | point.x = x or 0 | ||
| 22 | point.y = y or 0 | ||
| 23 | point | ||
| 24 | |||
| 25 | $field Point, ":", move, "(dx: number, dy: number)", (dx, dy)=> | ||
| 26 | @x += dx | ||
| 27 | @y += dy | ||
| 28 | |||
| 29 | $var p, "Point", Point.new 100, 100 | ||
| 30 | |||
| 31 | p\move 50, 50 | ||
| 32 | |||
