FunctionCallCanonicalizationPass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Compiler/FunctionCallCanonicalizationPass.h"
  7. #include "AST/AST.h"
  8. namespace JSSpecCompiler {
  9. RecursionDecision FunctionCallCanonicalizationPass::on_entry(Tree tree)
  10. {
  11. if (auto binary_operation = as<BinaryOperation>(tree); binary_operation) {
  12. if (binary_operation->m_operation == BinaryOperator::FunctionCall) {
  13. Vector<Tree> arguments;
  14. auto current_tree = binary_operation->m_right;
  15. while (true) {
  16. auto argument_tree = as<BinaryOperation>(current_tree);
  17. if (!argument_tree || argument_tree->m_operation != BinaryOperator::Comma)
  18. break;
  19. arguments.append(argument_tree->m_left);
  20. current_tree = argument_tree->m_right;
  21. }
  22. arguments.append(current_tree);
  23. replace_current_node_with(make_ref_counted<FunctionCall>(binary_operation->m_left, move(arguments)));
  24. }
  25. }
  26. return RecursionDecision::Recurse;
  27. }
  28. }