From b6725202f4a8cac5f829dac9a72a81f3ff73e787 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 21 Oct 2020 23:44:50 +0800 Subject: 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". --- spec/inputs/macro-export.mp | 28 +++++++++++++++++++++++++++ spec/inputs/macro-teal.mp | 46 +++++++++++++++++++++++++++++++++++++++++++++ spec/inputs/macro.mp | 23 ++++++++++++++--------- spec/inputs/macro_export.mp | 29 ---------------------------- spec/inputs/teal-lang.mp | 32 +++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 38 deletions(-) create mode 100644 spec/inputs/macro-export.mp create mode 100644 spec/inputs/macro-teal.mp delete mode 100644 spec/inputs/macro_export.mp create mode 100644 spec/inputs/teal-lang.mp (limited to 'spec/inputs') diff --git a/spec/inputs/macro-export.mp b/spec/inputs/macro-export.mp new file mode 100644 index 0000000..b6079ca --- /dev/null +++ b/spec/inputs/macro-export.mp @@ -0,0 +1,28 @@ +export macro block config = (debugging = true)-> + global debugMode = debugging == "true" + global debugMacro = true + "" + +export macro expr showMacro = (name,res)-> + if debugMacro + "do + txt = #{res} + print '[macro '..#{name}..']' + print txt + txt" + else + res + +export macro block asserts = (cond)-> + if debugMode + $showMacro "assert", "assert #{cond}" + else + "" + +export macro expr assert = (cond)-> + if debugMode + $showMacro "assert", "assert #{cond}" + else + "#{cond}" + +$config! 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 @@ +$ -> + import "moonp" as {:options} + if options.tl_enabled + options.target_extension = "tl" + +macro expr to_lua = (codes)-> + "require('moonp').to_lua(#{codes}, reserve_line_number:false, same_module:true)" + +macro expr trim = (name)-> + "if result = #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" + +export macro text var = (name, type, value = nil)-> + import "moonp" as {options:{:tl_enabled}} + value = $to_lua(value)\gsub "^return ", "" + if tl_enabled + "local #{name}:#{$trim type} = #{value}", {name} + else + "local #{name} = #{value}", {name} + +export macro text def = (name, type, value)-> + import "moonp" as {options:{:tl_enabled}} + if tl_enabled + value = $to_lua(value)\match "function%(.*%)(.*)end" + "local function #{name}#{$trim type}\n#{value}\nend", {name} + else + value = $to_lua(value)\gsub "^return ", "" + "local #{name} = #{value}", {name} + +export macro text record = (name, decl)-> + import "moonp" as {options:{:tl_enabled}} + if tl_enabled + "local record #{name} + #{decl} +end", {name} + else + "local #{name} = {}", {name} + +export macro text field = (tab, sym, func, type, value)-> + import "moonp" as {options:{:tl_enabled}} + if tl_enabled + value = $to_lua(value)\match "^return function%(.-%)\n(.*)end" + "function #{tab}#{$trim sym}#{func}#{$trim type}\n#{value}\nend" + else + value = $to_lua(value)\gsub "^return ", "" + "#{tab}.#{func} = #{value}" + 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 @@ -macro block init = -> - with require "moonp" - package.moonpath = "?.mp;./spec/inputs/?.mp" - "" +$ -> + package.moonpath = "?.mp;./spec/inputs/?.mp" -$init! - -import "macro_export" as { +import "macro-export" as { $, -- import all macros $config:$myconfig, -- rename macro $config to $myconfig } @@ -125,6 +121,12 @@ end x = x + f(3) ]] +$lua[[ +function tb:func() + print(123) +end +]] + print x macro lua def = (fname, ...)-> @@ -144,8 +146,11 @@ $def sel, a, b, c, [[ end ]] -$def dummy,[[ -]] +$def dummy,[[]] + +macro lua insertComment = (text)-> "-- #{text\match '[\'"](.*)[\'"]'}" + +$insertComment "a comment here" import 'underscore' as _ diff --git a/spec/inputs/macro_export.mp b/spec/inputs/macro_export.mp deleted file mode 100644 index 7208b2a..0000000 --- a/spec/inputs/macro_export.mp +++ /dev/null @@ -1,29 +0,0 @@ -export macro block config = (debugging = true)-> - global debugMode = debugging == "true" - global debugMacro = true - "" - -export macro expr showMacro = (name,res)-> - if debugMacro - "do - txt = #{res} - print '[macro '..#{name}..']' - print txt - txt" - else - res - -export macro block asserts = (cond)-> - if debugMode - $showMacro "assert", "assert #{cond}" - else - "" - -export macro expr assert = (cond)-> - if debugMode - $showMacro "assert", "assert #{cond}" - else - "#{cond}" - -$config! - 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 @@ +$ -> + package.moonpath = "?.mp;./spec/inputs/?.mp" + +import "macro-teal" as {$} + +$var a, "{string:number}", {value:123} +$var b, "number", a.value + +$def add, "(a:number,b:number):number", (a, b)-> a + b + +s = add(a.value, b) +print(s) + +$record Point, [[ + x: number + y: number +]] + +$field Point, '.', new, "(x: number, y: number):Point", (x, y)-> + $var point, "Point", setmetatable {}, __index: Point + point.x = x or 0 + point.y = y or 0 + point + +$field Point, ":", move, "(dx: number, dy: number)", (dx, dy)=> + @x += dx + @y += dy + +$var p, "Point", Point.new 100, 100 + +p\move 50, 50 + -- cgit v1.2.3-55-g6feb