aboutsummaryrefslogtreecommitdiff
path: root/spec
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
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')
-rw-r--r--spec/inputs/assign.yue4
-rw-r--r--spec/inputs/class.yue6
-rw-r--r--spec/inputs/destructure.yue31
-rw-r--r--spec/inputs/existential.yue4
-rw-r--r--spec/inputs/import.yue6
-rw-r--r--spec/inputs/macro.yue2
-rw-r--r--spec/inputs/metatable.yue92
-rw-r--r--spec/inputs/switch.yue4
-rw-r--r--spec/outputs/destructure.lua61
-rw-r--r--spec/outputs/metatable.lua33
10 files changed, 190 insertions, 53 deletions
diff --git a/spec/inputs/assign.yue b/spec/inputs/assign.yue
index d04bcb8..da44dff 100644
--- a/spec/inputs/assign.yue
+++ b/spec/inputs/assign.yue
@@ -34,7 +34,7 @@ x = (do
34 34
35(using nil) <- _ 35(using nil) <- _
36 36
37a.# = do 37a.<> = do
38 print 123 38 print 123
39 {} 39 {}
40 40
@@ -63,7 +63,7 @@ do
63 123, tb 63 123, tb
64 64
65do 65do
66 a, b[], c, d.add# = if x 66 a, b[], c, d.<add> = if x
67 switch y 67 switch y
68 when 1 68 when 1
69 f! 69 f!
diff --git a/spec/inputs/class.yue b/spec/inputs/class.yue
index 9091d23..49537c2 100644
--- a/spec/inputs/class.yue
+++ b/spec/inputs/class.yue
@@ -237,9 +237,9 @@ class Example
237 237
238class Foo 238class Foo
239 new: (x) => @x = x 239 new: (x) => @x = x
240 mul#: (y) => @x * y 240 <mul>: (y) => @x * y
241 ["dsd-dsd"]#: 123 241 <"dsd-dsd">: 123
242 :add 242 :add
243 :add# 243 :<add>
244 244
245nil 245nil
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue
index 53f9ea3..ce593ec 100644
--- a/spec/inputs/destructure.yue
+++ b/spec/inputs/destructure.yue
@@ -160,17 +160,17 @@ do
160 160
161 {key1: {key2: value1 = 123}, :key3 = "abc"} = tb 161 {key1: {key2: value1 = 123}, :key3 = "abc"} = tb
162 162
163 {#: mt = {__index: {abc: 123}}, #: {:call# = (-> {}), :add#}} = tb 163 {<>: mt = {__index: {abc: 123}}, <>: {:<call> = (-> {}), :<add>}} = tb
164 164
165 {x: {#: mtx = {}}, :y, z: zItem, :index# = -> nil} = tb 165 {x: {<>: mtx = {}}, :y, z: zItem, :<index> = -> nil} = tb
166 166
167 {#: {func: a.b(-> 123).c = item?.defVal}} = tb 167 {<>: {func: a.b(-> 123).c = item?.defVal}} = tb
168 168
169do 169do
170 {#: mt = {}, sub#: subFunc} = tb.x 170 {<>: mt = {}, <sub>: subFunc} = tb.x
171 171
172do 172do
173 {x: {#: mt = {}, sub#: subFunc}} = tb 173 {x: {<>: mt = {}, <sub>: subFunc}} = tb
174 174
175do 175do
176 {a = 1, b = 2, c: {d.e = 3}} = tb 176 {a = 1, b = 2, c: {d.e = 3}} = tb
@@ -189,5 +189,24 @@ do
189 const {:x = 0.0, :y = 0.0} = point 189 const {:x = 0.0, :y = 0.0} = point
190 190
191do 191do
192 x1, x2, x3, a[], d, {b}, e, c.# = 1, 2, 3, f! 192 x1, x2, x3, a[], d, {b}, e, c.<> = 1, 2, 3, f!
193 y1, :y2, :y3, y4 = f1!, f2! 193 y1, :y2, :y3, y4 = f1!, f2!
194
195do
196 {
197 [["abc"]]: v1 = 111,
198 [1 + 1]: {v2 = 222, v3 = 333},
199 @x: v4 = 444
200 }, \
201 'x-y-z': v5, [func!]: {[func2!]: v6, v7} = tb, tb2
202
203do
204 <[name]>: value_meta, [name]: value = tb
205
206do
207 {:<tostring> = (-> "name"), :<add>} = tb
208
209 switch tb
210 when {:<name> = "item", <"123">: meta_field}
211 print name, meta_field
212
diff --git a/spec/inputs/existential.yue b/spec/inputs/existential.yue
index 101e1d8..e73b421 100644
--- a/spec/inputs/existential.yue
+++ b/spec/inputs/existential.yue
@@ -49,9 +49,9 @@ with? io.open "test.txt", "w"
49 \write "hello" 49 \write "hello"
50 \close! 50 \close!
51 51
52tb?.a#? 123 52tb?.<a>? 123
53 53
54with? tb.#?.index# 54with? tb.<>?.<index>
55 .a = 1 55 .a = 1
56 56
57nil 57nil
diff --git a/spec/inputs/import.yue b/spec/inputs/import.yue
index 570b909..af5545d 100644
--- a/spec/inputs/import.yue
+++ b/spec/inputs/import.yue
@@ -64,8 +64,8 @@ do
64 import "org.package.module" as function:func, if:ifVar 64 import "org.package.module" as function:func, if:ifVar
65 65
66do 66do
67 import "m" as {a#: b} 67 import "m" as {<a>: b}
68 import "m" as {e: f, a#: c} 68 import "m" as {e: f, <a>: c}
69 import "m" as {c: d} 69 import "m" as {c: d}
70 import "m" as {g, {h#: i}} 70 import "m" as {g, {<h>: i}}
71 71
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue
index 3c89c1c..ac51d85 100644
--- a/spec/inputs/macro.yue
+++ b/spec/inputs/macro.yue
@@ -262,7 +262,7 @@ $chainC(
262 Destroy! 262 Destroy!
263) 263)
264 264
265macro tb = -> "{'abc', a:123, call#:=> 998}" 265macro tb = -> "{'abc', a:123, <call>:=> 998}"
266print $tb[1], $tb.a, ($tb)!, $tb! 266print $tb[1], $tb.a, ($tb)!, $tb!
267 267
268print "current line: #{ $LINE }" 268print "current line: #{ $LINE }"
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
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue
index 442d15f..cb1eb31 100644
--- a/spec/inputs/switch.yue
+++ b/spec/inputs/switch.yue
@@ -101,7 +101,7 @@ do
101 print "Object A" 101 print "Object A"
102 when ClassB 102 when ClassB
103 print "Object B" 103 print "Object B"
104 when #: mt 104 when <>: mt
105 print "A table with metatable" 105 print "A table with metatable"
106 else 106 else
107 print "item not accepted!" 107 print "item not accepted!"
@@ -156,7 +156,7 @@ do
156 156
157do 157do
158 switch y 158 switch y
159 when {x: #: mt} 159 when {x: <>: mt}
160 print mt 160 print mt
161 161
162nil 162nil
diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua
index dfac42c..0da920e 100644
--- a/spec/outputs/destructure.lua
+++ b/spec/outputs/destructure.lua
@@ -428,3 +428,64 @@ do
428 y2, y3 = _obj_0.y2, _obj_0.y3 428 y2, y3 = _obj_0.y2, _obj_0.y3
429 end 429 end
430end 430end
431do
432 local v1, v2, v3, v4
433 do
434 local _obj_0 = tb
435 local _tmp_0, _tmp_1 = 1 + 1, self.x
436 v1, v2, v3, v4 = _obj_0[ [["abc"]]], _obj_0[_tmp_0][1], _obj_0[_tmp_0][2], _obj_0[_tmp_1]
437 if v1 == nil then
438 v1 = 111
439 end
440 if v2 == nil then
441 v2 = 222
442 end
443 if v3 == nil then
444 v3 = 333
445 end
446 if v4 == nil then
447 v4 = 444
448 end
449 end
450 local v5, v6, v7
451 do
452 local _obj_0 = tb2
453 local _tmp_2, _tmp_3 = func(), func2()
454 v5, v6, v7 = _obj_0['x-y-z'], _obj_0[_tmp_2][_tmp_3], _obj_0[_tmp_2][1]
455 end
456end
457do
458 local _obj_0 = tb
459 local value = _obj_0[name]
460 local value_meta = getmetatable(_obj_0)[name]
461end
462do
463 local tostring, add
464 do
465 local _obj_0 = getmetatable(tb)
466 tostring, add = _obj_0.__tostring, _obj_0.__add
467 if tostring == nil then
468 tostring = (function()
469 return "name"
470 end)
471 end
472 end
473 local _exp_0 = tb
474 do
475 local _tab_0 = "table" == type(_exp_0)
476 if _tab_0 then
477 local name, meta_field
478 do
479 local _obj_0 = getmetatable(_exp_0)
480 name = _obj_0.__name
481 meta_field = _obj_0["123"]
482 if name == nil then
483 name = "item"
484 end
485 end
486 if meta_field ~= nil then
487 return print(name, meta_field)
488 end
489 end
490 end
491end
diff --git a/spec/outputs/metatable.lua b/spec/outputs/metatable.lua
index 6f5ceed..937136f 100644
--- a/spec/outputs/metatable.lua
+++ b/spec/outputs/metatable.lua
@@ -111,4 +111,37 @@ do
111 ctor, update = _obj_0.new, _obj_0.update 111 ctor, update = _obj_0.new, _obj_0.update
112 end 112 end
113end 113end
114local tb = { }
115do
116 do
117 local _obj_0 = getmetatable(tb)
118 f = _obj_0["value" .. tostring(x < y)](_obj_0, 123, ...)
119 end
120 f((function(...)
121 local _obj_0 = getmetatable(tb)
122 return _obj_0['value'](_obj_0, 123, ...)
123 end)(...))
124 do
125 local _obj_0 = getmetatable(tb)
126 _obj_0[ [[ value
127 1
128 ]]](_obj_0, 123, ...)
129 end
130 local _obj_0 = getmetatable(tb)
131 return _obj_0["value" .. tostring(x > y)](_obj_0, 123, ...)
132end
133do
134 f = getmetatable(tb):__value(123, ...)
135 f(getmetatable(tb):__value(123, ...))
136 getmetatable(tb):__value(123, ...)
137 return getmetatable(tb):__value(123, ...)
138end
139do
140 f = getmetatable(tb).__value(123, ...)
141 f = getmetatable(tb)["value" .. tostring(x < y)](123, ...)
142 f(getmetatable(tb)['value'](123, ...))
143 getmetatable(tb)[ [[ value
1441]]](123, ...)
145 return getmetatable(tb)["value" .. tostring(x > y)](123, ...)
146end
114return nil 147return nil