1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +00:00

JSSpecCompiler: Make function arguments parsing much simpler

In a nutshell, when we understand that the expression is a function
call (this happens at '(' after an expression), stop parsing expression
and start parsing function arguments. This makes
`FunctionCallCanonicalizationPass` and the workaround for zero argument
function calls obsolete.
This commit is contained in:
Dan Klishch 2024-01-21 13:51:05 -05:00 committed by Andrew Kaster
parent 990e30f458
commit d14bb7e91e
10 changed files with 37 additions and 114 deletions

View file

@ -1,38 +0,0 @@
/*
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Compiler/Passes/FunctionCallCanonicalizationPass.h"
#include "AST/AST.h"
namespace JSSpecCompiler {
void FunctionCallCanonicalizationPass::on_leave(Tree tree)
{
if (auto binary_operation = as<BinaryOperation>(tree)) {
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);
if (arguments[0] == zero_argument_function_call) {
VERIFY(arguments.size() == 1);
arguments.clear();
}
replace_current_node_with(make_ref_counted<FunctionCall>(binary_operation->m_left, move(arguments)));
}
}
}
}

View file

@ -1,28 +0,0 @@
/*
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Compiler/GenericASTPass.h"
namespace JSSpecCompiler {
// FunctionCallCanonicalizationPass simplifies ladders of BinaryOperators nodes in the function call
// arguments into nice and neat FunctionCall nodes.
//
// 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))))`.
class FunctionCallCanonicalizationPass : public GenericASTPass {
public:
inline static constexpr StringView name = "function-call-canonicalization"sv;
using GenericASTPass::GenericASTPass;
protected:
void on_leave(Tree tree) override;
};
}