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 | |
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')
-rw-r--r-- | spec/inputs/assign.yue | 4 | ||||
-rw-r--r-- | spec/inputs/class.yue | 6 | ||||
-rw-r--r-- | spec/inputs/destructure.yue | 31 | ||||
-rw-r--r-- | spec/inputs/existential.yue | 4 | ||||
-rw-r--r-- | spec/inputs/import.yue | 6 | ||||
-rw-r--r-- | spec/inputs/macro.yue | 2 | ||||
-rw-r--r-- | spec/inputs/metatable.yue | 92 | ||||
-rw-r--r-- | spec/inputs/switch.yue | 4 | ||||
-rw-r--r-- | spec/outputs/destructure.lua | 61 | ||||
-rw-r--r-- | spec/outputs/metatable.lua | 33 |
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 | ||
37 | a.# = do | 37 | a.<> = do |
38 | print 123 | 38 | print 123 |
39 | {} | 39 | {} |
40 | 40 | ||
@@ -63,7 +63,7 @@ do | |||
63 | 123, tb | 63 | 123, tb |
64 | 64 | ||
65 | do | 65 | do |
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 | ||
238 | class Foo | 238 | class 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 | ||
245 | nil | 245 | nil |
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 | ||
169 | do | 169 | do |
170 | {#: mt = {}, sub#: subFunc} = tb.x | 170 | {<>: mt = {}, <sub>: subFunc} = tb.x |
171 | 171 | ||
172 | do | 172 | do |
173 | {x: {#: mt = {}, sub#: subFunc}} = tb | 173 | {x: {<>: mt = {}, <sub>: subFunc}} = tb |
174 | 174 | ||
175 | do | 175 | do |
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 | ||
191 | do | 191 | do |
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 | |||
195 | do | ||
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 | |||
203 | do | ||
204 | <[name]>: value_meta, [name]: value = tb | ||
205 | |||
206 | do | ||
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 | ||
52 | tb?.a#? 123 | 52 | tb?.<a>? 123 |
53 | 53 | ||
54 | with? tb.#?.index# | 54 | with? tb.<>?.<index> |
55 | .a = 1 | 55 | .a = 1 |
56 | 56 | ||
57 | nil | 57 | nil |
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 | ||
66 | do | 66 | do |
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 | ||
265 | macro tb = -> "{'abc', a:123, call#:=> 998}" | 265 | macro tb = -> "{'abc', a:123, <call>:=> 998}" |
266 | print $tb[1], $tb.a, ($tb)!, $tb! | 266 | print $tb[1], $tb.a, ($tb)!, $tb! |
267 | 267 | ||
268 | print "current line: #{ $LINE }" | 268 | print "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 @@ | |||
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 |
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 | ||
157 | do | 157 | do |
158 | switch y | 158 | switch y |
159 | when {x: #: mt} | 159 | when {x: <>: mt} |
160 | print mt | 160 | print mt |
161 | 161 | ||
162 | nil | 162 | nil |
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 |
430 | end | 430 | end |
431 | do | ||
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 | ||
456 | end | ||
457 | do | ||
458 | local _obj_0 = tb | ||
459 | local value = _obj_0[name] | ||
460 | local value_meta = getmetatable(_obj_0)[name] | ||
461 | end | ||
462 | do | ||
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 | ||
491 | end | ||
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 |
113 | end | 113 | end |
114 | local tb = { } | ||
115 | do | ||
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, ...) | ||
132 | end | ||
133 | do | ||
134 | f = getmetatable(tb):__value(123, ...) | ||
135 | f(getmetatable(tb):__value(123, ...)) | ||
136 | getmetatable(tb):__value(123, ...) | ||
137 | return getmetatable(tb):__value(123, ...) | ||
138 | end | ||
139 | do | ||
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 | ||
144 | 1]]](123, ...) | ||
145 | return getmetatable(tb)["value" .. tostring(x > y)](123, ...) | ||
146 | end | ||
114 | return nil | 147 | return nil |