diff options
| author | Li Jin <dragon-fly@qq.com> | 2019-09-09 09:02:13 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2019-09-09 09:02:13 +0800 |
| commit | 4e6f4e8124316866a08f9ddf3322fde87abc3c21 (patch) | |
| tree | a127f02552b1a6b67f016e9676d9450d1a7ab926 | |
| parent | fabc309a0a0a6f210a8a7ed8f0d1f8498971f409 (diff) | |
| download | yuescript-4e6f4e8124316866a08f9ddf3322fde87abc3c21.tar.gz yuescript-4e6f4e8124316866a08f9ddf3322fde87abc3c21.tar.bz2 yuescript-4e6f4e8124316866a08f9ddf3322fde87abc3c21.zip | |
updating assign
| -rw-r--r-- | MoonParser/moon_ast.cpp | 93 |
1 files changed, 51 insertions, 42 deletions
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp index 4fb0212..a98e75e 100644 --- a/MoonParser/moon_ast.cpp +++ b/MoonParser/moon_ast.cpp | |||
| @@ -160,7 +160,7 @@ public: | |||
| 160 | switch (node->getId()) { | 160 | switch (node->getId()) { |
| 161 | case "Block"_id: | 161 | case "Block"_id: |
| 162 | pushScope(); | 162 | pushScope(); |
| 163 | transformBlock(node, out); | 163 | transformBody(node, out); |
| 164 | popScope(); | 164 | popScope(); |
| 165 | break; | 165 | break; |
| 166 | default: break; | 166 | default: break; |
| @@ -301,26 +301,6 @@ private: | |||
| 301 | // out.push_back(trim(str) + nll(node)); | 301 | // out.push_back(trim(str) + nll(node)); |
| 302 | } | 302 | } |
| 303 | 303 | ||
| 304 | void transformBlock(ast_node* block, std::vector<std::string>& out) { | ||
| 305 | std::vector<std::string> temp; | ||
| 306 | block->eachChild([&](ast_node* node) { | ||
| 307 | switch (node->getId()) { | ||
| 308 | case "Line"_id: transformLine(node, temp); break; | ||
| 309 | default: break; | ||
| 310 | } | ||
| 311 | }); | ||
| 312 | out.push_back(join(temp)); | ||
| 313 | } | ||
| 314 | |||
| 315 | void transformLine(ast_node* line, std::vector<std::string>& out) { | ||
| 316 | line->eachChild([&](ast_node* node) { | ||
| 317 | switch (node->getId()) { | ||
| 318 | case "Statement"_id: transformStatement(node, out); break; | ||
| 319 | default: break; | ||
| 320 | } | ||
| 321 | }); | ||
| 322 | } | ||
| 323 | |||
| 324 | void transformStatement(ast_node* statement, std::vector<std::string>& out) { | 304 | void transformStatement(ast_node* statement, std::vector<std::string>& out) { |
| 325 | std::vector<std::string> temp; | 305 | std::vector<std::string> temp; |
| 326 | auto transformContent = [&](ast_node* node, std::vector<std::string>& out) { | 306 | auto transformContent = [&](ast_node* node, std::vector<std::string>& out) { |
| @@ -419,12 +399,51 @@ private: | |||
| 419 | break; | 399 | break; |
| 420 | } | 400 | } |
| 421 | case "Update"_id: transformUpdate(node, temp); break; | 401 | case "Update"_id: transformUpdate(node, temp); break; |
| 422 | case "Assign"_id: transformAssign(node, temp); break; | 402 | case "Assign"_id: { |
| 403 | auto child = node->getChild(0); | ||
| 404 | switch (child->getId()) { | ||
| 405 | case "With"_id: transformWith(child, temp); break; | ||
| 406 | case "If"_id: | ||
| 407 | transformIfClosure(child, temp); | ||
| 408 | break; | ||
| 409 | case "Switch"_id: transformSwitch(child, temp); break; | ||
| 410 | case "TableBlock"_id: transformTableBlock(child, temp); break; | ||
| 411 | case "ExpListLow"_id: | ||
| 412 | transformExpListLow(child, temp); | ||
| 413 | break; | ||
| 414 | default: break; | ||
| 415 | } | ||
| 416 | break; | ||
| 417 | } | ||
| 423 | default: break; | 418 | default: break; |
| 424 | } | 419 | } |
| 425 | }); | 420 | }); |
| 426 | out.push_back(preDefined + indent() + temp[0] + s(" = "sv) + temp[1] + nll(assignment)); | 421 | out.push_back(preDefined + indent() + temp[0] + s(" = "sv) + temp[1] + nll(assignment)); |
| 427 | } | 422 | } |
| 423 | void transformIfClosure(ast_node* ifNode, std::vector<std::string>& out) { | ||
| 424 | std::vector<std::string> temp; | ||
| 425 | temp.push_back(s("(function()"sv) + nll(ifNode)); | ||
| 426 | pushScope(); | ||
| 427 | ifNode->traverse([&](ast_node* node) { | ||
| 428 | switch (node->getId()) { | ||
| 429 | case "IfCond"_id: { | ||
| 430 | std::vector<std::string> tmp; | ||
| 431 | auto exp = node->getChild(0); | ||
| 432 | transformExp(exp, tmp); | ||
| 433 | _buf << indent() << "if "sv << tmp.front() << " then"sv << nll(exp); | ||
| 434 | temp.push_back(clearBuf()); | ||
| 435 | return traversal::Return; | ||
| 436 | } | ||
| 437 | case "Body"_id: | ||
| 438 | transformBody(node, temp); | ||
| 439 | return traversal::Return; | ||
| 440 | default: return traversal::Continue; | ||
| 441 | } | ||
| 442 | }); | ||
| 443 | popScope(); | ||
| 444 | temp.push_back(indent() + s("end)()"sv)); | ||
| 445 | out.push_back(join(temp)); | ||
| 446 | } | ||
| 428 | 447 | ||
| 429 | void transformExpList(ast_node* expList, std::vector<std::string>& out) { | 448 | void transformExpList(ast_node* expList, std::vector<std::string>& out) { |
| 430 | std::vector<std::string> temp; | 449 | std::vector<std::string> temp; |
| @@ -448,19 +467,6 @@ private: | |||
| 448 | out.push_back(join(temp, ", "sv)); | 467 | out.push_back(join(temp, ", "sv)); |
| 449 | } | 468 | } |
| 450 | 469 | ||
| 451 | void transformAssign(ast_node* assign, std::vector<std::string>& out) { | ||
| 452 | assign->eachChild([&](ast_node* node) { | ||
| 453 | switch (node->getId()) { | ||
| 454 | case "With"_id: transformWith(node, out); break; | ||
| 455 | case "If"_id: transformIf(node, out); break; | ||
| 456 | case "Switch"_id: transformSwitch(node, out); break; | ||
| 457 | case "TableBlock"_id: transformTableBlock(node, out); break; | ||
| 458 | case "ExpListLow"_id: transformExpListLow(node, out); break; | ||
| 459 | default: break; | ||
| 460 | } | ||
| 461 | }); | ||
| 462 | } | ||
| 463 | |||
| 464 | void transformExp(ast_node* exp, std::vector<std::string>& out) { | 470 | void transformExp(ast_node* exp, std::vector<std::string>& out) { |
| 465 | std::vector<std::string> temp; | 471 | std::vector<std::string> temp; |
| 466 | exp->eachChild([&](ast_node* node) { | 472 | exp->eachChild([&](ast_node* node) { |
| @@ -603,13 +609,16 @@ private: | |||
| 603 | } | 609 | } |
| 604 | 610 | ||
| 605 | void transformBody(ast_node* body, std::vector<std::string>& out) { | 611 | void transformBody(ast_node* body, std::vector<std::string>& out) { |
| 606 | body->eachChild([&](ast_node* node) { | 612 | std::vector<std::string> temp; |
| 613 | body->traverse([&](ast_node* node) { | ||
| 607 | switch (node->getId()) { | 614 | switch (node->getId()) { |
| 608 | case "Block"_id: transformBlock(node, out); break; | 615 | case "Statement"_id: |
| 609 | case "Statement"_id: transformStatement(node, out); break; | 616 | transformStatement(node, temp); |
| 610 | default: break; | 617 | return traversal::Return; |
| 618 | default: return traversal::Continue; | ||
| 611 | } | 619 | } |
| 612 | }); | 620 | }); |
| 621 | out.push_back(join(temp)); | ||
| 613 | } | 622 | } |
| 614 | 623 | ||
| 615 | void transformFnArgsDef(ast_node* argsDef, std::vector<std::string>& out) { | 624 | void transformFnArgsDef(ast_node* argsDef, std::vector<std::string>& out) { |
| @@ -798,8 +807,8 @@ private: | |||
| 798 | default: break; | 807 | default: break; |
| 799 | } | 808 | } |
| 800 | }); | 809 | }); |
| 801 | std::string args = join(temp, ", "); | 810 | std::string args = join(temp, ", "sv); |
| 802 | out.push_back(args.empty() ? "()" : s("(") + args + ")"); | 811 | out.push_back(args.empty() ? s("()"sv) : s("("sv) + args + s(")"sv)); |
| 803 | } | 812 | } |
| 804 | 813 | ||
| 805 | void transformColonChain(ast_node* colonChain, std::vector<std::string>& out) { | 814 | void transformColonChain(ast_node* colonChain, std::vector<std::string>& out) { |
| @@ -833,8 +842,8 @@ private: | |||
| 833 | void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 842 | void transformImport(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 834 | void transformWhile(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 843 | void transformWhile(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 835 | void transformWith(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 844 | void transformWith(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 836 | void transformIf(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | ||
| 837 | void transformFor(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 845 | void transformFor(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 846 | void transformIf(ast_node* node, std::vector<std::string>& out) { noopnl(node, out); } | ||
| 838 | void transformForEach(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 847 | void transformForEach(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 839 | void transformSwitch(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 848 | void transformSwitch(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
| 840 | void transformReturn(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} | 849 | void transformReturn(ast_node* node, std::vector<std::string>& out) {noopnl(node, out);} |
