diff options
author | Li Jin <dragon-fly@qq.com> | 2022-09-08 09:26:49 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-09-08 09:26:49 +0800 |
commit | d4af1fa275b1d27229fc995f4a45137380040933 (patch) | |
tree | 955c76511d7021e6d1a0f06b46de3852eeac4176 /spec/inputs/metatable.yue | |
parent | df85ad2e7f975026ca1e6bd84b26fff81c8d99c8 (diff) | |
download | yuescript-d4af1fa275b1d27229fc995f4a45137380040933.tar.gz yuescript-d4af1fa275b1d27229fc995f4a45137380040933.tar.bz2 yuescript-d4af1fa275b1d27229fc995f4a45137380040933.zip |
redesigned metatable syntax. add support for destructuring a field with string and expression
Diffstat (limited to 'spec/inputs/metatable.yue')
-rw-r--r-- | spec/inputs/metatable.yue | 92 |
1 files changed, 58 insertions, 34 deletions
diff --git a/spec/inputs/metatable.yue b/spec/inputs/metatable.yue index 86991c2..61cc266 100644 --- a/spec/inputs/metatable.yue +++ b/spec/inputs/metatable.yue | |||
@@ -1,62 +1,86 @@ | |||
1 | a = close: true, close#: => print "out of scope" | 1 | a = close: true, <close>: => print "out of scope" |
2 | b = add#: (left, right)-> right - left | 2 | b = <add>: (left, right)-> right - left |
3 | c = key1: true, :add#, key2: true | 3 | c = key1: true, :<add>, key2: true |
4 | w = [name]#:123, ["new"]#:(val)=> {val} | 4 | w = <[name]>:123, <"new">:(val)=> {val} |
5 | w.#["new"] w.#[name] | 5 | w.<>["new"] w.<>[name] |
6 | 6 | ||
7 | do close _ = close#: -> print "out of scope" | 7 | do close _ = <close>: -> print "out of scope" |
8 | 8 | ||
9 | d, e = a.close, a.close# | 9 | d, e = a.close, a.<close> |
10 | 10 | ||
11 | f = a\close# 1 | 11 | f = a\<close> 1 |
12 | a.add# = (x, y)-> x + y | 12 | a.<add> = (x, y)-> x + y |
13 | 13 | ||
14 | do | 14 | do |
15 | {:new, :close#, close#: closeA} = a | 15 | {:new, :<close>, <close>: closeA} = a |
16 | print new, close, closeA | 16 | print new, close, closeA |
17 | 17 | ||
18 | do | 18 | do |
19 | local * | 19 | local * |
20 | x, \ | 20 | x, \ |
21 | {:new, :var, :close#, close#: closeA}, \ | 21 | {:new, :var, :<close>, <close>: closeA}, \ |
22 | :num, :add#, :sub# \ | 22 | :num, :<add>, :<sub> \ |
23 | = 123, a.b.c, func! | 23 | = 123, a.b.c, func! |
24 | 24 | ||
25 | x.abc, a.b.# = 123, {} | 25 | x.abc, a.b.<> = 123, {} |
26 | func!.# = mt --, extra | 26 | func!.<> = mt --, extra |
27 | a, b.c.#, d, e = 1, mt, "abc", nil | 27 | a, b.c.<>, d, e = 1, mt, "abc", nil |
28 | 28 | ||
29 | is_same = a.#.__index == a.index# | 29 | is_same = a.<>.__index == a.<index> |
30 | 30 | ||
31 | -- | 31 | -- |
32 | a.# = __index: tb | 32 | a.<> = __index: tb |
33 | a.#.__index = tb | 33 | a.<>.__index = tb |
34 | a.index# = tb | 34 | a.<index> = tb |
35 | -- | 35 | -- |
36 | 36 | ||
37 | mt = a.# | 37 | mt = a.<> |
38 | 38 | ||
39 | tb\func #list | 39 | tb\func #list |
40 | tb\func#list | 40 | tb\<func>list |
41 | tb\func# list | 41 | tb\<func> list |
42 | 42 | ||
43 | import "module" as :index#, newindex#:setFunc | 43 | import "module" as :<index>, <newindex>:setFunc |
44 | 44 | ||
45 | with tb | 45 | with tb |
46 | print .add#, .x\index# "key" | 46 | print .<add>, .x\<index> "key" |
47 | a = .index#.add#\new# 123 | 47 | a = .<index>.<add>\<new> 123 |
48 | b = t#.close#.test | 48 | b = t#.<close>.test |
49 | c = t #.close# .test | 49 | c = t #.<close> .test |
50 | 50 | ||
51 | #:mt = a | 51 | <>:mt = a |
52 | a = #:mt | 52 | a = <>:mt |
53 | a = #:__index:mt | 53 | a = <>:__index:mt |
54 | 54 | ||
55 | local index | 55 | local index |
56 | #:__index:index = a | 56 | <>:__index:index = a |
57 | :index# = a | 57 | :<index> = a |
58 | 58 | ||
59 | do #:{new:ctor, :update} = a | 59 | do <>:{new:ctor, :update} = a |
60 | do {new:ctor, :update} = a.# | 60 | do {new:ctor, :update} = a.<> |
61 | |||
62 | tb = {} | ||
63 | do | ||
64 | f = tb\<"value#{x < y}">(123, ...) | ||
65 | f tb\<'value'> 123, ... | ||
66 | tb\<[[ | ||
67 | value | ||
68 | 1 | ||
69 | ]]>(123, ...) | ||
70 | return tb\<["value" .. tostring x > y]>(123, ...) | ||
71 | |||
72 | do | ||
73 | f = tb\<value>(123, ...) | ||
74 | f tb\<value>(123, ...) | ||
75 | tb\<value>(123, ...) | ||
76 | return tb\<value> 123, ... | ||
77 | |||
78 | do | ||
79 | f = tb.<value> 123, ... | ||
80 | f = tb.<"value#{x < y}">(123, ...) | ||
81 | f tb.<'value'> 123, ... | ||
82 | tb.<[[ value | ||
83 | 1]]>(123, ...) | ||
84 | return tb.<["value" .. tostring x > y]>(123, ...) | ||
61 | 85 | ||
62 | nil | 86 | nil |