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 +- 8 files changed, 96 insertions(+), 53 deletions(-) (limited to 'spec/inputs') 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 -- cgit v1.2.3-55-g6feb