From 05283b77da4ba236f02b42d1205433f6b1fcb0d6 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 5 Mar 2021 10:14:10 +0800 Subject: fix issue #41, add square brackets support for metatable syntax. fix using metatable with "with" syntax issue. --- spec/inputs/metatable.yue | 8 ++++++++ src/yuescript/yue_ast.h | 2 +- src/yuescript/yue_compiler.cpp | 40 +++++++++++++++++++++++++++++++--------- src/yuescript/yue_parser.cpp | 2 +- 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 @@ 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] close _ = close#: -> print "out of scope" @@ -40,4 +42,10 @@ tb\func# list import "module" as :index#, newindex#:setFunc +with tb + print .add#, .x\index# "key" + a = .index#.add#\new# 123 + b = t#.close#.test + c = t #.close# .test + nil 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) AST_END(meta_variable_pair) AST_NODE(meta_normal_pair) - ast_sel key; + ast_sel key; ast_sel value; AST_MEMBER(meta_normal_pair, &key, &value) AST_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 #include #include #include +#include #include "yuescript/yue_parser.h" #include "yuescript/yue_compiler.h" @@ -58,7 +59,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.7.0"sv; +const std::string_view version = "0.7.1"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1549,11 +1550,20 @@ private: } case id(): { auto mp = static_cast(item); - auto key = _parser.toString(mp->key); - _buf << "__"sv << key; - auto newKey = toAst(clearBuf(), item); auto newPair = item->new_ptr(); - newPair->key.set(newKey); + switch (mp->key->getId()) { + case id(): { + auto key = _parser.toString(mp->key); + _buf << "__"sv << key; + auto newKey = toAst(clearBuf(), mp->key); + newPair->key.set(newKey); + break; + } + case id(): + newPair->key.set(mp->key); + break; + default: YUEE("AST node mismatch", mp->key); break; + } newPair->value.set(mp->value); subMetaDestruct->values.push_back(newPair); break; @@ -3167,6 +3177,9 @@ private: if (opIt == chainList.end()) return false; auto x = chainList.front(); auto chain = x->new_ptr(); + if (opIt == chainList.begin() && ast_is(x)) { + chain->items.push_back(toAst(_withVars.top(), x)); + } for (auto it = chainList.begin(); it != opIt; ++it) { chain->items.push_back(*it); } @@ -5157,11 +5170,20 @@ private: case id(): { isMetamethod = true; auto mp = static_cast(pair); - auto key = _parser.toString(mp->key); - _buf << "__"sv << key; - auto newKey = toAst(clearBuf(), pair); auto newPair = pair->new_ptr(); - newPair->key.set(newKey); + switch (mp->key->getId()) { + case id(): { + auto key = _parser.toString(mp->key); + _buf << "__"sv << key; + auto newKey = toAst(clearBuf(), mp->key); + newPair->key.set(newKey); + break; + } + case id(): + newPair->key.set(mp->key); + break; + default: YUEE("AST node mismatch", mp->key); break; + } newPair->value.set(mp->value); metatable->pairs.push_back(newPair); 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() { meta_variable_pair = sym(':') >> Variable >> expr('#'); - meta_normal_pair = Space >> Name >> expr("#:") >> + meta_normal_pair = (Space >> Name | sym('[') >> Exp >> sym(']')) >> expr("#:") >> (Exp | TableBlock | +(SpaceBreak) >> Exp); KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; -- cgit v1.2.3-55-g6feb