diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_ast.h | 2 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 54 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 2 |
3 files changed, 53 insertions, 5 deletions
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 1937eb8..6e1bb88 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
| @@ -780,7 +780,7 @@ AST_NODE(Export) | |||
| 780 | AST_END(Export) | 780 | AST_END(Export) |
| 781 | 781 | ||
| 782 | AST_NODE(FnArgDef) | 782 | AST_NODE(FnArgDef) |
| 783 | ast_sel<true, Variable_t, SelfItem_t> name; | 783 | ast_sel<true, Variable_t, SelfItem_t, SimpleTable_t, TableLit_t> name; |
| 784 | ast_ptr<false, ExistentialOp_t> op; | 784 | ast_ptr<false, ExistentialOp_t> op; |
| 785 | ast_ptr<false, Name_t> label; | 785 | ast_ptr<false, Name_t> label; |
| 786 | ast_ptr<false, Exp_t> defaultValue; | 786 | ast_ptr<false, Exp_t> defaultValue; |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index d676750..33161a7 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
| 78 | "close"s // Lua 5.4 | 78 | "close"s // Lua 5.4 |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | const std::string_view version = "0.29.3"sv; | 81 | const std::string_view version = "0.29.4"sv; |
| 82 | const std::string_view extension = "yue"sv; | 82 | const std::string_view extension = "yue"sv; |
| 83 | 83 | ||
| 84 | class CompileError : public std::logic_error { | 84 | class CompileError : public std::logic_error { |
| @@ -1982,7 +1982,7 @@ private: | |||
| 1982 | return indent() + "local "s + join(defs, ", "sv); | 1982 | return indent() + "local "s + join(defs, ", "sv); |
| 1983 | } | 1983 | } |
| 1984 | 1984 | ||
| 1985 | std::string getDestrucureDefine(ExpListAssign_t* assignment) { | 1985 | std::string getDestructureDefine(ExpListAssign_t* assignment) { |
| 1986 | auto info = extractDestructureInfo(assignment, true, false); | 1986 | auto info = extractDestructureInfo(assignment, true, false); |
| 1987 | if (!info.destructures.empty()) { | 1987 | if (!info.destructures.empty()) { |
| 1988 | str_list defs; | 1988 | str_list defs; |
| @@ -2013,8 +2013,31 @@ private: | |||
| 2013 | return clearBuf(); | 2013 | return clearBuf(); |
| 2014 | } | 2014 | } |
| 2015 | 2015 | ||
| 2016 | str_list getArgDestructureList(ExpListAssign_t* assignment) { | ||
| 2017 | str_list defs; | ||
| 2018 | auto info = extractDestructureInfo(assignment, true, false); | ||
| 2019 | if (!info.destructures.empty()) { | ||
| 2020 | for (const auto& des : info.destructures) { | ||
| 2021 | if (std::holds_alternative<Destructure>(des)) { | ||
| 2022 | const auto& destruct = std::get<Destructure>(des); | ||
| 2023 | for (const auto& item : destruct.items) { | ||
| 2024 | if (item.targetVar.empty()) { | ||
| 2025 | throw CompileError("can only destruct argument to variable"sv, item.target); | ||
| 2026 | } else { | ||
| 2027 | defs.push_back(item.targetVar); | ||
| 2028 | } | ||
| 2029 | } | ||
| 2030 | } else { | ||
| 2031 | const auto& assignment = std::get<AssignmentPtr>(des); | ||
| 2032 | YUEE("AST node mismatch", assignment.ptr); | ||
| 2033 | } | ||
| 2034 | } | ||
| 2035 | } | ||
| 2036 | return defs; | ||
| 2037 | } | ||
| 2038 | |||
| 2016 | std::string getPreDefine(ExpListAssign_t* assignment) { | 2039 | std::string getPreDefine(ExpListAssign_t* assignment) { |
| 2017 | auto preDefine = getDestrucureDefine(assignment); | 2040 | auto preDefine = getDestructureDefine(assignment); |
| 2018 | if (preDefine.empty()) { | 2041 | if (preDefine.empty()) { |
| 2019 | preDefine = toLocalDecl(transformAssignDefs(assignment->expList, DefOp::Mark)); | 2042 | preDefine = toLocalDecl(transformAssignDefs(assignment->expList, DefOp::Mark)); |
| 2020 | } | 2043 | } |
| @@ -5652,6 +5675,7 @@ private: | |||
| 5652 | bool checkExistence = false; | 5675 | bool checkExistence = false; |
| 5653 | std::string name; | 5676 | std::string name; |
| 5654 | std::string assignSelf; | 5677 | std::string assignSelf; |
| 5678 | ast_ptr<false, ExpListAssign_t> assignment; | ||
| 5655 | }; | 5679 | }; |
| 5656 | std::list<ArgItem> argItems; | 5680 | std::list<ArgItem> argItems; |
| 5657 | str_list temp; | 5681 | str_list temp; |
| @@ -5706,6 +5730,22 @@ private: | |||
| 5706 | } | 5730 | } |
| 5707 | break; | 5731 | break; |
| 5708 | } | 5732 | } |
| 5733 | case id<TableLit_t>(): { | ||
| 5734 | arg.name = getUnusedName("_arg_"sv); | ||
| 5735 | auto simpleValue = def->new_ptr<SimpleValue_t>(); | ||
| 5736 | simpleValue->value.set(def->name); | ||
| 5737 | auto asmt = assignmentFrom(newExp(simpleValue, def), toAst<Exp_t>(arg.name, def), def); | ||
| 5738 | arg.assignment = asmt; | ||
| 5739 | break; | ||
| 5740 | } | ||
| 5741 | case id<SimpleTable_t>(): { | ||
| 5742 | arg.name = getUnusedName("_arg_"sv); | ||
| 5743 | auto value = def->new_ptr<Value_t>(); | ||
| 5744 | value->item.set(def->name); | ||
| 5745 | auto asmt = assignmentFrom(newExp(value, def), toAst<Exp_t>(arg.name, def), def); | ||
| 5746 | arg.assignment = asmt; | ||
| 5747 | break; | ||
| 5748 | } | ||
| 5709 | default: YUEE("AST node mismatch", def->name.get()); break; | 5749 | default: YUEE("AST node mismatch", def->name.get()); break; |
| 5710 | } | 5750 | } |
| 5711 | forceAddToScope(arg.name); | 5751 | forceAddToScope(arg.name); |
| @@ -5724,6 +5764,14 @@ private: | |||
| 5724 | _buf << indent() << "end"sv << nll(def); | 5764 | _buf << indent() << "end"sv << nll(def); |
| 5725 | temp.back() = clearBuf(); | 5765 | temp.back() = clearBuf(); |
| 5726 | } | 5766 | } |
| 5767 | if (arg.assignment) { | ||
| 5768 | auto names = getArgDestructureList(arg.assignment); | ||
| 5769 | for (const auto& name : names) { | ||
| 5770 | forceAddToScope(name); | ||
| 5771 | } | ||
| 5772 | temp.emplace_back(indent() + "local "s + join(names, ", "sv) + nll(def)); | ||
| 5773 | transformAssignment(arg.assignment, temp); | ||
| 5774 | } | ||
| 5727 | if (varNames.empty()) | 5775 | if (varNames.empty()) |
| 5728 | varNames = arg.name; | 5776 | varNames = arg.name; |
| 5729 | else | 5777 | else |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 1942e23..01ca083 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
| @@ -893,7 +893,7 @@ YueParser::YueParser() { | |||
| 893 | 893 | ||
| 894 | fn_arg_def_lit_lines = fn_arg_def_lit_line >> *(-(space >> ',') >> space_break >> fn_arg_def_lit_line); | 894 | fn_arg_def_lit_lines = fn_arg_def_lit_line >> *(-(space >> ',') >> space_break >> fn_arg_def_lit_line); |
| 895 | 895 | ||
| 896 | FnArgDef = (Variable | SelfItem >> -ExistentialOp) >> -(space >> '`' >> space >> Name) >> -(space >> '=' >> space >> Exp); | 896 | FnArgDef = (Variable | SelfItem >> -ExistentialOp) >> -(space >> '`' >> space >> Name) >> -(space >> '=' >> space >> Exp) | TableLit | SimpleTable; |
| 897 | 897 | ||
| 898 | FnArgDefList = Seperator >> ( | 898 | FnArgDefList = Seperator >> ( |
| 899 | fn_arg_def_lit_lines >> -(-(space >> ',') >> white >> VarArg >> -(space >> '`' >> space >> Name)) | | 899 | fn_arg_def_lit_lines >> -(-(space >> ',') >> white >> VarArg >> -(space >> '`' >> space >> Name)) | |
