aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MoonParser/moon_ast.cpp793
-rw-r--r--MoonParser/moon_ast.h16
-rw-r--r--MoonParser/moon_parser.cpp4
3 files changed, 560 insertions, 253 deletions
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp
index 2893672..61b997b 100644
--- a/MoonParser/moon_ast.cpp
+++ b/MoonParser/moon_ast.cpp
@@ -10,6 +10,8 @@
10using namespace std::string_view_literals; 10using namespace std::string_view_literals;
11#include "moon_ast.h" 11#include "moon_ast.h"
12 12
13typedef std::list<std::string> str_list;
14
13const input& AstLeaf::getValue() { 15const input& AstLeaf::getValue() {
14 if (_value.empty()) { 16 if (_value.empty()) {
15 _value.assign(m_begin.m_it, m_end.m_it); 17 _value.assign(m_begin.m_it, m_end.m_it);
@@ -86,7 +88,6 @@ AST_IMPL(Invoke)
86AST_IMPL(TableLit) 88AST_IMPL(TableLit)
87AST_IMPL(TableBlock) 89AST_IMPL(TableBlock)
88AST_IMPL(class_member_list) 90AST_IMPL(class_member_list)
89AST_IMPL(ClassLine)
90AST_IMPL(ClassBlock) 91AST_IMPL(ClassBlock)
91AST_IMPL(ClassDecl) 92AST_IMPL(ClassDecl)
92AST_IMPL(export_values) 93AST_IMPL(export_values)
@@ -112,7 +113,6 @@ AST_IMPL(statement_appendix)
112AST_IMPL(BreakLoop) 113AST_IMPL(BreakLoop)
113AST_IMPL(Statement) 114AST_IMPL(Statement)
114AST_IMPL(Body) 115AST_IMPL(Body)
115AST_IMPL(Line)
116AST_IMPL(Block) 116AST_IMPL(Block)
117AST_IMPL(BlockEnd) 117AST_IMPL(BlockEnd)
118 118
@@ -132,7 +132,7 @@ public:
132 auto root = parse<BlockEnd_t>(input, BlockEnd, el, &st); 132 auto root = parse<BlockEnd_t>(input, BlockEnd, el, &st);
133 if (root) { 133 if (root) {
134 std::cout << "compiled!\n\n"; 134 std::cout << "compiled!\n\n";
135 std::vector<std::string> out; 135 str_list out;
136 pushScope(); 136 pushScope();
137 transformBlock(root->block, out); 137 transformBlock(root->block, out);
138 popScope(); 138 popScope();
@@ -146,17 +146,25 @@ public:
146 } 146 }
147 } 147 }
148 _codeCache.clear(); 148 _codeCache.clear();
149 std::stack<std::string> empty; 149 std::stack<std::string> emptyWith;
150 _withVars.swap(empty); 150 _withVars.swap(emptyWith);
151 std::stack<std::string> emptyContinue;
152 _continueVars.swap(emptyContinue);
151 } 153 }
152private: 154private:
153 int _indentOffset = 0; 155 int _indentOffset = 0;
154 Converter _converter; 156 Converter _converter;
155 std::list<input> _codeCache; 157 std::list<input> _codeCache;
156 std::stack<std::string> _withVars; 158 std::stack<std::string> _withVars;
159 std::stack<std::string> _continueVars;
157 std::ostringstream _buf; 160 std::ostringstream _buf;
158 std::string _newLine = "\n"; 161 std::string _newLine = "\n";
159 std::vector<int> _lineTable; 162 std::vector<int> _lineTable;
163 enum class LocalMode {
164 None = 0,
165 Capital = 1,
166 Any = 2
167 };
160 enum class ExportMode { 168 enum class ExportMode {
161 None = 0, 169 None = 0,
162 Capital = 1, 170 Capital = 1,
@@ -347,18 +355,20 @@ private:
347 return str; 355 return str;
348 } 356 }
349 357
350 std::string join(const std::vector<std::string>& items) { 358 std::string join(const str_list& items) {
351 if (items.empty()) return Empty; 359 if (items.empty()) return Empty;
352 else if (items.size() == 1) return items.front(); 360 else if (items.size() == 1) return items.front();
353 return std::accumulate(items.begin()+1, items.end(), items.front(), 361 auto begin = ++items.begin();
362 return std::accumulate(begin, items.end(), items.front(),
354 [&](const std::string& a, const std::string& b) { return a + b; }); 363 [&](const std::string& a, const std::string& b) { return a + b; });
355 } 364 }
356 365
357 std::string join(const std::vector<std::string>& items, std::string_view sep) { 366 std::string join(const str_list& items, std::string_view sep) {
358 if (items.empty()) return Empty; 367 if (items.empty()) return Empty;
359 else if (items.size() == 1) return items.front(); 368 else if (items.size() == 1) return items.front();
360 std::string sepStr = s(sep); 369 std::string sepStr = s(sep);
361 return std::accumulate(items.begin()+1, items.end(), items.front(), 370 auto begin = ++items.begin();
371 return std::accumulate(begin, items.end(), items.front(),
362 [&](const std::string& a, const std::string& b) { return a + sepStr + b; }); 372 [&](const std::string& a, const std::string& b) { return a + sepStr + b; });
363 } 373 }
364 374
@@ -407,29 +417,30 @@ private:
407 return firstValue; 417 return firstValue;
408 } 418 }
409 419
410 void noop(ast_node* node, std::vector<std::string>& out) { 420 void noop(ast_node* node, str_list& out) {
411 auto str = _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); 421 auto str = _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it));
412 out.push_back(s("<"sv) + node->getName() + s(">"sv) + trim(str)); 422 out.push_back(s("<"sv) + node->getName() + s(">"sv) + trim(str));
413 // out.push_back(trim(str)); 423 // out.push_back(trim(str));
414 } 424 }
415 425
416 void noopnl(ast_node* node, std::vector<std::string>& out) { 426 void noopnl(ast_node* node, str_list& out) {
417 auto str = _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); 427 auto str = _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it));
418 out.push_back(s("<"sv) + node->getName() + s(">"sv) + trim(str) + nll(node)); 428 out.push_back(s("<"sv) + node->getName() + s(">"sv) + trim(str) + nll(node));
419 // out.push_back(trim(str) + nll(node)); 429 // out.push_back(trim(str) + nll(node));
420 } 430 }
421 431
422 Statement_t* lastStatementFrom(ast_node* body) { 432 Statement_t* lastStatementFrom(Body_t* body) {
423 ast_node* last = nullptr; 433 if (auto stmt = body->content.as<Statement_t>()) {
424 body->traverse([&](ast_node* n) { 434 return stmt;
425 switch (n->getId()) { 435 } else {
426 case "Statement"_id: 436 auto node = body->content.to<Block_t>()->statements.objects().back();
427 last = n; 437 return static_cast<Statement_t*>(node);
428 return traversal::Return; 438 }
429 default: return traversal::Continue; 439 }
430 } 440
431 }); 441 Statement_t* lastStatementFrom(Block_t* block) {
432 return static_cast<Statement_t*>(last); 442 auto node = block->statements.objects().back();
443 return static_cast<Statement_t*>(node);
433 } 444 }
434 445
435 template <class T> 446 template <class T>
@@ -497,7 +508,7 @@ private:
497 return temp; 508 return temp;
498 } 509 }
499 510
500 void transformStatement(Statement_t* statement, std::vector<std::string>& out) { 511 void transformStatement(Statement_t* statement, str_list& out) {
501 if (statement->appendix) { 512 if (statement->appendix) {
502 if (auto assignment = statement->content.as<Assignment_t>()) { 513 if (auto assignment = statement->content.as<Assignment_t>()) {
503 auto preDefine = getPredefine(transformAssignDefs(assignment->assignable)); 514 auto preDefine = getPredefine(transformAssignDefs(assignment->assignable));
@@ -593,9 +604,9 @@ private:
593 case "ForEach"_id: transformForEach(static_cast<ForEach_t*>(content), out); break; 604 case "ForEach"_id: transformForEach(static_cast<ForEach_t*>(content), out); break;
594 case "Switch"_id: transformSwitch(static_cast<Switch_t*>(content), out); break; 605 case "Switch"_id: transformSwitch(static_cast<Switch_t*>(content), out); break;
595 case "Return"_id: transformReturn(static_cast<Return_t*>(content), out); break; 606 case "Return"_id: transformReturn(static_cast<Return_t*>(content), out); break;
596 case "Local"_id: transformLocal(content, out); break; 607 case "Local"_id: transformLocal(static_cast<Local_t*>(content), out); break;
597 case "Export"_id: transformExport(static_cast<Export_t*>(content), out); break; 608 case "Export"_id: transformExport(static_cast<Export_t*>(content), out); break;
598 case "BreakLoop"_id: transformBreakLoop(content, out); break; 609 case "BreakLoop"_id: transformBreakLoop(static_cast<BreakLoop_t*>(content), out); break;
599 case "Assignment"_id: transformAssignment(static_cast<Assignment_t*>(content), out); break; 610 case "Assignment"_id: transformAssignment(static_cast<Assignment_t*>(content), out); break;
600 case "Comprehension"_id: transformCompCommon(static_cast<Comprehension_t*>(content), out); break; 611 case "Comprehension"_id: transformCompCommon(static_cast<Comprehension_t*>(content), out); break;
601 case "ExpList"_id: { 612 case "ExpList"_id: {
@@ -644,17 +655,27 @@ private:
644 } 655 }
645 } 656 }
646 657
647 std::vector<std::string> getAssignVars(ExpList_t* expList) { 658 str_list getAssignVars(Assignment_t* assignment) {
648 std::vector<std::string> vars; 659 str_list vars;
649 for (auto exp : expList->exprs.objects()) { 660 if (!assignment->target.is<Assign_t>()) return vars;
661 for (auto exp : assignment->assignable->exprs.objects()) {
650 auto var = variableFrom(exp); 662 auto var = variableFrom(exp);
651 vars.push_back(var.empty() ? Empty : var); 663 vars.push_back(var.empty() ? Empty : var);
652 } 664 }
653 return vars; 665 return vars;
654 } 666 }
655 667
656 std::vector<std::string> transformAssignDefs(ExpList_t* expList) { 668 str_list getAssignVars(With_t* with) {
657 std::vector<std::string> preDefs; 669 str_list vars;
670 for (auto exp : with->valueList->exprs.objects()) {
671 auto var = variableFrom(exp);
672 vars.push_back(var.empty() ? Empty : var);
673 }
674 return vars;
675 }
676
677 str_list transformAssignDefs(ExpList_t* expList) {
678 str_list preDefs;
658 expList->traverse([&](ast_node* child) { 679 expList->traverse([&](ast_node* child) {
659 if (child->getId() == "Value"_id) { 680 if (child->getId() == "Value"_id) {
660 if (auto callable = child->getByPath<ChainValue_t, Callable_t>()) { 681 if (auto callable = child->getByPath<ChainValue_t, Callable_t>()) {
@@ -677,7 +698,7 @@ private:
677 return preDefs; 698 return preDefs;
678 } 699 }
679 700
680 std::string getPredefine(const std::vector<std::string>& defs) { 701 std::string getPredefine(const str_list& defs) {
681 if (defs.empty()) return Empty; 702 if (defs.empty()) return Empty;
682 return indent() + s("local "sv) + join(defs, ", "sv); 703 return indent() + s("local "sv) + join(defs, ", "sv);
683 } 704 }
@@ -686,7 +707,7 @@ private:
686 auto info = extractDestructureInfo(assignment); 707 auto info = extractDestructureInfo(assignment);
687 if (!info.first.empty()) { 708 if (!info.first.empty()) {
688 for (const auto& destruct : info.first) { 709 for (const auto& destruct : info.first) {
689 std::vector<std::string> defs; 710 str_list defs;
690 for (const auto& item : destruct.items) { 711 for (const auto& item : destruct.items) {
691 if (item.isVariable && addToScope(item.name)) { 712 if (item.isVariable && addToScope(item.name)) {
692 defs.push_back(item.name); 713 defs.push_back(item.name);
@@ -719,7 +740,7 @@ private:
719 } 740 }
720 } 741 }
721 742
722 void transformAssignment(Assignment_t* assignment, std::vector<std::string>& out) { 743 void transformAssignment(Assignment_t* assignment, str_list& out) {
723 auto assign = ast_cast<Assign_t>(assignment->target); 744 auto assign = ast_cast<Assign_t>(assignment->target);
724 do { 745 do {
725 if (!assign || assign->values.objects().size() != 1) break; 746 if (!assign || assign->values.objects().size() != 1) break;
@@ -734,7 +755,7 @@ private:
734 } 755 }
735 if (item) { 756 if (item) {
736 auto expList = assignment->assignable.get(); 757 auto expList = assignment->assignable.get();
737 std::vector<std::string> temp; 758 str_list temp;
738 auto defs = transformAssignDefs(expList); 759 auto defs = transformAssignDefs(expList);
739 if (!defs.empty()) temp.push_back(getPredefine(defs) + nll(expList)); 760 if (!defs.empty()) temp.push_back(getPredefine(defs) + nll(expList));
740 item->traverse([&](ast_node* node) { 761 item->traverse([&](ast_node* node) {
@@ -798,7 +819,7 @@ private:
798 return; 819 return;
799 } 820 }
800 case "For"_id: { 821 case "For"_id: {
801 std::vector<std::string> temp; 822 str_list temp;
802 auto expList = assignment->assignable.get(); 823 auto expList = assignment->assignable.get();
803 std::string preDefine = getPredefine(assignment); 824 std::string preDefine = getPredefine(assignment);
804 transformForInPlace(static_cast<For_t*>(valueItem), temp, expList); 825 transformForInPlace(static_cast<For_t*>(valueItem), temp, expList);
@@ -806,7 +827,7 @@ private:
806 return; 827 return;
807 } 828 }
808 case "ForEach"_id: { 829 case "ForEach"_id: {
809 std::vector<std::string> temp; 830 str_list temp;
810 auto expList = assignment->assignable.get(); 831 auto expList = assignment->assignable.get();
811 std::string preDefine = getPredefine(assignment); 832 std::string preDefine = getPredefine(assignment);
812 transformForEachInPlace(static_cast<ForEach_t*>(valueItem), temp, expList); 833 transformForEachInPlace(static_cast<ForEach_t*>(valueItem), temp, expList);
@@ -814,7 +835,7 @@ private:
814 return; 835 return;
815 } 836 }
816 case "ClassDecl"_id: { 837 case "ClassDecl"_id: {
817 std::vector<std::string> temp; 838 str_list temp;
818 auto expList = assignment->assignable.get(); 839 auto expList = assignment->assignable.get();
819 std::string preDefine = getPredefine(assignment); 840 std::string preDefine = getPredefine(assignment);
820 transformClassDecl(static_cast<ClassDecl_t*>(valueItem), temp, ExpUsage::Assignment, expList); 841 transformClassDecl(static_cast<ClassDecl_t*>(valueItem), temp, ExpUsage::Assignment, expList);
@@ -822,7 +843,7 @@ private:
822 return; 843 return;
823 } 844 }
824 case "While"_id: { 845 case "While"_id: {
825 std::vector<std::string> temp; 846 str_list temp;
826 auto expList = assignment->assignable.get(); 847 auto expList = assignment->assignable.get();
827 std::string preDefine = getPredefine(assignment); 848 std::string preDefine = getPredefine(assignment);
828 transformWhileClosure(static_cast<While_t*>(valueItem), temp, expList); 849 transformWhileClosure(static_cast<While_t*>(valueItem), temp, expList);
@@ -846,7 +867,7 @@ private:
846 if (info.first.empty()) { 867 if (info.first.empty()) {
847 transformAssignmentCommon(assignment, out); 868 transformAssignmentCommon(assignment, out);
848 } else { 869 } else {
849 std::vector<std::string> temp; 870 str_list temp;
850 for (const auto& destruct : info.first) { 871 for (const auto& destruct : info.first) {
851 if (destruct.items.size() == 1) { 872 if (destruct.items.size() == 1) {
852 auto& pair = destruct.items.front(); 873 auto& pair = destruct.items.front();
@@ -857,7 +878,7 @@ private:
857 _buf << pair.name << " = "sv << info.first.front().value << pair.structure << nll(assignment); 878 _buf << pair.name << " = "sv << info.first.front().value << pair.structure << nll(assignment);
858 temp.push_back(clearBuf()); 879 temp.push_back(clearBuf());
859 } else if (matchAst(Name, destruct.value)) { 880 } else if (matchAst(Name, destruct.value)) {
860 std::vector<std::string> defs, names, values; 881 str_list defs, names, values;
861 for (const auto& item : destruct.items) { 882 for (const auto& item : destruct.items) {
862 if (item.isVariable && addToScope(item.name)) { 883 if (item.isVariable && addToScope(item.name)) {
863 defs.push_back(item.name); 884 defs.push_back(item.name);
@@ -877,7 +898,7 @@ private:
877 } 898 }
878 temp.push_back(clearBuf()); 899 temp.push_back(clearBuf());
879 } else { 900 } else {
880 std::vector<std::string> defs, names, values; 901 str_list defs, names, values;
881 for (const auto& item : destruct.items) { 902 for (const auto& item : destruct.items) {
882 if (item.isVariable && addToScope(item.name)) { 903 if (item.isVariable && addToScope(item.name)) {
883 defs.push_back(item.name); 904 defs.push_back(item.name);
@@ -904,7 +925,7 @@ private:
904 } 925 }
905 } 926 }
906 927
907 void transformAssignItem(ast_node* value, std::vector<std::string>& out) { 928 void transformAssignItem(ast_node* value, str_list& out) {
908 switch (value->getId()) { 929 switch (value->getId()) {
909 case "With"_id: transformWith(static_cast<With_t*>(value), out); break; 930 case "With"_id: transformWith(static_cast<With_t*>(value), out); break;
910 case "If"_id: transformIf(static_cast<If_t*>(value), out, IfUsage::Closure); break; 931 case "If"_id: transformIf(static_cast<If_t*>(value), out, IfUsage::Closure); break;
@@ -947,7 +968,7 @@ private:
947 s("["sv) + std::to_string(index) + s("]"sv) + p.structure}); 968 s("["sv) + std::to_string(index) + s("]"sv) + p.structure});
948 } 969 }
949 } else { 970 } else {
950 std::vector<std::string> temp; 971 str_list temp;
951 transformExp(static_cast<Exp_t*>(pair), temp); 972 transformExp(static_cast<Exp_t*>(pair), temp);
952 pairs.push_back({ 973 pairs.push_back({
953 item->getByPath<Callable_t, Variable_t>() != nullptr, 974 item->getByPath<Callable_t, Variable_t>() != nullptr,
@@ -978,7 +999,7 @@ private:
978 s("."sv) + toString(key) + p.structure}); 999 s("."sv) + toString(key) + p.structure});
979 } 1000 }
980 } else { 1001 } else {
981 std::vector<std::string> temp; 1002 str_list temp;
982 transformExp(exp, temp); 1003 transformExp(exp, temp);
983 pairs.push_back({ 1004 pairs.push_back({
984 item->getByPath<Callable_t, Variable_t>() != nullptr, 1005 item->getByPath<Callable_t, Variable_t>() != nullptr,
@@ -1020,7 +1041,7 @@ private:
1020 } 1041 }
1021 using iter = std::list<ast_node*>::iterator; 1042 using iter = std::list<ast_node*>::iterator;
1022 std::vector<std::pair<iter, iter>> destructPairs; 1043 std::vector<std::pair<iter, iter>> destructPairs;
1023 std::vector<std::string> temp; 1044 str_list temp;
1024 for (auto i = exprs.begin(), j = values.begin(); i != exprs.end(); ++i, ++j) { 1045 for (auto i = exprs.begin(), j = values.begin(); i != exprs.end(); ++i, ++j) {
1025 auto expr = *i; 1046 auto expr = *i;
1026 ast_node* destructNode = expr->getByPath<Value_t, SimpleValue_t, TableLit_t>(); 1047 ast_node* destructNode = expr->getByPath<Value_t, SimpleValue_t, TableLit_t>();
@@ -1054,8 +1075,8 @@ private:
1054 return {std::move(destructs), newAssignment}; 1075 return {std::move(destructs), newAssignment};
1055 } 1076 }
1056 1077
1057 void transformAssignmentCommon(Assignment_t* assignment, std::vector<std::string>& out) { 1078 void transformAssignmentCommon(Assignment_t* assignment, str_list& out) {
1058 std::vector<std::string> temp; 1079 str_list temp;
1059 auto expList = assignment->assignable.get(); 1080 auto expList = assignment->assignable.get();
1060 auto action = assignment->target.get(); 1081 auto action = assignment->target.get();
1061 switch (action->getId()) { 1082 switch (action->getId()) {
@@ -1103,7 +1124,7 @@ private:
1103 } 1124 }
1104 } 1125 }
1105 1126
1106 void transformCond(const std::list<ast_node*>& nodes, std::vector<std::string>& out, IfUsage usage = IfUsage::Common, bool unless = false) { 1127 void transformCond(const std::list<ast_node*>& nodes, str_list& out, IfUsage usage = IfUsage::Common, bool unless = false) {
1107 std::vector<ast_ptr<ast_node, false, false>> ns; 1128 std::vector<ast_ptr<ast_node, false, false>> ns;
1108 for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { 1129 for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) {
1109 ns.push_back(*it); 1130 ns.push_back(*it);
@@ -1138,7 +1159,7 @@ private:
1138 transformCond(newIf->nodes.objects(), out, usage, unless); 1159 transformCond(newIf->nodes.objects(), out, usage, unless);
1139 return; 1160 return;
1140 } 1161 }
1141 std::vector<std::string> temp; 1162 str_list temp;
1142 if (usage == IfUsage::Closure) { 1163 if (usage == IfUsage::Closure) {
1143 temp.push_back(s("(function()"sv) + nll(nodes.front())); 1164 temp.push_back(s("(function()"sv) + nll(nodes.front()));
1144 pushScope(); 1165 pushScope();
@@ -1206,7 +1227,7 @@ private:
1206 } 1227 }
1207 for (const auto& pair : ifCondPairs) { 1228 for (const auto& pair : ifCondPairs) {
1208 if (pair.first) { 1229 if (pair.first) {
1209 std::vector<std::string> tmp; 1230 str_list tmp;
1210 auto condition = pair.first->condition.get(); 1231 auto condition = pair.first->condition.get();
1211 transformExp(condition, tmp); 1232 transformExp(condition, tmp);
1212 _buf << indent() << (pair == ifCondPairs.front() ? ""sv : "else"sv) << 1233 _buf << indent() << (pair == ifCondPairs.front() ? ""sv : "else"sv) <<
@@ -1237,32 +1258,32 @@ private:
1237 out.push_back(join(temp)); 1258 out.push_back(join(temp));
1238 } 1259 }
1239 1260
1240 void transformIf(If_t* ifNode, std::vector<std::string>& out, IfUsage usage = IfUsage::Common) { 1261 void transformIf(If_t* ifNode, str_list& out, IfUsage usage = IfUsage::Common) {
1241 transformCond(ifNode->nodes.objects(), out, usage); 1262 transformCond(ifNode->nodes.objects(), out, usage);
1242 } 1263 }
1243 1264
1244 void transformUnless(Unless_t* unless, std::vector<std::string>& out, IfUsage usage = IfUsage::Common) { 1265 void transformUnless(Unless_t* unless, str_list& out, IfUsage usage = IfUsage::Common) {
1245 transformCond(unless->nodes.objects(), out, usage, true); 1266 transformCond(unless->nodes.objects(), out, usage, true);
1246 } 1267 }
1247 1268
1248 void transformExpList(ExpList_t* expList, std::vector<std::string>& out) { 1269 void transformExpList(ExpList_t* expList, str_list& out) {
1249 std::vector<std::string> temp; 1270 str_list temp;
1250 for (auto exp : expList->exprs.objects()) { 1271 for (auto exp : expList->exprs.objects()) {
1251 transformExp(static_cast<Exp_t*>(exp), temp); 1272 transformExp(static_cast<Exp_t*>(exp), temp);
1252 } 1273 }
1253 out.push_back(join(temp, ", "sv)); 1274 out.push_back(join(temp, ", "sv));
1254 } 1275 }
1255 1276
1256 void transformExpListLow(ExpListLow_t* expListLow, std::vector<std::string>& out) { 1277 void transformExpListLow(ExpListLow_t* expListLow, str_list& out) {
1257 std::vector<std::string> temp; 1278 str_list temp;
1258 for (auto exp : expListLow->exprs.objects()) { 1279 for (auto exp : expListLow->exprs.objects()) {
1259 transformExp(static_cast<Exp_t*>(exp), temp); 1280 transformExp(static_cast<Exp_t*>(exp), temp);
1260 } 1281 }
1261 out.push_back(join(temp, ", "sv)); 1282 out.push_back(join(temp, ", "sv));
1262 } 1283 }
1263 1284
1264 void transformExp(Exp_t* exp, std::vector<std::string>& out) { 1285 void transformExp(Exp_t* exp, str_list& out) {
1265 std::vector<std::string> temp; 1286 str_list temp;
1266 transformValue(exp->value, temp); 1287 transformValue(exp->value, temp);
1267 for (auto _opValue : exp->opValues.objects()) { 1288 for (auto _opValue : exp->opValues.objects()) {
1268 auto opValue = static_cast<exp_op_value_t*>(_opValue); 1289 auto opValue = static_cast<exp_op_value_t*>(_opValue);
@@ -1272,7 +1293,7 @@ private:
1272 out.push_back(join(temp, " "sv)); 1293 out.push_back(join(temp, " "sv));
1273 } 1294 }
1274 1295
1275 void transformValue(Value_t* value, std::vector<std::string>& out) { 1296 void transformValue(Value_t* value, str_list& out) {
1276 auto item = value->item.get(); 1297 auto item = value->item.get();
1277 switch (item->getId()) { 1298 switch (item->getId()) {
1278 case "SimpleValue"_id: transformSimpleValue(static_cast<SimpleValue_t*>(item), out); break; 1299 case "SimpleValue"_id: transformSimpleValue(static_cast<SimpleValue_t*>(item), out); break;
@@ -1291,8 +1312,8 @@ private:
1291 } 1312 }
1292 } 1313 }
1293 1314
1294 void transformChainValue(ChainValue_t* chainValue, std::vector<std::string>& out) { 1315 void transformChainValue(ChainValue_t* chainValue, str_list& out) {
1295 std::vector<std::string> temp; 1316 str_list temp;
1296 auto caller = chainValue->caller.get(); 1317 auto caller = chainValue->caller.get();
1297 bool hasArgs = chainValue->arguments; 1318 bool hasArgs = chainValue->arguments;
1298 switch (caller->getId()) { 1319 switch (caller->getId()) {
@@ -1302,13 +1323,13 @@ private:
1302 } 1323 }
1303 if (hasArgs) { 1324 if (hasArgs) {
1304 transformInvokeArgs(chainValue->arguments, temp); 1325 transformInvokeArgs(chainValue->arguments, temp);
1305 out.push_back(temp[0] + s("("sv) + temp[1] + s(")"sv)); 1326 out.push_back(temp.front() + s("("sv) + temp.back() + s(")"sv));
1306 } else { 1327 } else {
1307 out.push_back(temp[0]); 1328 out.push_back(temp.front());
1308 } 1329 }
1309 } 1330 }
1310 1331
1311 void transformCallable(Callable_t* callable, std::vector<std::string>& out, bool invoke) { 1332 void transformCallable(Callable_t* callable, str_list& out, bool invoke) {
1312 auto item = callable->item.get(); 1333 auto item = callable->item.get();
1313 switch (item->getId()) { 1334 switch (item->getId()) {
1314 case "Variable"_id: transformVariable(static_cast<Variable_t*>(item), out); break; 1335 case "Variable"_id: transformVariable(static_cast<Variable_t*>(item), out); break;
@@ -1319,13 +1340,13 @@ private:
1319 } 1340 }
1320 } 1341 }
1321 1342
1322 void transformParens(Parens_t* parans, std::vector<std::string>& out) { 1343 void transformParens(Parens_t* parans, str_list& out) {
1323 std::vector<std::string> temp; 1344 str_list temp;
1324 transformExp(parans->expr, temp); 1345 transformExp(parans->expr, temp);
1325 out.push_back(s("("sv) + temp.front() + s(")"sv)); 1346 out.push_back(s("("sv) + temp.front() + s(")"sv));
1326 } 1347 }
1327 1348
1328 void transformSimpleValue(SimpleValue_t* simpleValue, std::vector<std::string>& out) { 1349 void transformSimpleValue(SimpleValue_t* simpleValue, str_list& out) {
1329 auto value = simpleValue->value.get(); 1350 auto value = simpleValue->value.get();
1330 switch (value->getId()) { 1351 switch (value->getId()) {
1331 case "const_value"_id: transform_const_value(static_cast<const_value_t*>(value), out); break; 1352 case "const_value"_id: transform_const_value(static_cast<const_value_t*>(value), out); break;
@@ -1348,8 +1369,8 @@ private:
1348 } 1369 }
1349 } 1370 }
1350 1371
1351 void transformFunLit(FunLit_t* funLit, std::vector<std::string>& out) { 1372 void transformFunLit(FunLit_t* funLit, str_list& out) {
1352 std::vector<std::string> temp; 1373 str_list temp;
1353 bool isFatArrow = toString(funLit->arrow) == "=>"sv; 1374 bool isFatArrow = toString(funLit->arrow) == "=>"sv;
1354 pushScope(); 1375 pushScope();
1355 if (auto argsDef = funLit->argsDef.get()) { 1376 if (auto argsDef = funLit->argsDef.get()) {
@@ -1359,9 +1380,10 @@ private:
1359 } else { 1380 } else {
1360 temp.push_back(Empty); 1381 temp.push_back(Empty);
1361 } 1382 }
1362 auto& args = temp[0]; 1383 auto it = temp.begin();
1363 auto& initArgs = temp[1]; 1384 auto& args = *it;
1364 auto& bodyCodes = temp[2]; 1385 auto& initArgs = *(++it);
1386 auto& bodyCodes = *(++it);
1365 _buf << "function("sv << 1387 _buf << "function("sv <<
1366 (isFatArrow ? s("self, "sv) : Empty) << 1388 (isFatArrow ? s("self, "sv) : Empty) <<
1367 args << ')'; 1389 args << ')';
@@ -1395,9 +1417,81 @@ private:
1395 out.push_back(clearBuf()); 1417 out.push_back(clearBuf());
1396 } 1418 }
1397 1419
1398 void transformCodes(ast_node* nodes, std::vector<std::string>& out, bool implicitReturn) { 1420 void transformCodes(const std::list<ast_node*>& nodes, str_list& out, bool implicitReturn) {
1421 LocalMode mode = LocalMode::None;
1422 Local_t* any = nullptr, *capital = nullptr;
1423 for (auto node : nodes) {
1424 auto stmt = static_cast<Statement_t*>(node);
1425 if (auto local = stmt->content.as<Local_t>()) {
1426 if (auto flag = local->name.as<local_flag_t>()) {
1427 LocalMode newMode = toString(flag) == "*"sv ? LocalMode::Any : LocalMode::Capital;
1428 if (int(newMode) > int(mode)) {
1429 mode = newMode;
1430 }
1431 if (mode == LocalMode::Any) {
1432 if (!any) any = local;
1433 if (!capital) capital = local;
1434 } else {
1435 if (!capital) capital = local;
1436 }
1437 } else {
1438 auto names = local->name.to<NameList_t>();
1439 for (auto name : names->names.objects()) {
1440 local->forceDecls.push_back(toString(name));
1441 }
1442 }
1443 } else if (mode != LocalMode::None) {
1444 ClassDecl_t* classDecl = nullptr;
1445 if (auto assignment = stmt->content.as<Assignment_t>()) {
1446 auto vars = getAssignVars(assignment);
1447 for (const auto& var : vars) {
1448 if (var.empty()) continue;
1449 if (std::isupper(var[0]) && capital) {
1450 capital->decls.push_back(var);
1451 } else if (any) {
1452 any->decls.push_back(var);
1453 }
1454 }
1455 auto info = extractDestructureInfo(assignment);
1456 if (!info.first.empty()) {
1457 for (const auto& destruct : info.first)
1458 for (const auto& item : destruct.items)
1459 if (item.isVariable) {
1460 if (std::isupper(item.name[0]) && capital) { capital->decls.push_back(item.name);
1461 } else if (any) {
1462 any->decls.push_back(item.name);
1463 }
1464 }
1465 }
1466 do {
1467 auto assign = assignment->target.as<Assign_t>();
1468 if (!assign) break;
1469 if (assign->values.objects().size() != 1) break;
1470 auto exp = ast_cast<Exp_t>(assign->values.objects().front());
1471 if (!exp) break;
1472 auto value = singleValueFrom(exp);
1473 classDecl = value->getByPath<SimpleValue_t, ClassDecl_t>();
1474 } while (false);
1475 } else if (auto expList = stmt->content.as<ExpList_t>()) {
1476 auto value = singleValueFrom(expList);
1477 classDecl = value->getByPath<SimpleValue_t, ClassDecl_t>();
1478 }
1479 if (classDecl) {
1480 if (auto variable = classDecl->name->item.as<Variable_t>()) {
1481 auto className = toString(variable);
1482 if (!className.empty()) {
1483 if (std::isupper(className[0]) && capital) {
1484 capital->decls.push_back(className);
1485 } else if (any) {
1486 any->decls.push_back(className);
1487 }
1488 }
1489 }
1490 }
1491 }
1492 }
1399 if (implicitReturn) { 1493 if (implicitReturn) {
1400 auto last = lastStatementFrom(nodes); 1494 auto last = static_cast<Statement_t*>(nodes.back());
1401 if (ast_is<ExpList_t>(last->content) && (!last->appendix || 1495 if (ast_is<ExpList_t>(last->content) && (!last->appendix ||
1402 !last->appendix->item.is<CompInner_t>())) { 1496 !last->appendix->item.is<CompInner_t>())) {
1403 auto expList = static_cast<ExpList_t*>(last->content.get()); 1497 auto expList = static_cast<ExpList_t*>(last->content.get());
@@ -1408,27 +1502,26 @@ private:
1408 last->content.set(returnNode); 1502 last->content.set(returnNode);
1409 } 1503 }
1410 } 1504 }
1411 std::vector<std::string> temp; 1505 str_list temp;
1412 nodes->traverse([&](ast_node* node) { 1506 for (auto node : nodes) {
1413 switch (node->getId()) { 1507 transformStatement(static_cast<Statement_t*>(node), temp);
1414 case "Statement"_id: 1508 }
1415 transformStatement(static_cast<Statement_t*>(node), temp);
1416 return traversal::Return;
1417 default: return traversal::Continue;
1418 }
1419 });
1420 out.push_back(join(temp)); 1509 out.push_back(join(temp));
1421 } 1510 }
1422 1511
1423 void transformBody(Body_t* body, std::vector<std::string>& out, bool implicitReturn = false) { 1512 void transformBody(Body_t* body, str_list& out, bool implicitReturn = false) {
1424 transformCodes(body, out, implicitReturn); 1513 if (auto stmt = body->content.as<Statement_t>()) {
1514 transformCodes(std::list<ast_node*>{stmt}, out, implicitReturn);
1515 } else {
1516 transformCodes(body->content.to<Block_t>()->statements.objects(), out, implicitReturn);
1517 }
1425 } 1518 }
1426 1519
1427 void transformBlock(Block_t* block, std::vector<std::string>& out, bool implicitReturn = true) { 1520 void transformBlock(Block_t* block, str_list& out, bool implicitReturn = true) {
1428 transformCodes(block, out, implicitReturn); 1521 transformCodes(block->statements.objects(), out, implicitReturn);
1429 } 1522 }
1430 1523
1431 void transformReturn(Return_t* returnNode, std::vector<std::string>& out) { 1524 void transformReturn(Return_t* returnNode, str_list& out) {
1432 if (auto valueList = returnNode->valueList.get()) { 1525 if (auto valueList = returnNode->valueList.get()) {
1433 if (auto singleValue = singleValueFrom(valueList)) { 1526 if (auto singleValue = singleValueFrom(valueList)) {
1434 if (auto comp = singleValue->getByPath<SimpleValue_t, Comprehension_t>()) { 1527 if (auto comp = singleValue->getByPath<SimpleValue_t, Comprehension_t>()) {
@@ -1465,7 +1558,7 @@ private:
1465 out.back() = indent() + s("return "sv) + out.back() + nlr(returnNode); 1558 out.back() = indent() + s("return "sv) + out.back() + nlr(returnNode);
1466 return; 1559 return;
1467 } else { 1560 } else {
1468 std::vector<std::string> temp; 1561 str_list temp;
1469 transformExpListLow(valueList, temp); 1562 transformExpListLow(valueList, temp);
1470 out.push_back(indent() + s("return "sv) + temp.back() + nlr(returnNode)); 1563 out.push_back(indent() + s("return "sv) + temp.back() + nlr(returnNode));
1471 } 1564 }
@@ -1474,7 +1567,7 @@ private:
1474 } 1567 }
1475 } 1568 }
1476 1569
1477 void transformFnArgsDef(FnArgsDef_t* argsDef, std::vector<std::string>& out) { 1570 void transformFnArgsDef(FnArgsDef_t* argsDef, str_list& out) {
1478 if (!argsDef->defList) { 1571 if (!argsDef->defList) {
1479 out.push_back(Empty); 1572 out.push_back(Empty);
1480 out.push_back(Empty); 1573 out.push_back(Empty);
@@ -1486,7 +1579,7 @@ private:
1486 } 1579 }
1487 } 1580 }
1488 1581
1489 void transform_outer_var_shadow(outer_var_shadow_t* shadow, std::vector<std::string>& out) { 1582 void transform_outer_var_shadow(outer_var_shadow_t* shadow, str_list& out) {
1490 markVarShadowed(); 1583 markVarShadowed();
1491 if (shadow->varList) { 1584 if (shadow->varList) {
1492 for (auto name : shadow->varList->names.objects()) { 1585 for (auto name : shadow->varList->names.objects()) {
@@ -1495,13 +1588,13 @@ private:
1495 } 1588 }
1496 } 1589 }
1497 1590
1498 void transformFnArgDefList(FnArgDefList_t* argDefList, std::vector<std::string>& out) { 1591 void transformFnArgDefList(FnArgDefList_t* argDefList, str_list& out) {
1499 struct ArgItem { 1592 struct ArgItem {
1500 std::string name; 1593 std::string name;
1501 std::string assignSelf; 1594 std::string assignSelf;
1502 }; 1595 };
1503 std::list<ArgItem> argItems; 1596 std::list<ArgItem> argItems;
1504 std::vector<std::string> temp; 1597 str_list temp;
1505 std::string varNames; 1598 std::string varNames;
1506 bool assignSelf = false; 1599 bool assignSelf = false;
1507 for (auto _def : argDefList->definitions.objects()) { 1600 for (auto _def : argDefList->definitions.objects()) {
@@ -1576,7 +1669,7 @@ private:
1576 out.push_back(initCodes); 1669 out.push_back(initCodes);
1577 } 1670 }
1578 1671
1579 void transformSelfName(SelfName_t* selfName, std::vector<std::string>& out, bool invoke) { 1672 void transformSelfName(SelfName_t* selfName, str_list& out, bool invoke) {
1580 auto name = selfName->name.get(); 1673 auto name = selfName->name.get();
1581 switch (name->getId()) { 1674 switch (name->getId()) {
1582 case "self_class_name"_id: 1675 case "self_class_name"_id:
@@ -1594,8 +1687,8 @@ private:
1594 } 1687 }
1595 } 1688 }
1596 1689
1597 void transformColonChainClosure(ChainValue_t* chainValue, std::vector<std::string>& out) { 1690 void transformColonChainClosure(ChainValue_t* chainValue, str_list& out) {
1598 std::vector<std::string> temp; 1691 str_list temp;
1599 temp.push_back(s("(function()"sv) + nll(chainValue)); 1692 temp.push_back(s("(function()"sv) + nll(chainValue));
1600 pushScope(); 1693 pushScope();
1601 transformColonChain(chainValue, temp, ExpUsage::Return); 1694 transformColonChain(chainValue, temp, ExpUsage::Return);
@@ -1604,8 +1697,8 @@ private:
1604 out.push_back(join(temp)); 1697 out.push_back(join(temp));
1605 } 1698 }
1606 1699
1607 void transformColonChain(ChainValue_t* chainValue, std::vector<std::string>& out, ExpUsage usage = ExpUsage::Common, ExpList_t* expList = nullptr) { 1700 void transformColonChain(ChainValue_t* chainValue, str_list& out, ExpUsage usage = ExpUsage::Common, ExpList_t* expList = nullptr) {
1608 std::vector<std::string> temp; 1701 str_list temp;
1609 auto chain = chainValue->caller.to<Chain_t>(); 1702 auto chain = chainValue->caller.to<Chain_t>();
1610 const auto& chainList = chain->items.objects(); 1703 const auto& chainList = chain->items.objects();
1611 auto end = --chainList.end(); 1704 auto end = --chainList.end();
@@ -1639,7 +1732,7 @@ private:
1639 auto funcName = toString(colonChainItem->name); 1732 auto funcName = toString(colonChainItem->name);
1640 std::string assignList; 1733 std::string assignList;
1641 if (expList) { 1734 if (expList) {
1642 std::vector<std::string> tmp; 1735 str_list tmp;
1643 transformExpList(expList, tmp); 1736 transformExpList(expList, tmp);
1644 assignList = tmp.back(); 1737 assignList = tmp.back();
1645 } 1738 }
@@ -1674,8 +1767,8 @@ private:
1674 out.push_back(clearBuf()); 1767 out.push_back(clearBuf());
1675 } 1768 }
1676 1769
1677 void transformChain(Chain_t* chain, std::vector<std::string>& out) { 1770 void transformChain(Chain_t* chain, str_list& out) {
1678 std::vector<std::string> temp; 1771 str_list temp;
1679 const auto& chainList = chain->items.objects(); 1772 const auto& chainList = chain->items.objects();
1680 switch (chainList.front()->getId()) { 1773 switch (chainList.front()->getId()) {
1681 case "DotChainItem"_id: 1774 case "DotChainItem"_id:
@@ -1715,20 +1808,20 @@ private:
1715 out.push_back(join(temp)); 1808 out.push_back(join(temp));
1716 } 1809 }
1717 1810
1718 void transformDotChainItem(DotChainItem_t* dotChainItem, std::vector<std::string>& out) { 1811 void transformDotChainItem(DotChainItem_t* dotChainItem, str_list& out) {
1719 out.push_back(s("."sv) + toString(dotChainItem->name)); 1812 out.push_back(s("."sv) + toString(dotChainItem->name));
1720 } 1813 }
1721 1814
1722 void transformColonChainItem(ColonChainItem_t* colonChainItem, std::vector<std::string>& out) { 1815 void transformColonChainItem(ColonChainItem_t* colonChainItem, str_list& out) {
1723 out.push_back(s(colonChainItem->switchToDot ? "."sv : ":"sv) + toString(colonChainItem->name)); 1816 out.push_back(s(colonChainItem->switchToDot ? "."sv : ":"sv) + toString(colonChainItem->name));
1724 } 1817 }
1725 1818
1726 void transformSlice(Slice_t* slice, std::vector<std::string>& out) { 1819 void transformSlice(Slice_t* slice, str_list& out) {
1727 throw std::logic_error("Slice syntax not supported here"); 1820 throw std::logic_error("Slice syntax not supported here");
1728 } 1821 }
1729 1822
1730 void transformInvoke(Invoke_t* invoke, std::vector<std::string>& out) { 1823 void transformInvoke(Invoke_t* invoke, str_list& out) {
1731 std::vector<std::string> temp; 1824 str_list temp;
1732 for (auto arg : invoke->args.objects()) { 1825 for (auto arg : invoke->args.objects()) {
1733 switch (arg->getId()) { 1826 switch (arg->getId()) {
1734 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break; 1827 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break;
@@ -1741,27 +1834,27 @@ private:
1741 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv)); 1834 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv));
1742 } 1835 }
1743 1836
1744 void transform_unary_exp(unary_exp_t* unary_exp, std::vector<std::string>& out) { 1837 void transform_unary_exp(unary_exp_t* unary_exp, str_list& out) {
1745 std::string op = toString(unary_exp->m_begin.m_it, unary_exp->item->m_begin.m_it); 1838 std::string op = toString(unary_exp->m_begin.m_it, unary_exp->item->m_begin.m_it);
1746 std::vector<std::string> temp{op + (op == "not"sv ? op + " " : Empty)}; 1839 str_list temp{op + (op == "not"sv ? op + " " : Empty)};
1747 transformExp(unary_exp->item, temp); 1840 transformExp(unary_exp->item, temp);
1748 out.push_back(join(temp)); 1841 out.push_back(join(temp));
1749 } 1842 }
1750 1843
1751 void transformVariable(Variable_t* name, std::vector<std::string>& out) { 1844 void transformVariable(Variable_t* name, str_list& out) {
1752 out.push_back(toString(name)); 1845 out.push_back(toString(name));
1753 } 1846 }
1754 1847
1755 void transformNum(Num_t* num, std::vector<std::string>& out) { 1848 void transformNum(Num_t* num, str_list& out) {
1756 out.push_back(toString(num)); 1849 out.push_back(toString(num));
1757 } 1850 }
1758 1851
1759 void transformTableLit(TableLit_t* table, std::vector<std::string>& out) { 1852 void transformTableLit(TableLit_t* table, str_list& out) {
1760 transformTable(table, table->values.objects(), out); 1853 transformTable(table, table->values.objects(), out);
1761 } 1854 }
1762 1855
1763 void transformCompCommon(Comprehension_t* comp, std::vector<std::string>& out) { 1856 void transformCompCommon(Comprehension_t* comp, str_list& out) {
1764 std::vector<std::string> temp; 1857 str_list temp;
1765 auto compInner = comp->forLoop.get(); 1858 auto compInner = comp->forLoop.get();
1766 for (auto item : compInner->items.objects()) { 1859 for (auto item : compInner->items.objects()) {
1767 switch (item->getId()) { 1860 switch (item->getId()) {
@@ -1789,8 +1882,8 @@ private:
1789 out.push_back(clearBuf()); 1882 out.push_back(clearBuf());
1790 } 1883 }
1791 1884
1792 void transformComprehension(Comprehension_t* comp, std::vector<std::string>& out) { 1885 void transformComprehension(Comprehension_t* comp, str_list& out) {
1793 std::vector<std::string> temp; 1886 str_list temp;
1794 std::string accum = getUnusedName("_accum_"); 1887 std::string accum = getUnusedName("_accum_");
1795 std::string len = getUnusedName("_len_"); 1888 std::string len = getUnusedName("_len_");
1796 addToScope(accum); 1889 addToScope(accum);
@@ -1829,8 +1922,8 @@ private:
1829 out.push_back(clearBuf()); 1922 out.push_back(clearBuf());
1830 } 1923 }
1831 1924
1832 void transformCompInPlace(Comprehension_t* comp, ExpList_t* expList, std::vector<std::string>& out) { 1925 void transformCompInPlace(Comprehension_t* comp, ExpList_t* expList, str_list& out) {
1833 std::vector<std::string> temp; 1926 str_list temp;
1834 pushScope(); 1927 pushScope();
1835 transformComprehension(comp, temp); 1928 transformComprehension(comp, temp);
1836 auto assign = new_ptr<Assign_t>(); 1929 auto assign = new_ptr<Assign_t>();
@@ -1843,20 +1936,20 @@ private:
1843 transformAssignment(assignment, temp); 1936 transformAssignment(assignment, temp);
1844 out.push_back( 1937 out.push_back(
1845 s("do"sv) + nll(comp) + 1938 s("do"sv) + nll(comp) +
1846 temp[1] + 1939 *(++temp.begin()) +
1847 temp.back()); 1940 temp.back());
1848 popScope(); 1941 popScope();
1849 out.back() = out.back() + indent() + s("end"sv) + nlr(comp); 1942 out.back() = out.back() + indent() + s("end"sv) + nlr(comp);
1850 } 1943 }
1851 1944
1852 void transformCompReturn(Comprehension_t* comp, std::vector<std::string>& out) { 1945 void transformCompReturn(Comprehension_t* comp, str_list& out) {
1853 std::vector<std::string> temp; 1946 str_list temp;
1854 transformComprehension(comp, temp); 1947 transformComprehension(comp, temp);
1855 out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp)); 1948 out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp));
1856 } 1949 }
1857 1950
1858 void transformCompClosure(Comprehension_t* comp, std::vector<std::string>& out) { 1951 void transformCompClosure(Comprehension_t* comp, str_list& out) {
1859 std::vector<std::string> temp; 1952 str_list temp;
1860 std::string before = s("(function()"sv) + nll(comp); 1953 std::string before = s("(function()"sv) + nll(comp);
1861 pushScope(); 1954 pushScope();
1862 transformComprehension(comp, temp); 1955 transformComprehension(comp, temp);
@@ -1868,9 +1961,9 @@ private:
1868 out.back() = out.back() + indent() + s("end)()"sv); 1961 out.back() = out.back() + indent() + s("end)()"sv);
1869 } 1962 }
1870 1963
1871 void transformForEachHead(AssignableNameList_t* nameList, ast_node* loopTarget, std::vector<std::string>& out) { 1964 void transformForEachHead(AssignableNameList_t* nameList, ast_node* loopTarget, str_list& out) {
1872 std::vector<std::string> temp; 1965 str_list temp;
1873 std::vector<std::string> vars; 1966 str_list vars;
1874 std::list<std::pair<ast_node*, ast_ptr<ast_node,false,false>>> destructPairs; 1967 std::list<std::pair<ast_node*, ast_ptr<ast_node,false,false>>> destructPairs;
1875 for (auto _item : nameList->items.objects()) { 1968 for (auto _item : nameList->items.objects()) {
1876 auto item = static_cast<NameOrDestructure_t*>(_item)->item.get(); 1969 auto item = static_cast<NameOrDestructure_t*>(_item)->item.get();
@@ -1887,7 +1980,7 @@ private:
1887 default: break; 1980 default: break;
1888 } 1981 }
1889 } 1982 }
1890 std::list<std::string> varBefore, varAfter; 1983 str_list varBefore, varAfter;
1891 switch (loopTarget->getId()) { 1984 switch (loopTarget->getId()) {
1892 case "star_exp"_id: { 1985 case "star_exp"_id: {
1893 auto star_exp = static_cast<star_exp_t*>(loopTarget); 1986 auto star_exp = static_cast<star_exp_t*>(loopTarget);
@@ -2009,12 +2102,12 @@ private:
2009 for (auto& var : varAfter) addToScope(var); 2102 for (auto& var : varAfter) addToScope(var);
2010 } 2103 }
2011 2104
2012 void transformCompForEach(CompForEach_t* comp, std::vector<std::string>& out) { 2105 void transformCompForEach(CompForEach_t* comp, str_list& out) {
2013 transformForEachHead(comp->nameList, comp->loopValue, out); 2106 transformForEachHead(comp->nameList, comp->loopValue, out);
2014 } 2107 }
2015 2108
2016 void transformInvokeArgs(InvokeArgs_t* invokeArgs, std::vector<std::string>& out) { 2109 void transformInvokeArgs(InvokeArgs_t* invokeArgs, str_list& out) {
2017 std::vector<std::string> temp; 2110 str_list temp;
2018 for (auto arg : invokeArgs->args.objects()) { 2111 for (auto arg : invokeArgs->args.objects()) {
2019 switch (arg->getId()) { 2112 switch (arg->getId()) {
2020 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break; 2113 case "Exp"_id: transformExp(static_cast<Exp_t*>(arg), temp); break;
@@ -2025,8 +2118,8 @@ private:
2025 out.push_back(join(temp, ", "sv)); 2118 out.push_back(join(temp, ", "sv));
2026 } 2119 }
2027 2120
2028 void transformForHead(For_t* forNode, std::vector<std::string>& out) { 2121 void transformForHead(For_t* forNode, str_list& out) {
2029 std::vector<std::string> temp; 2122 str_list temp;
2030 std::string varName = toString(forNode->varName); 2123 std::string varName = toString(forNode->varName);
2031 transformExp(forNode->startValue, temp); 2124 transformExp(forNode->startValue, temp);
2032 transformExp(forNode->stopValue, temp); 2125 transformExp(forNode->stopValue, temp);
@@ -2035,21 +2128,63 @@ private:
2035 } else { 2128 } else {
2036 temp.emplace_back(); 2129 temp.emplace_back();
2037 } 2130 }
2038 _buf << indent() << "for "sv << varName << " = "sv << temp[0] << ", "sv << temp[1] << (temp[2].empty() ? Empty : s(", "sv) + temp[2]) << " do"sv << nll(forNode); 2131 auto it = temp.begin();
2132 const auto& start = *it;
2133 const auto& stop = *(++it);
2134 const auto& step = *(++it);
2135 _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : s(", "sv) + step) << " do"sv << nll(forNode);
2039 out.push_back(clearBuf()); 2136 out.push_back(clearBuf());
2040 } 2137 }
2041 2138
2042 void transformFor(For_t* forNode, std::vector<std::string>& out) { 2139 void transformLoopBody(Body_t* body, str_list& out) {
2043 std::vector<std::string> temp; 2140 str_list temp;
2141 bool withContinue = traversal::Stop == body->traverse([&](ast_node* node) {
2142 switch (node->getId()) {
2143 case "For"_id:
2144 case "ForEach"_id:
2145 return traversal::Return;
2146 case "BreakLoop"_id: {
2147 return toString(node) == "continue"sv ?
2148 traversal::Stop : traversal::Return;
2149 }
2150 default:
2151 return traversal::Continue;
2152 }
2153 });
2154 if (withContinue) {
2155 auto continueVar = getUnusedName("_continue_"sv);
2156 addToScope(continueVar);
2157 _buf << indent() << "local "sv << continueVar << " = false"sv << nll(body);
2158 _buf << indent() << "repeat"sv << nll(body);
2159 temp.push_back(clearBuf());
2160 _continueVars.push(continueVar);
2161 pushScope();
2162 }
2163 transformBody(body, temp);
2164 if (withContinue) {
2165 _buf << indent() << _continueVars.top() << " = true"sv << nll(body);
2166 popScope();
2167 _buf << indent() << "until true"sv << nlr(body);
2168 _buf << indent() << "if not "sv << _continueVars.top() << " then"sv << nlr(body);
2169 _buf << indent(1) << "break"sv << nlr(body);
2170 _buf << indent() << "end"sv << nlr(body);
2171 temp.push_back(clearBuf());
2172 _continueVars.pop();
2173 }
2174 out.push_back(join(temp));
2175 }
2176
2177 void transformFor(For_t* forNode, str_list& out) {
2178 str_list temp;
2044 transformForHead(forNode, temp); 2179 transformForHead(forNode, temp);
2045 pushScope(); 2180 pushScope();
2046 transformBody(forNode->body, temp); 2181 transformLoopBody(forNode->body, temp);
2047 popScope(); 2182 popScope();
2048 out.push_back(temp[0] + temp[1] + indent() + s("end"sv) + nlr(forNode)); 2183 out.push_back(join(temp) + indent() + s("end"sv) + nlr(forNode));
2049 } 2184 }
2050 2185
2051 void transformForClosure(For_t* forNode, std::vector<std::string>& out) { 2186 void transformForClosure(For_t* forNode, str_list& out) {
2052 std::vector<std::string> temp; 2187 str_list temp;
2053 std::string accum = getUnusedName("_accum_"); 2188 std::string accum = getUnusedName("_accum_");
2054 std::string len = getUnusedName("_len_"); 2189 std::string len = getUnusedName("_len_");
2055 addToScope(accum); 2190 addToScope(accum);
@@ -2074,7 +2209,7 @@ private:
2074 last->content.set(assignment); 2209 last->content.set(assignment);
2075 } 2210 }
2076 pushScope(); 2211 pushScope();
2077 transformBody(forNode->body, temp); 2212 transformLoopBody(forNode->body, temp);
2078 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forNode->body)); 2213 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forNode->body));
2079 popScope(); 2214 popScope();
2080 temp.push_back(indent() + s("end"sv) + nlr(forNode) + indent() + s("return "sv) + accum + nlr(forNode)); 2215 temp.push_back(indent() + s("end"sv) + nlr(forNode) + indent() + s("return "sv) + accum + nlr(forNode));
@@ -2083,8 +2218,8 @@ private:
2083 out.push_back(join(temp)); 2218 out.push_back(join(temp));
2084 } 2219 }
2085 2220
2086 void transformForInPlace(For_t* forNode, std::vector<std::string>& out, ExpList_t* assignExpList) { 2221 void transformForInPlace(For_t* forNode, str_list& out, ExpList_t* assignExpList) {
2087 std::vector<std::string> temp; 2222 str_list temp;
2088 std::string accum = getUnusedName("_accum_"); 2223 std::string accum = getUnusedName("_accum_");
2089 std::string len = getUnusedName("_len_"); 2224 std::string len = getUnusedName("_len_");
2090 _buf << indent() << "do"sv << nll(forNode); 2225 _buf << indent() << "do"sv << nll(forNode);
@@ -2109,7 +2244,7 @@ private:
2109 last->content.set(assignment); 2244 last->content.set(assignment);
2110 } 2245 }
2111 pushScope(); 2246 pushScope();
2112 transformBody(forNode->body, temp); 2247 transformLoopBody(forNode->body, temp);
2113 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forNode->body)); 2248 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forNode->body));
2114 popScope(); 2249 popScope();
2115 temp.push_back(indent() + s("end"sv) + nlr(forNode)); 2250 temp.push_back(indent() + s("end"sv) + nlr(forNode));
@@ -2126,22 +2261,21 @@ private:
2126 out.push_back(join(temp)); 2261 out.push_back(join(temp));
2127 } 2262 }
2128 2263
2129 void transformBinaryOperator(BinaryOperator_t* node, std::vector<std::string>& out) { 2264 void transformBinaryOperator(BinaryOperator_t* node, str_list& out) {
2130 auto op = toString(node); 2265 auto op = toString(node);
2131 out.push_back(op == "!="sv ? s("~="sv) : op); 2266 out.push_back(op == "!="sv ? s("~="sv) : op);
2132 } 2267 }
2133 2268
2134 void transformForEach(ForEach_t* forEach, std::vector<std::string>& out) { 2269 void transformForEach(ForEach_t* forEach, str_list& out) {
2135 std::vector<std::string> temp; 2270 str_list temp;
2136 transformForEachHead(forEach->nameList, forEach->loopValue, temp); 2271 transformForEachHead(forEach->nameList, forEach->loopValue, temp);
2137 pushScope(); 2272 transformLoopBody(forEach->body, temp);
2138 transformBody(forEach->body, temp);
2139 popScope(); 2273 popScope();
2140 out.push_back(temp[0] + temp[1] + indent() + s("end"sv) + nlr(forEach)); 2274 out.push_back(temp.front() + temp.back() + indent() + s("end"sv) + nlr(forEach));
2141 } 2275 }
2142 2276
2143 void transformForEachClosure(ForEach_t* forEach, std::vector<std::string>& out) { 2277 void transformForEachClosure(ForEach_t* forEach, str_list& out) {
2144 std::vector<std::string> temp; 2278 str_list temp;
2145 std::string accum = getUnusedName("_accum_"); 2279 std::string accum = getUnusedName("_accum_");
2146 std::string len = getUnusedName("_len_"); 2280 std::string len = getUnusedName("_len_");
2147 addToScope(accum); 2281 addToScope(accum);
@@ -2165,8 +2299,7 @@ private:
2165 assignment->target.set(assign); 2299 assignment->target.set(assign);
2166 last->content.set(assignment); 2300 last->content.set(assignment);
2167 } 2301 }
2168 pushScope(); 2302 transformLoopBody(forEach->body, temp);
2169 transformBody(forEach->body, temp);
2170 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forEach->body)); 2303 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forEach->body));
2171 popScope(); 2304 popScope();
2172 temp.push_back(indent() + s("end"sv) + nlr(forEach) + indent() + s("return "sv) + accum + nlr(forEach)); 2305 temp.push_back(indent() + s("end"sv) + nlr(forEach) + indent() + s("return "sv) + accum + nlr(forEach));
@@ -2175,8 +2308,8 @@ private:
2175 out.push_back(join(temp)); 2308 out.push_back(join(temp));
2176 } 2309 }
2177 2310
2178 void transformForEachInPlace(ForEach_t* forEach, std::vector<std::string>& out, ExpList_t* assignExpList) { 2311 void transformForEachInPlace(ForEach_t* forEach, str_list& out, ExpList_t* assignExpList) {
2179 std::vector<std::string> temp; 2312 str_list temp;
2180 std::string accum = getUnusedName("_accum_"); 2313 std::string accum = getUnusedName("_accum_");
2181 std::string len = getUnusedName("_len_"); 2314 std::string len = getUnusedName("_len_");
2182 _buf << indent() << "do"sv << nll(forEach); 2315 _buf << indent() << "do"sv << nll(forEach);
@@ -2200,8 +2333,7 @@ private:
2200 assignment->target.set(assign); 2333 assignment->target.set(assign);
2201 last->content.set(assignment); 2334 last->content.set(assignment);
2202 } 2335 }
2203 pushScope(); 2336 transformLoopBody(forEach->body, temp);
2204 transformBody(forEach->body, temp);
2205 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forEach->body)); 2337 temp.push_back(indent() + len + s(" = "sv) + len + s(" + 1"sv) + nlr(forEach->body));
2206 popScope(); 2338 popScope();
2207 temp.push_back(indent() + s("end"sv) + nlr(forEach)); 2339 temp.push_back(indent() + s("end"sv) + nlr(forEach));
@@ -2218,14 +2350,14 @@ private:
2218 out.push_back(join(temp)); 2350 out.push_back(join(temp));
2219 } 2351 }
2220 2352
2221 void transform_variable_pair(variable_pair_t* pair, std::vector<std::string>& out) { 2353 void transform_variable_pair(variable_pair_t* pair, str_list& out) {
2222 auto name = toString(pair->name); 2354 auto name = toString(pair->name);
2223 out.push_back(name + s(" = "sv) + name); 2355 out.push_back(name + s(" = "sv) + name);
2224 } 2356 }
2225 2357
2226 void transform_normal_pair(normal_pair_t* pair, std::vector<std::string>& out) { 2358 void transform_normal_pair(normal_pair_t* pair, str_list& out) {
2227 auto key = pair->key.get(); 2359 auto key = pair->key.get();
2228 std::vector<std::string> temp; 2360 str_list temp;
2229 switch (key->getId()) { 2361 switch (key->getId()) {
2230 case "KeyName"_id: transformKeyName(static_cast<KeyName_t*>(key), temp); break; 2362 case "KeyName"_id: transformKeyName(static_cast<KeyName_t*>(key), temp); break;
2231 case "Exp"_id: 2363 case "Exp"_id:
@@ -2247,10 +2379,10 @@ private:
2247 case "TableBlock"_id: transformTableBlock(static_cast<TableBlock_t*>(value), temp); break; 2379 case "TableBlock"_id: transformTableBlock(static_cast<TableBlock_t*>(value), temp); break;
2248 default: break; 2380 default: break;
2249 } 2381 }
2250 out.push_back(temp[0] + s(" = "sv) + temp[1]); 2382 out.push_back(temp.front() + s(" = "sv) + temp.back());
2251 } 2383 }
2252 2384
2253 void transformKeyName(KeyName_t* keyName, std::vector<std::string>& out) { 2385 void transformKeyName(KeyName_t* keyName, str_list& out) {
2254 auto name = keyName->name.get(); 2386 auto name = keyName->name.get();
2255 switch (name->getId()) { 2387 switch (name->getId()) {
2256 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(name), out, false); break; 2388 case "SelfName"_id: transformSelfName(static_cast<SelfName_t*>(name), out, false); break;
@@ -2259,23 +2391,38 @@ private:
2259 } 2391 }
2260 } 2392 }
2261 2393
2262 void transformLuaString(LuaString_t* luaString, std::vector<std::string>& out) { 2394 void transformLuaString(LuaString_t* luaString, str_list& out) {
2263 out.push_back(toString(luaString)); 2395 out.push_back(toString(luaString));
2264 } 2396 }
2265 2397
2266 void transformSingleString(SingleString_t* singleString, std::vector<std::string>& out) { 2398 void replace(std::string& str, std::string_view from, std::string_view to) {
2267 out.push_back(toString(singleString)); 2399 size_t start_pos = 0;
2400 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
2401 str.replace(start_pos, from.size(), to);
2402 start_pos += to.size();
2403 }
2404 }
2405
2406 void transformSingleString(SingleString_t* singleString, str_list& out) {
2407 auto str = toString(singleString);
2408 replace(str, "\r"sv, "");
2409 replace(str, "\n"sv, "\\n"sv);
2410 out.push_back(str);
2268 } 2411 }
2269 2412
2270 void transformDoubleString(DoubleString_t* doubleString, std::vector<std::string>& out) { 2413 void transformDoubleString(DoubleString_t* doubleString, str_list& out) {
2271 std::vector<std::string> temp; 2414 str_list temp;
2272 for (auto _seg : doubleString->segments.objects()) { 2415 for (auto _seg : doubleString->segments.objects()) {
2273 auto seg = static_cast<double_string_content_t*>(_seg); 2416 auto seg = static_cast<double_string_content_t*>(_seg);
2274 auto content = seg->content.get(); 2417 auto content = seg->content.get();
2275 switch (content->getId()) { 2418 switch (content->getId()) {
2276 case "double_string_inner"_id: 2419 case "double_string_inner"_id: {
2277 temp.push_back(s("\""sv) + toString(content) + s("\""sv)); 2420 auto str = toString(content);
2421 replace(str, "\r"sv, "");
2422 replace(str, "\n"sv, "\\n"sv);
2423 temp.push_back(s("\""sv) + str + s("\""sv));
2278 break; 2424 break;
2425 }
2279 case "Exp"_id: 2426 case "Exp"_id:
2280 transformExp(static_cast<Exp_t*>(content), temp); 2427 transformExp(static_cast<Exp_t*>(content), temp);
2281 temp.back() = s("tostring("sv) + temp.back() + s(")"sv); 2428 temp.back() = s("tostring("sv) + temp.back() + s(")"sv);
@@ -2286,7 +2433,7 @@ private:
2286 out.push_back(join(temp, " .. "sv)); 2433 out.push_back(join(temp, " .. "sv));
2287 } 2434 }
2288 2435
2289 void transformString(String_t* string, std::vector<std::string>& out) { 2436 void transformString(String_t* string, str_list& out) {
2290 auto str = string->str.get(); 2437 auto str = string->str.get();
2291 switch (str->getId()) { 2438 switch (str->getId()) {
2292 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(str), out); break; 2439 case "SingleString"_id: transformSingleString(static_cast<SingleString_t*>(str), out); break;
@@ -2309,8 +2456,8 @@ private:
2309 return {Empty, false}; 2456 return {Empty, false};
2310 } 2457 }
2311 2458
2312 void transformClassDeclClosure(ClassDecl_t* classDecl, std::vector<std::string>& out) { 2459 void transformClassDeclClosure(ClassDecl_t* classDecl, str_list& out) {
2313 std::vector<std::string> temp; 2460 str_list temp;
2314 temp.push_back(s("(function()"sv) + nll(classDecl)); 2461 temp.push_back(s("(function()"sv) + nll(classDecl));
2315 pushScope(); 2462 pushScope();
2316 transformClassDecl(classDecl, temp, ExpUsage::Return); 2463 transformClassDecl(classDecl, temp, ExpUsage::Return);
@@ -2319,8 +2466,8 @@ private:
2319 out.push_back(join(temp)); 2466 out.push_back(join(temp));
2320 } 2467 }
2321 2468
2322 void transformClassDecl(ClassDecl_t* classDecl, std::vector<std::string>& out, ExpUsage usage = ExpUsage::Common, ExpList_t* expList = nullptr) { 2469 void transformClassDecl(ClassDecl_t* classDecl, str_list& out, ExpUsage usage = ExpUsage::Common, ExpList_t* expList = nullptr) {
2323 std::vector<std::string> temp; 2470 str_list temp;
2324 auto body = classDecl->body.get(); 2471 auto body = classDecl->body.get();
2325 auto assignable = classDecl->name.get(); 2472 auto assignable = classDecl->name.get();
2326 auto extend = classDecl->extend.get(); 2473 auto extend = classDecl->extend.get();
@@ -2364,9 +2511,10 @@ private:
2364 addToScope(classVar); 2511 addToScope(classVar);
2365 temp.push_back(indent() + s("local "sv) + classVar + nll(classDecl)); 2512 temp.push_back(indent() + s("local "sv) + classVar + nll(classDecl));
2366 if (body) { 2513 if (body) {
2367 std::vector<std::string> varDefs; 2514 str_list varDefs;
2368 body->traverse([&](ast_node* node) { 2515 body->traverse([&](ast_node* node) {
2369 if (node->getId() == "Statement"_id) { 2516 if (node->getId() == "Statement"_id) {
2517 ClassDecl_t* clsDecl = nullptr;
2370 if (auto assignment = node->getByPath<Assignment_t>()) { 2518 if (auto assignment = node->getByPath<Assignment_t>()) {
2371 auto names = transformAssignDefs(assignment->assignable.get()); 2519 auto names = transformAssignDefs(assignment->assignable.get());
2372 varDefs.insert(varDefs.end(), names.begin(), names.end()); 2520 varDefs.insert(varDefs.end(), names.begin(), names.end());
@@ -2377,6 +2525,24 @@ private:
2377 if (item.isVariable && addToScope(item.name)) 2525 if (item.isVariable && addToScope(item.name))
2378 varDefs.push_back(item.name); 2526 varDefs.push_back(item.name);
2379 } 2527 }
2528 do {
2529 auto assign = assignment->target.as<Assign_t>();
2530 if (!assign) break;
2531 if (assign->values.objects().size() != 1) break;
2532 auto exp = ast_cast<Exp_t>(assign->values.objects().front());
2533 if (!exp) break;
2534 auto value = singleValueFrom(exp);
2535 clsDecl = value->getByPath<SimpleValue_t, ClassDecl_t>();
2536 } while (false);
2537 } else if (auto expList = node->getByPath<ExpList_t>()) {
2538 auto value = singleValueFrom(expList);
2539 clsDecl = value->getByPath<SimpleValue_t, ClassDecl_t>();
2540 }
2541 if (clsDecl) {
2542 std::string clsName;
2543 bool newDefined = false;
2544 std::tie(clsName,newDefined) = defineClassVariable(clsDecl->name);
2545 if (newDefined) varDefs.push_back(clsName);
2380 } 2546 }
2381 return traversal::Return; 2547 return traversal::Return;
2382 } 2548 }
@@ -2400,14 +2566,12 @@ private:
2400 addToScope(baseVar); 2566 addToScope(baseVar);
2401 addToScope(selfVar); 2567 addToScope(selfVar);
2402 temp.push_back(indent() + s("local "sv) + baseVar + s(" = "sv)); 2568 temp.push_back(indent() + s("local "sv) + baseVar + s(" = "sv));
2403 std::vector<std::string> builtins; 2569 str_list builtins;
2404 std::vector<std::string> commons; 2570 str_list commons;
2405 std::vector<std::string> statements; 2571 str_list statements;
2406 if (body) { 2572 if (body) {
2407 std::list<ClassMember> members; 2573 std::list<ClassMember> members;
2408 for (auto _classLine : classDecl->body->lines.objects()) { 2574 for (auto content : classDecl->body->contents.objects()) {
2409 auto classLine = static_cast<ClassLine_t*>(_classLine);
2410 auto content = classLine->content.get();
2411 switch (content->getId()) { 2575 switch (content->getId()) {
2412 case "class_member_list"_id: { 2576 case "class_member_list"_id: {
2413 size_t inc = transform_class_member_list(static_cast<class_member_list_t*>(content), members, classVar); 2577 size_t inc = transform_class_member_list(static_cast<class_member_list_t*>(content), members, classVar);
@@ -2451,7 +2615,7 @@ private:
2451 temp.back() += s("{ }"sv) + nll(classDecl); 2615 temp.back() += s("{ }"sv) + nll(classDecl);
2452 } 2616 }
2453 temp.push_back(indent() + baseVar + s(".__index = "sv) + baseVar + nll(classDecl)); 2617 temp.push_back(indent() + baseVar + s(".__index = "sv) + baseVar + nll(classDecl));
2454 std::vector<std::string> tmp; 2618 str_list tmp;
2455 if (usage == ExpUsage::Assignment) { 2619 if (usage == ExpUsage::Assignment) {
2456 auto assign = new_ptr<Assign_t>(); 2620 auto assign = new_ptr<Assign_t>();
2457 assign->values.push_back(toAst<Exp_t>(classVar, Exp)); 2621 assign->values.push_back(toAst<Exp_t>(classVar, Exp));
@@ -2535,7 +2699,7 @@ private:
2535 } 2699 }
2536 2700
2537 size_t transform_class_member_list(class_member_list_t* class_member_list, std::list<ClassMember>& out, const std::string& classVar) { 2701 size_t transform_class_member_list(class_member_list_t* class_member_list, std::list<ClassMember>& out, const std::string& classVar) {
2538 std::vector<std::string> temp; 2702 str_list temp;
2539 size_t count = 0; 2703 size_t count = 0;
2540 for (auto keyValue : class_member_list->values.objects()) { 2704 for (auto keyValue : class_member_list->values.objects()) {
2541 MemType type = MemType::Common; 2705 MemType type = MemType::Common;
@@ -2645,7 +2809,7 @@ private:
2645 return count; 2809 return count;
2646 } 2810 }
2647 2811
2648 void transformAssignable(Assignable_t* assignable, std::vector<std::string>& out) { 2812 void transformAssignable(Assignable_t* assignable, str_list& out) {
2649 auto item = assignable->item.get(); 2813 auto item = assignable->item.get();
2650 switch (item->getId()) { 2814 switch (item->getId()) {
2651 case "Chain"_id: transformChain(static_cast<Chain_t*>(item), out); break; 2815 case "Chain"_id: transformChain(static_cast<Chain_t*>(item), out); break;
@@ -2655,12 +2819,12 @@ private:
2655 } 2819 }
2656 } 2820 }
2657 2821
2658 void transformWith(With_t* with, std::vector<std::string>& out) { 2822 void transformWith(With_t* with, str_list& out) {
2659 std::vector<std::string> temp; 2823 str_list temp;
2660 std::string withVar; 2824 std::string withVar;
2661 bool scoped = false; 2825 bool scoped = false;
2662 if (with->assigns) { 2826 if (with->assigns) {
2663 auto vars = getAssignVars(with->valueList); 2827 auto vars = getAssignVars(with);
2664 if (vars.front().empty()) { 2828 if (vars.front().empty()) {
2665 if (with->assigns->values.objects().size() == 1) { 2829 if (with->assigns->values.objects().size() == 1) {
2666 auto var = variableFrom(with->assigns->values.objects().front()); 2830 auto var = variableFrom(with->assigns->values.objects().front());
@@ -2731,11 +2895,11 @@ private:
2731 out.push_back(join(temp)); 2895 out.push_back(join(temp));
2732 } 2896 }
2733 2897
2734 void transform_const_value(const_value_t* const_value, std::vector<std::string>& out) { 2898 void transform_const_value(const_value_t* const_value, str_list& out) {
2735 out.push_back(toString(const_value)); 2899 out.push_back(toString(const_value));
2736 } 2900 }
2737 2901
2738 void transformExport(Export_t* exportNode, std::vector<std::string>& out) { 2902 void transformExport(Export_t* exportNode, str_list& out) {
2739 auto item = exportNode->item.get(); 2903 auto item = exportNode->item.get();
2740 switch (item->getId()) { 2904 switch (item->getId()) {
2741 case "ClassDecl"_id: { 2905 case "ClassDecl"_id: {
@@ -2789,8 +2953,8 @@ private:
2789 } 2953 }
2790 } 2954 }
2791 2955
2792 void transformTable(ast_node* table, const std::list<ast_node*>& pairs, std::vector<std::string>& out) { 2956 void transformTable(ast_node* table, const std::list<ast_node*>& pairs, str_list& out) {
2793 std::vector<std::string> temp; 2957 str_list temp;
2794 pushScope(); 2958 pushScope();
2795 for (auto pair : pairs) { 2959 for (auto pair : pairs) {
2796 switch (pair->getId()) { 2960 switch (pair->getId()) {
@@ -2805,15 +2969,15 @@ private:
2805 out.back() += (indent() + s("}"sv)); 2969 out.back() += (indent() + s("}"sv));
2806 } 2970 }
2807 2971
2808 void transform_simple_table(simple_table_t* table, std::vector<std::string>& out) { 2972 void transform_simple_table(simple_table_t* table, str_list& out) {
2809 transformTable(table, table->pairs.objects(), out); 2973 transformTable(table, table->pairs.objects(), out);
2810 } 2974 }
2811 2975
2812 void transformTblComprehension(TblComprehension_t* comp, std::vector<std::string>& out) { 2976 void transformTblComprehension(TblComprehension_t* comp, str_list& out) {
2813 std::vector<std::string> kv; 2977 str_list kv;
2814 std::string tbl = getUnusedName("_tbl_"); 2978 std::string tbl = getUnusedName("_tbl_");
2815 addToScope(tbl); 2979 addToScope(tbl);
2816 std::vector<std::string> temp; 2980 str_list temp;
2817 auto compInner = comp->forLoop.get(); 2981 auto compInner = comp->forLoop.get();
2818 for (auto item : compInner->items.objects()) { 2982 for (auto item : compInner->items.objects()) {
2819 switch (item->getId()) { 2983 switch (item->getId()) {
@@ -2857,8 +3021,8 @@ private:
2857 out.push_back(clearBuf()); 3021 out.push_back(clearBuf());
2858 } 3022 }
2859 3023
2860 void transformTblCompInPlace(TblComprehension_t* comp, ExpList_t* expList, std::vector<std::string>& out) { 3024 void transformTblCompInPlace(TblComprehension_t* comp, ExpList_t* expList, str_list& out) {
2861 std::vector<std::string> temp; 3025 str_list temp;
2862 pushScope(); 3026 pushScope();
2863 transformTblComprehension(comp, temp); 3027 transformTblComprehension(comp, temp);
2864 auto assign = new_ptr<Assign_t>(); 3028 auto assign = new_ptr<Assign_t>();
@@ -2871,20 +3035,20 @@ private:
2871 transformAssignment(assignment, temp); 3035 transformAssignment(assignment, temp);
2872 out.push_back( 3036 out.push_back(
2873 s("do"sv) + nll(comp) + 3037 s("do"sv) + nll(comp) +
2874 temp[1] + 3038 *(++temp.begin()) +
2875 temp.back()); 3039 temp.back());
2876 popScope(); 3040 popScope();
2877 out.back() = out.back() + indent() + s("end"sv) + nlr(comp); 3041 out.back() = out.back() + indent() + s("end"sv) + nlr(comp);
2878 } 3042 }
2879 3043
2880 void transformTblCompReturn(TblComprehension_t* comp, std::vector<std::string>& out) { 3044 void transformTblCompReturn(TblComprehension_t* comp, str_list& out) {
2881 std::vector<std::string> temp; 3045 str_list temp;
2882 transformTblComprehension(comp, temp); 3046 transformTblComprehension(comp, temp);
2883 out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp)); 3047 out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp));
2884 } 3048 }
2885 3049
2886 void transformTblCompClosure(TblComprehension_t* comp, std::vector<std::string>& out) { 3050 void transformTblCompClosure(TblComprehension_t* comp, str_list& out) {
2887 std::vector<std::string> temp; 3051 str_list temp;
2888 std::string before = s("(function()"sv) + nll(comp); 3052 std::string before = s("(function()"sv) + nll(comp);
2889 pushScope(); 3053 pushScope();
2890 transformTblComprehension(comp, temp); 3054 transformTblComprehension(comp, temp);
@@ -2898,8 +3062,8 @@ private:
2898 out.back() = out.back() + indent() + s("end)()"sv); 3062 out.back() = out.back() + indent() + s("end)()"sv);
2899 } 3063 }
2900 3064
2901 void transformCompFor(CompFor_t* comp, std::vector<std::string>& out) { 3065 void transformCompFor(CompFor_t* comp, str_list& out) {
2902 std::vector<std::string> temp; 3066 str_list temp;
2903 std::string varName = toString(comp->varName); 3067 std::string varName = toString(comp->varName);
2904 transformExp(comp->startValue, temp); 3068 transformExp(comp->startValue, temp);
2905 transformExp(comp->stopValue, temp); 3069 transformExp(comp->stopValue, temp);
@@ -2908,18 +3072,22 @@ private:
2908 } else { 3072 } else {
2909 temp.emplace_back(); 3073 temp.emplace_back();
2910 } 3074 }
2911 _buf << indent() << "for "sv << varName << " = "sv << temp[0] << ", "sv << temp[1] << (temp[2].empty() ? Empty : s(", "sv) + temp[2]) << " do"sv << nll(comp); 3075 auto it = temp.begin();
3076 const auto& start = *it;
3077 const auto& stop = *(++it);
3078 const auto& step = *(++it);
3079 _buf << indent() << "for "sv << varName << " = "sv << start << ", "sv << stop << (step.empty() ? Empty : s(", "sv) + step) << " do"sv << nll(comp);
2912 out.push_back(clearBuf()); 3080 out.push_back(clearBuf());
2913 pushScope(); 3081 pushScope();
2914 addToScope(varName); 3082 addToScope(varName);
2915 } 3083 }
2916 3084
2917 void transformTableBlock(TableBlock_t* table, std::vector<std::string>& out) { 3085 void transformTableBlock(TableBlock_t* table, str_list& out) {
2918 transformTable(table, table->values.objects(), out); 3086 transformTable(table, table->values.objects(), out);
2919 } 3087 }
2920 3088
2921 void transformDo(Do_t* doNode, std::vector<std::string>& out, bool implicitReturn = false) { 3089 void transformDo(Do_t* doNode, str_list& out, bool implicitReturn = false) {
2922 std::vector<std::string> temp; 3090 str_list temp;
2923 temp.push_back(indent() + s("do"sv) + nll(doNode)); 3091 temp.push_back(indent() + s("do"sv) + nll(doNode));
2924 pushScope(); 3092 pushScope();
2925 transformBody(doNode->body, temp, implicitReturn); 3093 transformBody(doNode->body, temp, implicitReturn);
@@ -2928,8 +3096,8 @@ private:
2928 out.push_back(join(temp)); 3096 out.push_back(join(temp));
2929 } 3097 }
2930 3098
2931 void transformDoClosure(Do_t* doNode, std::vector<std::string>& out) { 3099 void transformDoClosure(Do_t* doNode, str_list& out) {
2932 std::vector<std::string> temp; 3100 str_list temp;
2933 temp.push_back(s("(function()"sv) + nll(doNode)); 3101 temp.push_back(s("(function()"sv) + nll(doNode));
2934 pushScope(); 3102 pushScope();
2935 transformBody(doNode->body, temp, true); 3103 transformBody(doNode->body, temp, true);
@@ -2938,8 +3106,8 @@ private:
2938 out.push_back(join(temp)); 3106 out.push_back(join(temp));
2939 } 3107 }
2940 3108
2941 void transformImport(Import_t* import, std::vector<std::string>& out) { 3109 void transformImport(Import_t* import, str_list& out) {
2942 std::vector<std::string> temp; 3110 str_list temp;
2943 auto objVar = variableFrom(import->exp); 3111 auto objVar = variableFrom(import->exp);
2944 ast_ptr<Assignment_t, false, false> objAssign; 3112 ast_ptr<Assignment_t, false, false> objAssign;
2945 if (objVar.empty()) { 3113 if (objVar.empty()) {
@@ -3035,8 +3203,8 @@ private:
3035 out.push_back(join(temp)); 3203 out.push_back(join(temp));
3036 } 3204 }
3037 3205
3038 void transformWhileClosure(While_t* whileNode, std::vector<std::string>& out, ExpList_t* expList = nullptr) { 3206 void transformWhileClosure(While_t* whileNode, str_list& out, ExpList_t* expList = nullptr) {
3039 std::vector<std::string> temp; 3207 str_list temp;
3040 if (expList) { 3208 if (expList) {
3041 temp.push_back(indent() + s("do"sv) + nll(whileNode)); 3209 temp.push_back(indent() + s("do"sv) + nll(whileNode));
3042 } else { 3210 } else {
@@ -3062,7 +3230,7 @@ private:
3062 newAssignment->target.set(assign); 3230 newAssignment->target.set(assign);
3063 last->content.set(newAssignment); 3231 last->content.set(newAssignment);
3064 } 3232 }
3065 transformBody(whileNode->body, temp); 3233 transformLoopBody(whileNode->body, temp);
3066 temp.push_back(indent() + lenVar + s(" = "sv) + lenVar + s(" + 1"sv) + nlr(whileNode)); 3234 temp.push_back(indent() + lenVar + s(" = "sv) + lenVar + s(" + 1"sv) + nlr(whileNode));
3067 popScope(); 3235 popScope();
3068 temp.push_back(indent() + s("end"sv) + nlr(whileNode)); 3236 temp.push_back(indent() + s("end"sv) + nlr(whileNode));
@@ -3085,11 +3253,11 @@ private:
3085 out.push_back(join(temp)); 3253 out.push_back(join(temp));
3086 } 3254 }
3087 3255
3088 void transformWhile(While_t* whileNode, std::vector<std::string>& out) { 3256 void transformWhile(While_t* whileNode, str_list& out) {
3089 std::vector<std::string> temp; 3257 str_list temp;
3090 pushScope(); 3258 pushScope();
3091 transformExp(whileNode->condition, temp); 3259 transformExp(whileNode->condition, temp);
3092 transformBody(whileNode->body, temp); 3260 transformLoopBody(whileNode->body, temp);
3093 popScope(); 3261 popScope();
3094 _buf << indent() << "while "sv << temp.front() << " do"sv << nll(whileNode); 3262 _buf << indent() << "while "sv << temp.front() << " do"sv << nll(whileNode);
3095 _buf << temp.back(); 3263 _buf << temp.back();
@@ -3097,8 +3265,8 @@ private:
3097 out.push_back(clearBuf()); 3265 out.push_back(clearBuf());
3098 } 3266 }
3099 3267
3100 void transformSwitchClosure(Switch_t* switchNode, std::vector<std::string>& out) { 3268 void transformSwitchClosure(Switch_t* switchNode, str_list& out) {
3101 std::vector<std::string> temp; 3269 str_list temp;
3102 temp.push_back(s("(function()"sv) + nll(switchNode)); 3270 temp.push_back(s("(function()"sv) + nll(switchNode));
3103 pushScope(); 3271 pushScope();
3104 transformSwitch(switchNode, temp, true); 3272 transformSwitch(switchNode, temp, true);
@@ -3107,8 +3275,8 @@ private:
3107 out.push_back(join(temp)); 3275 out.push_back(join(temp));
3108 } 3276 }
3109 3277
3110 void transformSwitch(Switch_t* switchNode, std::vector<std::string>& out, bool implicitReturn = false) { 3278 void transformSwitch(Switch_t* switchNode, str_list& out, bool implicitReturn = false) {
3111 std::vector<std::string> temp; 3279 str_list temp;
3112 auto objVar = variableFrom(switchNode->target); 3280 auto objVar = variableFrom(switchNode->target);
3113 if (objVar.empty()) { 3281 if (objVar.empty()) {
3114 objVar = getUnusedName("_exp_"sv); 3282 objVar = getUnusedName("_exp_"sv);
@@ -3121,7 +3289,7 @@ private:
3121 for (auto branch_ : branches) { 3289 for (auto branch_ : branches) {
3122 auto branch = static_cast<SwitchCase_t*>(branch_); 3290 auto branch = static_cast<SwitchCase_t*>(branch_);
3123 temp.push_back(indent() + s(branches.front() == branch ? "if"sv : "elseif"sv)); 3291 temp.push_back(indent() + s(branches.front() == branch ? "if"sv : "elseif"sv));
3124 std::vector<std::string> tmp; 3292 str_list tmp;
3125 const auto& exprs = branch->valueList->exprs.objects(); 3293 const auto& exprs = branch->valueList->exprs.objects();
3126 for (auto exp_ : exprs) { 3294 for (auto exp_ : exprs) {
3127 auto exp = static_cast<Exp_t*>(exp_); 3295 auto exp = static_cast<Exp_t*>(exp_);
@@ -3144,8 +3312,36 @@ private:
3144 out.push_back(join(temp)); 3312 out.push_back(join(temp));
3145 } 3313 }
3146 3314
3147 void transformLocal(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} 3315 void transformLocal(Local_t* local, str_list& out) {
3148 void transformBreakLoop(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} 3316 if (!local->forceDecls.empty() || !local->decls.empty()) {
3317 str_list defs;
3318 for (const auto& decl : local->forceDecls) {
3319 forceAddToScope(decl);
3320 defs.push_back(decl);
3321 }
3322 for (const auto& decl : local->decls) {
3323 if (addToScope(decl)) {
3324 defs.push_back(decl);
3325 }
3326 }
3327 auto preDefine = getPredefine(defs);
3328 if (!preDefine.empty()) {
3329 out.push_back(preDefine + nll(local));
3330 }
3331 }
3332 }
3333
3334 void transformBreakLoop(BreakLoop_t* breakLoop, str_list& out) {
3335 auto keyword = toString(breakLoop);
3336 if (keyword == "break"sv) {
3337 out.push_back(indent() + keyword + nll(breakLoop));
3338 return;
3339 }
3340 if (_continueVars.empty()) throw std::logic_error("continue must be inside of a loop");
3341 _buf << indent() << _continueVars.top() << " = true"sv << nll(breakLoop);
3342 _buf << indent() << "break"sv << nll(breakLoop);
3343 out.push_back(clearBuf());
3344 }
3149}; 3345};
3150 3346
3151const std::string MoonCompliler::Empty; 3347const std::string MoonCompliler::Empty;
@@ -3153,22 +3349,139 @@ const std::string MoonCompliler::Empty;
3153int main() 3349int main()
3154{ 3350{
3155 std::string s = R"TestCodesHere( 3351 std::string s = R"TestCodesHere(
3156switch abc.x 3352
3157 when "a","b" then 1 3353for x=1,10
3158 when "c" then 2 3354 print "yeah"
3159 else 3 3355
3160a = switch abc.x 3356for x=1,#something
3161 when "a","b" then 1 3357 print "yeah"
3162 when "c" then 2 3358
3163 else 3 3359for y=100,60,-3
3164f switch abc.x 3360 print "count down", y
3165 when "a","b" then 1 3361
3166 when "c" then 2 3362for a=1,10 do print "okay"
3167 else 3 3363
3168switch abc.x 3364for a=1,10
3169 when "a","b" then 1 3365 for b = 2,43
3170 when "c" then 2 3366 print a,b
3171 else 3 3367
3368for i in iter
3369 for j in yeah
3370 x = 343 + i + j
3371 print i, j
3372
3373for x in *something
3374 print x
3375
3376for k,v in pairs hello do print k,v
3377
3378for x in y, z
3379 print x
3380
3381for x in y, z, k
3382 print x
3383
3384
3385x = ->
3386 for x in y
3387 y
3388
3389hello = {1,2,3,4,5}
3390
3391x = for y in *hello
3392 if y % 2 == 0
3393 y
3394
3395x = ->
3396 for x in *hello
3397 y
3398
3399t = for i=10,20 do i * 2
3400
3401hmm = 0
3402y = for j = 3,30, 8
3403 hmm += 1
3404 j * hmm
3405
3406->
3407 for k=10,40
3408 "okay"
3409
3410->
3411 return for k=10,40
3412 "okay"
3413
3414while true do print "name"
3415
3416while 5 + 5
3417 print "okay world"
3418 working man
3419
3420while also do
3421 i work too
3422 "okay"
3423
3424i = 0
3425x = while i < 10
3426 i += 1
3427
3428-- values that can'e be coerced
3429
3430x = for thing in *3
3431 y = "hello"
3432
3433x = for x=1,2
3434 y = "hello"
3435
3436
3437-- continue
3438
3439while true
3440 continue if false
3441 print "yes"
3442 break if true
3443 print "no"
3444
3445
3446for x=1,10
3447 continue if x > 3 and x < 7
3448 print x
3449
3450
3451list = for x=1,10
3452 continue if x > 3 and x < 7
3453 x
3454
3455
3456for a in *{1,2,3,4,5,6}
3457 continue if a == 1
3458 continue if a == 3
3459 print a
3460
3461
3462
3463for x=1,10
3464 continue if x % 2 == 0
3465 for y = 2,12
3466 continue if y % 3 == 0
3467
3468
3469while true
3470 continue if false
3471 break
3472
3473while true
3474 continue if false
3475 return 22
3476
3477--
3478
3479do
3480 xxx = {1,2,3,4}
3481 for thing in *xxx
3482 print thing
3483
3484
3172)TestCodesHere"; 3485)TestCodesHere";
3173 MoonCompliler{}.complile(s); 3486 MoonCompliler{}.complile(s);
3174 return 0; 3487 return 0;
diff --git a/MoonParser/moon_ast.h b/MoonParser/moon_ast.h
index adaf29f..1e5d733 100644
--- a/MoonParser/moon_ast.h
+++ b/MoonParser/moon_ast.h
@@ -93,6 +93,8 @@ AST_END(NameList)
93 93
94AST_NODE(Local, "Local"_id) 94AST_NODE(Local, "Local"_id)
95 ast_ptr<ast_node> name; // local_flag_t | NameList_t 95 ast_ptr<ast_node> name; // local_flag_t | NameList_t
96 std::list<std::string> forceDecls;
97 std::list<std::string> decls;
96AST_END(Local) 98AST_END(Local)
97 99
98AST_NODE(colon_import_name, "colon_import_name"_id) 100AST_NODE(colon_import_name, "colon_import_name"_id)
@@ -370,13 +372,9 @@ AST_NODE(class_member_list, "class_member_list"_id)
370 ast_sel_list<variable_pair_t, normal_pair_t> values; 372 ast_sel_list<variable_pair_t, normal_pair_t> values;
371AST_END(class_member_list) 373AST_END(class_member_list)
372 374
373AST_NODE(ClassLine, "ClassLine"_id)
374 ast_ptr<ast_node> content; // class_member_list_t | Statement_t
375AST_END(ClassLine)
376
377AST_NODE(ClassBlock, "ClassBlock"_id) 375AST_NODE(ClassBlock, "ClassBlock"_id)
378 ast_ptr<Seperator_t> sep; 376 ast_ptr<Seperator_t> sep;
379 ast_list<ClassLine_t> lines; 377 ast_sel_list<class_member_list_t, Statement_t> contents;
380AST_END(ClassBlock) 378AST_END(ClassBlock)
381 379
382AST_NODE(ClassDecl, "ClassDecl"_id) 380AST_NODE(ClassDecl, "ClassDecl"_id)
@@ -471,7 +469,7 @@ AST_END(BreakLoop)
471AST_NODE(Statement, "Statement"_id) 469AST_NODE(Statement, "Statement"_id)
472 ast_ptr<ast_node> content; /* 470 ast_ptr<ast_node> content; /*
473 Import_t | While_t | With_t | For_t | ForEach_t | 471 Import_t | While_t | With_t | For_t | ForEach_t |
474 Switch_t | Return_t | Local_t | Export_t | BreakLoop_t | 472 Return_t | Local_t | Export_t | BreakLoop_t |
475 Assignment_t | ExpList_t 473 Assignment_t | ExpList_t
476 */ 474 */
477 ast_ptr<statement_appendix_t, true> appendix; 475 ast_ptr<statement_appendix_t, true> appendix;
@@ -483,13 +481,9 @@ AST_NODE(Body, "Body"_id)
483 ast_ptr<ast_node> content; // Block | Statement 481 ast_ptr<ast_node> content; // Block | Statement
484AST_END(Body) 482AST_END(Body)
485 483
486AST_NODE(Line, "Line"_id)
487 ast_ptr<Statement_t, true> statment;
488AST_END(Line)
489
490AST_NODE(Block, "Block"_id) 484AST_NODE(Block, "Block"_id)
491 ast_ptr<Seperator_t> sep; 485 ast_ptr<Seperator_t> sep;
492 ast_list<Line_t> lines; 486 ast_list<Statement_t> statements;
493AST_END(Block) 487AST_END(Block)
494 488
495AST_NODE(BlockEnd, "BlockEnd"_id) 489AST_NODE(BlockEnd, "BlockEnd"_id)
diff --git a/MoonParser/moon_parser.cpp b/MoonParser/moon_parser.cpp
index c33469b..481c885 100644
--- a/MoonParser/moon_parser.cpp
+++ b/MoonParser/moon_parser.cpp
@@ -135,7 +135,7 @@ rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak |
135extern rule Exp; 135extern rule Exp;
136 136
137rule Import = key("import") >> ImportNameList >> *SpaceBreak >> key("from") >> Exp; 137rule Import = key("import") >> ImportNameList >> *SpaceBreak >> key("from") >> Exp;
138rule BreakLoop = key("break") | key("continue"); 138rule BreakLoop = (expr("break") | expr("continue")) >> not_(AlphaNum);
139 139
140extern rule ExpListLow, ExpList, Assign; 140extern rule ExpListLow, ExpList, Assign;
141 141
@@ -481,7 +481,7 @@ rule statement_appendix = (if_else_line | unless_line | CompInner) >> Space;
481rule Statement = 481rule Statement =
482( 482(
483 Import | While | With | For | ForEach | 483 Import | While | With | For | ForEach |
484 Return | Local | Export | BreakLoop | 484 Return | Local | Export | Space >> BreakLoop |
485 Assignment | ExpList 485 Assignment | ExpList
486) >> Space >> 486) >> Space >>
487-statement_appendix; 487-statement_appendix;