1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 02:54:58 +00:00
serenity/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/FunctionCallCanonicalizationPass.cpp
Dan Klishch 72794e7843 JSSpecCompiler: Add function call canonicalization pass
It simplifies ladders of BinaryOperators nodes in the function call
arguments into nice and neat FunctionCall node. Ladders initially appear
since I do not want to complicate expression parser, so it interprets
`f(a, b, c, d)` as `f "function_call_operator" (a, (b, (c, d))))`.
2023-09-09 11:20:43 -06:00

34 lines
1.1 KiB
C++

/*
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Compiler/FunctionCallCanonicalizationPass.h"
#include "AST/AST.h"
namespace JSSpecCompiler {
RecursionDecision FunctionCallCanonicalizationPass::on_entry(Tree tree)
{
if (auto binary_operation = as<BinaryOperation>(tree); binary_operation) {
if (binary_operation->m_operation == BinaryOperator::FunctionCall) {
Vector<Tree> arguments;
auto current_tree = binary_operation->m_right;
while (true) {
auto argument_tree = as<BinaryOperation>(current_tree);
if (!argument_tree || argument_tree->m_operation != BinaryOperator::Comma)
break;
arguments.append(argument_tree->m_left);
current_tree = argument_tree->m_right;
}
arguments.append(current_tree);
replace_current_node_with(make_ref_counted<FunctionCall>(binary_operation->m_left, move(arguments)));
}
}
return RecursionDecision::Recurse;
}
}