aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-03-05 10:14:10 +0800
committerLi Jin <dragon-fly@qq.com>2021-03-05 10:14:10 +0800
commit05283b77da4ba236f02b42d1205433f6b1fcb0d6 (patch)
treeecb5e6688109ed3e9be6acd5f1b8c22d6fd407dc
parent1df786307c1983b8ce693e3916081a8bcd4e95ae (diff)
downloadyuescript-05283b77da4ba236f02b42d1205433f6b1fcb0d6.tar.gz
yuescript-05283b77da4ba236f02b42d1205433f6b1fcb0d6.tar.bz2
yuescript-05283b77da4ba236f02b42d1205433f6b1fcb0d6.zip
fix issue #41, add square brackets support for metatable syntax. fix using metatable with "with" syntax issue.
-rw-r--r--spec/inputs/metatable.yue8
-rw-r--r--src/yuescript/yue_ast.h2
-rw-r--r--src/yuescript/yue_compiler.cpp40
-rw-r--r--src/yuescript/yue_parser.cpp2
4 files changed, 41 insertions, 11 deletions
diff --git a/spec/inputs/metatable.yue b/spec/inputs/metatable.yue
index 6f7b418..13ea917 100644
--- a/spec/inputs/metatable.yue
+++ b/spec/inputs/metatable.yue
@@ -1,6 +1,8 @@
1a = close: true, close#: => print "out of scope" 1a = close: true, close#: => print "out of scope"
2b = add#: (left, right)-> right - left 2b = add#: (left, right)-> right - left
3c = key1: true, :add#, key2: true 3c = key1: true, :add#, key2: true
4w = [name]#:123, ["new"]#:(val)=> {val}
5w.#["new"] w.#[name]
4 6
5close _ = close#: -> print "out of scope" 7close _ = close#: -> print "out of scope"
6 8
@@ -40,4 +42,10 @@ tb\func# list
40 42
41import "module" as :index#, newindex#:setFunc 43import "module" as :index#, newindex#:setFunc
42 44
45with tb
46 print .add#, .x\index# "key"
47 a = .index#.add#\new# 123
48 b = t#.close#.test
49 c = t #.close# .test
50
43nil 51nil
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h
index 535a239..b673eb3 100644
--- a/src/yuescript/yue_ast.h
+++ b/src/yuescript/yue_ast.h
@@ -431,7 +431,7 @@ AST_NODE(meta_variable_pair)
431AST_END(meta_variable_pair) 431AST_END(meta_variable_pair)
432 432
433AST_NODE(meta_normal_pair) 433AST_NODE(meta_normal_pair)
434 ast_sel<true, Name_t> key; 434 ast_sel<true, Name_t, Exp_t> key;
435 ast_sel<true, Exp_t, TableBlock_t> value; 435 ast_sel<true, Exp_t, TableBlock_t> value;
436 AST_MEMBER(meta_normal_pair, &key, &value) 436 AST_MEMBER(meta_normal_pair, &key, &value)
437AST_END(meta_normal_pair) 437AST_END(meta_normal_pair)
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 043c705..a958462 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -12,6 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
12#include <stack> 12#include <stack>
13#include <vector> 13#include <vector>
14#include <memory> 14#include <memory>
15#include <set>
15 16
16#include "yuescript/yue_parser.h" 17#include "yuescript/yue_parser.h"
17#include "yuescript/yue_compiler.h" 18#include "yuescript/yue_compiler.h"
@@ -58,7 +59,7 @@ inline std::string s(std::string_view sv) {
58 return std::string(sv); 59 return std::string(sv);
59} 60}
60 61
61const std::string_view version = "0.7.0"sv; 62const std::string_view version = "0.7.1"sv;
62const std::string_view extension = "yue"sv; 63const std::string_view extension = "yue"sv;
63 64
64class YueCompilerImpl { 65class YueCompilerImpl {
@@ -1549,11 +1550,20 @@ private:
1549 } 1550 }
1550 case id<meta_normal_pair_t>(): { 1551 case id<meta_normal_pair_t>(): {
1551 auto mp = static_cast<meta_normal_pair_t*>(item); 1552 auto mp = static_cast<meta_normal_pair_t*>(item);
1552 auto key = _parser.toString(mp->key);
1553 _buf << "__"sv << key;
1554 auto newKey = toAst<KeyName_t>(clearBuf(), item);
1555 auto newPair = item->new_ptr<normal_pair_t>(); 1553 auto newPair = item->new_ptr<normal_pair_t>();
1556 newPair->key.set(newKey); 1554 switch (mp->key->getId()) {
1555 case id<Name_t>(): {
1556 auto key = _parser.toString(mp->key);
1557 _buf << "__"sv << key;
1558 auto newKey = toAst<KeyName_t>(clearBuf(), mp->key);
1559 newPair->key.set(newKey);
1560 break;
1561 }
1562 case id<Exp_t>():
1563 newPair->key.set(mp->key);
1564 break;
1565 default: YUEE("AST node mismatch", mp->key); break;
1566 }
1557 newPair->value.set(mp->value); 1567 newPair->value.set(mp->value);
1558 subMetaDestruct->values.push_back(newPair); 1568 subMetaDestruct->values.push_back(newPair);
1559 break; 1569 break;
@@ -3167,6 +3177,9 @@ private:
3167 if (opIt == chainList.end()) return false; 3177 if (opIt == chainList.end()) return false;
3168 auto x = chainList.front(); 3178 auto x = chainList.front();
3169 auto chain = x->new_ptr<ChainValue_t>(); 3179 auto chain = x->new_ptr<ChainValue_t>();
3180 if (opIt == chainList.begin() && ast_is<ColonChainItem_t, DotChainItem_t>(x)) {
3181 chain->items.push_back(toAst<Callable_t>(_withVars.top(), x));
3182 }
3170 for (auto it = chainList.begin(); it != opIt; ++it) { 3183 for (auto it = chainList.begin(); it != opIt; ++it) {
3171 chain->items.push_back(*it); 3184 chain->items.push_back(*it);
3172 } 3185 }
@@ -5157,11 +5170,20 @@ private:
5157 case id<meta_normal_pair_t>(): { 5170 case id<meta_normal_pair_t>(): {
5158 isMetamethod = true; 5171 isMetamethod = true;
5159 auto mp = static_cast<meta_normal_pair_t*>(pair); 5172 auto mp = static_cast<meta_normal_pair_t*>(pair);
5160 auto key = _parser.toString(mp->key);
5161 _buf << "__"sv << key;
5162 auto newKey = toAst<KeyName_t>(clearBuf(), pair);
5163 auto newPair = pair->new_ptr<normal_pair_t>(); 5173 auto newPair = pair->new_ptr<normal_pair_t>();
5164 newPair->key.set(newKey); 5174 switch (mp->key->getId()) {
5175 case id<Name_t>(): {
5176 auto key = _parser.toString(mp->key);
5177 _buf << "__"sv << key;
5178 auto newKey = toAst<KeyName_t>(clearBuf(), mp->key);
5179 newPair->key.set(newKey);
5180 break;
5181 }
5182 case id<Exp_t>():
5183 newPair->key.set(mp->key);
5184 break;
5185 default: YUEE("AST node mismatch", mp->key); break;
5186 }
5165 newPair->value.set(mp->value); 5187 newPair->value.set(mp->value);
5166 metatable->pairs.push_back(newPair); 5188 metatable->pairs.push_back(newPair);
5167 break; 5189 break;
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index c72c005..ec6c997 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -525,7 +525,7 @@ YueParser::YueParser() {
525 525
526 meta_variable_pair = sym(':') >> Variable >> expr('#'); 526 meta_variable_pair = sym(':') >> Variable >> expr('#');
527 527
528 meta_normal_pair = Space >> Name >> expr("#:") >> 528 meta_normal_pair = (Space >> Name | sym('[') >> Exp >> sym(']')) >> expr("#:") >>
529 (Exp | TableBlock | +(SpaceBreak) >> Exp); 529 (Exp | TableBlock | +(SpaceBreak) >> Exp);
530 530
531 KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; 531 KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair;