diff options
author | Li Jin <dragon-fly@qq.com> | 2020-10-23 17:32:48 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-10-23 17:32:48 +0800 |
commit | 1c6a9651beffd9cbbb3641179f3a738d5555d3c9 (patch) | |
tree | 31f442c3685bf19e851f62c4b2957d445e313eac /spec/inputs | |
parent | a51a728d847e790329e41c75928a81630200b63f (diff) | |
download | yuescript-1c6a9651beffd9cbbb3641179f3a738d5555d3c9.tar.gz yuescript-1c6a9651beffd9cbbb3641179f3a738d5555d3c9.tar.bz2 yuescript-1c6a9651beffd9cbbb3641179f3a738d5555d3c9.zip |
make teal-macro look better.
Diffstat (limited to 'spec/inputs')
-rw-r--r-- | spec/inputs/macro-teal.mp | 34 | ||||
-rw-r--r-- | spec/inputs/teal-lang.mp | 22 |
2 files changed, 43 insertions, 13 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}" |
diff --git a/spec/inputs/teal-lang.mp b/spec/inputs/teal-lang.mp index 3c9c79b..29769d5 100644 --- a/spec/inputs/teal-lang.mp +++ b/spec/inputs/teal-lang.mp | |||
@@ -3,10 +3,10 @@ $ -> | |||
3 | 3 | ||
4 | import "macro-teal" as {$} | 4 | import "macro-teal" as {$} |
5 | 5 | ||
6 | $var a, "{string:number}", {value:123} | 6 | $local "a:{string:number}", {value:123} |
7 | $var b, "number", a.value | 7 | $local "b:number", a.value |
8 | 8 | ||
9 | $def add, "(a:number,b:number):number", (a, b)-> a + b | 9 | $function "add(a:number, b:number):number", -> a + b |
10 | 10 | ||
11 | s = add(a.value, b) | 11 | s = add(a.value, b) |
12 | print(s) | 12 | print(s) |
@@ -16,17 +16,25 @@ $record Point, [[ | |||
16 | y: number | 16 | y: number |
17 | ]] | 17 | ]] |
18 | 18 | ||
19 | $field Point, '.', new, "(x: number, y: number):Point", (x, y)-> | 19 | $method "Point.new(x:number, y:number):Point", -> |
20 | $var point, "Point", setmetatable {}, __index: Point | 20 | $local "point:Point", setmetatable {}, __index: Point |
21 | point.x = x or 0 | 21 | point.x = x or 0 |
22 | point.y = y or 0 | 22 | point.y = y or 0 |
23 | point | 23 | point |
24 | 24 | ||
25 | $field Point, ":", move, "(dx: number, dy: number)", (dx, dy)=> | 25 | $method "Point:move(dx:number, dy:number)", -> |
26 | @x += dx | 26 | @x += dx |
27 | @y += dy | 27 | @y += dy |
28 | 28 | ||
29 | $var p, "Point", Point.new 100, 100 | 29 | $local "p:Point", Point.new 100, 100 |
30 | 30 | ||
31 | p\move 50, 50 | 31 | p\move 50, 50 |
32 | 32 | ||
33 | $function "filter(tab:{string}, handler:function(item:string):boolean):{string}", -> | ||
34 | [item for item in *tab when handler item] | ||
35 | |||
36 | $function "cond(item:string):boolean", -> item ~= "a" | ||
37 | |||
38 | res = filter {"a", "b", "c", "a"}, cond | ||
39 | for s in *res | ||
40 | print s | ||