diff options
author | Li Jin <dragon-fly@qq.com> | 2022-05-16 10:25:19 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-05-16 10:25:19 +0800 |
commit | de91fff7af15ae9c4a27d34b5b9266f900412a29 (patch) | |
tree | 9ececd242d326c8d5fec84043112782c95174b76 | |
parent | a9fe6a5c4c17cf7e8305c1d614939ce510fb1103 (diff) | |
download | yuescript-de91fff7af15ae9c4a27d34b5b9266f900412a29.tar.gz yuescript-de91fff7af15ae9c4a27d34b5b9266f900412a29.tar.bz2 yuescript-de91fff7af15ae9c4a27d34b5b9266f900412a29.zip |
fix issue #101 by supporting metamethod syntax in class block.
-rw-r--r-- | spec/inputs/class.yue | 11 | ||||
-rw-r--r-- | spec/outputs/class.lua | 30 | ||||
-rwxr-xr-x | src/yuescript/yue_ast.h | 2 | ||||
-rwxr-xr-x | src/yuescript/yue_compiler.cpp | 26 |
4 files changed, 65 insertions, 4 deletions
diff --git a/spec/inputs/class.yue b/spec/inputs/class.yue index c7be481..9091d23 100644 --- a/spec/inputs/class.yue +++ b/spec/inputs/class.yue | |||
@@ -232,7 +232,14 @@ class extends lapis.Application | |||
232 | class A using B, C, D, {value: 123} | 232 | class A using B, C, D, {value: 123} |
233 | 233 | ||
234 | class Example | 234 | class Example |
235 | @field1 = 1 | 235 | @field1 = 1 |
236 | @field2 = @field1 + 1 | 236 | @field2 = @field1 + 1 |
237 | |||
238 | class Foo | ||
239 | new: (x) => @x = x | ||
240 | mul#: (y) => @x * y | ||
241 | ["dsd-dsd"]#: 123 | ||
242 | :add | ||
243 | :add# | ||
237 | 244 | ||
238 | nil | 245 | nil |
diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 8f7c126..9a61a67 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua | |||
@@ -1146,4 +1146,34 @@ do | |||
1146 | self.field2 = self.field1 + 1 | 1146 | self.field2 = self.field1 + 1 |
1147 | Example = _class_0 | 1147 | Example = _class_0 |
1148 | end | 1148 | end |
1149 | do | ||
1150 | local _class_0 | ||
1151 | local _base_0 = { | ||
1152 | __mul = function(self, y) | ||
1153 | return self.x * y | ||
1154 | end, | ||
1155 | ["dsd-dsd"] = 123, | ||
1156 | add = add, | ||
1157 | __add = add | ||
1158 | } | ||
1159 | if _base_0.__index == nil then | ||
1160 | _base_0.__index = _base_0 | ||
1161 | end | ||
1162 | _class_0 = setmetatable({ | ||
1163 | __init = function(self, x) | ||
1164 | self.x = x | ||
1165 | end, | ||
1166 | __base = _base_0, | ||
1167 | __name = "Foo" | ||
1168 | }, { | ||
1169 | __index = _base_0, | ||
1170 | __call = function(cls, ...) | ||
1171 | local _self_0 = setmetatable({ }, _base_0) | ||
1172 | cls.__init(_self_0, ...) | ||
1173 | return _self_0 | ||
1174 | end | ||
1175 | }) | ||
1176 | _base_0.__class = _class_0 | ||
1177 | Foo = _class_0 | ||
1178 | end | ||
1149 | return nil | 1179 | return nil |
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index d255236..38e97ad 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
@@ -639,7 +639,7 @@ AST_END(TableBlock, "table_block"sv) | |||
639 | 639 | ||
640 | AST_NODE(class_member_list) | 640 | AST_NODE(class_member_list) |
641 | ast_ptr<true, Seperator_t> sep; | 641 | ast_ptr<true, Seperator_t> sep; |
642 | ast_sel_list<true, variable_pair_t, normal_pair_t> values; | 642 | ast_sel_list<true, variable_pair_t, normal_pair_t, meta_variable_pair_t, meta_normal_pair_t> values; |
643 | AST_MEMBER(class_member_list, &sep, &values) | 643 | AST_MEMBER(class_member_list, &sep, &values) |
644 | AST_END(class_member_list, "class_member_list"sv) | 644 | AST_END(class_member_list, "class_member_list"sv) |
645 | 645 | ||
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6d90d24..51ece7c 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -60,7 +60,7 @@ using namespace parserlib; | |||
60 | 60 | ||
61 | typedef std::list<std::string> str_list; | 61 | typedef std::list<std::string> str_list; |
62 | 62 | ||
63 | const std::string_view version = "0.10.21"sv; | 63 | const std::string_view version = "0.10.22"sv; |
64 | const std::string_view extension = "yue"sv; | 64 | const std::string_view extension = "yue"sv; |
65 | 65 | ||
66 | class YueCompilerImpl { | 66 | class YueCompilerImpl { |
@@ -5985,6 +5985,30 @@ private: | |||
5985 | size_t count = 0; | 5985 | size_t count = 0; |
5986 | for (auto keyValue : class_member_list->values.objects()) { | 5986 | for (auto keyValue : class_member_list->values.objects()) { |
5987 | MemType type = MemType::Common; | 5987 | MemType type = MemType::Common; |
5988 | ast_ptr<false, ast_node> ref; | ||
5989 | switch (keyValue->getId()) { | ||
5990 | case id<meta_variable_pair_t>(): { | ||
5991 | auto mtPair = static_cast<meta_variable_pair_t*>(keyValue); | ||
5992 | auto nameStr = _parser.toString(mtPair->name); | ||
5993 | ref.set(toAst<normal_pair_t>("__"s + nameStr + ':' + nameStr, keyValue)); | ||
5994 | keyValue = ref.get(); | ||
5995 | break; | ||
5996 | } | ||
5997 | case id<meta_normal_pair_t>(): { | ||
5998 | auto mtPair = static_cast<meta_normal_pair_t*>(keyValue); | ||
5999 | auto normal_pair = keyValue->new_ptr<normal_pair_t>(); | ||
6000 | if (auto name = mtPair->key.as<Name_t>()) { | ||
6001 | auto nameStr = _parser.toString(name); | ||
6002 | normal_pair->key.set(toAst<KeyName_t>("__"s + nameStr, keyValue)); | ||
6003 | } else { | ||
6004 | normal_pair->key.set(mtPair->key); | ||
6005 | } | ||
6006 | normal_pair->value.set(mtPair->value); | ||
6007 | ref.set(normal_pair); | ||
6008 | keyValue = ref.get(); | ||
6009 | break; | ||
6010 | } | ||
6011 | } | ||
5988 | BLOCK_START | 6012 | BLOCK_START |
5989 | auto normal_pair = ast_cast<normal_pair_t>(keyValue); | 6013 | auto normal_pair = ast_cast<normal_pair_t>(keyValue); |
5990 | BREAK_IF(!normal_pair); | 6014 | BREAK_IF(!normal_pair); |