From 29db60797bd74656b57a0f20778c76adc78095ac Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sat, 28 Feb 2026 18:03:14 +0800 Subject: Fixed destructuring with empty and comment lines in table issue. Updated docs. --- .../de/doc/objects/object-oriented-programming.md | 22 ++++++++ .../doc/objects/object-oriented-programming.md | 22 ++++++++ .../doc/objects/object-oriented-programming.md | 22 ++++++++ .../doc/objects/object-oriented-programming.md | 22 ++++++++ .../zh/doc/objects/object-oriented-programming.md | 22 ++++++++ doc/yue-de.md | 10 ++++ doc/yue-en.md | 10 ++++ doc/yue-id-id.md | 10 ++++ doc/yue-pt-br.md | 10 ++++ doc/yue-zh.md | 10 ++++ spec/inputs/whitespace.yue | 13 +++++ spec/outputs/codes_from_doc_de.lua | 58 ++++++++++++++++++++++ spec/outputs/codes_from_doc_en.lua | 58 ++++++++++++++++++++++ spec/outputs/codes_from_doc_id-id.lua | 58 ++++++++++++++++++++++ spec/outputs/codes_from_doc_pt-br.lua | 58 ++++++++++++++++++++++ spec/outputs/codes_from_doc_zh.lua | 58 ++++++++++++++++++++++ spec/outputs/whitespace.lua | 5 ++ src/yuescript/yue_compiler.cpp | 8 ++- 18 files changed, 475 insertions(+), 1 deletion(-) diff --git a/doc/docs/de/doc/objects/object-oriented-programming.md b/doc/docs/de/doc/objects/object-oriented-programming.md index 706cfda..705fc08 100644 --- a/doc/docs/de/doc/objects/object-oriented-programming.md +++ b/doc/docs/de/doc/objects/object-oriented-programming.md @@ -40,6 +40,28 @@ Beachte, dass alle Methoden in der Klasse die Fat-Arrow-Syntax verwenden. Beim A Das `@`-Präfix ist Kurzform für `self.`. `@items` wird zu `self.items`. +Der Klassenblock unterstützt auch Metatable-Felder, indem Metamethod-Schlüssel in spitzen Klammern geschrieben werden, zum Beispiel ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + +```yue +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + Eine Instanz der Klasse wird erstellt, indem man den Klassennamen wie eine Funktion aufruft. ```yuescript diff --git a/doc/docs/doc/objects/object-oriented-programming.md b/doc/docs/doc/objects/object-oriented-programming.md index 781ab65..b3325fa 100644 --- a/doc/docs/doc/objects/object-oriented-programming.md +++ b/doc/docs/doc/objects/object-oriented-programming.md @@ -40,6 +40,28 @@ Notice how all the methods in the class use the fat arrow function syntax. When The @ prefix on a variable name is shorthand for self.. @items becomes self.items. +The class block also supports metatable fields by writing metamethod keys in angle brackets, such as ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + +```yue +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + Creating an instance of the class is done by calling the name of the class as a function. ```yuescript diff --git a/doc/docs/id-id/doc/objects/object-oriented-programming.md b/doc/docs/id-id/doc/objects/object-oriented-programming.md index 7d5d420..cc74690 100644 --- a/doc/docs/id-id/doc/objects/object-oriented-programming.md +++ b/doc/docs/id-id/doc/objects/object-oriented-programming.md @@ -40,6 +40,28 @@ Perhatikan bahwa semua method di kelas menggunakan sintaks fungsi panah tebal. S Prefiks `@` pada nama variabel adalah singkatan untuk `self.`. `@items` menjadi `self.items`. +Blok kelas juga mendukung field metatable dengan menulis key metamethod dalam tanda kurung sudut, misalnya ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + +```yue +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + Membuat instance kelas dilakukan dengan memanggil nama kelas sebagai fungsi. ```yuescript diff --git a/doc/docs/pt-br/doc/objects/object-oriented-programming.md b/doc/docs/pt-br/doc/objects/object-oriented-programming.md index 54a559a..01aeff3 100644 --- a/doc/docs/pt-br/doc/objects/object-oriented-programming.md +++ b/doc/docs/pt-br/doc/objects/object-oriented-programming.md @@ -40,6 +40,28 @@ Observe como todos os métodos da classe usam a sintaxe de função seta fat. Ao O prefixo @ em um nome de variável é abreviação para self.. @items torna-se self.items. +O bloco da classe também suporta campos de metatable escrevendo chaves de metamétodo entre `<>`, como ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + +```yue +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + Criar uma instância da classe é feito chamando o nome da classe como uma função. ```yuescript diff --git a/doc/docs/zh/doc/objects/object-oriented-programming.md b/doc/docs/zh/doc/objects/object-oriented-programming.md index 04f816a..270c2b3 100644 --- a/doc/docs/zh/doc/objects/object-oriented-programming.md +++ b/doc/docs/zh/doc/objects/object-oriented-programming.md @@ -38,6 +38,28 @@ class Inventory   此外,“@” 前缀在变量名上起到了简化作用,代表 “self”。例如,`@items` 就等同于 `self.items`。 +  类定义块中也支持通过尖括号写元方法字段,例如 ``。 + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + + +```yue +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + + +   为了创建类的一个新实例,可以将类名当作一个函数来调用,这样就可以生成并返回一个新的实例。 ```yuescript diff --git a/doc/yue-de.md b/doc/yue-de.md index f0979e0..97063f4 100644 --- a/doc/yue-de.md +++ b/doc/yue-de.md @@ -508,6 +508,16 @@ Beachte, dass alle Methoden in der Klasse die Fat-Arrow-Syntax verwenden. Beim A Das `@`-Präfix ist Kurzform für `self.`. `@items` wird zu `self.items`. +Der Klassenblock unterstützt auch Metatable-Felder, indem Metamethod-Schlüssel in spitzen Klammern geschrieben werden, zum Beispiel ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + Eine Instanz der Klasse wird erstellt, indem man den Klassennamen wie eine Funktion aufruft. ```yuescript diff --git a/doc/yue-en.md b/doc/yue-en.md index 6680281..8f8370d 100644 --- a/doc/yue-en.md +++ b/doc/yue-en.md @@ -508,6 +508,16 @@ Notice how all the methods in the class use the fat arrow function syntax. When The @ prefix on a variable name is shorthand for self.. @items becomes self.items. +The class block also supports metatable fields by writing metamethod keys in angle brackets, such as ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + Creating an instance of the class is done by calling the name of the class as a function. ```yuescript diff --git a/doc/yue-id-id.md b/doc/yue-id-id.md index 863e187..39ba24d 100644 --- a/doc/yue-id-id.md +++ b/doc/yue-id-id.md @@ -508,6 +508,16 @@ Perhatikan bahwa semua method di kelas menggunakan sintaks fungsi panah tebal. S Prefiks `@` pada nama variabel adalah singkatan untuk `self.`. `@items` menjadi `self.items`. +Blok kelas juga mendukung field metatable dengan menulis key metamethod dalam tanda kurung sudut, misalnya ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + Membuat instance kelas dilakukan dengan memanggil nama kelas sebagai fungsi. ```yuescript diff --git a/doc/yue-pt-br.md b/doc/yue-pt-br.md index a1408c2..a0e3268 100644 --- a/doc/yue-pt-br.md +++ b/doc/yue-pt-br.md @@ -508,6 +508,16 @@ Observe como todos os métodos da classe usam a sintaxe de função seta fat. Ao O prefixo @ em um nome de variável é abreviação para self.. @items torna-se self.items. +O bloco da classe também suporta campos de metatable escrevendo chaves de metamétodo entre `<>`, como ``. + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` + Criar uma instância da classe é feito chamando o nome da classe como uma função. ```yuescript diff --git a/doc/yue-zh.md b/doc/yue-zh.md index ac7e4b2..cb44d22 100644 --- a/doc/yue-zh.md +++ b/doc/yue-zh.md @@ -506,6 +506,16 @@ class Inventory   此外,“@” 前缀在变量名上起到了简化作用,代表 “self”。例如,`@items` 就等同于 `self.items`。 +  类定义块中也支持通过尖括号写元方法字段,例如 ``。 + +```yuescript +class User + new: (@name) => + : => "User(#{@name})" + +print tostring User "Yue" +``` +   为了创建类的一个新实例,可以将类名当作一个函数来调用,这样就可以生成并返回一个新的实例。 ```yuescript diff --git a/spec/inputs/whitespace.yue b/spec/inputs/whitespace.yue index d0acdde..18e1628 100644 --- a/spec/inputs/whitespace.yue +++ b/spec/inputs/whitespace.yue @@ -246,4 +246,17 @@ do -- init xd close xd = : => + +do + { + numbers: [ + first + second + ] + properties: + :color + + -- dsd + } = obj2 + nil diff --git a/spec/outputs/codes_from_doc_de.lua b/spec/outputs/codes_from_doc_de.lua index d8115ee..28ed4e7 100644 --- a/spec/outputs/codes_from_doc_de.lua +++ b/spec/outputs/codes_from_doc_de.lua @@ -939,6 +939,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("T-Shirt") inv:add_item("Hose") @@ -1559,6 +1588,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("T-Shirt") inv:add_item("Hose") diff --git a/spec/outputs/codes_from_doc_en.lua b/spec/outputs/codes_from_doc_en.lua index 66fb0b7..1fdc56d 100644 --- a/spec/outputs/codes_from_doc_en.lua +++ b/spec/outputs/codes_from_doc_en.lua @@ -939,6 +939,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") @@ -1559,6 +1588,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") diff --git a/spec/outputs/codes_from_doc_id-id.lua b/spec/outputs/codes_from_doc_id-id.lua index 6caf79c..2201dee 100644 --- a/spec/outputs/codes_from_doc_id-id.lua +++ b/spec/outputs/codes_from_doc_id-id.lua @@ -939,6 +939,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") @@ -1559,6 +1588,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") diff --git a/spec/outputs/codes_from_doc_pt-br.lua b/spec/outputs/codes_from_doc_pt-br.lua index 22f9bb9..b3bf0b3 100644 --- a/spec/outputs/codes_from_doc_pt-br.lua +++ b/spec/outputs/codes_from_doc_pt-br.lua @@ -939,6 +939,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") @@ -1559,6 +1588,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index cf08375..5756793 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua @@ -939,6 +939,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") @@ -1559,6 +1588,35 @@ _class_0 = setmetatable({ }) _base_0.__class = _class_0 Inventory = _class_0 +local User +do + local _class_0 + local _base_0 = { + __tostring = function(self) + return "User(" .. tostring(self.name) .. ")" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name) + self.name = name + end, + __base = _base_0, + __name = "User" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + User = _class_0 +end +print(tostring(User("Yue"))) local inv = Inventory() inv:add_item("t-shirt") inv:add_item("pants") diff --git a/spec/outputs/whitespace.lua b/spec/outputs/whitespace.lua index a0d225f..768f2ac 100644 --- a/spec/outputs/whitespace.lua +++ b/spec/outputs/whitespace.lua @@ -183,4 +183,9 @@ do local _close_0 = xd CY = _class_0 end +do + local first, second, color + local _obj_0 = obj2 + first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color +end return nil diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 10fd5d6..514b6f2 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.33.6"sv; +const std::string_view version = "0.33.7"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -3028,6 +3028,9 @@ private: auto subMetaDestruct = node->new_ptr(); for (auto pair : *tableItems) { switch (pair->get_id()) { + case id(): + case id(): + break; case id(): case id(): { ++index; @@ -3429,6 +3432,9 @@ private: } for (auto item : *dlist) { switch (item->get_id()) { + case id(): + case id(): + break; case id(): { auto mvp = static_cast(item); auto mp = mvp->pair.get(); -- cgit v1.2.3-55-g6feb