diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-07-18 16:45:50 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-07-18 16:45:50 +0800 |
| commit | 8c596dc1efa8a1267c222b168a4de9c8ba254760 (patch) | |
| tree | 699c748e101a48ae267b5f7b19adbfea15f3934e /src/MoonP | |
| parent | 8ab0038c09a79fa8401bb10b7a31d03ef5380417 (diff) | |
| download | yuescript-8c596dc1efa8a1267c222b168a4de9c8ba254760.tar.gz yuescript-8c596dc1efa8a1267c222b168a4de9c8ba254760.tar.bz2 yuescript-8c596dc1efa8a1267c222b168a4de9c8ba254760.zip | |
fix issue for using return statement with export.
Diffstat (limited to 'src/MoonP')
| -rw-r--r-- | src/MoonP/moon_compiler.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 41d29df..1d9161b 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
| @@ -49,7 +49,7 @@ inline std::string s(std::string_view sv) { | |||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | const std::string_view version() { | 51 | const std::string_view version() { |
| 52 | return "0.4.4"sv; | 52 | return "0.4.5"sv; |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | // name of table stored in lua registry | 55 | // name of table stored in lua registry |
| @@ -1498,6 +1498,7 @@ private: | |||
| 1498 | if (usage == ExpUsage::Closure) { | 1498 | if (usage == ExpUsage::Closure) { |
| 1499 | temp.push_back(s("(function()"sv) + nll(nodes.front())); | 1499 | temp.push_back(s("(function()"sv) + nll(nodes.front())); |
| 1500 | pushScope(); | 1500 | pushScope(); |
| 1501 | _enableReturn.push(true); | ||
| 1501 | } | 1502 | } |
| 1502 | std::list<std::pair<IfCond_t*, Body_t*>> ifCondPairs; | 1503 | std::list<std::pair<IfCond_t*, Body_t*>> ifCondPairs; |
| 1503 | ifCondPairs.emplace_back(); | 1504 | ifCondPairs.emplace_back(); |
| @@ -1613,6 +1614,7 @@ private: | |||
| 1613 | temp.push_back(indent() + s("end"sv) + nlr(nodes.front())); | 1614 | temp.push_back(indent() + s("end"sv) + nlr(nodes.front())); |
| 1614 | } | 1615 | } |
| 1615 | if (usage == ExpUsage::Closure) { | 1616 | if (usage == ExpUsage::Closure) { |
| 1617 | _enableReturn.pop(); | ||
| 1616 | popScope(); | 1618 | popScope(); |
| 1617 | temp.push_back(indent() + s("end)()"sv)); | 1619 | temp.push_back(indent() + s("end)()"sv)); |
| 1618 | } | 1620 | } |
| @@ -2588,6 +2590,7 @@ private: | |||
| 2588 | if (usage == ExpUsage::Closure) { | 2590 | if (usage == ExpUsage::Closure) { |
| 2589 | temp.push_back(s("(function()"sv) + nll(x)); | 2591 | temp.push_back(s("(function()"sv) + nll(x)); |
| 2590 | pushScope(); | 2592 | pushScope(); |
| 2593 | _enableReturn.push(true); | ||
| 2591 | } | 2594 | } |
| 2592 | auto partOne = x->new_ptr<ChainValue_t>(); | 2595 | auto partOne = x->new_ptr<ChainValue_t>(); |
| 2593 | for (auto it = chainList.begin();it != opIt;++it) { | 2596 | for (auto it = chainList.begin();it != opIt;++it) { |
| @@ -2718,6 +2721,7 @@ private: | |||
| 2718 | break; | 2721 | break; |
| 2719 | case ExpUsage::Closure: | 2722 | case ExpUsage::Closure: |
| 2720 | temp.push_back(indent() + s("return nil"sv) + nlr(x)); | 2723 | temp.push_back(indent() + s("return nil"sv) + nlr(x)); |
| 2724 | _enableReturn.pop(); | ||
| 2721 | popScope(); | 2725 | popScope(); |
| 2722 | temp.push_back(indent() + s("end)()"sv)); | 2726 | temp.push_back(indent() + s("end)()"sv)); |
| 2723 | break; | 2727 | break; |
| @@ -2742,6 +2746,7 @@ private: | |||
| 2742 | case ExpUsage::Closure: | 2746 | case ExpUsage::Closure: |
| 2743 | temp.push_back(s("(function()"sv) + nll(x)); | 2747 | temp.push_back(s("(function()"sv) + nll(x)); |
| 2744 | pushScope(); | 2748 | pushScope(); |
| 2749 | _enableReturn.push(true); | ||
| 2745 | break; | 2750 | break; |
| 2746 | default: | 2751 | default: |
| 2747 | break; | 2752 | break; |
| @@ -2813,6 +2818,7 @@ private: | |||
| 2813 | temp.push_back(indent() + s("end"sv) + nlr(x)); | 2818 | temp.push_back(indent() + s("end"sv) + nlr(x)); |
| 2814 | break; | 2819 | break; |
| 2815 | case ExpUsage::Closure: | 2820 | case ExpUsage::Closure: |
| 2821 | _enableReturn.pop(); | ||
| 2816 | popScope(); | 2822 | popScope(); |
| 2817 | temp.push_back(indent() + s("end)()"sv)); | 2823 | temp.push_back(indent() + s("end)()"sv)); |
| 2818 | break; | 2824 | break; |
| @@ -3359,6 +3365,9 @@ private: | |||
| 3359 | auto x = comp; | 3365 | auto x = comp; |
| 3360 | switch (usage) { | 3366 | switch (usage) { |
| 3361 | case ExpUsage::Closure: | 3367 | case ExpUsage::Closure: |
| 3368 | _enableReturn.push(true); | ||
| 3369 | pushScope(); | ||
| 3370 | break; | ||
| 3362 | case ExpUsage::Assignment: | 3371 | case ExpUsage::Assignment: |
| 3363 | pushScope(); | 3372 | pushScope(); |
| 3364 | break; | 3373 | break; |
| @@ -3413,6 +3422,7 @@ private: | |||
| 3413 | case ExpUsage::Common: | 3422 | case ExpUsage::Common: |
| 3414 | break; | 3423 | break; |
| 3415 | case ExpUsage::Closure: { | 3424 | case ExpUsage::Closure: { |
| 3425 | _enableReturn.pop(); | ||
| 3416 | out.push_back(clearBuf()); | 3426 | out.push_back(clearBuf()); |
| 3417 | out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); | 3427 | out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); |
| 3418 | popScope(); | 3428 | popScope(); |
| @@ -3697,8 +3707,10 @@ private: | |||
| 3697 | str_list temp; | 3707 | str_list temp; |
| 3698 | _buf << "(function()"sv << nll(forNode); | 3708 | _buf << "(function()"sv << nll(forNode); |
| 3699 | pushScope(); | 3709 | pushScope(); |
| 3710 | _enableReturn.push(true); | ||
| 3700 | auto accum = transformForInner(forNode, temp); | 3711 | auto accum = transformForInner(forNode, temp); |
| 3701 | temp.push_back(indent() + s("return "sv) + accum + nlr(forNode)); | 3712 | temp.push_back(indent() + s("return "sv) + accum + nlr(forNode)); |
| 3713 | _enableReturn.pop(); | ||
| 3702 | popScope(); | 3714 | popScope(); |
| 3703 | temp.push_back(indent() + s("end)()"sv)); | 3715 | temp.push_back(indent() + s("end)()"sv)); |
| 3704 | out.push_back(join(temp)); | 3716 | out.push_back(join(temp)); |
| @@ -3764,8 +3776,10 @@ private: | |||
| 3764 | str_list temp; | 3776 | str_list temp; |
| 3765 | _buf << "(function()"sv << nll(forEach); | 3777 | _buf << "(function()"sv << nll(forEach); |
| 3766 | pushScope(); | 3778 | pushScope(); |
| 3779 | _enableReturn.push(true); | ||
| 3767 | auto accum = transformForEachInner(forEach, temp); | 3780 | auto accum = transformForEachInner(forEach, temp); |
| 3768 | temp.push_back(indent() + s("return "sv) + accum + nlr(forEach)); | 3781 | temp.push_back(indent() + s("return "sv) + accum + nlr(forEach)); |
| 3782 | _enableReturn.pop(); | ||
| 3769 | popScope(); | 3783 | popScope(); |
| 3770 | temp.push_back(indent() + s("end)()"sv)); | 3784 | temp.push_back(indent() + s("end)()"sv)); |
| 3771 | out.push_back(join(temp)); | 3785 | out.push_back(join(temp)); |
| @@ -3918,7 +3932,9 @@ private: | |||
| 3918 | str_list temp; | 3932 | str_list temp; |
| 3919 | temp.push_back(s("(function()"sv) + nll(classDecl)); | 3933 | temp.push_back(s("(function()"sv) + nll(classDecl)); |
| 3920 | pushScope(); | 3934 | pushScope(); |
| 3935 | _enableReturn.push(true); | ||
| 3921 | transformClassDecl(classDecl, temp, ExpUsage::Return); | 3936 | transformClassDecl(classDecl, temp, ExpUsage::Return); |
| 3937 | _enableReturn.pop(); | ||
| 3922 | popScope(); | 3938 | popScope(); |
| 3923 | temp.push_back(s("end)()"sv)); | 3939 | temp.push_back(s("end)()"sv)); |
| 3924 | out.push_back(join(temp)); | 3940 | out.push_back(join(temp)); |
| @@ -4280,7 +4296,9 @@ private: | |||
| 4280 | str_list temp; | 4296 | str_list temp; |
| 4281 | temp.push_back(s("(function()"sv) + nll(with)); | 4297 | temp.push_back(s("(function()"sv) + nll(with)); |
| 4282 | pushScope(); | 4298 | pushScope(); |
| 4299 | _enableReturn.push(true); | ||
| 4283 | transformWith(with, temp, nullptr, true); | 4300 | transformWith(with, temp, nullptr, true); |
| 4301 | _enableReturn.pop(); | ||
| 4284 | popScope(); | 4302 | popScope(); |
| 4285 | temp.push_back(indent() + s("end)()"sv)); | 4303 | temp.push_back(indent() + s("end)()"sv)); |
| 4286 | out.push_back(join(temp)); | 4304 | out.push_back(join(temp)); |
| @@ -4605,6 +4623,9 @@ private: | |||
| 4605 | void transformTblComprehension(TblComprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { | 4623 | void transformTblComprehension(TblComprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { |
| 4606 | switch (usage) { | 4624 | switch (usage) { |
| 4607 | case ExpUsage::Closure: | 4625 | case ExpUsage::Closure: |
| 4626 | pushScope(); | ||
| 4627 | _enableReturn.push(true); | ||
| 4628 | break; | ||
| 4608 | case ExpUsage::Assignment: | 4629 | case ExpUsage::Assignment: |
| 4609 | pushScope(); | 4630 | pushScope(); |
| 4610 | break; | 4631 | break; |
| @@ -4660,6 +4681,7 @@ private: | |||
| 4660 | case ExpUsage::Closure: | 4681 | case ExpUsage::Closure: |
| 4661 | out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); | 4682 | out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); |
| 4662 | popScope(); | 4683 | popScope(); |
| 4684 | _enableReturn.pop(); | ||
| 4663 | out.back().insert(0, s("(function()"sv) + nll(comp)); | 4685 | out.back().insert(0, s("(function()"sv) + nll(comp)); |
| 4664 | out.back().append(indent() + s("end)()"sv)); | 4686 | out.back().append(indent() + s("end)()"sv)); |
| 4665 | break; | 4687 | break; |
| @@ -4713,6 +4735,7 @@ private: | |||
| 4713 | str_list temp; | 4735 | str_list temp; |
| 4714 | if (usage == ExpUsage::Closure) { | 4736 | if (usage == ExpUsage::Closure) { |
| 4715 | temp.push_back(s("(function()"sv) + nll(doNode)); | 4737 | temp.push_back(s("(function()"sv) + nll(doNode)); |
| 4738 | _enableReturn.push(true); | ||
| 4716 | } else { | 4739 | } else { |
| 4717 | temp.push_back(indent() + s("do"sv) + nll(doNode)); | 4740 | temp.push_back(indent() + s("do"sv) + nll(doNode)); |
| 4718 | } | 4741 | } |
| @@ -4720,6 +4743,7 @@ private: | |||
| 4720 | transformBody(doNode->body, temp, usage, assignList); | 4743 | transformBody(doNode->body, temp, usage, assignList); |
| 4721 | popScope(); | 4744 | popScope(); |
| 4722 | if (usage == ExpUsage::Closure) { | 4745 | if (usage == ExpUsage::Closure) { |
| 4746 | _enableReturn.pop(); | ||
| 4723 | temp.push_back(indent() + s("end)()"sv)); | 4747 | temp.push_back(indent() + s("end)()"sv)); |
| 4724 | } else { | 4748 | } else { |
| 4725 | temp.push_back(indent() + s("end"sv) + nlr(doNode)); | 4749 | temp.push_back(indent() + s("end"sv) + nlr(doNode)); |
| @@ -4990,6 +5014,7 @@ private: | |||
| 4990 | str_list temp; | 5014 | str_list temp; |
| 4991 | temp.push_back(s("(function() "sv) + nll(whileNode)); | 5015 | temp.push_back(s("(function() "sv) + nll(whileNode)); |
| 4992 | pushScope(); | 5016 | pushScope(); |
| 5017 | _enableReturn.push(true); | ||
| 4993 | auto accumVar = getUnusedName("_accum_"sv); | 5018 | auto accumVar = getUnusedName("_accum_"sv); |
| 4994 | addToScope(accumVar); | 5019 | addToScope(accumVar); |
| 4995 | auto lenVar = getUnusedName("_len_"sv); | 5020 | auto lenVar = getUnusedName("_len_"sv); |
| @@ -5005,6 +5030,7 @@ private: | |||
| 5005 | popScope(); | 5030 | popScope(); |
| 5006 | temp.push_back(indent() + s("end"sv) + nlr(whileNode)); | 5031 | temp.push_back(indent() + s("end"sv) + nlr(whileNode)); |
| 5007 | temp.push_back(indent() + s("return "sv) + accumVar + nlr(whileNode)); | 5032 | temp.push_back(indent() + s("return "sv) + accumVar + nlr(whileNode)); |
| 5033 | _enableReturn.pop(); | ||
| 5008 | popScope(); | 5034 | popScope(); |
| 5009 | temp.push_back(indent() + s("end)()"sv)); | 5035 | temp.push_back(indent() + s("end)()"sv)); |
| 5010 | out.push_back(join(temp)); | 5036 | out.push_back(join(temp)); |
| @@ -5039,6 +5065,7 @@ private: | |||
| 5039 | if (usage == ExpUsage::Closure) { | 5065 | if (usage == ExpUsage::Closure) { |
| 5040 | temp.push_back(s("(function()"sv) + nll(switchNode)); | 5066 | temp.push_back(s("(function()"sv) + nll(switchNode)); |
| 5041 | pushScope(); | 5067 | pushScope(); |
| 5068 | _enableReturn.push(true); | ||
| 5042 | } | 5069 | } |
| 5043 | auto objVar = singleVariableFrom(switchNode->target); | 5070 | auto objVar = singleVariableFrom(switchNode->target); |
| 5044 | if (objVar.empty()) { | 5071 | if (objVar.empty()) { |
| @@ -5076,6 +5103,7 @@ private: | |||
| 5076 | } | 5103 | } |
| 5077 | temp.push_back(indent() + s("end"sv) + nlr(switchNode)); | 5104 | temp.push_back(indent() + s("end"sv) + nlr(switchNode)); |
| 5078 | if (usage == ExpUsage::Closure) { | 5105 | if (usage == ExpUsage::Closure) { |
| 5106 | _enableReturn.pop(); | ||
| 5079 | popScope(); | 5107 | popScope(); |
| 5080 | temp.push_back(indent() + s("end)()"sv)); | 5108 | temp.push_back(indent() + s("end)()"sv)); |
| 5081 | } | 5109 | } |
