aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/metatable.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-09-08 09:26:49 +0800
committerLi Jin <dragon-fly@qq.com>2022-09-08 09:26:49 +0800
commitd4af1fa275b1d27229fc995f4a45137380040933 (patch)
tree955c76511d7021e6d1a0f06b46de3852eeac4176 /spec/inputs/metatable.yue
parentdf85ad2e7f975026ca1e6bd84b26fff81c8d99c8 (diff)
downloadyuescript-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.yue92
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 @@
1a = close: true, close#: => print "out of scope" 1a = close: true, <close>: => print "out of scope"
2b = add#: (left, right)-> right - left 2b = <add>: (left, right)-> right - left
3c = key1: true, :add#, key2: true 3c = key1: true, :<add>, key2: true
4w = [name]#:123, ["new"]#:(val)=> {val} 4w = <[name]>:123, <"new">:(val)=> {val}
5w.#["new"] w.#[name] 5w.<>["new"] w.<>[name]
6 6
7do close _ = close#: -> print "out of scope" 7do close _ = <close>: -> print "out of scope"
8 8
9d, e = a.close, a.close# 9d, e = a.close, a.<close>
10 10
11f = a\close# 1 11f = a\<close> 1
12a.add# = (x, y)-> x + y 12a.<add> = (x, y)-> x + y
13 13
14do 14do
15 {:new, :close#, close#: closeA} = a 15 {:new, :<close>, <close>: closeA} = a
16 print new, close, closeA 16 print new, close, closeA
17 17
18do 18do
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
25x.abc, a.b.# = 123, {} 25x.abc, a.b.<> = 123, {}
26func!.# = mt --, extra 26func!.<> = mt --, extra
27a, b.c.#, d, e = 1, mt, "abc", nil 27a, b.c.<>, d, e = 1, mt, "abc", nil
28 28
29is_same = a.#.__index == a.index# 29is_same = a.<>.__index == a.<index>
30 30
31-- 31--
32a.# = __index: tb 32a.<> = __index: tb
33a.#.__index = tb 33a.<>.__index = tb
34a.index# = tb 34a.<index> = tb
35-- 35--
36 36
37mt = a.# 37mt = a.<>
38 38
39tb\func #list 39tb\func #list
40tb\func#list 40tb\<func>list
41tb\func# list 41tb\<func> list
42 42
43import "module" as :index#, newindex#:setFunc 43import "module" as :<index>, <newindex>:setFunc
44 44
45with tb 45with 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
52a = #:mt 52a = <>:mt
53a = #:__index:mt 53a = <>:__index:mt
54 54
55local index 55local index
56#:__index:index = a 56<>:__index:index = a
57:index# = a 57:<index> = a
58 58
59do #:{new:ctor, :update} = a 59do <>:{new:ctor, :update} = a
60do {new:ctor, :update} = a.# 60do {new:ctor, :update} = a.<>
61
62tb = {}
63do
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
72do
73 f = tb\<value>(123, ...)
74 f tb\<value>(123, ...)
75 tb\<value>(123, ...)
76 return tb\<value> 123, ...
77
78do
79 f = tb.<value> 123, ...
80 f = tb.<"value#{x < y}">(123, ...)
81 f tb.<'value'> 123, ...
82 tb.<[[ value
831]]>(123, ...)
84 return tb.<["value" .. tostring x > y]>(123, ...)
61 85
62nil 86nil