diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_ast.h | 2 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 198 |
2 files changed, 99 insertions, 101 deletions
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 7bf17fe..cffc92d 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
| @@ -13,7 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 13 | namespace parserlib { | 13 | namespace parserlib { |
| 14 | 14 | ||
| 15 | template <class T> | 15 | template <class T> |
| 16 | std::string_view ast_name() { } | 16 | std::string_view ast_name() { return {}; } |
| 17 | 17 | ||
| 18 | #define AST_LEAF(type) \ | 18 | #define AST_LEAF(type) \ |
| 19 | COUNTER_INC; \ | 19 | COUNTER_INC; \ |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8ec131f..32db488 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -14,8 +14,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 14 | #include <string> | 14 | #include <string> |
| 15 | #include <unordered_map> | 15 | #include <unordered_map> |
| 16 | #include <unordered_set> | 16 | #include <unordered_set> |
| 17 | #include <vector> | ||
| 18 | #include <variant> | 17 | #include <variant> |
| 18 | #include <vector> | ||
| 19 | 19 | ||
| 20 | #include "yuescript/yue_compiler.h" | 20 | #include "yuescript/yue_compiler.h" |
| 21 | #include "yuescript/yue_parser.h" | 21 | #include "yuescript/yue_parser.h" |
| @@ -46,8 +46,7 @@ namespace yue { | |||
| 46 | #define BLOCK_START do { | 46 | #define BLOCK_START do { |
| 47 | #define BLOCK_END \ | 47 | #define BLOCK_END \ |
| 48 | } \ | 48 | } \ |
| 49 | while (false) \ | 49 | while (false); |
| 50 | ; | ||
| 51 | #define BREAK_IF(cond) \ | 50 | #define BREAK_IF(cond) \ |
| 52 | if (cond) break | 51 | if (cond) break |
| 53 | 52 | ||
| @@ -76,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
| 76 | "close"s // Lua 5.4 | 75 | "close"s // Lua 5.4 |
| 77 | }; | 76 | }; |
| 78 | 77 | ||
| 79 | const std::string_view version = "0.24.0"sv; | 78 | const std::string_view version = "0.24.1"sv; |
| 80 | const std::string_view extension = "yue"sv; | 79 | const std::string_view extension = "yue"sv; |
| 81 | 80 | ||
| 82 | class CompileError : public std::logic_error { | 81 | class CompileError : public std::logic_error { |
| @@ -1475,8 +1474,7 @@ private: | |||
| 1475 | } | 1474 | } |
| 1476 | 1475 | ||
| 1477 | bool isConditionChainingOperator(const std::string& op) { | 1476 | bool isConditionChainingOperator(const std::string& op) { |
| 1478 | return op == "=="sv || op == "~="sv || op == "!="sv || | 1477 | return op == "=="sv || op == "~="sv || op == "!="sv || op == "<"sv || op == "<="sv || op == ">"sv || op == ">="sv; |
| 1479 | op == "<"sv || op == "<="sv || op == ">"sv || op == ">="sv; | ||
| 1480 | } | 1478 | } |
| 1481 | 1479 | ||
| 1482 | bool isConditionChaining(Exp_t* exp) { | 1480 | bool isConditionChaining(Exp_t* exp) { |
| @@ -1522,7 +1520,6 @@ private: | |||
| 1522 | _gotoScope++; | 1520 | _gotoScope++; |
| 1523 | } | 1521 | } |
| 1524 | 1522 | ||
| 1525 | |||
| 1526 | void pushUserFunctionScope() { | 1523 | void pushUserFunctionScope() { |
| 1527 | pushFunctionScope(false); | 1524 | pushFunctionScope(false); |
| 1528 | } | 1525 | } |
| @@ -3728,7 +3725,7 @@ private: | |||
| 3728 | std::list<std::pair<std::string, ast_list<true, UnaryExp_t>*>> chains; | 3725 | std::list<std::pair<std::string, ast_list<true, UnaryExp_t>*>> chains; |
| 3729 | chains.emplace_back(std::string(), &exp->pipeExprs); | 3726 | chains.emplace_back(std::string(), &exp->pipeExprs); |
| 3730 | int conditionChainCount = 0; | 3727 | int conditionChainCount = 0; |
| 3731 | auto checkChains = [&]()-> ast_ptr<false, Exp_t> { | 3728 | auto checkChains = [&]() -> ast_ptr<false, Exp_t> { |
| 3732 | std::optional<str_list> result; | 3729 | std::optional<str_list> result; |
| 3733 | if (conditionChainCount > 1) { | 3730 | if (conditionChainCount > 1) { |
| 3734 | ast_ptr<false, Exp_t> newCondExp; | 3731 | ast_ptr<false, Exp_t> newCondExp; |
| @@ -3774,106 +3771,106 @@ private: | |||
| 3774 | { | 3771 | { |
| 3775 | stack.push_back(*item.second); | 3772 | stack.push_back(*item.second); |
| 3776 | } | 3773 | } |
| 3777 | reduce: { | 3774 | reduce: { |
| 3778 | if (stack.size() == 3) { | 3775 | if (stack.size() == 3) { |
| 3779 | auto condExp = exp->new_ptr<Exp_t>(); | 3776 | auto condExp = exp->new_ptr<Exp_t>(); |
| 3780 | const auto& one = std::get<ast_list<true, UnaryExp_t>>(stack.front()); | 3777 | const auto& one = std::get<ast_list<true, UnaryExp_t>>(stack.front()); |
| 3781 | condExp->pipeExprs.dup(one); | 3778 | condExp->pipeExprs.dup(one); |
| 3782 | stack.pop_front(); | 3779 | stack.pop_front(); |
| 3783 | auto opValue = exp->new_ptr<ExpOpValue_t>(); | 3780 | auto opValue = exp->new_ptr<ExpOpValue_t>(); |
| 3784 | const auto& two = std::get<std::string>(stack.front()); | 3781 | const auto& two = std::get<std::string>(stack.front()); |
| 3785 | auto op = toAst<BinaryOperator_t>(two, exp); | 3782 | auto op = toAst<BinaryOperator_t>(two, exp); |
| 3786 | opValue->op.set(op); | 3783 | opValue->op.set(op); |
| 3787 | stack.pop_front(); | 3784 | stack.pop_front(); |
| 3788 | const auto& three = std::get<ast_list<true, UnaryExp_t>>(stack.front()); | 3785 | const auto& three = std::get<ast_list<true, UnaryExp_t>>(stack.front()); |
| 3789 | opValue->pipeExprs.dup(three); | 3786 | opValue->pipeExprs.dup(three); |
| 3790 | condExp->opValues.push_back(opValue); | 3787 | condExp->opValues.push_back(opValue); |
| 3791 | if (preDefine) { | 3788 | if (preDefine) { |
| 3792 | auto ifNode = exp->new_ptr<If_t>(); | 3789 | auto ifNode = exp->new_ptr<If_t>(); |
| 3793 | ifNode->type.set(toAst<IfType_t>("unless"sv, exp)); | 3790 | ifNode->type.set(toAst<IfType_t>("unless"sv, exp)); |
| 3794 | auto ifCond = exp->new_ptr<IfCond_t>(); | 3791 | auto ifCond = exp->new_ptr<IfCond_t>(); |
| 3795 | ifCond->condition.set(condExp); | 3792 | ifCond->condition.set(condExp); |
| 3796 | ifNode->nodes.push_back(ifCond); | 3793 | ifNode->nodes.push_back(ifCond); |
| 3797 | ifNode->nodes.push_back(toAst<Statement_t>("false"sv, exp)); | 3794 | ifNode->nodes.push_back(toAst<Statement_t>("false"sv, exp)); |
| 3798 | YueFormat format{}; | 3795 | YueFormat format{}; |
| 3799 | auto code = ifNode->to_string(&format); | 3796 | auto code = ifNode->to_string(&format); |
| 3800 | if (newCondExp) { | 3797 | if (newCondExp) { |
| 3801 | if (!nodes) { | 3798 | if (!nodes) { |
| 3802 | auto ifNodePrev = exp->new_ptr<If_t>(); | 3799 | auto ifNodePrev = exp->new_ptr<If_t>(); |
| 3803 | ifNodePrev->type.set(toAst<IfType_t>("unless"sv, exp)); | 3800 | ifNodePrev->type.set(toAst<IfType_t>("unless"sv, exp)); |
| 3804 | auto ifCondPrev = exp->new_ptr<IfCond_t>(); | 3801 | auto ifCondPrev = exp->new_ptr<IfCond_t>(); |
| 3805 | ifCondPrev->condition.set(newCondExp); | 3802 | ifCondPrev->condition.set(newCondExp); |
| 3806 | ifNodePrev->nodes.push_back(ifCondPrev); | 3803 | ifNodePrev->nodes.push_back(ifCondPrev); |
| 3807 | ifNodePrev->nodes.push_back(toAst<Statement_t>("false", exp)); | 3804 | ifNodePrev->nodes.push_back(toAst<Statement_t>("false", exp)); |
| 3808 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3809 | simpleValue->value.set(ifNodePrev); | ||
| 3810 | newCondExp.set(newExp(simpleValue, exp)); | ||
| 3811 | nodes = &ifNodePrev->nodes; | ||
| 3812 | } | ||
| 3813 | auto block = exp->new_ptr<Block_t>(); | ||
| 3814 | auto stmt = exp->new_ptr<Statement_t>(); | ||
| 3815 | stmt->content.set(preDefine); | ||
| 3816 | preDefine.set(nullptr); | ||
| 3817 | block->statements.push_back(stmt); | ||
| 3818 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | 3805 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); |
| 3819 | simpleValue->value.set(ifNode); | 3806 | simpleValue->value.set(ifNodePrev); |
| 3807 | newCondExp.set(newExp(simpleValue, exp)); | ||
| 3808 | nodes = &ifNodePrev->nodes; | ||
| 3809 | } | ||
| 3810 | auto block = exp->new_ptr<Block_t>(); | ||
| 3811 | auto stmt = exp->new_ptr<Statement_t>(); | ||
| 3812 | stmt->content.set(preDefine); | ||
| 3813 | preDefine.set(nullptr); | ||
| 3814 | block->statements.push_back(stmt); | ||
| 3815 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3816 | simpleValue->value.set(ifNode); | ||
| 3817 | auto explist = exp->new_ptr<ExpList_t>(); | ||
| 3818 | explist->exprs.push_back(newExp(simpleValue, exp)); | ||
| 3819 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
| 3820 | expListAssign->expList.set(explist); | ||
| 3821 | stmt = exp->new_ptr<Statement_t>(); | ||
| 3822 | stmt->content.set(expListAssign); | ||
| 3823 | block->statements.push_back(stmt); | ||
| 3824 | nodes->push_back(block); | ||
| 3825 | nodes = &ifNode->nodes; | ||
| 3826 | } else { | ||
| 3827 | auto block = exp->new_ptr<Block_t>(); | ||
| 3828 | auto stmt = exp->new_ptr<Statement_t>(); | ||
| 3829 | stmt->content.set(preDefine); | ||
| 3830 | preDefine.set(nullptr); | ||
| 3831 | block->statements.push_back(stmt); | ||
| 3832 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3833 | simpleValue->value.set(ifNode); | ||
| 3834 | auto explist = exp->new_ptr<ExpList_t>(); | ||
| 3835 | explist->exprs.push_back(newExp(simpleValue, exp)); | ||
| 3836 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
| 3837 | expListAssign->expList.set(explist); | ||
| 3838 | stmt = exp->new_ptr<Statement_t>(); | ||
| 3839 | stmt->content.set(expListAssign); | ||
| 3840 | block->statements.push_back(stmt); | ||
| 3841 | auto body = exp->new_ptr<Body_t>(); | ||
| 3842 | body->content.set(block); | ||
| 3843 | auto doNode = exp->new_ptr<Do_t>(); | ||
| 3844 | doNode->body.set(body); | ||
| 3845 | simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3846 | simpleValue->value.set(doNode); | ||
| 3847 | newCondExp.set(newExp(simpleValue, exp)); | ||
| 3848 | nodes = &ifNode->nodes; | ||
| 3849 | } | ||
| 3850 | } else { | ||
| 3851 | if (newCondExp) { | ||
| 3852 | if (nodes) { | ||
| 3820 | auto explist = exp->new_ptr<ExpList_t>(); | 3853 | auto explist = exp->new_ptr<ExpList_t>(); |
| 3821 | explist->exprs.push_back(newExp(simpleValue, exp)); | 3854 | explist->exprs.push_back(condExp); |
| 3822 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | 3855 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); |
| 3823 | expListAssign->expList.set(explist); | 3856 | expListAssign->expList.set(explist); |
| 3824 | stmt = exp->new_ptr<Statement_t>(); | ||
| 3825 | stmt->content.set(expListAssign); | ||
| 3826 | block->statements.push_back(stmt); | ||
| 3827 | nodes->push_back(block); | ||
| 3828 | nodes = &ifNode->nodes; | ||
| 3829 | } else { | ||
| 3830 | auto block = exp->new_ptr<Block_t>(); | ||
| 3831 | auto stmt = exp->new_ptr<Statement_t>(); | 3857 | auto stmt = exp->new_ptr<Statement_t>(); |
| 3832 | stmt->content.set(preDefine); | ||
| 3833 | preDefine.set(nullptr); | ||
| 3834 | block->statements.push_back(stmt); | ||
| 3835 | auto simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3836 | simpleValue->value.set(ifNode); | ||
| 3837 | auto explist = exp->new_ptr<ExpList_t>(); | ||
| 3838 | explist->exprs.push_back(newExp(simpleValue, exp)); | ||
| 3839 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
| 3840 | expListAssign->expList.set(explist); | ||
| 3841 | stmt = exp->new_ptr<Statement_t>(); | ||
| 3842 | stmt->content.set(expListAssign); | 3858 | stmt->content.set(expListAssign); |
| 3843 | block->statements.push_back(stmt); | 3859 | nodes->push_back(stmt); |
| 3844 | auto body = exp->new_ptr<Body_t>(); | ||
| 3845 | body->content.set(block); | ||
| 3846 | auto doNode = exp->new_ptr<Do_t>(); | ||
| 3847 | doNode->body.set(body); | ||
| 3848 | simpleValue = exp->new_ptr<SimpleValue_t>(); | ||
| 3849 | simpleValue->value.set(doNode); | ||
| 3850 | newCondExp.set(newExp(simpleValue, exp)); | ||
| 3851 | nodes = &ifNode->nodes; | ||
| 3852 | } | ||
| 3853 | } else { | ||
| 3854 | if (newCondExp) { | ||
| 3855 | if (nodes) { | ||
| 3856 | auto explist = exp->new_ptr<ExpList_t>(); | ||
| 3857 | explist->exprs.push_back(condExp); | ||
| 3858 | auto expListAssign = exp->new_ptr<ExpListAssign_t>(); | ||
| 3859 | expListAssign->expList.set(explist); | ||
| 3860 | auto stmt = exp->new_ptr<Statement_t>(); | ||
| 3861 | stmt->content.set(expListAssign); | ||
| 3862 | nodes->push_back(stmt); | ||
| 3863 | } else { | ||
| 3864 | auto opValue = exp->new_ptr<ExpOpValue_t>(); | ||
| 3865 | opValue->op.set(toAst<BinaryOperator_t>("and"sv, exp)); | ||
| 3866 | opValue->pipeExprs.dup(condExp->pipeExprs); | ||
| 3867 | newCondExp->opValues.push_back(opValue); | ||
| 3868 | newCondExp->opValues.dup(condExp->opValues); | ||
| 3869 | } | ||
| 3870 | } else { | 3860 | } else { |
| 3871 | newCondExp.set(condExp); | 3861 | auto opValue = exp->new_ptr<ExpOpValue_t>(); |
| 3862 | opValue->op.set(toAst<BinaryOperator_t>("and"sv, exp)); | ||
| 3863 | opValue->pipeExprs.dup(condExp->pipeExprs); | ||
| 3864 | newCondExp->opValues.push_back(opValue); | ||
| 3865 | newCondExp->opValues.dup(condExp->opValues); | ||
| 3872 | } | 3866 | } |
| 3867 | } else { | ||
| 3868 | newCondExp.set(condExp); | ||
| 3873 | } | 3869 | } |
| 3874 | } | 3870 | } |
| 3875 | } | 3871 | } |
| 3876 | } | 3872 | } |
| 3873 | } | ||
| 3877 | popScope(); | 3874 | popScope(); |
| 3878 | return newCondExp; | 3875 | return newCondExp; |
| 3879 | } | 3876 | } |
| @@ -4079,7 +4076,6 @@ private: | |||
| 4079 | return std::nullopt; | 4076 | return std::nullopt; |
| 4080 | } | 4077 | } |
| 4081 | 4078 | ||
| 4082 | |||
| 4083 | std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Block_t* block, str_list* ensureArgListInTheEnd = nullptr, bool noGlobalVarPassing = false) { | 4079 | std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Block_t* block, str_list* ensureArgListInTheEnd = nullptr, bool noGlobalVarPassing = false) { |
| 4084 | if (checkUpValueFuncAvailable(block)) { | 4080 | if (checkUpValueFuncAvailable(block)) { |
| 4085 | return getUpValueFuncFromBlock(block, ensureArgListInTheEnd, noGlobalVarPassing); | 4081 | return getUpValueFuncFromBlock(block, ensureArgListInTheEnd, noGlobalVarPassing); |
| @@ -6315,6 +6311,9 @@ private: | |||
| 6315 | } // cur macroFunc | 6311 | } // cur macroFunc |
| 6316 | pushYue("pcall"sv); // cur macroFunc pcall | 6312 | pushYue("pcall"sv); // cur macroFunc pcall |
| 6317 | lua_insert(L, -2); // cur pcall macroFunc | 6313 | lua_insert(L, -2); // cur pcall macroFunc |
| 6314 | if (!lua_checkstack(L, argStrs.size())) { | ||
| 6315 | throw CompileError("too much macro params"s, x); | ||
| 6316 | } | ||
| 6318 | for (const auto& arg : argStrs) { | 6317 | for (const auto& arg : argStrs) { |
| 6319 | lua_pushlstring(L, arg.c_str(), arg.size()); | 6318 | lua_pushlstring(L, arg.c_str(), arg.size()); |
| 6320 | } // cur pcall macroFunc args... | 6319 | } // cur pcall macroFunc args... |
| @@ -8607,8 +8606,7 @@ private: | |||
| 8607 | case id<ClassMemberList_t>(): { | 8606 | case id<ClassMemberList_t>(): { |
| 8608 | size_t inc = transform_class_member_list(static_cast<ClassMemberList_t*>(content), members, classVar); | 8607 | size_t inc = transform_class_member_list(static_cast<ClassMemberList_t*>(content), members, classVar); |
| 8609 | auto it = members.end(); | 8608 | auto it = members.end(); |
| 8610 | for (size_t i = 0; i < inc; ++i, --it) | 8609 | for (size_t i = 0; i < inc; ++i, --it); |
| 8611 | ; | ||
| 8612 | for (; it != members.end(); ++it) { | 8610 | for (; it != members.end(); ++it) { |
| 8613 | auto& member = *it; | 8611 | auto& member = *it; |
| 8614 | if (member.type == MemType::Property) { | 8612 | if (member.type == MemType::Property) { |
| @@ -10179,7 +10177,7 @@ private: | |||
| 10179 | if (auto value = singleValueFrom(valueList)) { | 10177 | if (auto value = singleValueFrom(valueList)) { |
| 10180 | if (value->item.is<SimpleTable_t>()) { | 10178 | if (value->item.is<SimpleTable_t>()) { |
| 10181 | tableMatching = true; | 10179 | tableMatching = true; |
| 10182 | } else if (auto sVal = value->item.as<SimpleValue_t>()){ | 10180 | } else if (auto sVal = value->item.as<SimpleValue_t>()) { |
| 10183 | tableMatching = ast_is<TableLit_t, Comprehension_t>(sVal->value); | 10181 | tableMatching = ast_is<TableLit_t, Comprehension_t>(sVal->value); |
| 10184 | } | 10182 | } |
| 10185 | } | 10183 | } |
