From d4af1fa275b1d27229fc995f4a45137380040933 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 8 Sep 2022 09:26:49 +0800 Subject: redesigned metatable syntax. add support for destructuring a field with string and expression --- spec/inputs/assign.yue | 4 +- spec/inputs/class.yue | 6 +-- spec/inputs/destructure.yue | 31 ++++++++++++--- spec/inputs/existential.yue | 4 +- spec/inputs/import.yue | 6 +-- spec/inputs/macro.yue | 2 +- spec/inputs/metatable.yue | 92 ++++++++++++++++++++++++++++---------------- spec/inputs/switch.yue | 4 +- spec/outputs/destructure.lua | 61 +++++++++++++++++++++++++++++ spec/outputs/metatable.lua | 33 ++++++++++++++++ 10 files changed, 190 insertions(+), 53 deletions(-) (limited to 'spec') 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 (using nil) <- _ -a.# = do +a.<> = do print 123 {} @@ -63,7 +63,7 @@ do 123, tb do - a, b[], c, d.add# = if x + a, b[], c, d. = if x switch y when 1 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 class Foo new: (x) => @x = x - mul#: (y) => @x * y - ["dsd-dsd"]#: 123 + : (y) => @x * y + <"dsd-dsd">: 123 :add - :add# + : 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 {key1: {key2: value1 = 123}, :key3 = "abc"} = tb - {#: mt = {__index: {abc: 123}}, #: {:call# = (-> {}), :add#}} = tb + {<>: mt = {__index: {abc: 123}}, <>: {: = (-> {}), :}} = tb - {x: {#: mtx = {}}, :y, z: zItem, :index# = -> nil} = tb + {x: {<>: mtx = {}}, :y, z: zItem, : = -> nil} = tb - {#: {func: a.b(-> 123).c = item?.defVal}} = tb + {<>: {func: a.b(-> 123).c = item?.defVal}} = tb do - {#: mt = {}, sub#: subFunc} = tb.x + {<>: mt = {}, : subFunc} = tb.x do - {x: {#: mt = {}, sub#: subFunc}} = tb + {x: {<>: mt = {}, : subFunc}} = tb do {a = 1, b = 2, c: {d.e = 3}} = tb @@ -189,5 +189,24 @@ do const {:x = 0.0, :y = 0.0} = point do - x1, x2, x3, a[], d, {b}, e, c.# = 1, 2, 3, f! + x1, x2, x3, a[], d, {b}, e, c.<> = 1, 2, 3, f! y1, :y2, :y3, y4 = f1!, f2! + +do + { + [["abc"]]: v1 = 111, + [1 + 1]: {v2 = 222, v3 = 333}, + @x: v4 = 444 + }, \ + 'x-y-z': v5, [func!]: {[func2!]: v6, v7} = tb, tb2 + +do + <[name]>: value_meta, [name]: value = tb + +do + {: = (-> "name"), :} = tb + + switch tb + when {: = "item", <"123">: meta_field} + print name, meta_field + 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" \write "hello" \close! -tb?.a#? 123 +tb?.? 123 -with? tb.#?.index# +with? tb.<>?. .a = 1 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 import "org.package.module" as function:func, if:ifVar do - import "m" as {a#: b} - import "m" as {e: f, a#: c} + import "m" as {: b} + import "m" as {e: f, : c} import "m" as {c: d} - import "m" as {g, {h#: i}} + import "m" as {g, {: i}} 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( Destroy! ) -macro tb = -> "{'abc', a:123, call#:=> 998}" +macro tb = -> "{'abc', a:123, :=> 998}" print $tb[1], $tb.a, ($tb)!, $tb! 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 @@ -a = close: true, close#: => print "out of scope" -b = add#: (left, right)-> right - left -c = key1: true, :add#, key2: true -w = [name]#:123, ["new"]#:(val)=> {val} -w.#["new"] w.#[name] +a = close: true, : => print "out of scope" +b = : (left, right)-> right - left +c = key1: true, :, key2: true +w = <[name]>:123, <"new">:(val)=> {val} +w.<>["new"] w.<>[name] -do close _ = close#: -> print "out of scope" +do close _ = : -> print "out of scope" -d, e = a.close, a.close# +d, e = a.close, a. -f = a\close# 1 -a.add# = (x, y)-> x + y +f = a\ 1 +a. = (x, y)-> x + y do - {:new, :close#, close#: closeA} = a + {:new, :, : closeA} = a print new, close, closeA do local * x, \ - {:new, :var, :close#, close#: closeA}, \ - :num, :add#, :sub# \ + {:new, :var, :, : closeA}, \ + :num, :, : \ = 123, a.b.c, func! -x.abc, a.b.# = 123, {} -func!.# = mt --, extra -a, b.c.#, d, e = 1, mt, "abc", nil +x.abc, a.b.<> = 123, {} +func!.<> = mt --, extra +a, b.c.<>, d, e = 1, mt, "abc", nil -is_same = a.#.__index == a.index# +is_same = a.<>.__index == a. -- -a.# = __index: tb -a.#.__index = tb -a.index# = tb +a.<> = __index: tb +a.<>.__index = tb +a. = tb -- -mt = a.# +mt = a.<> tb\func #list -tb\func#list -tb\func# list +tb\list +tb\ list -import "module" as :index#, newindex#:setFunc +import "module" as :, :setFunc with tb - print .add#, .x\index# "key" - a = .index#.add#\new# 123 - b = t#.close#.test - c = t #.close# .test + print ., .x\ "key" + a = ..\ 123 + b = t#..test + c = t #. .test -#:mt = a -a = #:mt -a = #:__index:mt +<>:mt = a +a = <>:mt +a = <>:__index:mt local index -#:__index:index = a -:index# = a +<>:__index:index = a +: = a -do #:{new:ctor, :update} = a -do {new:ctor, :update} = a.# +do <>:{new:ctor, :update} = a +do {new:ctor, :update} = a.<> + +tb = {} +do + f = tb\<"value#{x < y}">(123, ...) + f tb\<'value'> 123, ... + tb\<[[ + value + 1 + ]]>(123, ...) + return tb\<["value" .. tostring x > y]>(123, ...) + +do + f = tb\(123, ...) + f tb\(123, ...) + tb\(123, ...) + return tb\ 123, ... + +do + f = tb. 123, ... + f = tb.<"value#{x < y}">(123, ...) + f tb.<'value'> 123, ... + tb.<[[ value +1]]>(123, ...) + return tb.<["value" .. tostring x > y]>(123, ...) 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 print "Object A" when ClassB print "Object B" - when #: mt + when <>: mt print "A table with metatable" else print "item not accepted!" @@ -156,7 +156,7 @@ do do switch y - when {x: #: mt} + when {x: <>: mt} print mt 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 y2, y3 = _obj_0.y2, _obj_0.y3 end end +do + local v1, v2, v3, v4 + do + local _obj_0 = tb + local _tmp_0, _tmp_1 = 1 + 1, self.x + v1, v2, v3, v4 = _obj_0[ [["abc"]]], _obj_0[_tmp_0][1], _obj_0[_tmp_0][2], _obj_0[_tmp_1] + if v1 == nil then + v1 = 111 + end + if v2 == nil then + v2 = 222 + end + if v3 == nil then + v3 = 333 + end + if v4 == nil then + v4 = 444 + end + end + local v5, v6, v7 + do + local _obj_0 = tb2 + local _tmp_2, _tmp_3 = func(), func2() + v5, v6, v7 = _obj_0['x-y-z'], _obj_0[_tmp_2][_tmp_3], _obj_0[_tmp_2][1] + end +end +do + local _obj_0 = tb + local value = _obj_0[name] + local value_meta = getmetatable(_obj_0)[name] +end +do + local tostring, add + do + local _obj_0 = getmetatable(tb) + tostring, add = _obj_0.__tostring, _obj_0.__add + if tostring == nil then + tostring = (function() + return "name" + end) + end + end + local _exp_0 = tb + do + local _tab_0 = "table" == type(_exp_0) + if _tab_0 then + local name, meta_field + do + local _obj_0 = getmetatable(_exp_0) + name = _obj_0.__name + meta_field = _obj_0["123"] + if name == nil then + name = "item" + end + end + if meta_field ~= nil then + return print(name, meta_field) + end + end + end +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 ctor, update = _obj_0.new, _obj_0.update end end +local tb = { } +do + do + local _obj_0 = getmetatable(tb) + f = _obj_0["value" .. tostring(x < y)](_obj_0, 123, ...) + end + f((function(...) + local _obj_0 = getmetatable(tb) + return _obj_0['value'](_obj_0, 123, ...) + end)(...)) + do + local _obj_0 = getmetatable(tb) + _obj_0[ [[ value + 1 + ]]](_obj_0, 123, ...) + end + local _obj_0 = getmetatable(tb) + return _obj_0["value" .. tostring(x > y)](_obj_0, 123, ...) +end +do + f = getmetatable(tb):__value(123, ...) + f(getmetatable(tb):__value(123, ...)) + getmetatable(tb):__value(123, ...) + return getmetatable(tb):__value(123, ...) +end +do + f = getmetatable(tb).__value(123, ...) + f = getmetatable(tb)["value" .. tostring(x < y)](123, ...) + f(getmetatable(tb)['value'](123, ...)) + getmetatable(tb)[ [[ value +1]]](123, ...) + return getmetatable(tb)["value" .. tostring(x > y)](123, ...) +end return nil -- cgit v1.2.3-55-g6feb