diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-03-11 00:21:33 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-03-11 00:21:33 +0800 |
| commit | 1ee056eedf773ac6166247755439092e0e0a9bca (patch) | |
| tree | 1b8863338d37fe6ded7fbaecd92d76cdb3801ab3 /spec | |
| parent | 015af4b70cd751e1c1580fd542997a796e1ca225 (diff) | |
| download | yuescript-1ee056eedf773ac6166247755439092e0e0a9bca.tar.gz yuescript-1ee056eedf773ac6166247755439092e0e0a9bca.tar.bz2 yuescript-1ee056eedf773ac6166247755439092e0e0a9bca.zip | |
add macro functions.
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/inputs/macro.moon | 69 | ||||
| -rw-r--r-- | spec/inputs/macro_export.moon | 29 |
2 files changed, 98 insertions, 0 deletions
diff --git a/spec/inputs/macro.moon b/spec/inputs/macro.moon new file mode 100644 index 0000000..eb1e224 --- /dev/null +++ b/spec/inputs/macro.moon | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | macro block init = -> | ||
| 2 | with require "moonp" | ||
| 3 | package.moonpath = "?.moon;./spec/inputs/?.moon" | ||
| 4 | "" | ||
| 5 | |||
| 6 | $init! | ||
| 7 | |||
| 8 | import "macro_export" as {$myconfig:$config, :$showMacro, :$asserts, :$assert} | ||
| 9 | |||
| 10 | $asserts item == nil | ||
| 11 | |||
| 12 | $myconfig false | ||
| 13 | |||
| 14 | v = $assert item == nil | ||
| 15 | |||
| 16 | macro expr and = (...)-> | ||
| 17 | values = [value for value in *{...}] | ||
| 18 | $showMacro "and", "#{ table.concat values, " and " }" | ||
| 19 | |||
| 20 | if $and f1 | ||
| 21 | print "OK" | ||
| 22 | |||
| 23 | if $and f1,f2,f3 | ||
| 24 | print "OK" | ||
| 25 | |||
| 26 | macro expr map = (items,action)-> | ||
| 27 | $showMacro "map", "[#{action} for _ in *#{items}]" | ||
| 28 | |||
| 29 | macro expr filter = (items,action)-> | ||
| 30 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | ||
| 31 | |||
| 32 | macro expr reduce = (items,def,action)-> | ||
| 33 | $showMacro "reduce", "if ##{items} == 0 | ||
| 34 | #{def} | ||
| 35 | else | ||
| 36 | _1 = #{def} | ||
| 37 | for _2 in *#{items} | ||
| 38 | _1 = #{action} | ||
| 39 | _1" | ||
| 40 | |||
| 41 | macro block foreach = (items,action)-> | ||
| 42 | $showMacro "foreach", "for _ in *#{items} | ||
| 43 | #{action}" | ||
| 44 | |||
| 45 | macro expr pipe = (...)-> | ||
| 46 | switch select "#",... | ||
| 47 | when 0 then return "" | ||
| 48 | when 1 then return ... | ||
| 49 | ops = {...} | ||
| 50 | last = ops[1] | ||
| 51 | stmts = for i = 2,#ops | ||
| 52 | stmt = "\tlocal _#{i} = #{last} |> #{ops[i]}" | ||
| 53 | last = "_#{i}" | ||
| 54 | stmt | ||
| 55 | res = "do | ||
| 56 | #{table.concat stmts,"\n"} | ||
| 57 | #{last}" | ||
| 58 | $showMacro "pipe", res | ||
| 59 | |||
| 60 | {1,2,3} |> $map(_ * 2) |> $filter(_ > 4) |> $foreach print _ | ||
| 61 | |||
| 62 | $foreach $filter($map({1,2,3}, _ * 2), _ > 4), print _ | ||
| 63 | |||
| 64 | print $pipe( | ||
| 65 | {1, 2, 3} | ||
| 66 | $map(_ * 2) | ||
| 67 | $filter(_ > 4) | ||
| 68 | $reduce(0, _1 + _2) | ||
| 69 | ) | ||
diff --git a/spec/inputs/macro_export.moon b/spec/inputs/macro_export.moon new file mode 100644 index 0000000..369b83b --- /dev/null +++ b/spec/inputs/macro_export.moon | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | export macro block config = (debugging = true)-> | ||
| 2 | global debugMode = debugging == "true" | ||
| 3 | global debugMacro = true | ||
| 4 | "" | ||
| 5 | |||
| 6 | export macro expr showMacro = (name,res)-> | ||
| 7 | if debugMacro | ||
| 8 | "do | ||
| 9 | txt = #{res} | ||
| 10 | print '['..#{name}..']' | ||
| 11 | print txt | ||
| 12 | txt" | ||
| 13 | else | ||
| 14 | "#{res}" | ||
| 15 | |||
| 16 | export macro block asserts = (cond)-> | ||
| 17 | if debugMode | ||
| 18 | $showMacro "assert", "assert #{cond}" | ||
| 19 | else | ||
| 20 | "" | ||
| 21 | |||
| 22 | export macro expr assert = (cond)-> | ||
| 23 | if debugMode | ||
| 24 | $showMacro "assert", "assert #{cond}" | ||
| 25 | else | ||
| 26 | "#{cond}" | ||
| 27 | |||
| 28 | $config! | ||
| 29 | |||
