diff options
Diffstat (limited to 'MoonParser/moon_ast.cpp')
-rw-r--r-- | MoonParser/moon_ast.cpp | 129 |
1 files changed, 84 insertions, 45 deletions
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp index fcbaa90..fa95097 100644 --- a/MoonParser/moon_ast.cpp +++ b/MoonParser/moon_ast.cpp | |||
@@ -11,10 +11,8 @@ | |||
11 | using namespace std::string_view_literals; | 11 | using namespace std::string_view_literals; |
12 | #include "moon_ast.h" | 12 | #include "moon_ast.h" |
13 | 13 | ||
14 | const input& AstLeaf::getValue() | 14 | const input& AstLeaf::getValue() { |
15 | { | 15 | if (_value.empty()) { |
16 | if (_value.empty()) | ||
17 | { | ||
18 | _value.assign(m_begin.m_it, m_end.m_it); | 16 | _value.assign(m_begin.m_it, m_end.m_it); |
19 | return trim(_value); | 17 | return trim(_value); |
20 | } | 18 | } |
@@ -289,13 +287,11 @@ private: | |||
289 | } | 287 | } |
290 | 288 | ||
291 | std::string toString(ast_node* node) { | 289 | std::string toString(ast_node* node) { |
292 | auto str = _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); | 290 | return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); |
293 | return trim(str); | ||
294 | } | 291 | } |
295 | 292 | ||
296 | std::string toString(input::iterator begin, input::iterator end) { | 293 | std::string toString(input::iterator begin, input::iterator end) { |
297 | auto str = _converter.to_bytes(std::wstring(begin, end)); | 294 | return _converter.to_bytes(std::wstring(begin, end)); |
298 | return trim(str); | ||
299 | } | 295 | } |
300 | 296 | ||
301 | void noop(ast_node* node, std::vector<std::string>& out) { | 297 | void noop(ast_node* node, std::vector<std::string>& out) { |
@@ -396,25 +392,25 @@ private: | |||
396 | default: break; | 392 | default: break; |
397 | } | 393 | } |
398 | } | 394 | } |
399 | auto node = statement->content.get(); | 395 | auto content = statement->content.get(); |
400 | if (!node) { | 396 | if (!content) { |
401 | out.push_back(Empty); | 397 | out.push_back(Empty); |
402 | return; | 398 | return; |
403 | } | 399 | } |
404 | switch (node->getId()) { | 400 | switch (content->getId()) { |
405 | case "Import"_id: transformImport(node, out); break; | 401 | case "Import"_id: transformImport(content, out); break; |
406 | case "While"_id: transformWhile(node, out); break; | 402 | case "While"_id: transformWhile(content, out); break; |
407 | case "With"_id: transformWith(node, out); break; | 403 | case "With"_id: transformWith(content, out); break; |
408 | case "For"_id: transformFor(static_cast<For_t*>(node), out); break; | 404 | case "For"_id: transformFor(static_cast<For_t*>(content), out); break; |
409 | case "ForEach"_id: transformForEach(static_cast<ForEach_t*>(node), out); break; | 405 | case "ForEach"_id: transformForEach(static_cast<ForEach_t*>(content), out); break; |
410 | case "Switch"_id: transformSwitch(node, out); break; | 406 | case "Switch"_id: transformSwitch(content, out); break; |
411 | case "Return"_id: transformReturn(static_cast<Return_t*>(node), out); break; | 407 | case "Return"_id: transformReturn(static_cast<Return_t*>(content), out); break; |
412 | case "Local"_id: transformLocal(node, out); break; | 408 | case "Local"_id: transformLocal(content, out); break; |
413 | case "Export"_id: transformExport(node, out); break; | 409 | case "Export"_id: transformExport(content, out); break; |
414 | case "BreakLoop"_id: transformBreakLoop(node, out); break; | 410 | case "BreakLoop"_id: transformBreakLoop(content, out); break; |
415 | case "Assignment"_id: transformStatementAssign(statement, out); break; | 411 | case "Assignment"_id: transformStatementAssign(statement, out); break; |
416 | case "ExpList"_id: { | 412 | case "ExpList"_id: { |
417 | auto expList = static_cast<ExpList_t*>(node); | 413 | auto expList = static_cast<ExpList_t*>(content); |
418 | if (expList->exprs.objects().empty()) { | 414 | if (expList->exprs.objects().empty()) { |
419 | out.push_back(Empty); | 415 | out.push_back(Empty); |
420 | break; | 416 | break; |
@@ -740,23 +736,23 @@ private: | |||
740 | } | 736 | } |
741 | 737 | ||
742 | void transformSimpleValue(SimpleValue_t* simpleValue, std::vector<std::string>& out) { | 738 | void transformSimpleValue(SimpleValue_t* simpleValue, std::vector<std::string>& out) { |
743 | auto node = simpleValue->value.get(); | 739 | auto value = simpleValue->value.get(); |
744 | switch (node->getId()) { | 740 | switch (value->getId()) { |
745 | case "const_value"_id: transform_const_value(node, out); break; | 741 | case "const_value"_id: transform_const_value(value, out); break; |
746 | case "If"_id: transformIfClosure(static_cast<If_t*>(node), out); break; | 742 | case "If"_id: transformIfClosure(static_cast<If_t*>(value), out); break; |
747 | case "Switch"_id: transformSwitch(node, out); break; | 743 | case "Switch"_id: transformSwitch(value, out); break; |
748 | case "With"_id: transformWith(node, out); break; | 744 | case "With"_id: transformWith(value, out); break; |
749 | case "ClassDecl"_id: transformClassDecl(node, out); break; | 745 | case "ClassDecl"_id: transformClassDecl(static_cast<ClassDecl_t*>(value), out); break; |
750 | case "ForEach"_id: transformForEachClosure(static_cast<ForEach_t*>(node), out); break; | 746 | case "ForEach"_id: transformForEachClosure(static_cast<ForEach_t*>(value), out); break; |
751 | case "For"_id: transformForClosure(static_cast<For_t*>(node), out); break; | 747 | case "For"_id: transformForClosure(static_cast<For_t*>(value), out); break; |
752 | case "While"_id: transformWhile(node, out); break; | 748 | case "While"_id: transformWhile(value, out); break; |
753 | case "Do"_id: transformDo(node, out); break; | 749 | case "Do"_id: transformDo(value, out); break; |
754 | case "unary_exp"_id: transform_unary_exp(static_cast<unary_exp_t*>(node), out); break; | 750 | case "unary_exp"_id: transform_unary_exp(static_cast<unary_exp_t*>(value), out); break; |
755 | case "TblComprehension"_id: transformTblComprehension(node, out); break; | 751 | case "TblComprehension"_id: transformTblComprehension(value, out); break; |
756 | case "TableLit"_id: transformTableLit(static_cast<TableLit_t*>(node), out); break; | 752 | case "TableLit"_id: transformTableLit(static_cast<TableLit_t*>(value), out); break; |
757 | case "Comprehension"_id: transformComprehension(static_cast<Comprehension_t*>(node), out); break; | 753 | case "Comprehension"_id: transformComprehension(static_cast<Comprehension_t*>(value), out); break; |
758 | case "FunLit"_id: transformFunLit(static_cast<FunLit_t*>(node), out); break; | 754 | case "FunLit"_id: transformFunLit(static_cast<FunLit_t*>(value), out); break; |
759 | case "Num"_id: transformNum(static_cast<Num_t*>(node), out); break; | 755 | case "Num"_id: transformNum(static_cast<Num_t*>(value), out); break; |
760 | default: break; | 756 | default: break; |
761 | } | 757 | } |
762 | } | 758 | } |
@@ -1020,6 +1016,7 @@ private: | |||
1020 | case "LuaString"_id: transformLuaString(static_cast<LuaString_t*>(argument), out); break; | 1016 | case "LuaString"_id: transformLuaString(static_cast<LuaString_t*>(argument), out); break; |
1021 | default: break; | 1017 | default: break; |
1022 | } | 1018 | } |
1019 | out.back() = s("("sv) + out.back() + s(")"sv); | ||
1023 | } | 1020 | } |
1024 | 1021 | ||
1025 | void transformFnArgs(FnArgs_t* fnArgs, std::vector<std::string>& out) { | 1022 | void transformFnArgs(FnArgs_t* fnArgs, std::vector<std::string>& out) { |
@@ -1028,7 +1025,7 @@ private: | |||
1028 | transformExp(static_cast<Exp_t*>(node), temp); | 1025 | transformExp(static_cast<Exp_t*>(node), temp); |
1029 | } | 1026 | } |
1030 | std::string args = join(temp, ", "sv); | 1027 | std::string args = join(temp, ", "sv); |
1031 | out.push_back(args.empty() ? s("()"sv) : s("("sv) + args + s(")"sv)); | 1028 | out.push_back(args); |
1032 | } | 1029 | } |
1033 | 1030 | ||
1034 | void transformColonChain(ColonChain_t* colonChain, std::vector<std::string>& out) { | 1031 | void transformColonChain(ColonChain_t* colonChain, std::vector<std::string>& out) { |
@@ -1211,13 +1208,14 @@ private: | |||
1211 | 1208 | ||
1212 | void transformAssignableNameList(AssignableNameList_t* nameList, std::vector<std::string>& out) { | 1209 | void transformAssignableNameList(AssignableNameList_t* nameList, std::vector<std::string>& out) { |
1213 | std::vector<std::string> temp; | 1210 | std::vector<std::string> temp; |
1214 | for (auto node : nameList->items.objects()) { | 1211 | for (auto _item : nameList->items.objects()) { |
1215 | switch (node->getId()) { | 1212 | auto item = static_cast<NameOrDestructure_t*>(_item)->item.get(); |
1213 | switch (item->getId()) { | ||
1216 | case "Name"_id: | 1214 | case "Name"_id: |
1217 | transformName(static_cast<Name_t*>(node), temp); | 1215 | transformName(static_cast<Name_t*>(item), temp); |
1218 | break; | 1216 | break; |
1219 | case "TableLit"_id: | 1217 | case "TableLit"_id: |
1220 | transformTableLit(static_cast<TableLit_t*>(node), temp); | 1218 | transformTableLit(static_cast<TableLit_t*>(item), temp); |
1221 | break; | 1219 | break; |
1222 | default: break; | 1220 | default: break; |
1223 | } | 1221 | } |
@@ -1506,6 +1504,48 @@ private: | |||
1506 | } | 1504 | } |
1507 | } | 1505 | } |
1508 | 1506 | ||
1507 | void transformClassDecl(ClassDecl_t* classDecl, std::vector<std::string>& out) { | ||
1508 | std::vector<std::string> temp; | ||
1509 | if (classDecl->name) { | ||
1510 | transformAssignable(classDecl->name, temp); | ||
1511 | } | ||
1512 | if (classDecl->extend) { | ||
1513 | transformExp(classDecl->extend, temp); | ||
1514 | } | ||
1515 | if (classDecl->body) { | ||
1516 | transformClassBlock(classDecl->body, temp); | ||
1517 | } | ||
1518 | out.push_back(join(temp, "\n"sv)); | ||
1519 | } | ||
1520 | |||
1521 | void transformClassBlock(ClassBlock_t* classBlock, std::vector<std::string>& out) { | ||
1522 | std::vector<std::string> temp; | ||
1523 | for (auto _line : classBlock->lines.objects()) { | ||
1524 | auto line = static_cast<ClassLine_t*>(_line); | ||
1525 | transformClassLine(line, temp); | ||
1526 | } | ||
1527 | out.push_back(join(temp,"\n"sv)); | ||
1528 | } | ||
1529 | |||
1530 | void transformClassLine(ClassLine_t* classLine, std::vector<std::string>& out) { | ||
1531 | auto content = classLine->content.get(); | ||
1532 | switch (content->getId()) { | ||
1533 | case "class_member_list"_id: | ||
1534 | transform_class_member_list(static_cast<class_member_list_t*>(content), out); | ||
1535 | break; | ||
1536 | case "Statement"_id: | ||
1537 | transformStatement(static_cast<Statement_t*>(content), out); | ||
1538 | break; | ||
1539 | case "Exp"_id: | ||
1540 | transformExp(static_cast<Exp_t*>(content), out); | ||
1541 | break; | ||
1542 | } | ||
1543 | } | ||
1544 | |||
1545 | void transform_class_member_list(class_member_list_t* class_member_list, std::vector<std::string>& out) {noop(class_member_list, out);} | ||
1546 | |||
1547 | void transformAssignable(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | ||
1548 | |||
1509 | void transformUpdate(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1549 | void transformUpdate(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1510 | void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 1550 | void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
1511 | void transformWhile(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 1551 | void transformWhile(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
@@ -1518,7 +1558,6 @@ private: | |||
1518 | void transform_unless_line(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1558 | void transform_unless_line(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1519 | void transform_simple_table(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1559 | void transform_simple_table(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1520 | void transform_const_value(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1560 | void transform_const_value(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1521 | void transformClassDecl(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | ||
1522 | void transformDo(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1561 | void transformDo(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1523 | void transformTblComprehension(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1562 | void transformTblComprehension(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |
1524 | void transform_chain_dot_chain(ast_node* node, std::vector<std::string>& out) {noop(node, out);} | 1563 | void transform_chain_dot_chain(ast_node* node, std::vector<std::string>& out) {noop(node, out);} |