From c98c6053635ddfca7aab15b268b0f2c1fcc0c6ef Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 29 Jun 2023 10:58:56 +0800 Subject: fix issue #139. --- src/yuescript/ast.cpp | 10 +- src/yuescript/ast.hpp | 34 +- src/yuescript/yue_ast.cpp | 1239 ++++++++++++++++++++++++++++++++++++++++ src/yuescript/yue_ast.h | 28 +- src/yuescript/yue_compiler.cpp | 259 ++++----- src/yuescript/yue_parser.h | 296 +++++----- src/yuescript/yuescript.cpp | 6 +- 7 files changed, 1557 insertions(+), 315 deletions(-) create mode 100644 src/yuescript/yue_ast.cpp (limited to 'src') diff --git a/src/yuescript/ast.cpp b/src/yuescript/ast.cpp index 6db2c21..34ff5d9 100644 --- a/src/yuescript/ast.cpp +++ b/src/yuescript/ast.cpp @@ -19,14 +19,14 @@ traversal ast_node::traverse(const std::function& func) { return func(this); } -ast_node* ast_node::getByTypeIds(int* begin, int* end) { +ast_node* ast_node::get_by_type_ids(int* begin, int* end) { ast_node* current = this; auto it = begin; while (it != end) { ast_node* findNode = nullptr; int i = *it; - current->visitChild([&](ast_node* node) { - if (node->getId() == i) { + current->visit_child([&](ast_node* node) { + if (node->get_id() == i) { findNode = node; return true; } @@ -43,7 +43,7 @@ ast_node* ast_node::getByTypeIds(int* begin, int* end) { return current; } -bool ast_node::visitChild(const std::function&) { +bool ast_node::visit_child(const std::function&) { return false; } @@ -92,7 +92,7 @@ traversal ast_container::traverse(const std::function& fun return traversal::Continue; } -bool ast_container::visitChild(const std::function& func) { +bool ast_container::visit_child(const std::function& func) { const auto& members = this->members(); for (auto member : members) { switch (member->get_type()) { diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index f59b50f..c78d26c 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp @@ -98,16 +98,18 @@ public: using select_last_t = typename select_last::type; template - select_last_t* getByPath() { + select_last_t* get_by_path() { int types[] = {id()...}; - return static_cast*>(getByTypeIds(std::begin(types), std::end(types))); + return static_cast*>(get_by_type_ids(std::begin(types), std::end(types))); } - virtual bool visitChild(const std::function& func); + virtual bool visit_child(const std::function& func); - virtual int getId() const = 0; + virtual int get_id() const = 0; - virtual const std::string_view getName() const = 0; + virtual const std::string_view get_name() const = 0; + + virtual std::string to_string(void*) const { return {}; } template inline ast_ptr new_ptr() const { @@ -121,7 +123,7 @@ public: private: int _ref; - ast_node* getByTypeIds(int* begin, int* end); + ast_node* get_by_type_ids(int* begin, int* end); }; template @@ -130,12 +132,12 @@ id() { return 0; } template T* ast_cast(ast_node* node) { - return node && id() == node->getId() ? static_cast(node) : nullptr; + return node && id() == node->get_id() ? static_cast(node) : nullptr; } template T* ast_to(ast_node* node) { - assert(node->getId() == id()); + assert(node->get_id() == id()); return static_cast(node); } @@ -143,7 +145,7 @@ template bool ast_is(ast_node* node) { if (!node) return false; bool result = false; - int i = node->getId(); + int i = node->get_id(); using swallow = bool[]; (void)swallow{result || (result = id() == i)...}; return result; @@ -181,7 +183,7 @@ public: virtual traversal traverse(const std::function& func) override; - virtual bool visitChild(const std::function& func) override; + virtual bool visit_child(const std::function& func) override; private: ast_member_vector m_members; @@ -235,13 +237,13 @@ public: template T* to() const { - assert(m_ptr && m_ptr->getId() == id()); + assert(m_ptr && m_ptr->get_id() == id()); return static_cast(m_ptr); } template bool is() const { - return m_ptr && m_ptr->getId() == id(); + return m_ptr && m_ptr->get_id() == id(); } void set(ast_node* node) { @@ -333,7 +335,7 @@ public: private: virtual bool accept(ast_node* node) override { - return node && (std::is_same() || id() == node->getId()); + return node && (std::is_same() || id() == node->get_id()); } }; @@ -380,7 +382,7 @@ private: if (!node) return false; using swallow = bool[]; bool result = false; - (void)swallow{result || (result = id() == node->getId())...}; + (void)swallow{result || (result = id() == node->get_id())...}; return result; } }; @@ -515,7 +517,7 @@ public: private: virtual bool accept(ast_node* node) override { - return node && (std::is_same() || id() == node->getId()); + return node && (std::is_same() || id() == node->get_id()); } }; @@ -557,7 +559,7 @@ private: if (!node) return false; using swallow = bool[]; bool result = false; - (void)swallow{result || (result = id() == node->getId())...}; + (void)swallow{result || (result = id() == node->get_id())...}; return result; } }; diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp new file mode 100644 index 0000000..d9f8419 --- /dev/null +++ b/src/yuescript/yue_ast.cpp @@ -0,0 +1,1239 @@ +/* Copyright (c) 2023 Jin Li, dragon-fly@qq.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#include "yuescript/yue_ast.h" + +#include + +namespace parserlib { +using namespace std::string_view_literals; +using namespace std::string_literals; + +void YueFormat::pushScope() { + indent++; +} + +void YueFormat::popScope() { + indent--; +} + +std::string YueFormat::ind() const { + if (spaceOverTab) { + return std::string(indent * tabSpaces, ' '); + } + return std::string(indent, '\t'); +} + +std::string YueFormat::convert(const ast_node* node) { + return converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); +} + +std::string YueFormat::toString(ast_node* node) { + return node->to_string(this); +} + +typedef std::list str_list; + +static std::string join(const str_list& items, std::string_view sep = {}) { + if (items.empty()) + return {}; + else if (items.size() == 1) + return items.front(); + std::ostringstream joinBuf; + if (sep.empty()) { + for (const auto& item : items) { + joinBuf << item; + } + } else { + joinBuf << items.front(); + auto begin = ++items.begin(); + for (auto it = begin; it != items.end(); ++it) { + joinBuf << sep << *it; + } + } + return joinBuf.str(); +} + +namespace yue { + +std::string Num_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string Name_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string Self_t::to_string(void*) const { + return "@"s; +} +std::string SelfClass_t::to_string(void*) const { + return "@@"s; +} +std::string VarArg_t::to_string(void*) const { + return "..."s; +} +std::string Seperator_t::to_string(void*) const { + return {}; +} +std::string LocalFlag_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string ConstAttrib_t::to_string(void*) const { + return "const"s; +} +std::string CloseAttrib_t::to_string(void*) const { + return "close"s; +} +std::string ImportLiteralInner_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string ImportAllMacro_t::to_string(void*) const { + return "$"s; +} +std::string FnArrowBack_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string IfType_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string WhileType_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string UpdateOp_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string BinaryOperator_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string UnaryOperator_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto op = info->convert(this); + if (op == "not"sv) { + return "not "s; + } + return op; +} +std::string LuaStringOpen_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string LuaStringContent_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string LuaStringClose_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string SingleString_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string DoubleStringInner_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string Metatable_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string DefaultValue_t::to_string(void*) const { + return {}; +} +std::string ExistentialOp_t::to_string(void*) const { + return "?"s; +} +std::string TableAppendingOp_t::to_string(void*) const { + return "[]"s; +} +std::string GlobalOp_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string ExportDefault_t::to_string(void*) const { + return "default"s; +} +std::string FnArrow_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string ConstValue_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string InRangeOpen_t::to_string(void*) const { + return {}; +} +std::string InRangeClose_t::to_string(void*) const { + return {}; +} +std::string NotIn_t::to_string(void*) const { + return {}; +} +std::string BreakLoop_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return info->convert(this); +} +std::string YueLineComment_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return "--"s + info->convert(this); +} +std::string MultilineCommentInner_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + return "--[["s + info->convert(this) + "]]"s; +} +std::string Variable_t::to_string(void* ud) const { + return name->to_string(ud); +} +std::string LabelName_t::to_string(void* ud) const { + return name->to_string(ud); +} +std::string LuaKeyword_t::to_string(void* ud) const { + return name->to_string(ud); +} +std::string SelfName_t::to_string(void* ud) const { + return "@"s + name->to_string(ud); +} +std::string SelfClassName_t::to_string(void* ud) const { + return "@@"s + name->to_string(ud); +} +std::string SelfItem_t::to_string(void* ud) const { + return name->to_string(ud); +} +std::string KeyName_t::to_string(void* ud) const { + return name->to_string(ud); +} +std::string NameList_t::to_string(void* ud) const { + str_list temp; + for (auto name : names.objects()) { + temp.emplace_back(name->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string LocalValues_t::to_string(void* ud) const { + str_list temp; + temp.emplace_back(nameList->to_string(ud)); + if (valueList) { + if (valueList.is()) { + temp.emplace_back("="s + valueList->to_string(ud)); + } else { + temp.emplace_back("="s); + temp.emplace_back(valueList->to_string(ud)); + } + } + return join(temp, " "sv); +} +std::string Local_t::to_string(void* ud) const { + return "local "s + item->to_string(ud); +} +std::string LocalAttrib_t::to_string(void* ud) const { + str_list temp; + for (auto item : leftList.objects()) { + temp.emplace_back(item->to_string(ud)); + } + return attrib->to_string(ud) + ' ' + join(temp, ", "s) + ' ' + assign->to_string(ud); +} +std::string ColonImportName_t::to_string(void* ud) const { + return '\\' + name->to_string(ud); +} +std::string ImportLiteral_t::to_string(void* ud) const { + str_list temp; + for (auto inner : inners.objects()) { + temp.emplace_back(inner->to_string(ud)); + } + return '"' + join(temp, "."sv) + '"'; +} +std::string ImportFrom_t::to_string(void* ud) const { + str_list temp; + for (auto name : names.objects()) { + temp.emplace_back(name->to_string(ud)); + } + return join(temp, ", "sv) + " from "s + exp->to_string(ud); +} +std::string MacroNamePair_t::to_string(void* ud) const { + return key->to_string(ud) + ": "s + value->to_string(ud); +} +std::string ImportTabLit_t::to_string(void* ud) const { + str_list temp; + for (auto item : items.objects()) { + if (ast_is(item)) { + temp.emplace_back(':' + item->to_string(ud)); + } else { + temp.emplace_back(item->to_string(ud)); + } + } + return '{' + join(temp, ", "sv) + '}'; +} +std::string ImportAs_t::to_string(void* ud) const { + str_list temp{literal->to_string(ud)}; + if (target) { + temp.emplace_back("as"s); + temp.emplace_back(target->to_string(ud)); + } + return join(temp, " "s); +} +std::string Import_t::to_string(void* ud) const { + return "import "s + content->to_string(ud); +} +std::string Label_t::to_string(void* ud) const { + return "::"s + label->to_string(ud) + "::"s; +} +std::string Goto_t::to_string(void* ud) const { + return "goto "s + label->to_string(ud); +} +std::string ShortTabAppending_t::to_string(void* ud) const { + return "[] "s + assign->to_string(ud); +} +std::string Backcall_t::to_string(void* ud) const { + str_list temp; + if (argsDef) { + temp.emplace_back(argsDef->to_string(ud)); + } + temp.emplace_back(arrow->to_string(ud)); + temp.emplace_back(value->to_string(ud)); + return join(temp, " "sv); +} +std::string PipeBody_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + for (auto value : values.objects()) { + temp.emplace_back(info->ind() + "|> "s + value->to_string(ud)); + } + return join(temp, "\n"sv); +} +std::string ExpListLow_t::to_string(void* ud) const { + str_list temp; + for (auto exp : exprs.objects()) { + temp.emplace_back(exp->to_string(ud)); + } + return join(temp, "; "sv); +} +std::string ExpList_t::to_string(void* ud) const { + str_list temp; + for (auto exp : exprs.objects()) { + temp.emplace_back(exp->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string Return_t::to_string(void* ud) const { + str_list temp{"return"s}; + if (valueList) { + temp.emplace_back(valueList.is() ? ""s : " "s); + temp.emplace_back(valueList->to_string(ud)); + } + return join(temp); +} +std::string With_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp{ + eop ? "with?"s : "with"s, + valueList->to_string(ud)}; + if (assigns) { + temp.push_back(assigns->to_string(ud)); + } + if (body.is()) { + return join(temp, " "sv) + " do "s + body->to_string(ud); + } else { + auto line = join(temp, " "sv); + if (line.find('\n') != std::string::npos) { + line += " do"s; + } + info->pushScope(); + auto code = body->to_string(ud); + info->popScope(); + return line + '\n' + code; + } +} +std::string SwitchList_t::to_string(void* ud) const { + str_list temp; + for (auto exp : exprs.objects()) { + temp.emplace_back(exp->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string SwitchCase_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + if (body.is()) { + return "when "s + condition->to_string(ud) + " then "s + body->to_string(ud); + } else { + info->pushScope(); + auto block = body->to_string(ud); + info->popScope(); + auto line = "when "s + condition->to_string(ud); + if (line.find('\n') != std::string::npos) { + line += " then"s; + } + return line + '\n' + block; + } +} +std::string Switch_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp{"switch "s + target->to_string(ud)}; + info->pushScope(); + for (auto branch : branches.objects()) { + temp.emplace_back(info->ind() + branch->to_string(ud)); + } + if (lastBranch) { + if (lastBranch.is()) { + temp.emplace_back(info->ind() + "else "s + lastBranch->to_string(ud)); + } else { + temp.emplace_back(info->ind() + "else"s); + info->pushScope(); + temp.emplace_back(lastBranch->to_string(ud)); + info->popScope(); + } + } + info->popScope(); + return join(temp, "\n"sv); +} +std::string Assignment_t::to_string(void* ud) const { + return expList->to_string(ud) + ' ' + assign->to_string(ud); +} +std::string IfCond_t::to_string(void* ud) const { + return condition->to_string(ud); +} +std::string If_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto it = nodes.objects().begin(); + str_list temp{ + type->to_string(ud) + ' ' + (*it)->to_string(ud)}; + if (temp.back().find('\n') != std::string::npos) { + temp.back() += " then"s; + } + ++it; + bool condition = true; + for (; it != nodes.objects().end(); ++it) { + auto node = *it; + switch (node->get_id()) { + case id(): + temp.emplace_back(info->ind() + "elseif "s + node->to_string(ud)); + condition = true; + break; + case id(): { + if (condition) { + temp.back() += " then "s + node->to_string(ud); + } else { + temp.emplace_back(info->ind() + "else "s + node->to_string(ud)); + } + condition = false; + break; + } + case id(): { + if (condition) { + info->pushScope(); + temp.emplace_back(node->to_string(ud)); + info->popScope(); + } else { + temp.emplace_back(info->ind() + "else"s); + info->pushScope(); + temp.emplace_back(node->to_string(ud)); + info->popScope(); + } + condition = false; + break; + } + } + } + return join(temp, "\n"sv); +} +std::string While_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp{ + type->to_string(ud) + ' ' + condition->to_string(ud)}; + if (body.is()) { + temp.back() += " do "s + body->to_string(ud); + } else { + if (temp.back().find('\n') != std::string::npos) { + temp.back() += " do"s; + } + info->pushScope(); + temp.emplace_back(body->to_string(ud)); + info->popScope(); + } + return join(temp, "\n"sv); +} +std::string Repeat_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + if (body->content.is()) { + temp.emplace_back("repeat "s + body->to_string(ud)); + } else { + temp.emplace_back("repeat"s); + info->pushScope(); + temp.emplace_back(body->to_string(ud)); + info->popScope(); + } + temp.emplace_back(info->ind() + "until "s + condition->to_string(ud)); + return join(temp, "\n"sv); +} +std::string ForStepValue_t::to_string(void* ud) const { + return value->to_string(ud); +} +std::string For_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto line = "for "s + varName->to_string(ud) + " = "s + startValue->to_string(ud) + ", "s + stopValue->to_string(ud); + if (stepValue) { + line += ", "s + stepValue->to_string(ud); + } + if (body.is()) { + return line + " do "s + body->to_string(ud); + } else { + if (line.find('\n') != std::string::npos) { + line += " do"s; + } + info->pushScope(); + auto block = body->to_string(ud); + info->popScope(); + return line + '\n' + block; + } +} +std::string ForEach_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto line = "for "s + nameList->to_string(ud) + " in "s + loopValue->to_string(ud); + if (body.is()) { + return line + " do "s + body->to_string(ud); + } else { + if (line.find('\n') != std::string::npos) { + line += " do"s; + } + info->pushScope(); + auto block = body->to_string(ud); + info->popScope(); + return line + '\n' + block; + } +} +std::string Do_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + if (body->content.is()) { + return "do "s + body->to_string(ud); + } else { + info->pushScope(); + auto block = body->to_string(ud); + info->popScope(); + return "do\n"s + block; + } +} +std::string CatchBlock_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto line = "catch "s + err->to_string(ud); + info->pushScope(); + auto block = body->to_string(ud); + info->popScope(); + return line + '\n' + block; +} +std::string Try_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + if (func.is()) { + temp.emplace_back("try "s + func->to_string(ud)); + } else { + temp.emplace_back("try"s); + info->pushScope(); + temp.emplace_back(func->to_string(ud)); + info->popScope(); + } + if (catchBlock) { + temp.emplace_back(info->ind() + catchBlock->to_string(ud)); + } + return join(temp, "\n"sv); +} +std::string Comprehension_t::to_string(void* ud) const { + auto valueStr = value->to_string(ud); + return '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ' ' + forLoop->to_string(ud) + ']'; +} +std::string CompValue_t::to_string(void* ud) const { + return value->to_string(ud); +} +std::string TblComprehension_t::to_string(void* ud) const { + auto line = '{' + key->to_string(ud); + if (value) { + line += ", "s + value->to_string(ud); + } + return line + ' ' + forLoop->to_string(ud) + '}'; +} +std::string StarExp_t::to_string(void* ud) const { + return '*' + value->to_string(ud); +} +std::string CompForEach_t::to_string(void* ud) const { + return "for "s + nameList->to_string(ud) + " in "s + loopValue->to_string(ud); +} +std::string CompFor_t::to_string(void* ud) const { + auto line = "for "s + varName->to_string(ud) + " = "s + startValue->to_string(ud) + ", "s + stopValue->to_string(ud); + if (stepValue) { + line += stepValue->to_string(ud); + } + return line; +} +std::string CompInner_t::to_string(void* ud) const { + str_list temp; + for (auto item : items.objects()) { + if (ast_is(item)) { + temp.emplace_back("when "s + item->to_string(ud)); + } else { + temp.emplace_back(item->to_string(ud)); + } + } + return join(temp, " "sv); +} +std::string Assign_t::to_string(void* ud) const { + str_list temp; + if (values.size() == 1 && ast_is(values.front())) { + return '=' + values.front()->to_string(ud); + } + for (auto value : values.objects()) { + temp.emplace_back(value->to_string(ud)); + } + return "= "s + join(temp, "; "sv); +} +std::string Update_t::to_string(void* ud) const { + return op->to_string(ud) + "= "s + value->to_string(ud); +} +std::string Assignable_t::to_string(void* ud) const { + return item->to_string(ud); +} +std::string AssignableChain_t::to_string(void* ud) const { + str_list temp; + for (auto item : items.objects()) { + if (ast_is(item)) { + auto valueStr = item->to_string(ud); + temp.emplace_back('[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'); + } else { + temp.emplace_back(item->to_string(ud)); + } + } + return join(temp); +} +std::string ExpOpValue_t::to_string(void* ud) const { + str_list pipes; + for (auto uexp : pipeExprs.objects()) { + pipes.emplace_back(uexp->to_string(ud)); + } + return op->to_string(ud) + ' ' + join(pipes, " |> "sv); +} +std::string Exp_t::to_string(void* ud) const { + str_list pipes; + for (auto uexp : pipeExprs.objects()) { + pipes.emplace_back(uexp->to_string(ud)); + } + str_list temp{join(pipes, " |> "sv)}; + for (auto opValue : opValues.objects()) { + temp.emplace_back(opValue->to_string(ud)); + } + if (nilCoalesed) { + temp.emplace_back("??"s); + temp.emplace_back(nilCoalesed->to_string(ud)); + } + return join(temp, " "sv); +} +std::string Callable_t::to_string(void* ud) const { + return item->to_string(ud); +} +std::string ChainValue_t::to_string(void* ud) const { + str_list temp; + bool isMultilineChain = false; + for (auto item : items.objects()) { + if (item != items.back() && ast_is(item)) { + isMultilineChain = true; + break; + } + } + if (isMultilineChain) { + auto it = items.objects().begin(); + auto node = *it; + if (ast_is(node)) { + auto valueStr = node->to_string(ud); + temp.emplace_back('[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'); + } else { + temp.emplace_back(node->to_string(ud)); + } + ++it; + auto info = reinterpret_cast(ud); + info->pushScope(); + for (; it != items.objects().end(); ++it) { + node = *it; + switch (node->get_id()) { + case id(): { + auto valueStr = node->to_string(ud); + temp.emplace_back(info->ind() + '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'); + break; + } + case id(): + case id(): + temp.back() += node->to_string(ud); + break; + default: + temp.emplace_back(info->ind() + node->to_string(ud)); + break; + } + } + info->popScope(); + return join(temp, "\n"sv); + } else { + for (auto item : items.objects()) { + if (ast_is(item)) { + auto valueStr = item->to_string(ud); + temp.emplace_back('[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'); + } else { + temp.emplace_back(item->to_string(ud)); + } + } + return join(temp); + } +} +std::string SimpleTable_t::to_string(void* ud) const { + str_list temp; + for (auto pair : pairs.objects()) { + temp.emplace_back(pair->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string SimpleValue_t::to_string(void* ud) const { + return value->to_string(ud); +} +std::string Value_t::to_string(void* ud) const { + return item->to_string(ud); +} +std::string LuaString_t::to_string(void* ud) const { + auto str = content->to_string(ud); + auto newLine = str.find_first_of("\n"s) != std::string::npos ? "\n"s : ""s; + return open->to_string(ud) + newLine + str + close->to_string(ud); +} +std::string DoubleStringContent_t::to_string(void* ud) const { + if (content.is()) { + return "#{"s + content->to_string(ud) + '}'; + } + return content->to_string(ud); +} +std::string DoubleString_t::to_string(void* ud) const { + str_list temp; + for (auto seg : segments.objects()) { + temp.emplace_back(seg->to_string(ud)); + } + return '"' + join(temp) + '"'; +} +std::string String_t::to_string(void* ud) const { + return str->to_string(ud); +} +std::string Parens_t::to_string(void* ud) const { + return '(' + expr->to_string(ud) + ')'; +} +std::string DotChainItem_t::to_string(void* ud) const { + return '.' + name->to_string(ud); +} +std::string ColonChainItem_t::to_string(void* ud) const { + return '\\' + name->to_string(ud); +} +std::string Metamethod_t::to_string(void* ud) const { + if (item.is()) { + auto valueStr = item->to_string(ud); + return "<["s + (valueStr[0] == '[' ? " "s : ""s) + valueStr + "]>"s; + } else { + return '<' + item->to_string(ud) + '>'; + } +} +std::string Slice_t::to_string(void* ud) const { + str_list temp; + if (startValue.is()) { + temp.emplace_back(startValue->to_string(ud)); + } + temp.emplace_back(", "s); + if (stopValue.is()) { + temp.emplace_back(stopValue->to_string(ud)); + } + if (stepValue.is()) { + temp.emplace_back(", "s + stepValue->to_string(ud)); + } + auto valueStr = join(temp); + return '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'; +} +static bool isInBlockExp(ast_node* node) { + if (auto exp = ast_cast(node)) { + auto unaryExp = static_cast(exp->pipeExprs.front()); + auto value = static_cast(unaryExp->expos.front()); + if (auto simpleValue = value->item.as()) { + if (!ast_is(simpleValue->value)) { + return true; + } + } + } + return false; +} +std::string Invoke_t::to_string(void* ud) const { + if (args.empty()) { + return "!"s; + } else if (args.size() == 1) { + auto arg = args.front(); + if (ast_is(arg)) { + return '(' + arg->to_string(ud) + ')'; + } else { + return arg->to_string(ud); + } + } else { + auto info = reinterpret_cast(ud); + bool hasInBlockExp = false; + for (auto arg : args.objects()) { + if (arg != args.back() && isInBlockExp(arg)) { + hasInBlockExp = true; + break; + } + } + str_list temp; + if (hasInBlockExp) { + temp.push_back("("s); + info->pushScope(); + for (auto arg : args.objects()) { + temp.emplace_back(info->ind() + arg->to_string(ud)); + } + info->popScope(); + temp.push_back(info->ind() + ')'); + return join(temp, "\n"sv); + } else { + for (auto arg : args.objects()) { + temp.emplace_back(arg->to_string(ud)); + } + return '(' + join(temp, ", "sv) + ')'; + } + } +} +std::string SpreadExp_t::to_string(void* ud) const { + return "..."s + exp->to_string(ud); +} +std::string TableLit_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + if (values.empty()) { + return "{ }"s; + } + str_list temp; + temp.emplace_back("{"s); + info->pushScope(); + for (auto value : values.objects()) { + temp.emplace_back(info->ind() + value->to_string(ud)); + } + info->popScope(); + temp.emplace_back(info->ind() + '}'); + return join(temp, "\n"sv); +} +std::string TableBlock_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + info->pushScope(); + for (auto value : values.objects()) { + switch (value->get_id()) { + case id(): + case id(): + temp.emplace_back(info->ind() + "* "s + value->to_string(ud)); + break; + case id(): + temp.emplace_back(info->ind() + "*"s + value->to_string(ud)); + break; + default: + temp.emplace_back(info->ind() + value->to_string(ud)); + break; + } + } + info->popScope(); + return '\n' + join(temp, "\n"sv); +} +std::string TableBlockIndent_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + info->pushScope(); + for (auto value : values.objects()) { + if (value == values.front()) { + temp.emplace_back("* "s + value->to_string(ud)); + } else { + temp.emplace_back(info->ind() + value->to_string(ud)); + } + } + info->popScope(); + return join(temp, "\n"sv); +} +std::string ClassMemberList_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + for (auto value : values.objects()) { + temp.emplace_back(info->ind() + value->to_string(ud)); + } + return join(temp, "\n"sv); +} +std::string ClassBlock_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + info->pushScope(); + for (auto content : contents.objects()) { + if (ast_is(content)) { + temp.emplace_back(info->ind() + content->to_string(ud)); + } else { + temp.emplace_back(content->to_string(ud)); + } + } + info->popScope(); + return '\n' + join(temp, "\n"sv); +} +std::string ClassDecl_t::to_string(void* ud) const { + auto line = "class"s; + if (name) { + line += ' ' + name->to_string(ud); + } + if (extend) { + line += " extends "s + extend->to_string(ud); + } + if (mixes) { + line += " using "s + mixes->to_string(ud); + } + if (body) { + line += body->to_string(ud); + } + return line; +} +std::string GlobalValues_t::to_string(void* ud) const { + auto line = nameList->to_string(ud); + if (valueList) { + if (valueList.is()) { + line += " ="s + valueList->to_string(ud); + } else { + line += " = "s + valueList->to_string(ud); + } + } + return line; +} +std::string Global_t::to_string(void* ud) const { + return "global "s + item->to_string(ud); +} +std::string Export_t::to_string(void* ud) const { + auto line = "export"s; + if (def) { + line += " default "s; + } + switch (target->get_id()) { + case id(): + line += target->to_string(ud); + break; + case id(): { + auto valueStr = target->to_string(ud); + line += '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'; + break; + } + default: + line += ' ' + target->to_string(ud); + break; + } + if (assign) { + line += ' ' + assign->to_string(ud); + } + return line; +} +std::string VariablePair_t::to_string(void* ud) const { + return ':' + name->to_string(ud); +} +std::string NormalPair_t::to_string(void* ud) const { + std::string line; + if (key.is()) { + auto valueStr = key->to_string(ud); + line = '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + "]:"s; + } else { + line = key->to_string(ud) + ":"s; + } + line += (value.is() ? ""s : " "s) + value->to_string(ud); + return line; +} +std::string MetaVariablePair_t::to_string(void* ud) const { + return ":<"s + name->to_string(ud) + '>'; +} +std::string MetaNormalPair_t::to_string(void* ud) const { + std::string line; + if (!key) { + line = "<>:"s; + } else if (key.is()) { + auto valueStr = key->to_string(ud); + line = "<["s + (valueStr[0] == '[' ? " "s : ""s) + valueStr + "]>:"s; + } else { + line = '<' + key->to_string(ud) + ">:"s; + } + line += (value.is() ? ""s : " "s) + value->to_string(ud); + return line; +} +std::string VariablePairDef_t::to_string(void* ud) const { + if (defVal) { + return pair->to_string(ud) + " = "s + defVal->to_string(ud); + } else { + return pair->to_string(ud); + } +} +std::string NormalPairDef_t::to_string(void* ud) const { + if (defVal) { + return pair->to_string(ud) + " = "s + defVal->to_string(ud); + } else { + return pair->to_string(ud); + } +} +std::string NormalDef_t::to_string(void* ud) const { + if (defVal) { + return item->to_string(ud) + " = "s + defVal->to_string(ud); + } else { + return item->to_string(ud); + } +} +std::string MetaVariablePairDef_t::to_string(void* ud) const { + if (defVal) { + return pair->to_string(ud) + " = "s + defVal->to_string(ud); + } else { + return pair->to_string(ud); + } +} +std::string MetaNormalPairDef_t::to_string(void* ud) const { + if (defVal) { + return pair->to_string(ud) + " = "s + defVal->to_string(ud); + } else { + return pair->to_string(ud); + } +} +std::string FnArgDef_t::to_string(void* ud) const { + auto line = name->to_string(ud); + if (op) { + line += op->to_string(ud); + } + if (defaultValue) { + if (isInBlockExp(defaultValue)) { + line += " = ("s + defaultValue->to_string(ud) + ')'; + } else { + line += " = "s + defaultValue->to_string(ud); + } + } + return line; +} +std::string FnArgDefList_t::to_string(void* ud) const { + str_list temp; + for (auto def : definitions.objects()) { + temp.emplace_back(def->to_string(ud)); + } + if (varArg) { + temp.emplace_back(varArg->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string OuterVarShadow_t::to_string(void* ud) const { + if (varList) { + return "using "s + varList->to_string(ud); + } else { + return "using nil"s; + } +} +std::string FnArgsDef_t::to_string(void* ud) const { + std::string line; + if (defList) { + line += defList->to_string(ud); + } + if (shadowOption) { + line += (line.empty() ? ""s : " "s) + shadowOption->to_string(ud); + } + return '(' + line + ')'; +} +std::string FunLit_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + std::string line; + if (argsDef) { + line = argsDef->to_string(ud); + } + line += arrow->to_string(ud); + if (body) { + if (body->content.is()) { + line += ' ' + body->to_string(ud); + } else { + info->pushScope(); + line += '\n' + body->to_string(ud); + info->popScope(); + } + } + return line; +} +std::string MacroName_t::to_string(void* ud) const { + return '$' + name->to_string(ud); +} +std::string MacroLit_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + std::string line; + if (argsDef) { + line = '(' + argsDef->to_string(ud) + ')'; + } + line += "->"s; + if (body) { + if (body->content.is()) { + line += ' ' + body->to_string(ud); + } else { + info->pushScope(); + line += '\n' + body->to_string(ud); + info->popScope(); + } + } + return line; +} +std::string Macro_t::to_string(void* ud) const { + return "macro "s + name->to_string(ud) + " = "s + macroLit->to_string(ud); +} +std::string MacroInPlace_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + auto line = "$->"s; + if (body) { + if (body->content.is()) { + line += ' ' + body->to_string(ud); + } else { + info->pushScope(); + line += '\n' + body->to_string(ud); + info->popScope(); + } + } + return line; +} +std::string NameOrDestructure_t::to_string(void* ud) const { + return item->to_string(ud); +} +std::string AssignableNameList_t::to_string(void* ud) const { + str_list temp; + for (auto item : items.objects()) { + temp.emplace_back(item->to_string(ud)); + } + return join(temp, ", "sv); +} +std::string InvokeArgs_t::to_string(void* ud) const { + if (!args.empty() && ast_is(args.back())) { + str_list temp; + for (auto arg : args.objects()) { + if (arg != args.back()) { + temp.emplace_back(arg->to_string(ud)); + } + } + auto list = join(temp, ", "sv); + return ' ' + list + (list.empty() ? ""s : ","s) + args.back()->to_string(ud); + } else { + str_list temp; + for (auto arg : args.objects()) { + temp.emplace_back(arg->to_string(ud)); + } + return ' ' + join(temp, ", "sv); + } +} +std::string UnaryValue_t::to_string(void* ud) const { + std::string line; + for (auto op : ops.objects()) { + line += op->to_string(ud); + } + line += value->to_string(ud); + return line; +} +std::string UnaryExp_t::to_string(void* ud) const { + std::string line; + for (auto op : ops.objects()) { + line += op->to_string(ud); + } + str_list temp; + for (auto expo : expos.objects()) { + temp.push_back(expo->to_string(ud)); + } + line += join(temp, "^"sv); + if (inExp) { + line += ' ' + inExp->to_string(ud); + } + return line; +} +std::string InRange_t::to_string(void* ud) const { + auto valueStr = openValue->to_string(ud); + return "in "s + (open.is() ? "("s : "["s + (valueStr[0] == '[' ? " "s : ""s)) + valueStr + ", "s + closeValue->to_string(ud) + (close.is() ? ')' : ']'); +} +std::string InDiscrete_t::to_string(void* ud) const { + str_list temp; + for (auto value : values.objects()) { + temp.emplace_back(value->to_string(ud)); + } + return "in "s + '{' + join(temp, ", "sv) + '}'; +} +std::string In_t::to_string(void* ud) const { + if (not_) { + return "not "s + item->to_string(ud); + } else { + return item->to_string(ud); + } +} +std::string ExpListAssign_t::to_string(void* ud) const { + if (action) { + return expList->to_string(ud) + ' ' + action->to_string(ud); + } else { + return expList->to_string(ud); + } +} +std::string IfLine_t::to_string(void* ud) const { + return type->to_string(ud) + ' ' + condition->to_string(ud); +} +std::string WhileLine_t::to_string(void* ud) const { + return type->to_string(ud) + ' ' + condition->to_string(ud); +} +std::string StatementAppendix_t::to_string(void* ud) const { + return item->to_string(ud); +} +std::string Statement_t::to_string(void* ud) const { + std::string line; + if (appendix) { + return content->to_string(ud) + ' ' + appendix->to_string(ud); + } else { + return content->to_string(ud); + } +} +std::string StatementSep_t::to_string(void*) const { + return {}; +} +std::string YueMultilineComment_t::to_string(void* ud) const { + return "--[["s + inner->to_string(ud) + "]]"s; +} +std::string ChainAssign_t::to_string(void* ud) const { + str_list temp; + for (auto exp : exprs.objects()) { + temp.emplace_back(exp->to_string(ud)); + } + return join(temp, " = "sv) + ' ' + assign->to_string(ud); +} +std::string Body_t::to_string(void* ud) const { + return content->to_string(ud); +} +std::string Block_t::to_string(void* ud) const { + auto info = reinterpret_cast(ud); + str_list temp; + for (auto stmt_ : statements.objects()) { + auto stmt = static_cast(stmt_); + if (stmt->content.is()) { + info->pushScope(); + temp.emplace_back(stmt->to_string(ud)); + info->popScope(); + } else { + temp.emplace_back(info->ind() + stmt->to_string(ud)); + } + } + return join(temp, "\n"sv); +} +std::string BlockEnd_t::to_string(void* ud) const { + return block->to_string(ud); +} +std::string File_t::to_string(void* ud) const { + if (block) { + return block->to_string(ud); + } else { + return {}; + } +} + +} // namespace yue + +} // namespace parserlib diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index e95e35c..c47ba8a 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -17,14 +17,16 @@ namespace parserlib { namespace yue { \ class type##_t : public ast_node { \ public: \ - virtual int getId() const override { return COUNTER_READ; } + virtual int get_id() const override { return COUNTER_READ; } \ + virtual std::string to_string(void*) const override; #define AST_NODE(type) \ COUNTER_INC; \ namespace yue { \ class type##_t : public ast_container { \ public: \ - virtual int getId() const override { return COUNTER_READ; } + virtual int get_id() const override { return COUNTER_READ; } \ + virtual std::string to_string(void*) const override; #define AST_MEMBER(type, ...) \ type##_t() { \ @@ -32,7 +34,7 @@ namespace parserlib { } #define AST_END(type, name) \ - virtual const std::string_view getName() const override { return name; } \ + virtual const std::string_view get_name() const override { return name; } \ } \ ; \ } \ @@ -362,7 +364,7 @@ AST_NODE(Try) AST_END(Try, "try"sv) AST_NODE(Comprehension) - ast_sel value; + ast_sel value; ast_ptr forLoop; AST_MEMBER(Comprehension, &value, &forLoop) AST_END(Comprehension, "comp"sv) @@ -659,8 +661,9 @@ AST_NODE(TableLit) ast_sel_list values; + VariablePair_t, NormalPair_t, Exp_t, + MetaVariablePair_t, MetaNormalPair_t, + /*non-syntax-rule*/ TableBlockIndent_t> values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit, "table_lit"sv) @@ -901,4 +904,17 @@ AST_END(File, "file"sv) // clang-format on +struct YueFormat { + int indent = 0; + bool spaceOverTab = false; + int tabSpaces = 4; + std::string toString(ast_node* node); + + Converter converter; + void pushScope(); + void popScope(); + std::string convert(const ast_node* node); + std::string ind() const; +}; + } // namespace parserlib diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 477815d..c1426f9 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -72,7 +72,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.17.1"sv; +const std::string_view version = "0.17.2"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -176,7 +176,7 @@ public: if (_info.exportMacro) { for (auto stmt_ : block->statements.objects()) { auto stmt = static_cast(stmt_); - switch (stmt->content->getId()) { + switch (stmt->content->get_id()) { case id(): case id(): break; @@ -702,30 +702,21 @@ private: return str; } - std::string join(const str_list& items) { + std::string join(const str_list& items, std::string_view sep = {}) { if (items.empty()) return Empty; else if (items.size() == 1) return items.front(); - for (const auto& item : items) { - _joinBuf << item; - } - auto result = _joinBuf.str(); - _joinBuf.str(""); - _joinBuf.clear(); - return result; - } - - std::string join(const str_list& items, std::string_view sep) { - if (items.empty()) - return Empty; - else if (items.size() == 1) - return items.front(); - std::string sepStr = std::string(sep); - auto begin = ++items.begin(); - _joinBuf << items.front(); - for (auto it = begin; it != items.end(); ++it) { - _joinBuf << sepStr << *it; + if (sep.empty()) { + for (const auto& item : items) { + _joinBuf << item; + } + } else { + auto begin = ++items.begin(); + _joinBuf << items.front(); + for (auto it = begin; it != items.end(); ++it) { + _joinBuf << sep << *it; + } } auto result = _joinBuf.str(); _joinBuf.str(""); @@ -735,7 +726,7 @@ private: UnaryExp_t* singleUnaryExpFrom(ast_node* item) const { Exp_t* exp = nullptr; - switch (item->getId()) { + switch (item->get_id()) { case id(): exp = static_cast(item); break; @@ -786,7 +777,7 @@ private: if (unary->ops.empty()) { Value_t* value = static_cast(unary->expos.back()); if (auto chain = ast_cast(value->item); chain && chain->items.size() == 1) { - if (auto exp = chain->getByPath()) { + if (auto exp = chain->get_by_path()) { if (auto insideValue = singleValueFrom(exp)) { return insideValue; } @@ -855,7 +846,7 @@ private: } Statement_t* lastStatementFrom(ast_node* body) const { - switch (body->getId()) { + switch (body->get_id()) { case id(): return lastStatementFrom(static_cast(body)); case id(): { @@ -893,7 +884,7 @@ private: } Exp_t* lastExpFromAssign(ast_node* action) { - switch (action->getId()) { + switch (action->get_id()) { case id(): { auto update = static_cast(action); return update->value; @@ -908,7 +899,7 @@ private: Exp_t* lastExpFromStatement(Statement_t* stmt) { if (!stmt->content) return nullptr; - switch (stmt->content->getId()) { + switch (stmt->content->get_id()) { case id(): { auto expListAssign = static_cast(stmt->content.get()); if (auto action = expListAssign->action.get()) { @@ -922,7 +913,7 @@ private: if (auto action = exportNode->assign.get()) { return lastExpFromAssign(action); } else { - switch (exportNode->target->getId()) { + switch (exportNode->target->get_id()) { case id(): return exportNode->target.to(); case id(): return static_cast(exportNode->target.to()->exprs.back()); } @@ -1040,7 +1031,7 @@ private: BREAK_IF(!chainValue); BREAK_IF(chainValue->items.size() != 1); auto callable = ast_cast(chainValue->items.front()); - BREAK_IF(!callable || !(callable->item.is() || callable->getByPath())); + BREAK_IF(!callable || !(callable->item.is() || callable->get_by_path())); str_list tmp; if (accessing) { transformCallable(callable, tmp); @@ -1073,7 +1064,7 @@ private: if (chainItems.size() == 1) { auto firstItem = chainItems.back(); if (auto callable = ast_cast(firstItem)) { - switch (callable->item->getId()) { + switch (callable->item->get_id()) { case id(): checkConst(_parser.toString(callable->item.get()), callable->item.get()); return true; @@ -1081,7 +1072,7 @@ private: return true; } } else - switch (firstItem->getId()) { + switch (firstItem->get_id()) { case id(): case id(): return true; @@ -1094,7 +1085,7 @@ private: return false; } auto lastItem = chainItems.back(); - switch (lastItem->getId()) { + switch (lastItem->get_id()) { case id(): case id(): case id(): @@ -1107,7 +1098,7 @@ private: bool isAssignable(Exp_t* exp) { if (auto value = singleValueFrom(exp)) { auto item = value->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): return true; case id(): { @@ -1265,7 +1256,7 @@ private: auto x = statement; if (_config.reserveComment && !x->comments.empty()) { for (ast_node* node : x->comments.objects()) { - switch (node->getId()) { + switch (node->get_id()) { case id(): { auto comment = ast_cast(node); out.push_back(indent() + "--"s + _parser.toString(comment) + '\n'); @@ -1290,7 +1281,7 @@ private: } } else if (auto attrib = statement->content.as()) { auto appendix = statement->appendix.get(); - switch (appendix->item->getId()) { + switch (appendix->item->get_id()) { case id(): { auto if_line = static_cast(appendix->item.get()); auto ifNode = x->new_ptr(); @@ -1299,7 +1290,7 @@ private: auto expList = x->new_ptr(); for (auto val : attrib->assign->values.objects()) { - switch (val->getId()) { + switch (val->get_id()) { case id(): case id(): case id(): { @@ -1350,7 +1341,7 @@ private: } } else if (!statement->appendix->item.is()) { auto appendix = statement->appendix->item.get(); - switch (statement->content->getId()) { + switch (statement->content->get_id()) { case id(): throw CompileError("loop line decorator can not be used in a return statement"sv, appendix); break; @@ -1360,7 +1351,7 @@ private: } } auto appendix = statement->appendix.get(); - switch (appendix->item->getId()) { + switch (appendix->item->get_id()) { case id(): { auto if_line = static_cast(appendix->item.get()); auto ifNode = x->new_ptr(); @@ -1429,7 +1420,7 @@ private: out.push_back(Empty); return; } - switch (content->getId()) { + switch (content->get_id()) { case id(): transformImport(static_cast(content), out); break; case id(): transformWhile(static_cast(content), out); break; case id(): transformRepeat(static_cast(content), out); break; @@ -1461,7 +1452,7 @@ private: if (auto simpleValue = singleValue->item.as()) { auto value = simpleValue->value.get(); bool specialSingleValue = true; - switch (value->getId()) { + switch (value->get_id()) { case id(): transformIf(static_cast(value), out, ExpUsage::Common); break; case id(): transformClassDecl(static_cast(value), out, ExpUsage::Common); break; case id(): transformSwitch(static_cast(value), out, ExpUsage::Common); break; @@ -1669,7 +1660,7 @@ private: bool checkValuesLater = false; if (exprs.size() > values.size()) { BLOCK_START - switch (values.back()->getId()) { + switch (values.back()->get_id()) { case id(): case id(): checkValuesLater = true; @@ -1682,7 +1673,7 @@ private: throw CompileError(clearBuf(), values.front()); } if (auto val = value->item.as()) { - switch (val->value->getId()) { + switch (val->value->get_id()) { case id(): case id(): case id(): @@ -1711,7 +1702,7 @@ private: BLOCK_START auto value = singleValueFrom(*it); BREAK_IF(!value); - if (value->item.is() || value->getByPath()) { + if (value->item.is() || value->get_by_path()) { holdItem = true; break; } @@ -1868,7 +1859,7 @@ private: value = val->value.get(); } } - switch (value->getId()) { + switch (value->get_id()) { case id(): { auto ifNode = static_cast(value); auto assignList = assignment->expList.get(); @@ -2252,7 +2243,7 @@ private: } void transformAssignItem(ast_node* value, str_list& out) { - switch (value->getId()) { + switch (value->get_id()) { case id(): transformWithClosure(static_cast(value), out); break; case id(): transformIf(static_cast(value), out, ExpUsage::Closure); break; case id(): transformSwitch(static_cast(value), out, ExpUsage::Closure); break; @@ -2265,11 +2256,11 @@ private: std::list destructFromExp(ast_node* node, bool optional) { const node_container* tableItems = nullptr; ast_ptr sep = optional ? node->new_ptr() : nullptr; - switch (node->getId()) { + switch (node->get_id()) { case id(): { auto item = singleValueFrom(node)->item.get(); if (!item) throw CompileError("invalid destructure value"sv, node); - auto tbA = item->getByPath(); + auto tbA = item->get_by_path(); if (tbA) { tableItems = &tbA->values.objects(); } else { @@ -2305,7 +2296,7 @@ private: int index = 0; auto subMetaDestruct = node->new_ptr(); for (auto pair : *tableItems) { - switch (pair->getId()) { + switch (pair->get_id()) { case id(): case id(): { Exp_t* defVal = nullptr; @@ -2319,7 +2310,7 @@ private: } auto value = singleValueFrom(pair); auto item = value->item.get(); - if (ast_is(item) || item->getByPath()) { + if (ast_is(item) || item->get_by_path()) { auto subPairs = destructFromExp(pair, optional); if (!subPairs.empty()) { if (defVal) { @@ -2372,14 +2363,14 @@ private: auto np = static_cast(pair); ast_ptr keyIndex; if (np->key) { - if (auto key = np->key->getByPath()) { + if (auto key = np->key->get_by_path()) { auto keyNameStr = _parser.toString(key); if (LuaKeywords.find(keyNameStr) != LuaKeywords.end()) { keyIndex = toAst('"' + keyNameStr + '"', key).get(); } else { keyIndex = toAst('.' + keyNameStr, key).get(); } - } else if (auto key = np->key->getByPath()) { + } else if (auto key = np->key->get_by_path()) { auto callable = np->new_ptr(); callable->item.set(key); auto chainValue = np->new_ptr(); @@ -2396,7 +2387,7 @@ private: if (auto exp = np->value.as()) { if (!isAssignable(exp)) throw CompileError("can't do destructure value"sv, exp); auto item = singleValueFrom(exp)->item.get(); - if (ast_is(item) || item->getByPath()) { + if (ast_is(item) || item->get_by_path()) { auto subPairs = destructFromExp(exp, optional); if (!subPairs.empty()) { if (defVal) { @@ -2476,7 +2467,7 @@ private: auto mp = static_cast(pair); auto newPair = pair->new_ptr(); if (mp->key) { - switch (mp->key->getId()) { + switch (mp->key->get_id()) { case id(): { auto key = _parser.toString(mp->key); checkMetamethod(key, mp->key); @@ -2550,11 +2541,11 @@ private: if (!value) { throw CompileError("invalid destructure"sv, expr); } - ast_node* destructNode = value->getByPath(); + ast_node* destructNode = value->get_by_path(); if (destructNode || (destructNode = value->item.as())) { if (*j != nil) { if (auto ssVal = simpleSingleValueFrom(*j)) { - switch (ssVal->value->getId()) { + switch (ssVal->value->get_id()) { case id(): throw CompileError("can not destructure a constant"sv, ssVal->value); break; @@ -2571,7 +2562,7 @@ private: auto subDestruct = destructNode->new_ptr(); auto subMetaDestruct = destructNode->new_ptr(); const node_container* dlist = nullptr; - switch (destructNode->getId()) { + switch (destructNode->get_id()) { case id(): dlist = &static_cast(destructNode)->values.objects(); break; @@ -2581,7 +2572,7 @@ private: default: YUEE("AST node mismatch", destructNode); break; } for (auto item : *dlist) { - switch (item->getId()) { + switch (item->get_id()) { case id(): { auto mvp = static_cast(item); auto mp = mvp->pair.get(); @@ -2598,7 +2589,7 @@ private: auto mp = mnp->pair.get(); auto newPair = item->new_ptr(); if (mp->key) { - switch (mp->key->getId()) { + switch (mp->key->get_id()) { case id(): { auto key = _parser.toString(mp->key); checkMetamethod(key, mp->key); @@ -2637,7 +2628,7 @@ private: auto mp = static_cast(item); auto newPair = item->new_ptr(); if (mp->key) { - switch (mp->key->getId()) { + switch (mp->key->get_id()) { case id(): { auto key = _parser.toString(mp->key); checkMetamethod(key, mp->key); @@ -2808,7 +2799,7 @@ private: str_list temp; auto expList = assignment->expList.get(); auto action = assignment->action.get(); - switch (action->getId()) { + switch (action->get_id()) { case id(): { if (expList->exprs.size() > 1) throw CompileError("can not apply update to multiple values"sv, expList); auto update = static_cast(action); @@ -3002,7 +2993,7 @@ private: std::list> ifCondPairs; ifCondPairs.emplace_back(); for (auto node : nodes) { - switch (node->getId()) { + switch (node->get_id()) { case id(): ifCondPairs.back().first = static_cast(node); break; @@ -3375,7 +3366,7 @@ private: void transformValue(Value_t* value, str_list& out) { auto item = value->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): transformSimpleValue(static_cast(item), out); break; case id(): transform_simple_table(static_cast(item), out); break; case id(): transformChainValue(static_cast(item), out, ExpUsage::Closure); break; @@ -3386,7 +3377,7 @@ private: void transformCallable(Callable_t* callable, str_list& out, const ast_sel& invoke = {}) { auto item = callable->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): { transformVariable(static_cast(item), out); if (_config.lintGlobalVariable && !isLocal(out.back())) { @@ -3414,7 +3405,7 @@ private: void transformSimpleValue(SimpleValue_t* simpleValue, str_list& out) { auto value = simpleValue->value.get(); - switch (value->getId()) { + switch (value->get_id()) { case id(): transformConstValue(static_cast(value), out); break; case id(): transformIf(static_cast(value), out, ExpUsage::Closure); break; case id(): transformSwitch(static_cast(value), out, ExpUsage::Closure); break; @@ -3619,7 +3610,7 @@ private: if (auto local = stmt->content.as()) { if (!local->collected) { local->collected = true; - switch (local->item->getId()) { + switch (local->item->get_id()) { case id(): { auto flag = static_cast(local->item.get()); LocalMode newMode = _parser.toString(flag) == "*"sv ? LocalMode::Any : LocalMode::Capital; @@ -3695,11 +3686,11 @@ private: BREAK_IF(!exp); auto value = singleValueFrom(exp); BREAK_IF(!value); - classDecl = value->getByPath(); + classDecl = value->get_by_path(); BLOCK_END } else if (auto expList = expListFrom(stmt)) { auto value = singleValueFrom(expList); - classDecl = value->getByPath(); + classDecl = value->get_by_path(); } if (classDecl) { if (auto variable = classDecl->name->item.as()) { @@ -3743,7 +3734,7 @@ private: if (!last) throw CompileError("block is not assignable"sv, block); if (last->appendix) { auto appendix = last->appendix->item.get(); - switch (appendix->getId()) { + switch (appendix->get_id()) { case id(): throw CompileError("while-loop line decorator is not supported here"sv, appendix); break; @@ -4068,7 +4059,7 @@ private: if (auto singleValue = singleValueFrom(valueList)) { if (auto simpleValue = singleValue->item.as()) { auto value = simpleValue->value.get(); - switch (value->getId()) { + switch (value->get_id()) { case id(): transformComprehension(static_cast(value), out, ExpUsage::Return); return; @@ -4169,7 +4160,7 @@ private: for (auto _def : argDefList->definitions.objects()) { auto def = static_cast(_def); auto& arg = argItems.emplace_back(); - switch (def->name->getId()) { + switch (def->name->get_id()) { case id(): arg.name = _parser.toString(def->name); break; case id(): { assignSelf = true; @@ -4180,7 +4171,7 @@ private: arg.checkExistence = true; } auto selfName = static_cast(def->name.get()); - switch (selfName->name->getId()) { + switch (selfName->name->get_id()) { case id(): { auto clsName = static_cast(selfName->name.get()); arg.name = _parser.toString(clsName->name); @@ -4254,7 +4245,7 @@ private: void transformSelfName(SelfItem_t* selfName, str_list& out, const ast_sel& invoke = {}) { auto x = selfName; auto name = selfName->name.get(); - switch (name->getId()) { + switch (name->get_id()) { case id(): { auto clsName = static_cast(name); auto nameStr = _parser.toString(clsName->name); @@ -4537,7 +4528,7 @@ private: break; } auto baseChain = x->new_ptr(); - switch (chainList.front()->getId()) { + switch (chainList.front()->get_id()) { case id(): case id(): case id(): @@ -4648,11 +4639,11 @@ private: chain = toAst("getmetatable()"sv, x); ast_to(chain->items.back())->args.push_back(exp); } - switch ((*opIt)->getId()) { + switch ((*opIt)->get_id()) { case id(): { auto colon = static_cast(*opIt); auto meta = colon->name.to(); - switch (meta->item->getId()) { + switch (meta->item->get_id()) { case id(): { auto name = _parser.toString(meta->item); checkMetamethod(name, meta->item); @@ -4747,7 +4738,7 @@ private: auto dot = static_cast(*opIt); if (dot->name.is()) break; auto meta = dot->name.to(); - switch (meta->item->getId()) { + switch (meta->item->get_id()) { case id(): { auto name = _parser.toString(meta->item); checkMetamethod(name, meta->item); @@ -4778,7 +4769,7 @@ private: void transformChainList(const node_container& chainList, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { auto x = chainList.front(); str_list temp; - switch (x->getId()) { + switch (x->get_id()) { case id(): case id(): case id(): @@ -4791,7 +4782,7 @@ private: } for (auto it = chainList.begin(); it != chainList.end(); ++it) { auto item = *it; - switch (item->getId()) { + switch (item->get_id()) { case id(): transformInvoke(static_cast(item), temp); break; @@ -4823,7 +4814,7 @@ private: auto block = x->new_ptr(); { auto chainValue = x->new_ptr(); - switch (chainList.front()->getId()) { + switch (chainList.front()->get_id()) { case id(): case id(): chainValue->items.push_back(toAst(_withVars.top(), x)); @@ -5058,7 +5049,7 @@ private: BREAK_IF(!exp); auto value = singleValueFrom(exp); BREAK_IF(!value); - auto lstr = value->getByPath(); + auto lstr = value->get_by_path(); BREAK_IF(!lstr); str = _parser.toString(lstr->content); rawString = true; @@ -5071,21 +5062,15 @@ private: auto exp = static_cast(arg); BLOCK_START BREAK_IF(!exp->opValues.empty()); - auto chainValue = exp->getByPath(); + auto chainValue = exp->get_by_path(); BREAK_IF(!chainValue); BREAK_IF(!isMacroChain(chainValue)); str = std::get<1>(expandMacroStr(chainValue)); BLOCK_END - if (str.empty() && arg->m_begin.m_it == arg->m_end.m_it) { - // exp is reassembled due to pipe expressions - // in transform stage, toString(exp) won't be able - // to convert its whole text content - str = _parser.toString(exp->pipeExprs.front()); - } } } if (!rawString && str.empty()) { - str = _parser.toString(arg); + str = YueFormat{}.toString(arg); } Utils::trim(str); Utils::replace(str, "\r\n"sv, "\n"sv); @@ -5364,7 +5349,7 @@ private: void transformInvoke(Invoke_t* invoke, str_list& out) { str_list temp; for (auto arg : invoke->args.objects()) { - switch (arg->getId()) { + switch (arg->get_id()) { case id(): transformExp(static_cast(arg), temp, ExpUsage::Closure); break; case id(): transformSingleString(static_cast(arg), temp); break; case id(): transformDoubleString(static_cast(arg), temp); break; @@ -5583,7 +5568,7 @@ private: } for (; it != values.end(); ++it) { auto item = *it; - switch (item->getId()) { + switch (item->get_id()) { case id(): { auto spread = static_cast(item); std::string indexVar = getUnusedName("_idx_"sv); @@ -5635,7 +5620,7 @@ private: auto assignment = toAst(tableVar + "=nil"s, item); auto chainValue = singleValueFrom(ast_to(assignment->expList->exprs.front()))->item.to(); auto key = normalPair->key.get(); - switch (key->getId()) { + switch (key->get_id()) { case id(): { auto keyName = static_cast(key); ast_ptr chainItem; @@ -5750,7 +5735,7 @@ private: auto assignment = toAst(tableVar + "=nil"s, item); auto chainValue = singleValueFrom(ast_to(assignment->expList->exprs.front()))->item.to(); auto key = metaNormalPair->key.get(); - switch (key->getId()) { + switch (key->get_id()) { case id(): { auto dotItem = x->new_ptr(); dotItem->name.set(key); @@ -5825,7 +5810,7 @@ private: ast_sel metatableItem; for (auto value : values) { auto item = value; - switch (item->getId()) { + switch (item->get_id()) { case id(): { auto pair = static_cast(item); if (pair->defVal) { @@ -5868,7 +5853,7 @@ private: } } bool isMetamethod = false; - switch (item->getId()) { + switch (item->get_id()) { case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); break; case id(): transform_variable_pair(static_cast(item), temp); break; case id(): transform_normal_pair(static_cast(item), temp, false); break; @@ -5895,7 +5880,7 @@ private: if (metatableItem) { throw CompileError("too many metatable declarations"sv, mp->key); } - switch (mp->key->getId()) { + switch (mp->key->get_id()) { case id(): { auto key = _parser.toString(mp->key); checkMetamethod(key, mp->key); @@ -5950,7 +5935,7 @@ private: if (!metatable->pairs.empty()) { transform_simple_table(metatable, tmp); } else - switch (metatableItem->getId()) { + switch (metatableItem->get_id()) { case id(): transformExp(static_cast(metatableItem.get()), tmp, ExpUsage::Closure); break; @@ -5978,7 +5963,7 @@ private: auto x = comp; auto compInner = comp->forLoop.get(); for (auto item : compInner->items.objects()) { - switch (item->getId()) { + switch (item->get_id()) { case id(): transformCompForEach(static_cast(item), temp); break; @@ -6035,7 +6020,7 @@ private: addToScope(lenVar); auto compInner = comp->forLoop.get(); for (auto item : compInner->items.objects()) { - switch (item->getId()) { + switch (item->get_id()) { case id(): transformCompForEach(static_cast(item), temp); break; @@ -6115,7 +6100,7 @@ private: std::list>> destructPairs; for (auto _item : nameList->items.objects()) { auto item = static_cast(_item)->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): transformVariable(static_cast(item), vars); varAfter.push_back(vars.back()); @@ -6130,7 +6115,7 @@ private: default: YUEE("AST node mismatch", item); break; } } - switch (loopTarget->getId()) { + switch (loopTarget->get_id()) { case id(): { auto star_exp = static_cast(loopTarget); auto listVar = singleVariableFrom(star_exp->value, true); @@ -6148,7 +6133,7 @@ private: BREAK_IF(!slice); endWithSlice = true; if (listVar.empty() && chainList.size() == 2) { - if (auto var = chainList.front()->getByPath()) { + if (auto var = chainList.front()->get_by_path()) { listVar = _parser.toString(var); if (!isLocal(listVar)) listVar.clear(); } @@ -6303,7 +6288,7 @@ private: } str_list temp; for (auto arg : invokeArgs->args.objects()) { - switch (arg->getId()) { + switch (arg->get_id()) { case id(): transformExp(static_cast(arg), temp, ExpUsage::Closure); break; case id(): transformTableBlock(static_cast(arg), temp); break; default: YUEE("AST node mismatch", arg); break; @@ -6337,7 +6322,7 @@ private: } void transform_plain_body(ast_node* body, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { - switch (body->getId()) { + switch (body->get_id()) { case id(): transformBlock(static_cast(body), out, usage, assignList); break; @@ -6363,7 +6348,7 @@ private: auto simpleValue = value->item.as(); BREAK_IF(!simpleValue); auto sVal = simpleValue->value.get(); - switch (sVal->getId()) { + switch (sVal->get_id()) { case id(): { auto withNode = static_cast(sVal); if (hasContinueStatement(withNode->body)) { @@ -6405,7 +6390,7 @@ private: BLOCK_END } } else { - switch (node->getId()) { + switch (node->get_id()) { case id(): case id(): return traversal::Continue; @@ -6749,7 +6734,7 @@ private: void transform_normal_pair(NormalPair_t* pair, str_list& out, bool assignClass) { auto key = pair->key.get(); str_list temp; - switch (key->getId()) { + switch (key->get_id()) { case id(): { auto keyName = static_cast(key); transformKeyName(keyName, temp); @@ -6765,7 +6750,7 @@ private: case id(): { auto strNode = static_cast(key); auto str = strNode->str.get(); - switch (str->getId()) { + switch (str->get_id()) { case id(): transformDoubleString(static_cast(str), temp); temp.back() = '[' + temp.back() + ']'; @@ -6785,7 +6770,7 @@ private: default: YUEE("AST node mismatch", key); break; } auto value = pair->value.get(); - switch (value->getId()) { + switch (value->get_id()) { case id(): transformExp(static_cast(value), temp, ExpUsage::Closure); break; case id(): transformTableBlock(static_cast(value), temp); break; default: YUEE("AST node mismatch", value); break; @@ -6795,7 +6780,7 @@ private: void transformKeyName(KeyName_t* keyName, str_list& out) { auto name = keyName->name.get(); - switch (name->getId()) { + switch (name->get_id()) { case id(): transformSelfName(static_cast(name), out); break; @@ -6830,7 +6815,7 @@ private: for (auto _seg : doubleString->segments.objects()) { auto seg = static_cast(_seg); auto content = seg->content.get(); - switch (content->getId()) { + switch (content->get_id()) { case id(): { auto str = _parser.toString(content); Utils::replace(str, "\r\n"sv, "\n"); @@ -6851,7 +6836,7 @@ private: void transformString(String_t* string, str_list& out) { auto str = string->str.get(); - switch (str->getId()) { + switch (str->get_id()) { case id(): transformSingleString(static_cast(str), out); break; case id(): transformDoubleString(static_cast(str), out); break; case id(): transformLuaString(static_cast(str), out); break; @@ -6908,7 +6893,7 @@ private: if (auto dotChain = ast_cast(chain->items.back())) { className = '\"' + _parser.toString(dotChain->name) + '\"'; } else if (auto index = ast_cast(chain->items.back())) { - if (auto name = index->getByPath()) { + if (auto name = index->get_by_path()) { transformString(name, temp); className = std::move(temp.back()); temp.pop_back(); @@ -6957,11 +6942,11 @@ private: BREAK_IF(!exp); auto value = singleValueFrom(exp); BREAK_IF(!value); - clsDecl = value->getByPath(); + clsDecl = value->get_by_path(); BLOCK_END } else if (auto expList = expListFrom(statement)) { auto value = singleValueFrom(expList); - clsDecl = value->getByPath(); + clsDecl = value->get_by_path(); } if (clsDecl) { std::string clsName; @@ -6993,7 +6978,7 @@ private: if (body) { std::list members; for (auto content : classDecl->body->contents.objects()) { - switch (content->getId()) { + switch (content->get_id()) { case id(): { size_t inc = transform_class_member_list(static_cast(content), members, classVar); auto it = members.end(); @@ -7153,7 +7138,7 @@ private: for (auto keyValue : class_member_list->values.objects()) { MemType type = MemType::Common; ast_ptr ref; - switch (keyValue->getId()) { + switch (keyValue->get_id()) { case id(): { auto mtPair = static_cast(keyValue); auto nameStr = _parser.toString(mtPair->name); @@ -7206,14 +7191,14 @@ private: } } normal_pair->value->traverse([&](ast_node* node) { - if (node->getId() == id()) return traversal::Return; + if (node->get_id() == id()) return traversal::Return; if (auto chainValue = ast_cast(node)) { if (auto callable = ast_cast(chainValue->items.front())) { auto var = callable->item.get(); if (_parser.toString(var) == "super"sv) { auto insertSelfToArguments = [&](ast_node* item) { auto x = item; - switch (item->getId()) { + switch (item->get_id()) { case id(): { auto invoke = static_cast(item); invoke->args.push_front(toAst("self"sv, x)); @@ -7259,7 +7244,7 @@ private: if (type == MemType::Property) { decIndentOffset(); } - switch (keyValue->getId()) { + switch (keyValue->get_id()) { case id(): transform_variable_pair(static_cast(keyValue), temp); break; @@ -7281,7 +7266,7 @@ private: void transformAssignable(Assignable_t* assignable, str_list& out) { auto item = assignable->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): transformAssignableChain(static_cast(item), out); break; case id(): transformVariable(static_cast(item), out); break; case id(): transformSelfName(static_cast(item), out); break; @@ -7399,12 +7384,12 @@ private: auto exp = ast_cast(assign->values.objects().front()); BREAK_IF(!exp); if (auto value = singleValueFrom(exp)) { - clsDecl = value->getByPath(); + clsDecl = value->get_by_path(); } BLOCK_END } else if (auto expList = expListFrom(statement)) { auto value = singleValueFrom(expList); - clsDecl = value->getByPath(); + clsDecl = value->get_by_path(); } if (clsDecl) { auto variable = clsDecl->name.as(); @@ -7459,10 +7444,10 @@ private: void transformGlobal(Global_t* global, str_list& out) { auto x = global; auto item = global->item.get(); - switch (item->getId()) { + switch (item->get_id()) { case id(): { auto classDecl = static_cast(item); - if (classDecl->name && classDecl->name->item->getId() == id()) { + if (classDecl->name && classDecl->name->item->get_id() == id()) { markVarsGlobal(GlobalMode::Any); addGlobalVar(_parser.toString(classDecl->name->item), classDecl->name->item); } @@ -7513,13 +7498,13 @@ private: } std::optional getExportKey(ast_node* node) { - switch (node->getId()) { + switch (node->get_id()) { case id(): { return _parser.toString(node); } case id(): { auto strNode = static_cast(node); - switch (strNode->str->getId()) { + switch (strNode->str->get_id()) { case id(): { auto str = static_cast(strNode->str.get()); if (str->segments.size() == 1) { @@ -7614,7 +7599,7 @@ private: } for (auto _exp : expList->exprs.objects()) { auto exp = static_cast(_exp); - if (!variableFrom(exp) && !exp->getByPath() && !exp->getByPath()) { + if (!variableFrom(exp) && !exp->get_by_path() && !exp->get_by_path()) { throw CompileError("left hand expressions must be variables in export statement"sv, x); } } @@ -7664,8 +7649,8 @@ private: auto assignList = toAst(_info.moduleName + "[#"s + _info.moduleName + "+1]"s, x); assignment->expList.set(assignList); for (auto exp : expList->exprs.objects()) { - if (auto classDecl = exp->getByPath()) { - if (classDecl->name && classDecl->name->item->getId() == id()) { + if (auto classDecl = exp->get_by_path()) { + if (classDecl->name && classDecl->name->item->get_id() == id()) { transformClassDecl(classDecl, temp, ExpUsage::Common); auto name = _parser.toString(classDecl->name->item); assignment->expList.set(toAst(_info.moduleName + "[\""s + name + "\"]"s, x)); @@ -7711,7 +7696,7 @@ private: str_list temp; auto compInner = comp->forLoop.get(); for (auto item : compInner->items.objects()) { - switch (item->getId()) { + switch (item->get_id()) { case id(): transformCompForEach(static_cast(item), temp); break; @@ -7926,7 +7911,7 @@ private: auto expList = x->new_ptr(); auto assign = x->new_ptr(); for (auto name : import->names.objects()) { - switch (name->getId()) { + switch (name->get_id()) { case id(): { auto var = static_cast(name); { @@ -8015,7 +8000,7 @@ private: auto newTab = x->new_ptr(); if (auto tabLit = import->target.as()) { for (auto item : tabLit->items.objects()) { - switch (item->getId()) { + switch (item->get_id()) { #ifdef YUE_NO_MACRO case id(): case id(): @@ -8145,7 +8130,7 @@ private: auto simpleValue = x->new_ptr(); auto tableLit = x->new_ptr(); for (auto pair : tabLit->items.objects()) { - switch (pair->getId()) { + switch (pair->get_id()) { case id(): { auto pairDef = pair->new_ptr(); pairDef->pair.set(pair); @@ -8203,7 +8188,7 @@ private: void transformImport(Import_t* import, str_list& out) { auto content = import->content.get(); - switch (content->getId()) { + switch (content->get_id()) { case id(): transformImportAs(static_cast(content), out); break; @@ -8360,7 +8345,7 @@ private: } auto valueList = branch->condition.to(); if (auto value = singleValueFrom(valueList); - value && (value->item.is() || value->getByPath())) { + value && (value->item.is() || value->get_by_path())) { if (!firstBranch) { temp.push_back(indent() + "else"s + nll(branch)); pushScope(); @@ -8496,7 +8481,7 @@ private: local->defined = true; transformLocalDef(local, temp); } - switch (local->item->getId()) { + switch (local->item->get_id()) { case id(): { auto values = static_cast(local->item.get()); if (values->valueList) { @@ -8556,7 +8541,7 @@ private: } else { auto item = *i; auto value = item->new_ptr(); - switch (item->getId()) { + switch (item->get_id()) { case id(): value->item.set(item); break; diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index d7fbf90..9b4adae 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -250,159 +250,159 @@ private: NONE_AST_RULE(line); NONE_AST_RULE(shebang); - AST_RULE(Num) - AST_RULE(Name) - AST_RULE(Variable) - AST_RULE(LabelName) - AST_RULE(LuaKeyword) - AST_RULE(Self) - AST_RULE(SelfName) - AST_RULE(SelfClass) - AST_RULE(SelfClassName) - AST_RULE(SelfItem) - AST_RULE(KeyName) - AST_RULE(VarArg) - AST_RULE(Seperator) - AST_RULE(NameList) - AST_RULE(LocalFlag) - AST_RULE(LocalValues) - AST_RULE(Local) - AST_RULE(ConstAttrib) - AST_RULE(CloseAttrib) + AST_RULE(Num); + AST_RULE(Name); + AST_RULE(Variable); + AST_RULE(LabelName); + AST_RULE(LuaKeyword); + AST_RULE(Self); + AST_RULE(SelfName); + AST_RULE(SelfClass); + AST_RULE(SelfClassName); + AST_RULE(SelfItem); + AST_RULE(KeyName); + AST_RULE(VarArg); + AST_RULE(Seperator); + AST_RULE(NameList); + AST_RULE(LocalFlag); + AST_RULE(LocalValues); + AST_RULE(Local); + AST_RULE(ConstAttrib); + AST_RULE(CloseAttrib); AST_RULE(LocalAttrib); - AST_RULE(ColonImportName) - AST_RULE(ImportLiteralInner) - AST_RULE(ImportLiteral) - AST_RULE(ImportFrom) - AST_RULE(MacroNamePair) - AST_RULE(ImportAllMacro) - AST_RULE(ImportTabLit) - AST_RULE(ImportAs) - AST_RULE(Import) - AST_RULE(Label) - AST_RULE(Goto) - AST_RULE(ShortTabAppending) - AST_RULE(FnArrowBack) - AST_RULE(Backcall) - AST_RULE(PipeBody) - AST_RULE(ExpListLow) - AST_RULE(ExpList) - AST_RULE(Return) - AST_RULE(With) - AST_RULE(SwitchList) - AST_RULE(SwitchCase) - AST_RULE(Switch) - AST_RULE(Assignment) - AST_RULE(IfCond) - AST_RULE(IfType) - AST_RULE(If) - AST_RULE(WhileType) - AST_RULE(While) - AST_RULE(Repeat) - AST_RULE(ForStepValue) - AST_RULE(For) - AST_RULE(ForEach) - AST_RULE(Do) - AST_RULE(CatchBlock) - AST_RULE(Try) - AST_RULE(Comprehension) - AST_RULE(CompValue) - AST_RULE(TblComprehension) - AST_RULE(StarExp) - AST_RULE(CompForEach) - AST_RULE(CompFor) - AST_RULE(CompInner) - AST_RULE(Assign) - AST_RULE(UpdateOp) - AST_RULE(Update) - AST_RULE(BinaryOperator) - AST_RULE(UnaryOperator) - AST_RULE(Assignable) - AST_RULE(AssignableChain) - AST_RULE(ExpOpValue) - AST_RULE(Exp) - AST_RULE(Callable) - AST_RULE(ChainValue) - AST_RULE(SimpleTable) - AST_RULE(SimpleValue) - AST_RULE(Value) + AST_RULE(ColonImportName); + AST_RULE(ImportLiteralInner); + AST_RULE(ImportLiteral); + AST_RULE(ImportFrom); + AST_RULE(MacroNamePair); + AST_RULE(ImportAllMacro); + AST_RULE(ImportTabLit); + AST_RULE(ImportAs); + AST_RULE(Import); + AST_RULE(Label); + AST_RULE(Goto); + AST_RULE(ShortTabAppending); + AST_RULE(FnArrowBack); + AST_RULE(Backcall); + AST_RULE(PipeBody); + AST_RULE(ExpListLow); + AST_RULE(ExpList); + AST_RULE(Return); + AST_RULE(With); + AST_RULE(SwitchList); + AST_RULE(SwitchCase); + AST_RULE(Switch); + AST_RULE(Assignment); + AST_RULE(IfCond); + AST_RULE(IfType); + AST_RULE(If); + AST_RULE(WhileType); + AST_RULE(While); + AST_RULE(Repeat); + AST_RULE(ForStepValue); + AST_RULE(For); + AST_RULE(ForEach); + AST_RULE(Do); + AST_RULE(CatchBlock); + AST_RULE(Try); + AST_RULE(Comprehension); + AST_RULE(CompValue); + AST_RULE(TblComprehension); + AST_RULE(StarExp); + AST_RULE(CompForEach); + AST_RULE(CompFor); + AST_RULE(CompInner); + AST_RULE(Assign); + AST_RULE(UpdateOp); + AST_RULE(Update); + AST_RULE(BinaryOperator); + AST_RULE(UnaryOperator); + AST_RULE(Assignable); + AST_RULE(AssignableChain); + AST_RULE(ExpOpValue); + AST_RULE(Exp); + AST_RULE(Callable); + AST_RULE(ChainValue); + AST_RULE(SimpleTable); + AST_RULE(SimpleValue); + AST_RULE(Value); AST_RULE(LuaStringOpen); AST_RULE(LuaStringContent); AST_RULE(LuaStringClose); - AST_RULE(LuaString) - AST_RULE(SingleString) - AST_RULE(DoubleStringInner) - AST_RULE(DoubleStringContent) - AST_RULE(DoubleString) - AST_RULE(String) - AST_RULE(Parens) - AST_RULE(DotChainItem) - AST_RULE(ColonChainItem) - AST_RULE(Metatable) - AST_RULE(Metamethod) - AST_RULE(DefaultValue) - AST_RULE(Slice) - AST_RULE(Invoke) - AST_RULE(ExistentialOp) - AST_RULE(TableAppendingOp) - AST_RULE(SpreadExp) - AST_RULE(TableLit) - AST_RULE(TableBlock) - AST_RULE(TableBlockIndent) - AST_RULE(ClassMemberList) - AST_RULE(ClassBlock) - AST_RULE(ClassDecl) - AST_RULE(GlobalValues) - AST_RULE(GlobalOp) - AST_RULE(Global) - AST_RULE(ExportDefault) - AST_RULE(Export) - AST_RULE(VariablePair) - AST_RULE(NormalPair) - AST_RULE(MetaVariablePair) - AST_RULE(MetaNormalPair) - AST_RULE(VariablePairDef) - AST_RULE(NormalPairDef) - AST_RULE(NormalDef) - AST_RULE(MetaVariablePairDef) - AST_RULE(MetaNormalPairDef) - AST_RULE(FnArgDef) - AST_RULE(FnArgDefList) - AST_RULE(OuterVarShadow) - AST_RULE(FnArgsDef) - AST_RULE(FnArrow) - AST_RULE(FunLit) - AST_RULE(MacroName) - AST_RULE(MacroLit) - AST_RULE(Macro) - AST_RULE(MacroInPlace) - AST_RULE(NameOrDestructure) - AST_RULE(AssignableNameList) - AST_RULE(InvokeArgs) - AST_RULE(ConstValue) - AST_RULE(UnaryValue) - AST_RULE(UnaryExp) - AST_RULE(InRangeOpen) - AST_RULE(InRangeClose) - AST_RULE(NotIn) - AST_RULE(InRange) - AST_RULE(InDiscrete) - AST_RULE(In) - AST_RULE(ExpListAssign) - AST_RULE(IfLine) - AST_RULE(WhileLine) - AST_RULE(BreakLoop) - AST_RULE(StatementAppendix) - AST_RULE(Statement) + AST_RULE(LuaString); + AST_RULE(SingleString); + AST_RULE(DoubleStringInner); + AST_RULE(DoubleStringContent); + AST_RULE(DoubleString); + AST_RULE(String); + AST_RULE(Parens); + AST_RULE(DotChainItem); + AST_RULE(ColonChainItem); + AST_RULE(Metatable); + AST_RULE(Metamethod); + AST_RULE(DefaultValue); + AST_RULE(Slice); + AST_RULE(Invoke); + AST_RULE(ExistentialOp); + AST_RULE(TableAppendingOp); + AST_RULE(SpreadExp); + AST_RULE(TableLit); + AST_RULE(TableBlock); + AST_RULE(TableBlockIndent); + AST_RULE(ClassMemberList); + AST_RULE(ClassBlock); + AST_RULE(ClassDecl); + AST_RULE(GlobalValues); + AST_RULE(GlobalOp); + AST_RULE(Global); + AST_RULE(ExportDefault); + AST_RULE(Export); + AST_RULE(VariablePair); + AST_RULE(NormalPair); + AST_RULE(MetaVariablePair); + AST_RULE(MetaNormalPair); + AST_RULE(VariablePairDef); + AST_RULE(NormalPairDef); + AST_RULE(NormalDef); + AST_RULE(MetaVariablePairDef); + AST_RULE(MetaNormalPairDef); + AST_RULE(FnArgDef); + AST_RULE(FnArgDefList); + AST_RULE(OuterVarShadow); + AST_RULE(FnArgsDef); + AST_RULE(FnArrow); + AST_RULE(FunLit); + AST_RULE(MacroName); + AST_RULE(MacroLit); + AST_RULE(Macro); + AST_RULE(MacroInPlace); + AST_RULE(NameOrDestructure); + AST_RULE(AssignableNameList); + AST_RULE(InvokeArgs); + AST_RULE(ConstValue); + AST_RULE(UnaryValue); + AST_RULE(UnaryExp); + AST_RULE(InRangeOpen); + AST_RULE(InRangeClose); + AST_RULE(NotIn); + AST_RULE(InRange); + AST_RULE(InDiscrete); + AST_RULE(In); + AST_RULE(ExpListAssign); + AST_RULE(IfLine); + AST_RULE(WhileLine); + AST_RULE(BreakLoop); + AST_RULE(StatementAppendix); + AST_RULE(Statement); AST_RULE(StatementSep); - AST_RULE(YueLineComment) - AST_RULE(MultilineCommentInner) - AST_RULE(YueMultilineComment) - AST_RULE(ChainAssign) - AST_RULE(Body) - AST_RULE(Block) - AST_RULE(BlockEnd) - AST_RULE(File) + AST_RULE(YueLineComment); + AST_RULE(MultilineCommentInner); + AST_RULE(YueMultilineComment); + AST_RULE(ChainAssign); + AST_RULE(Body); + AST_RULE(Block); + AST_RULE(BlockEnd); + AST_RULE(File); }; namespace Utils { diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 22ba2a1..3954fb9 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -240,11 +240,11 @@ static int yuetoast(lua_State* L) { lua_createtable(L, 0, 0); int cacheIndex = lua_gettop(L); auto getName = [&](yue::ast_node* node) { - int id = node->getId(); + int id = node->get_id(); lua_rawgeti(L, cacheIndex, id); if (lua_isnil(L, -1) != 0) { lua_pop(L, 1); - auto name = node->getName(); + auto name = node->get_name(); lua_pushlstring(L, &name.front(), name.length()); lua_pushvalue(L, -1); lua_rawseti(L, cacheIndex, id); @@ -268,7 +268,7 @@ static int yuetoast(lua_State* L) { switch (continuation) { case 0: { if (!current.children) { - node->visitChild([&](yue::ast_node* child) { + node->visit_child([&](yue::ast_node* child) { if (yue::ast_is(child)) { current.hasSep = true; return false; -- cgit v1.2.3-55-g6feb