From 3b270690501cfcc9220c8d5b63ab6f13fc2bd6b0 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 19 Jul 2017 15:26:04 +0800 Subject: stop implementing moon compiler. --- MoonParser/ast.hpp | 13 +- MoonParser/moon_ast.cpp | 393 +----------------------------------------------- MoonParser/moon_ast.h | 18 --- 3 files changed, 14 insertions(+), 410 deletions(-) diff --git a/MoonParser/ast.hpp b/MoonParser/ast.hpp index 1b40e1b..955cdc0 100644 --- a/MoonParser/ast.hpp +++ b/MoonParser/ast.hpp @@ -80,11 +80,20 @@ private: }; template -T* ast_cast(ast_node *node) -{ +T* ast_cast(ast_node *node) { return node && ast_type() == node->get_type() ? static_cast(node) : nullptr; } +template +bool ast_is(ast_node* node) { + if (!node) return false; + bool result = false; + int type = node->get_type(); + using swallow = bool[]; + (void)swallow{result || (result = ast_type() == type)...}; + return result; +} + class ast_member; diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp index 41d0c55..d8a0db9 100644 --- a/MoonParser/moon_ast.cpp +++ b/MoonParser/moon_ast.cpp @@ -7,125 +7,6 @@ #include #include "moon_ast.h" -class Data -{ -public: - Data() - { - indent = 0; - callerStack.push(false); - } - - Converter conv; - std::stringstream temp; - std::stringstream buffer; - std::vector lineTable; - std::stack callerStack; - std::stack withStack; - - void beginLine(int line = -1) - { - for (int i = 0; i < indent; i++) - { - buffer << '\t'; - } - lineTable.push_back(line == -1 ? lineTable.back() : line); - } - - void endLine() - { - buffer << '\n'; - } - - int indent; - struct Scope - { - Scope() - { - localObjIndex = 0; - localFnIndex = 0; - localBaseIndex = 0; - localWithIndex = 0; - } - int localObjIndex; - int localFnIndex; - int localBaseIndex; - int localWithIndex; - std::vector locals; - }; - - std::string getNewLocalObj() - { - temp << "_obj_" << scope().localObjIndex; - scope().localObjIndex++; - std::string local = temp.str(); - temp.str(""); - temp.clear(); - return local; - } - - std::string getNewLocalFn() - { - temp << "_fn_" << scope().localObjIndex; - scope().localFnIndex++; - std::string local = temp.str(); - temp.str(""); - temp.clear(); - return local; - } - - std::string getNewLocalBase() - { - temp << "_base_" << scope().localBaseIndex; - scope().localBaseIndex++; - std::string local = temp.str(); - temp.str(""); - temp.clear(); - return local; - } - - bool isLocal(const std::string& var) - { - return _localVars.find(var) != _localVars.end(); - } - - void putLocal(const std::string& var) - { - if (_localVars.find(var) == _localVars.end()) - { - _localVars.insert(var); - _scopeStack.top().locals.push_back(var); - } - } - - void pushScope() - { - int lastWithIndex = scope().localWithIndex; - _scopeStack.emplace(); - scope().localWithIndex = lastWithIndex + 1; - indent++; - } - - Scope& scope() - { - return _scopeStack.top(); - } - - void popScope() - { - const auto& scope = _scopeStack.top(); - for (const auto& var : scope.locals) - { - _localVars.erase(var); - } - _scopeStack.pop(); - indent--; - } -private: - std::unordered_set _localVars; - std::stack _scopeStack; -}; - std::string& trim(std::string& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) @@ -266,288 +147,20 @@ AST_IMPL(Line) AST_IMPL(Block) AST_IMPL(BlockEnd) -void Num_t::visit(void* ud) -{ - Data* data = static_cast(ud); - std::string str = data->conv.to_bytes(&*m_begin.m_it, &*m_end.m_it); - data->buffer << trim(str); -} - -void _Name_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << getValue(); -} - -void Name_t::visit(void* ud) -{ - name->visit(ud); -} - -void self_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << "self"; -} - -void self_name_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << (data->callerStack.top() ? "self:" : "self."); - name->visit(ud); -} - -void self_class_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << "self.__class"; -} - -void self_class_name_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << (data->callerStack.top() ? "self.__class:" : "self.__class."); - name->visit(ud); -} - -void SelfName_t::visit(void* ud) -{ - name->visit(ud); -} - -void KeyName_t::visit(void* ud) -{ - name->visit(ud); -} - -void VarArg_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << "..."; -} - -void NameList_t::visit(void* ud) -{ - Data* data = static_cast(ud); - auto it = names.objects().begin(); - Name_t* name = *it; - name->visit(ud); - ++it; - for (; it != names.objects().end(); ++it) - { - name = *it; - data->buffer << ", "; - name->visit(ud); - } -} - -void Import_t::visit(void* ud) -{ - Data* data = static_cast(ud); - std::vector> nameItems; - nameItems.reserve(names.objects().size()); - for (ImportName_t* importName : names.objects()) - { - if (Name_t* name = ast_cast(importName->name)) - { - nameItems.push_back(std::make_tuple(&name->name->getValue(), false)); - } - else - { - colon_import_name_t* colonName = ast_cast(importName->name); - nameItems.push_back(std::make_tuple(&colonName->name->name->getValue(), true)); - } - } - data->buffer << "local "; - for (const auto& item : nameItems) - { - data->buffer << *std::get<0>(item); - if (&item != &nameItems.back()) - { - data->buffer << ", "; - } - } - data->endLine(); - - data->beginLine(); - data->pushScope(); - data->buffer << "do"; - data->endLine(); - - std::string fromObj = data->getNewLocalObj(); - - data->beginLine(); - data->buffer << "local " << fromObj << " = "; - exp->visit(ud); - data->endLine(); - - data->beginLine(); - for (const auto& item : nameItems) - { - data->buffer << *std::get<0>(item); - if (&item != &nameItems.back()) - { - data->buffer << ", "; - } - } - data->buffer << " = "; - for (const auto& item : nameItems) - { - if (std::get<1>(item)) - { - data->pushScope(); - data->buffer << "(function()"; - data->endLine(); - - std::string varBase = data->getNewLocalBase(); - - data->beginLine(); - data->buffer << "local " << varBase << " = " << fromObj; - data->endLine(); - - std::string varFn = data->getNewLocalFn(); - - data->beginLine(); - data->buffer << "local " << varFn << " = " << varBase << '.' << *std::get<0>(item); - data->endLine(); - - data->beginLine(); - data->buffer << "return function(...)"; - data->endLine(); - - data->beginLine(); - data->pushScope(); - data->buffer << varFn << '(' << varBase << ", ...)"; - data->endLine(); - - data->beginLine(); - data->buffer << "end"; - data->popScope(); - data->endLine(); - - data->beginLine(); - data->buffer << "end)()"; - data->popScope(); - } - else - { - data->buffer << fromObj << '.' << *std::get<0>(item); - } - if (&item != &nameItems.back()) - { - data->buffer << ", "; - } - } - data->endLine(); - - data->beginLine(); - data->buffer << "end"; - data->popScope(); -} - -void ExpListLow_t::visit(void* ud) -{ - Data* data = static_cast(ud); - for (Exp_t* expr : exprs.objects()) - { - expr->visit(ud); - if (expr != exprs.objects().back()) - { - data->buffer << ", "; - } - } -} - -void ExpList_t::visit(void* ud) -{ - Data* data = static_cast(ud); - for (Exp_t* expr : exprs.objects()) - { - expr->visit(ud); - if (expr != exprs.objects().back()) - { - data->buffer << ", "; - } - } -} - -void Return_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << "return"; - if (valueList && !valueList->exprs.objects().empty()) - { - data->buffer << ' '; - valueList->visit(ud); - } -} - -void With_t::visit(void* ud) -{ - Data* data = static_cast(ud); - data->buffer << "return"; - for (Exp_t* expr : valueList->exprs.objects()) - { - if (assigns && (!expr->opValues.objects().empty() || expr->value->getFlattened())) - { - throw std::logic_error("left hand expression is not assignable."); - } - // TODO - } - if (valueList && !valueList->exprs.objects().empty()) - { - data->buffer << ' '; - valueList->visit(ud); - } -} - -ast_node* Value_t::getFlattened() -{ - if (SimpleValue_t* simpleValue = ast_cast(item)) - { - return simpleValue->value; - } - else if (simple_table_t* simple_table = ast_cast(item)) - { - return simple_table; - } - else if (ChainValue_t* chainValue = ast_cast(item)) - { - if (chainValue->arguments) - { - return chainValue; - } - else - { - if (Chain_t* chain = ast_cast(chainValue->caller)) - { - return chain->item; - } - else if (Callable_t* callable = ast_cast(chainValue->caller)) - { - return callable->item; - } - } - } - return item; -} - #include int main() { std::wstring_convert>, char32_t> conv; - std::string s = R"baddog(import \x, func, \memFunc from require "utils")baddog"; + std::string s = R"TestCodesHere()TestCodesHere"; input i = conv.from_bytes(s); error_list el; - Import_t* root = nullptr; + BlockEnd_t* root = nullptr; State st; - if (parse(i, Import, el, root, &st)) + if (parse(i, BlockEnd, el, root, &st)) { std::cout << "matched!\n"; - Data data; - root->visit(&data); } else { diff --git a/MoonParser/moon_ast.h b/MoonParser/moon_ast.h index 4da4b8d..7a0e805 100644 --- a/MoonParser/moon_ast.h +++ b/MoonParser/moon_ast.h @@ -39,48 +39,38 @@ public: \ }; AST_LEAF(Num) - virtual void visit(void* ud) override; AST_END(Num) AST_LEAF(_Name) - virtual void visit(void* ud) override; AST_END(_Name) AST_NODE(Name) ast_ptr<_Name_t> name; - virtual void visit(void* ud) override; AST_END(Name) AST_LEAF(self) - virtual void visit(void* ud) override; AST_END(self) AST_NODE(self_name) ast_ptr<_Name_t> name; - virtual void visit(void* ud) override; AST_END(self_name) AST_LEAF(self_class) - virtual void visit(void* ud) override; AST_END(self_class) AST_NODE(self_class_name) ast_ptr<_Name_t> name; - virtual void visit(void* ud) override; AST_END(self_class_name) AST_NODE(SelfName) ast_ptr name; // self_class_name_t | self_class_t | self_name_t | self_t - virtual void visit(void* ud) override; AST_END(SelfName) AST_NODE(KeyName) ast_ptr name; // SelfName_t | _Name_t - virtual void visit(void* ud) override; AST_END(KeyName) AST_LEAF(VarArg) - virtual void visit(void* ud) override; AST_END(VarArg) AST_LEAF(local_flag) @@ -92,8 +82,6 @@ AST_END(Seperator) AST_NODE(NameList) ast_ptr sep; ast_list names; - - virtual void visit(void* ud) override; AST_END(NameList) AST_NODE(Local) @@ -114,24 +102,20 @@ AST_NODE(Import) ast_ptr sep; ast_list names; ast_ptr exp; - virtual void visit(void* ud) override; AST_END(Import) AST_NODE(ExpListLow) ast_ptr sep; ast_list exprs; - virtual void visit(void* ud) override; AST_END(ExpListLow) AST_NODE(ExpList) ast_ptr sep; ast_list exprs; - virtual void visit(void* ud) override; AST_END(ExpList) AST_NODE(Return) ast_ptr valueList; - virtual void visit(void* ud) override; AST_END(Return) class Assign_t; @@ -141,7 +125,6 @@ AST_NODE(With) ast_ptr valueList; ast_ptr assigns; ast_ptr body; - virtual void visit(void* ud) override; AST_END(With) AST_NODE(SwitchCase) @@ -324,7 +307,6 @@ AST_END(Chain) AST_NODE(Value) ast_ptr item; // SimpleValue_t | simple_table_t | ChainValue_t | String_t - ast_node* getFlattened(); AST_END(Value) AST_LEAF(LuaString) -- cgit v1.2.3-55-g6feb