diff options
Diffstat (limited to 'MoonParser/main.cpp')
| -rw-r--r-- | MoonParser/main.cpp | 597 |
1 files changed, 529 insertions, 68 deletions
diff --git a/MoonParser/main.cpp b/MoonParser/main.cpp index 202a8df..d0ef5c8 100644 --- a/MoonParser/main.cpp +++ b/MoonParser/main.cpp | |||
| @@ -1,19 +1,36 @@ | |||
| 1 | #include <iostream> | 1 | #include <iostream> |
| 2 | using std::cout; | ||
| 3 | #include <string> | 2 | #include <string> |
| 4 | using std::string; | 3 | #include <codecvt> |
| 5 | #include <unordered_set> | 4 | #include <unordered_set> |
| 6 | using std::unordered_set; | ||
| 7 | #include <stack> | 5 | #include <stack> |
| 8 | using std::stack; | ||
| 9 | #include <algorithm> | 6 | #include <algorithm> |
| 10 | #include <sstream> | 7 | #include <sstream> |
| 11 | using std::stringstream; | ||
| 12 | #include <vector> | 8 | #include <vector> |
| 13 | using std::vector; | ||
| 14 | #include "parserlib.hpp" | 9 | #include "parserlib.hpp" |
| 15 | using namespace parserlib; | 10 | using namespace parserlib; |
| 16 | 11 | ||
| 12 | template<class Facet> | ||
| 13 | struct deletable_facet : Facet | ||
| 14 | { | ||
| 15 | template<class ...Args> | ||
| 16 | deletable_facet(Args&& ...args): Facet(std::forward<Args>(args)...) {} | ||
| 17 | ~deletable_facet() {} | ||
| 18 | }; | ||
| 19 | typedef std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> Converter; | ||
| 20 | |||
| 21 | static inline std::string& trim(std::string& s) | ||
| 22 | { | ||
| 23 | s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) | ||
| 24 | { | ||
| 25 | return !std::isspace(ch); | ||
| 26 | })); | ||
| 27 | s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) | ||
| 28 | { | ||
| 29 | return !std::isspace(ch); | ||
| 30 | }).base(), s.end()); | ||
| 31 | return s; | ||
| 32 | } | ||
| 33 | |||
| 17 | #define p(expr) user(expr, [](const item_t& item) \ | 34 | #define p(expr) user(expr, [](const item_t& item) \ |
| 18 | { \ | 35 | { \ |
| 19 | stringstream stream; \ | 36 | stringstream stream; \ |
| @@ -29,11 +46,11 @@ struct State | |||
| 29 | indents.push(0); | 46 | indents.push(0); |
| 30 | stringOpen = -1; | 47 | stringOpen = -1; |
| 31 | } | 48 | } |
| 32 | stringstream buffer; | 49 | std::stringstream buffer; |
| 33 | size_t stringOpen; | 50 | size_t stringOpen; |
| 34 | stack<int> indents; | 51 | std::stack<int> indents; |
| 35 | stack<bool> doStack; | 52 | std::stack<bool> doStack; |
| 36 | unordered_set<string> keywords = { | 53 | std::unordered_set<std::string> keywords = { |
| 37 | "and", "while", "else", "using", "continue", | 54 | "and", "while", "else", "using", "continue", |
| 38 | "local", "not", "then", "return", "from", | 55 | "local", "not", "then", "return", "from", |
| 39 | "extends", "for", "do", "or", "export", | 56 | "extends", "for", "do", "or", "export", |
| @@ -42,9 +59,123 @@ struct State | |||
| 42 | }; | 59 | }; |
| 43 | }; | 60 | }; |
| 44 | 61 | ||
| 45 | struct Data | 62 | class Data |
| 46 | { | 63 | { |
| 47 | vector<char> bytes; | 64 | public: |
| 65 | Data() | ||
| 66 | { | ||
| 67 | indent = 0; | ||
| 68 | callerStack.push(false); | ||
| 69 | } | ||
| 70 | |||
| 71 | Converter conv; | ||
| 72 | std::stringstream temp; | ||
| 73 | std::stringstream buffer; | ||
| 74 | std::vector<int> lineTable; | ||
| 75 | std::stack<bool> callerStack; | ||
| 76 | std::stack<std::string> withStack; | ||
| 77 | |||
| 78 | void beginLine(int line = -1) | ||
| 79 | { | ||
| 80 | for (int i = 0; i < indent; i++) | ||
| 81 | { | ||
| 82 | buffer << '\t'; | ||
| 83 | } | ||
| 84 | lineTable.push_back(line == -1 ? lineTable.back() : line); | ||
| 85 | } | ||
| 86 | |||
| 87 | void endLine() | ||
| 88 | { | ||
| 89 | buffer << '\n'; | ||
| 90 | } | ||
| 91 | |||
| 92 | int indent; | ||
| 93 | struct Scope | ||
| 94 | { | ||
| 95 | Scope() | ||
| 96 | { | ||
| 97 | localObjIndex = 0; | ||
| 98 | localFnIndex = 0; | ||
| 99 | localBaseIndex = 0; | ||
| 100 | localWithIndex = 0; | ||
| 101 | } | ||
| 102 | int localObjIndex; | ||
| 103 | int localFnIndex; | ||
| 104 | int localBaseIndex; | ||
| 105 | int localWithIndex; | ||
| 106 | std::vector<std::string> locals; | ||
| 107 | }; | ||
| 108 | |||
| 109 | std::string getNewLocalObj() | ||
| 110 | { | ||
| 111 | temp << "_obj_" << scope().localObjIndex; | ||
| 112 | scope().localObjIndex++; | ||
| 113 | std::string local = temp.str(); | ||
| 114 | temp.str(""); | ||
| 115 | temp.clear(); | ||
| 116 | return local; | ||
| 117 | } | ||
| 118 | |||
| 119 | std::string getNewLocalFn() | ||
| 120 | { | ||
| 121 | temp << "_fn_" << scope().localObjIndex; | ||
| 122 | scope().localFnIndex++; | ||
| 123 | std::string local = temp.str(); | ||
| 124 | temp.str(""); | ||
| 125 | temp.clear(); | ||
| 126 | return local; | ||
| 127 | } | ||
| 128 | |||
| 129 | std::string getNewLocalBase() | ||
| 130 | { | ||
| 131 | temp << "_base_" << scope().localBaseIndex; | ||
| 132 | scope().localBaseIndex++; | ||
| 133 | std::string local = temp.str(); | ||
| 134 | temp.str(""); | ||
| 135 | temp.clear(); | ||
| 136 | return local; | ||
| 137 | } | ||
| 138 | |||
| 139 | bool isLocal(const std::string& var) | ||
| 140 | { | ||
| 141 | return _localVars.find(var) != _localVars.end(); | ||
| 142 | } | ||
| 143 | |||
| 144 | void putLocal(const std::string& var) | ||
| 145 | { | ||
| 146 | if (_localVars.find(var) == _localVars.end()) | ||
| 147 | { | ||
| 148 | _localVars.insert(var); | ||
| 149 | _scopeStack.top().locals.push_back(var); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | void pushScope() | ||
| 154 | { | ||
| 155 | int lastWithIndex = scope().localWithIndex; | ||
| 156 | _scopeStack.emplace(); | ||
| 157 | scope().localWithIndex = lastWithIndex + 1; | ||
| 158 | indent++; | ||
| 159 | } | ||
| 160 | |||
| 161 | Scope& scope() | ||
| 162 | { | ||
| 163 | return _scopeStack.top(); | ||
| 164 | } | ||
| 165 | |||
| 166 | void popScope() | ||
| 167 | { | ||
| 168 | const auto& scope = _scopeStack.top(); | ||
| 169 | for (const auto& var : scope.locals) | ||
| 170 | { | ||
| 171 | _localVars.erase(var); | ||
| 172 | } | ||
| 173 | _scopeStack.pop(); | ||
| 174 | indent--; | ||
| 175 | } | ||
| 176 | private: | ||
| 177 | std::unordered_set<std::string> _localVars; | ||
| 178 | std::stack<Scope> _scopeStack; | ||
| 48 | }; | 179 | }; |
| 49 | 180 | ||
| 50 | rule Any = any(); | 181 | rule Any = any(); |
| @@ -88,8 +219,8 @@ rule Seperator = true_(); | |||
| 88 | rule Name = user(SpaceName, [](const item_t& item) | 219 | rule Name = user(SpaceName, [](const item_t& item) |
| 89 | { | 220 | { |
| 90 | State* st = reinterpret_cast<State*>(item.user_data); | 221 | State* st = reinterpret_cast<State*>(item.user_data); |
| 91 | for (input_it i = item.begin; i != item.end; ++i) st->buffer << static_cast<char>(*i); | 222 | for (auto it = item.begin; it != item.end; ++it) st->buffer << static_cast<char>(*it); |
| 92 | string name; | 223 | std::string name; |
| 93 | st->buffer >> name; | 224 | st->buffer >> name; |
| 94 | st->buffer.str(""); | 225 | st->buffer.str(""); |
| 95 | st->buffer.clear(); | 226 | st->buffer.clear(); |
| @@ -395,7 +526,8 @@ rule ChainItems = Seperator >> (chain_with_colon | ColonChain); | |||
| 395 | 526 | ||
| 396 | extern rule Invoke, Slice; | 527 | extern rule Invoke, Slice; |
| 397 | 528 | ||
| 398 | rule ChainItem = Invoke | DotChainItem | Slice | symx('[') >> Exp >> sym(']'); | 529 | rule Index = symx('[') >> Exp >> sym(']'); |
| 530 | rule ChainItem = Invoke | DotChainItem | Slice | Index; | ||
| 399 | rule DotChainItem = symx('.') >> _Name; | 531 | rule DotChainItem = symx('.') >> _Name; |
| 400 | rule ColonChainItem = symx('\\') >> _Name; | 532 | rule ColonChainItem = symx('\\') >> _Name; |
| 401 | rule invoke_chain = Invoke >> -ChainItems; | 533 | rule invoke_chain = Invoke >> -ChainItems; |
| @@ -553,8 +685,28 @@ rule Line = CheckIndent >> Statement | empty_line_stop; | |||
| 553 | rule Block = Seperator >> Line >> *(+Break >> Line); | 685 | rule Block = Seperator >> Line >> *(+Break >> Line); |
| 554 | rule BlockEnd = Block >> eof(); | 686 | rule BlockEnd = Block >> eof(); |
| 555 | 687 | ||
| 688 | class AstLeaf : public ast_node | ||
| 689 | { | ||
| 690 | public: | ||
| 691 | const std::string& getValue() | ||
| 692 | { | ||
| 693 | if (_value.empty()) | ||
| 694 | { | ||
| 695 | for (auto it = m_begin.m_it; it != m_end.m_it; ++it) | ||
| 696 | { | ||
| 697 | char ch = static_cast<char>(*it); | ||
| 698 | _value.append(&ch, 1); | ||
| 699 | } | ||
| 700 | return trim(_value); | ||
| 701 | } | ||
| 702 | return _value; | ||
| 703 | } | ||
| 704 | private: | ||
| 705 | std::string _value; | ||
| 706 | }; | ||
| 707 | |||
| 556 | #define AST_LEAF(type) \ | 708 | #define AST_LEAF(type) \ |
| 557 | class type##_t : public ast_node \ | 709 | class type##_t : public AstLeaf \ |
| 558 | { \ | 710 | { \ |
| 559 | public: \ | 711 | public: \ |
| 560 | virtual int get_type() override { return ast_type<type##_t>(); } | 712 | virtual int get_type() override { return ast_type<type##_t>(); } |
| @@ -570,59 +722,88 @@ public: \ | |||
| 570 | ast<type##_t> __##type##_t(type); | 722 | ast<type##_t> __##type##_t(type); |
| 571 | 723 | ||
| 572 | AST_LEAF(Num) | 724 | AST_LEAF(Num) |
| 573 | double value; | ||
| 574 | virtual void visit(void* ud) override | 725 | virtual void visit(void* ud) override |
| 575 | { | 726 | { |
| 576 | stringstream stream; | 727 | Data* data = static_cast<Data*>(ud); |
| 577 | for (input::iterator it = m_begin.m_it; it != m_end.m_it; ++it) | 728 | std::string str = data->conv.to_bytes(&*m_begin.m_it, &*m_end.m_it); |
| 578 | { | 729 | data->buffer << trim(str); |
| 579 | stream << static_cast<char>(*it); | ||
| 580 | } | ||
| 581 | stream >> value; | ||
| 582 | } | 730 | } |
| 583 | AST_END(Num) | 731 | AST_END(Num) |
| 584 | 732 | ||
| 585 | AST_LEAF(_Name) | 733 | AST_LEAF(_Name) |
| 586 | virtual void construct(ast_stack& st) override | 734 | virtual void visit(void* ud) override |
| 587 | { | 735 | { |
| 588 | stringstream stream; | 736 | Data* data = static_cast<Data*>(ud); |
| 589 | for (input::iterator it = m_begin.m_it; it != m_end.m_it; ++it) | 737 | data->buffer << getValue(); |
| 590 | { | ||
| 591 | stream << (char)*it; | ||
| 592 | } | ||
| 593 | stream >> value; | ||
| 594 | ast_node::construct(st); | ||
| 595 | } | 738 | } |
| 596 | string value; | ||
| 597 | AST_END(_Name) | 739 | AST_END(_Name) |
| 598 | 740 | ||
| 599 | AST_NODE(Name) | 741 | AST_NODE(Name) |
| 600 | ast_ptr<_Name_t> name; | 742 | ast_ptr<_Name_t> name; |
| 743 | virtual void visit(void* ud) override | ||
| 744 | { | ||
| 745 | name->visit(ud); | ||
| 746 | } | ||
| 601 | AST_END(Name) | 747 | AST_END(Name) |
| 602 | 748 | ||
| 603 | AST_LEAF(self) | 749 | AST_LEAF(self) |
| 750 | virtual void visit(void* ud) override | ||
| 751 | { | ||
| 752 | Data* data = static_cast<Data*>(ud); | ||
| 753 | data->buffer << "self"; | ||
| 754 | } | ||
| 604 | AST_END(self) | 755 | AST_END(self) |
| 605 | 756 | ||
| 606 | AST_NODE(self_name) | 757 | AST_NODE(self_name) |
| 607 | ast_ptr<_Name_t> name; | 758 | ast_ptr<_Name_t> name; |
| 759 | virtual void visit(void* ud) override | ||
| 760 | { | ||
| 761 | Data* data = static_cast<Data*>(ud); | ||
| 762 | data->buffer << (data->callerStack.top() ? "self:" : "self."); | ||
| 763 | name->visit(ud); | ||
| 764 | } | ||
| 608 | AST_END(self_name) | 765 | AST_END(self_name) |
| 609 | 766 | ||
| 610 | AST_LEAF(self_class) | 767 | AST_LEAF(self_class) |
| 768 | virtual void visit(void* ud) override | ||
| 769 | { | ||
| 770 | Data* data = static_cast<Data*>(ud); | ||
| 771 | data->buffer << "self.__class"; | ||
| 772 | } | ||
| 611 | AST_END(self_class) | 773 | AST_END(self_class) |
| 612 | 774 | ||
| 613 | AST_NODE(self_class_name) | 775 | AST_NODE(self_class_name) |
| 614 | ast_ptr<_Name_t> name; | 776 | ast_ptr<_Name_t> name; |
| 777 | virtual void visit(void* ud) override | ||
| 778 | { | ||
| 779 | Data* data = static_cast<Data*>(ud); | ||
| 780 | data->buffer << (data->callerStack.top() ? "self.__class:" : "self.__class."); | ||
| 781 | name->visit(ud); | ||
| 782 | } | ||
| 615 | AST_END(self_class_name) | 783 | AST_END(self_class_name) |
| 616 | 784 | ||
| 617 | AST_NODE(SelfName) | 785 | AST_NODE(SelfName) |
| 618 | ast_ptr<ast_node> name; | 786 | ast_ptr<ast_node> name; // self_class_name_t | self_class_t | self_name_t | self_t |
| 787 | virtual void visit(void* ud) override | ||
| 788 | { | ||
| 789 | name->visit(ud); | ||
| 790 | } | ||
| 619 | AST_END(SelfName) | 791 | AST_END(SelfName) |
| 620 | 792 | ||
| 621 | AST_NODE(KeyName) | 793 | AST_NODE(KeyName) |
| 622 | ast_ptr<ast_node> name; | 794 | ast_ptr<ast_node> name; // SelfName_t | _Name_t |
| 795 | virtual void visit(void* ud) override | ||
| 796 | { | ||
| 797 | name->visit(ud); | ||
| 798 | } | ||
| 623 | AST_END(KeyName) | 799 | AST_END(KeyName) |
| 624 | 800 | ||
| 625 | AST_LEAF(VarArg) | 801 | AST_LEAF(VarArg) |
| 802 | virtual void visit(void* ud) override | ||
| 803 | { | ||
| 804 | Data* data = static_cast<Data*>(ud); | ||
| 805 | data->buffer << "..."; | ||
| 806 | } | ||
| 626 | AST_END(VarArg) | 807 | AST_END(VarArg) |
| 627 | 808 | ||
| 628 | AST_LEAF(local_flag) | 809 | AST_LEAF(local_flag) |
| @@ -634,6 +815,20 @@ AST_END(Seperator) | |||
| 634 | AST_NODE(NameList) | 815 | AST_NODE(NameList) |
| 635 | ast_ptr<Seperator_t> sep; | 816 | ast_ptr<Seperator_t> sep; |
| 636 | ast_list<Name_t> names; | 817 | ast_list<Name_t> names; |
| 818 | virtual void visit(void* ud) override | ||
| 819 | { | ||
| 820 | Data* data = static_cast<Data*>(ud); | ||
| 821 | auto it = names.objects().begin(); | ||
| 822 | Name_t* name = *it; | ||
| 823 | name->visit(ud); | ||
| 824 | ++it; | ||
| 825 | for (; it != names.objects().end(); ++it) | ||
| 826 | { | ||
| 827 | name = *it; | ||
| 828 | data->buffer << ", "; | ||
| 829 | name->visit(ud); | ||
| 830 | } | ||
| 831 | } | ||
| 637 | AST_END(NameList) | 832 | AST_END(NameList) |
| 638 | 833 | ||
| 639 | AST_NODE(Local) | 834 | AST_NODE(Local) |
| @@ -654,20 +849,157 @@ AST_NODE(Import) | |||
| 654 | ast_ptr<Seperator_t> sep; | 849 | ast_ptr<Seperator_t> sep; |
| 655 | ast_list<ImportName_t> names; | 850 | ast_list<ImportName_t> names; |
| 656 | ast_ptr<Exp_t> exp; | 851 | ast_ptr<Exp_t> exp; |
| 852 | virtual void visit(void* ud) override | ||
| 853 | { | ||
| 854 | Data* data = static_cast<Data*>(ud); | ||
| 855 | std::vector<std::tuple<const std::string*, bool>> nameItems; | ||
| 856 | nameItems.reserve(names.objects().size()); | ||
| 857 | for (ImportName_t* importName : names.objects()) | ||
| 858 | { | ||
| 859 | if (Name_t* name = ast_cast<Name_t>(importName->name)) | ||
| 860 | { | ||
| 861 | nameItems.push_back(std::make_tuple(&name->name->getValue(), false)); | ||
| 862 | } | ||
| 863 | else | ||
| 864 | { | ||
| 865 | colon_import_name_t* colonName = ast_cast<colon_import_name_t>(importName->name); | ||
| 866 | nameItems.push_back(std::make_tuple(&colonName->name->name->getValue(), true)); | ||
| 867 | } | ||
| 868 | } | ||
| 869 | data->buffer << "local "; | ||
| 870 | for (const auto& item : nameItems) | ||
| 871 | { | ||
| 872 | data->buffer << *std::get<0>(item); | ||
| 873 | if (&item != &nameItems.back()) | ||
| 874 | { | ||
| 875 | data->buffer << ", "; | ||
| 876 | } | ||
| 877 | } | ||
| 878 | data->endLine(); | ||
| 879 | |||
| 880 | data->beginLine(); | ||
| 881 | data->pushScope(); | ||
| 882 | data->buffer << "do"; | ||
| 883 | data->endLine(); | ||
| 884 | |||
| 885 | std::string fromObj = data->getNewLocalObj(); | ||
| 886 | |||
| 887 | data->beginLine(); | ||
| 888 | data->buffer << "local " << fromObj << " = "; | ||
| 889 | ((ast_node*)exp.get())->visit(ud); | ||
| 890 | data->endLine(); | ||
| 891 | |||
| 892 | data->beginLine(); | ||
| 893 | for (const auto& item : nameItems) | ||
| 894 | { | ||
| 895 | data->buffer << *std::get<0>(item); | ||
| 896 | if (&item != &nameItems.back()) | ||
| 897 | { | ||
| 898 | data->buffer << ", "; | ||
| 899 | } | ||
| 900 | } | ||
| 901 | data->buffer << " = "; | ||
| 902 | for (const auto& item : nameItems) | ||
| 903 | { | ||
| 904 | if (std::get<1>(item)) | ||
| 905 | { | ||
| 906 | data->pushScope(); | ||
| 907 | data->buffer << "(function()"; | ||
| 908 | data->endLine(); | ||
| 909 | |||
| 910 | std::string varBase = data->getNewLocalBase(); | ||
| 911 | |||
| 912 | data->beginLine(); | ||
| 913 | data->buffer << "local " << varBase << " = " << fromObj; | ||
| 914 | data->endLine(); | ||
| 915 | |||
| 916 | std::string varFn = data->getNewLocalFn(); | ||
| 917 | |||
| 918 | data->beginLine(); | ||
| 919 | data->buffer << "local " << varFn << " = " << varBase << '.' << *std::get<0>(item); | ||
| 920 | data->endLine(); | ||
| 921 | |||
| 922 | data->beginLine(); | ||
| 923 | data->buffer << "return function(...)"; | ||
| 924 | data->endLine(); | ||
| 925 | |||
| 926 | data->beginLine(); | ||
| 927 | data->pushScope(); | ||
| 928 | data->buffer << varFn << '(' << varBase << ", ...)"; | ||
| 929 | data->endLine(); | ||
| 930 | |||
| 931 | data->beginLine(); | ||
| 932 | data->buffer << "end"; | ||
| 933 | data->popScope(); | ||
| 934 | data->endLine(); | ||
| 935 | |||
| 936 | data->beginLine(); | ||
| 937 | data->buffer << "end)()"; | ||
| 938 | data->popScope(); | ||
| 939 | } | ||
| 940 | else | ||
| 941 | { | ||
| 942 | data->buffer << fromObj << '.' << *std::get<0>(item); | ||
| 943 | } | ||
| 944 | if (&item != &nameItems.back()) | ||
| 945 | { | ||
| 946 | data->buffer << ", "; | ||
| 947 | } | ||
| 948 | } | ||
| 949 | data->endLine(); | ||
| 950 | |||
| 951 | data->beginLine(); | ||
| 952 | data->buffer << "end"; | ||
| 953 | data->popScope(); | ||
| 954 | } | ||
| 657 | AST_END(Import) | 955 | AST_END(Import) |
| 658 | 956 | ||
| 659 | AST_NODE(ExpListLow) | 957 | AST_NODE(ExpListLow) |
| 660 | ast_ptr<Seperator_t> sep; | 958 | ast_ptr<Seperator_t> sep; |
| 661 | ast_list<Exp_t> exprs; | 959 | ast_list<Exp_t> exprs; |
| 960 | virtual void visit(void* ud) override | ||
| 961 | { | ||
| 962 | Data* data = static_cast<Data*>(ud); | ||
| 963 | for (Exp_t* expr : exprs.objects()) | ||
| 964 | { | ||
| 965 | ((ast_node*)expr)->visit(ud); | ||
| 966 | if (expr != exprs.objects().back()) | ||
| 967 | { | ||
| 968 | data->buffer << ", "; | ||
| 969 | } | ||
| 970 | } | ||
| 971 | } | ||
| 662 | AST_END(ExpListLow) | 972 | AST_END(ExpListLow) |
| 663 | 973 | ||
| 664 | AST_NODE(ExpList) | 974 | AST_NODE(ExpList) |
| 665 | ast_ptr<Seperator_t> sep; | 975 | ast_ptr<Seperator_t> sep; |
| 666 | ast_list<Exp_t> exprs; | 976 | ast_list<Exp_t> exprs; |
| 977 | virtual void visit(void* ud) override | ||
| 978 | { | ||
| 979 | Data* data = static_cast<Data*>(ud); | ||
| 980 | for (Exp_t* expr : exprs.objects()) | ||
| 981 | { | ||
| 982 | ((ast_node*)expr)->visit(ud); | ||
| 983 | if (expr != exprs.objects().back()) | ||
| 984 | { | ||
| 985 | data->buffer << ", "; | ||
| 986 | } | ||
| 987 | } | ||
| 988 | } | ||
| 667 | AST_END(ExpList) | 989 | AST_END(ExpList) |
| 668 | 990 | ||
| 669 | AST_NODE(Return) | 991 | AST_NODE(Return) |
| 670 | ast_ptr<ExpListLow_t, true> valueList; | 992 | ast_ptr<ExpListLow_t, true> valueList; |
| 993 | virtual void visit(void* ud) override | ||
| 994 | { | ||
| 995 | Data* data = static_cast<Data*>(ud); | ||
| 996 | data->buffer << "return"; | ||
| 997 | if (valueList && !valueList->exprs.objects().empty()) | ||
| 998 | { | ||
| 999 | data->buffer << ' '; | ||
| 1000 | valueList->visit(ud); | ||
| 1001 | } | ||
| 1002 | } | ||
| 671 | AST_END(Return) | 1003 | AST_END(Return) |
| 672 | 1004 | ||
| 673 | class Assign_t; | 1005 | class Assign_t; |
| @@ -677,6 +1009,18 @@ AST_NODE(With) | |||
| 677 | ast_ptr<ExpList_t> valueList; | 1009 | ast_ptr<ExpList_t> valueList; |
| 678 | ast_ptr<Assign_t, true> assigns; | 1010 | ast_ptr<Assign_t, true> assigns; |
| 679 | ast_ptr<Body_t> body; | 1011 | ast_ptr<Body_t> body; |
| 1012 | /* | ||
| 1013 | (function() | ||
| 1014 | do | ||
| 1015 | local _with_0 = Something() | ||
| 1016 | _with_0:write("hello world") | ||
| 1017 | return _with_0 | ||
| 1018 | end | ||
| 1019 | end)() | ||
| 1020 | */ | ||
| 1021 | virtual void visit(void* ud) override | ||
| 1022 | { | ||
| 1023 | } | ||
| 680 | AST_END(With) | 1024 | AST_END(With) |
| 681 | 1025 | ||
| 682 | AST_NODE(SwitchCase) | 1026 | AST_NODE(SwitchCase) |
| @@ -833,6 +1177,11 @@ class InvokeArgs_t; | |||
| 833 | AST_NODE(ChainValue) | 1177 | AST_NODE(ChainValue) |
| 834 | ast_ptr<ast_node> caller; // Chain_t | Callable_t | 1178 | ast_ptr<ast_node> caller; // Chain_t | Callable_t |
| 835 | ast_ptr<InvokeArgs_t, true> arguments; | 1179 | ast_ptr<InvokeArgs_t, true> arguments; |
| 1180 | |||
| 1181 | virtual void visit(void* ud) override | ||
| 1182 | { | ||
| 1183 | |||
| 1184 | } | ||
| 836 | AST_END(ChainValue) | 1185 | AST_END(ChainValue) |
| 837 | 1186 | ||
| 838 | class KeyValue_t; | 1187 | class KeyValue_t; |
| @@ -843,10 +1192,52 @@ AST_NODE(simple_table) | |||
| 843 | AST_END(simple_table) | 1192 | AST_END(simple_table) |
| 844 | 1193 | ||
| 845 | class String_t; | 1194 | class String_t; |
| 846 | class SimpleValue_t; | 1195 | |
| 1196 | AST_NODE(SimpleValue) | ||
| 1197 | ast_ptr<ast_node> value; /* | ||
| 1198 | const_value_t | | ||
| 1199 | If_t | Unless_t | Switch_t | With_t | ClassDecl_t | ForEach_t | For_t | While_t | Do_t | | ||
| 1200 | unary_exp_t | | ||
| 1201 | TblComprehension_t | TableLit_t | Comprehension_t | FunLit_t | Num_t; | ||
| 1202 | */ | ||
| 1203 | AST_END(SimpleValue) | ||
| 1204 | |||
| 1205 | AST_NODE(Chain) | ||
| 1206 | ast_ptr<ast_node> item; // chain_call_t | chain_item_t | chain_dot_chain_t | ColonChain_t | ||
| 1207 | AST_END(Chain) | ||
| 847 | 1208 | ||
| 848 | AST_NODE(Value) | 1209 | AST_NODE(Value) |
| 849 | ast_ptr<ast_node> item; // SimpleValue_t | simple_table_t | ChainValue_t | String_t | 1210 | ast_ptr<ast_node> item; // SimpleValue_t | simple_table_t | ChainValue_t | String_t |
| 1211 | virtual ast_node* get_flattened() override | ||
| 1212 | { | ||
| 1213 | if (SimpleValue_t* simpleValue = ast_cast<SimpleValue_t>(item)) | ||
| 1214 | { | ||
| 1215 | return simpleValue->value; | ||
| 1216 | } | ||
| 1217 | else if (simple_table_t* simple_table = ast_cast<simple_table_t>(item)) | ||
| 1218 | { | ||
| 1219 | return simple_table; | ||
| 1220 | } | ||
| 1221 | else if (ChainValue_t* chainValue = ast_cast<ChainValue_t>(item)) | ||
| 1222 | { | ||
| 1223 | if (chainValue->arguments) | ||
| 1224 | { | ||
| 1225 | return chainValue; | ||
| 1226 | } | ||
| 1227 | else | ||
| 1228 | { | ||
| 1229 | if (Chain_t* chain = ast_cast<Chain_t>(chainValue->caller)) | ||
| 1230 | { | ||
| 1231 | return chain->item; | ||
| 1232 | } | ||
| 1233 | else if (Callable_t* callable = ast_cast<Callable_t>(chainValue->caller)) | ||
| 1234 | { | ||
| 1235 | return callable->item; | ||
| 1236 | } | ||
| 1237 | } | ||
| 1238 | } | ||
| 1239 | return item; | ||
| 1240 | } | ||
| 850 | AST_END(Value) | 1241 | AST_END(Value) |
| 851 | 1242 | ||
| 852 | AST_LEAF(LuaString) | 1243 | AST_LEAF(LuaString) |
| @@ -906,10 +1297,6 @@ AST_END(chain_dot_chain) | |||
| 906 | 1297 | ||
| 907 | class ColonChain_t; | 1298 | class ColonChain_t; |
| 908 | 1299 | ||
| 909 | AST_NODE(Chain) | ||
| 910 | ast_ptr<ast_node> item; // chain_call_t | chain_item_t | chain_dot_chain_t | ColonChain_t | ||
| 911 | AST_END(Chain) | ||
| 912 | |||
| 913 | class Invoke_t; | 1300 | class Invoke_t; |
| 914 | class Slice_t; | 1301 | class Slice_t; |
| 915 | 1302 | ||
| @@ -1068,15 +1455,6 @@ AST_NODE(unary_exp) | |||
| 1068 | ast_ptr<Exp_t> item; | 1455 | ast_ptr<Exp_t> item; |
| 1069 | AST_END(unary_exp) | 1456 | AST_END(unary_exp) |
| 1070 | 1457 | ||
| 1071 | AST_NODE(SimpleValue) | ||
| 1072 | ast_ptr<ast_node> value; /* | ||
| 1073 | const_value_t | | ||
| 1074 | If_t | Unless_t | Switch_t | With_t | ClassDecl_t | ForEach_t | For_t | While_t | Do_t | | ||
| 1075 | unary_exp_t | | ||
| 1076 | TblComprehension_t | TableLit_t | Comprehension_t | FunLit_t | Num_t; | ||
| 1077 | */ | ||
| 1078 | AST_END(SimpleValue) | ||
| 1079 | |||
| 1080 | AST_NODE(Assignment) | 1458 | AST_NODE(Assignment) |
| 1081 | ast_ptr<ExpList_t> assignable; | 1459 | ast_ptr<ExpList_t> assignable; |
| 1082 | ast_ptr<ast_node> target; // Update_t | Assign_t | 1460 | ast_ptr<ast_node> target; // Update_t | Assign_t |
| @@ -1099,7 +1477,7 @@ AST_LEAF(BreakLoop) | |||
| 1099 | AST_END(BreakLoop) | 1477 | AST_END(BreakLoop) |
| 1100 | 1478 | ||
| 1101 | AST_NODE(Statement) | 1479 | AST_NODE(Statement) |
| 1102 | ast_ptr<ast_node> body; /* | 1480 | ast_ptr<ast_node> content; /* |
| 1103 | Import_t | While_t | With_t | For_t | ForEach_t | | 1481 | Import_t | While_t | With_t | For_t | ForEach_t | |
| 1104 | Switch_t | Return_t | Local_t | Export_t | BreakLoop_t | | 1482 | Switch_t | Return_t | Local_t | Export_t | BreakLoop_t | |
| 1105 | Assignment_t | ExpList_t | 1483 | Assignment_t | ExpList_t |
| @@ -1108,26 +1486,29 @@ AST_NODE(Statement) | |||
| 1108 | 1486 | ||
| 1109 | virtual void construct(ast_stack& st) override | 1487 | virtual void construct(ast_stack& st) override |
| 1110 | { | 1488 | { |
| 1111 | stringstream stream; | 1489 | std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> conv; |
| 1112 | for (input::iterator it = m_begin.m_it; it != m_end.m_it; ++it) | 1490 | value = conv.to_bytes(&*m_begin.m_it, &*m_end.m_it); |
| 1113 | { | ||
| 1114 | stream << (char)*it; | ||
| 1115 | } | ||
| 1116 | value = stream.str(); | ||
| 1117 | ast_container::construct(st); | 1491 | ast_container::construct(st); |
| 1118 | } | 1492 | } |
| 1119 | 1493 | ||
| 1120 | virtual void visit(void* ud) override | 1494 | virtual void visit(void* ud) override |
| 1121 | { | 1495 | { |
| 1122 | cout << value << '\n'; | 1496 | std::cout << value << '\n'; |
| 1123 | } | 1497 | } |
| 1124 | string value; | 1498 | std::string value; |
| 1125 | AST_END(Statement) | 1499 | AST_END(Statement) |
| 1126 | 1500 | ||
| 1127 | class Block_t; | 1501 | class Block_t; |
| 1128 | 1502 | ||
| 1129 | AST_NODE(Body) | 1503 | AST_NODE(Body) |
| 1130 | ast_ptr<ast_node> content; // Block | Statement | 1504 | ast_ptr<ast_node> content; // Block | Statement |
| 1505 | virtual void visit(void* ud) override | ||
| 1506 | { | ||
| 1507 | Data* data = static_cast<Data*>(ud); | ||
| 1508 | data->pushScope(); | ||
| 1509 | content->visit(ud); | ||
| 1510 | data->popScope(); | ||
| 1511 | } | ||
| 1131 | AST_END(Body) | 1512 | AST_END(Body) |
| 1132 | 1513 | ||
| 1133 | AST_NODE(Line) | 1514 | AST_NODE(Line) |
| @@ -1144,7 +1525,7 @@ AST_NODE(Line) | |||
| 1144 | { | 1525 | { |
| 1145 | if (statment) | 1526 | if (statment) |
| 1146 | { | 1527 | { |
| 1147 | cout << line << ": "; | 1528 | std::cout << line << ": "; |
| 1148 | statment->visit(ud); | 1529 | statment->visit(ud); |
| 1149 | } | 1530 | } |
| 1150 | } | 1531 | } |
| @@ -1153,37 +1534,117 @@ AST_END(Line) | |||
| 1153 | AST_NODE(Block) | 1534 | AST_NODE(Block) |
| 1154 | ast_ptr<Seperator_t> sep; | 1535 | ast_ptr<Seperator_t> sep; |
| 1155 | ast_list<Line_t> lines; | 1536 | ast_list<Line_t> lines; |
| 1156 | |||
| 1157 | virtual void visit(void* ud) override | 1537 | virtual void visit(void* ud) override |
| 1158 | { | 1538 | { |
| 1159 | for (auto item : lines.objects()) | 1539 | Data* data = static_cast<Data*>(ud); |
| 1540 | const auto& objs = lines.objects(); | ||
| 1541 | for (auto it = objs.begin(); it != objs.end(); ++it) | ||
| 1160 | { | 1542 | { |
| 1161 | item->visit(ud); | 1543 | Line_t* line = *it; |
| 1544 | if (!line->statment) continue; | ||
| 1545 | if (Local_t* local = ast_cast<Local_t>(line->statment->content)) | ||
| 1546 | { | ||
| 1547 | if (local_flag_t* local_flag = ast_cast<local_flag_t>(local->name)) | ||
| 1548 | { | ||
| 1549 | std::vector<const std::string*> names; | ||
| 1550 | for (auto cur = it; cur != objs.end(); ++cur) | ||
| 1551 | { | ||
| 1552 | Line_t* item = *cur; | ||
| 1553 | if (!item->statment) continue; | ||
| 1554 | if (Assignment_t* assignment = ast_cast<Assignment_t>(item->statment->content)) | ||
| 1555 | { | ||
| 1556 | for (Exp_t* expr : assignment->assignable->exprs.objects()) | ||
| 1557 | { | ||
| 1558 | if (ChainValue_t* chainValue = ast_cast<ChainValue_t>(expr->value->item)) | ||
| 1559 | if (Callable_t* callable = ast_cast<Callable_t>(chainValue->caller)) | ||
| 1560 | if (Name_t* name = ast_cast<Name_t>(callable->item)) | ||
| 1561 | { | ||
| 1562 | const std::string& value = name->name->getValue(); | ||
| 1563 | if (local_flag->getValue() == "*") | ||
| 1564 | { | ||
| 1565 | names.push_back(&value); | ||
| 1566 | } | ||
| 1567 | else if (std::isupper(value[0])) | ||
| 1568 | { | ||
| 1569 | names.push_back(&value); | ||
| 1570 | } | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | } | ||
| 1574 | } | ||
| 1575 | if (!names.empty()) | ||
| 1576 | { | ||
| 1577 | data->beginLine(line->m_begin.m_line); | ||
| 1578 | data->buffer << "local "; | ||
| 1579 | auto nameIt = names.begin(); | ||
| 1580 | auto name = *(*nameIt); | ||
| 1581 | data->putLocal(name); | ||
| 1582 | data->buffer << name; | ||
| 1583 | nameIt++; | ||
| 1584 | for (; nameIt != names.end(); ++nameIt) | ||
| 1585 | { | ||
| 1586 | auto name = *(*nameIt); | ||
| 1587 | data->putLocal(name); | ||
| 1588 | data->buffer << ", "; | ||
| 1589 | data->buffer << name; | ||
| 1590 | } | ||
| 1591 | data->endLine(); | ||
| 1592 | } | ||
| 1593 | } | ||
| 1594 | else | ||
| 1595 | { | ||
| 1596 | NameList_t* nameList = static_cast<NameList_t*>(local->name.get()); | ||
| 1597 | data->beginLine(line->m_begin.m_line); | ||
| 1598 | data->buffer << "local "; | ||
| 1599 | nameList->visit(ud); | ||
| 1600 | for (Name_t* name : nameList->names.objects()) | ||
| 1601 | { | ||
| 1602 | data->putLocal(name->name->getValue()); | ||
| 1603 | } | ||
| 1604 | data->endLine(); | ||
| 1605 | } | ||
| 1606 | } | ||
| 1607 | else | ||
| 1608 | { | ||
| 1609 | line->visit(ud); | ||
| 1610 | } | ||
| 1162 | } | 1611 | } |
| 1163 | } | 1612 | } |
| 1164 | AST_END(Block) | 1613 | AST_END(Block) |
| 1165 | 1614 | ||
| 1615 | AST_NODE(BlockEnd) | ||
| 1616 | ast_ptr<Block_t> block; | ||
| 1617 | virtual void visit(void* ud) override | ||
| 1618 | { | ||
| 1619 | Data* data = static_cast<Data*>(ud); | ||
| 1620 | data->pushScope(); | ||
| 1621 | block->visit(ud); | ||
| 1622 | data->popScope(); | ||
| 1623 | } | ||
| 1624 | AST_END(BlockEnd) | ||
| 1625 | |||
| 1166 | int main() | 1626 | int main() |
| 1167 | { | 1627 | { |
| 1168 | string s = R"baddog()baddog"; | 1628 | std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> conv; |
| 1169 | input i(s.begin(), s.end()); | 1629 | std::string s = R"baddog(import \x, func, \memFunc from require "utils")baddog"; |
| 1630 | input i = conv.from_bytes(s); | ||
| 1170 | 1631 | ||
| 1171 | error_list el; | 1632 | error_list el; |
| 1172 | Block_t* root = nullptr; | 1633 | Import_t* root = nullptr; |
| 1173 | State st; | 1634 | State st; |
| 1174 | if (parse(i, BlockEnd, el, root, &st)) | 1635 | if (parse(i, Import, el, root, &st)) |
| 1175 | { | 1636 | { |
| 1176 | cout << "matched!\n"; | 1637 | std::cout << "matched!\n"; |
| 1177 | Data data; | 1638 | Data data; |
| 1178 | root->visit(&data); | 1639 | root->visit(&data); |
| 1179 | } | 1640 | } |
| 1180 | else | 1641 | else |
| 1181 | { | 1642 | { |
| 1182 | cout << "not matched!\n"; | 1643 | std::cout << "not matched!\n"; |
| 1183 | for (error_list::iterator it = el.begin(); it != el.end(); ++it) | 1644 | for (error_list::iterator it = el.begin(); it != el.end(); ++it) |
| 1184 | { | 1645 | { |
| 1185 | const error& err = *it; | 1646 | const error& err = *it; |
| 1186 | cout << "line " << err.m_begin.m_line << ", col " << err.m_begin.m_col << ": syntax error\n"; | 1647 | std::cout << "line " << err.m_begin.m_line << ", col " << err.m_begin.m_col << ": syntax error\n"; |
| 1187 | } | 1648 | } |
| 1188 | } | 1649 | } |
| 1189 | system("pause"); | 1650 | system("pause"); |
