2023-08-18 14:12:53 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-10-02 00:25:08 -03:00
|
|
|
#include "Compiler/Passes/FunctionCallCanonicalizationPass.h"
|
2023-08-18 14:12:53 -03:00
|
|
|
#include "AST/AST.h"
|
|
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
2024-01-16 03:07:00 -03:00
|
|
|
void FunctionCallCanonicalizationPass::on_leave(Tree tree)
|
2023-08-18 14:12:53 -03:00
|
|
|
{
|
2024-01-16 03:07:00 -03:00
|
|
|
if (auto binary_operation = as<BinaryOperation>(tree)) {
|
2023-08-18 14:12:53 -03:00
|
|
|
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);
|
|
|
|
|
|
2024-01-16 02:21:59 -03:00
|
|
|
if (arguments[0] == zero_argument_function_call) {
|
|
|
|
|
VERIFY(arguments.size() == 1);
|
|
|
|
|
arguments.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 14:12:53 -03:00
|
|
|
replace_current_node_with(make_ref_counted<FunctionCall>(binary_operation->m_left, move(arguments)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|