Commit 06eec139 authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[parser] Allow nary comma expressions

Following up on adding n-ary nodes, this extends the parser to support
n-ary comma operations, including support for n-ary arrow function
parameters.

Bug: v8:6964
Bug: chromium:777302
Change-Id: Iba9c93b9eaa5a0870815b4fa29e84aa9d0c511e2
Reviewed-on: https://chromium-review.googlesource.com/735156
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarRoss McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48927}
parent 52ab610b
......@@ -3985,8 +3985,13 @@ void BytecodeGenerator::VisitCommaExpression(BinaryOperation* binop) {
}
void BytecodeGenerator::VisitNaryCommaExpression(NaryOperation* expr) {
// TODO(leszeks): Implement
UNREACHABLE();
DCHECK_GT(expr->subsequent_length(), 0);
VisitForEffect(expr->first());
for (size_t i = 0; i < expr->subsequent_length() - 1; ++i) {
VisitForEffect(expr->subsequent(i));
}
Visit(expr->subsequent(expr->subsequent_length() - 1));
}
void BytecodeGenerator::BuildLogicalTest(Token::Value token, Expression* left,
......
......@@ -1975,6 +1975,9 @@ ParserBase<Impl>::ParseExpressionCoverGrammar(bool accept_IN, bool* ok) {
if (impl()->IsNull(result)) {
// First time through the loop.
result = right;
} else if (impl()->CollapseNaryExpression(&result, right, Token::COMMA,
comma_pos)) {
// Do nothing, "result" is already updated.
} else {
result =
factory()->NewBinaryOperation(Token::COMMA, result, right, comma_pos);
......
......@@ -310,9 +310,9 @@ bool Parser::ShortcutNumericLiteralBinaryExpression(Expression** x,
bool Parser::CollapseNaryExpression(Expression** x, Expression* y,
Token::Value op, int pos) {
// Filter out unsupported ops.
// TODO(leszeks): Support AND, OR and COMMA in bytecode generator.
// TODO(leszeks): Support AND and OR in bytecode generator.
if (!Token::IsBinaryOp(op) || op == Token::AND || op == Token::OR ||
op == Token::EXP || op == Token::COMMA)
op == Token::EXP)
return false;
// Convert *x into an nary operation with the given op, returning false if
......@@ -2397,6 +2397,7 @@ void Parser::AddArrowFunctionFormalParameters(
ParserFormalParameters* parameters, Expression* expr, int end_pos,
bool* ok) {
// ArrowFunctionFormals ::
// Nary(Token::COMMA, VariableProxy*, Tail)
// Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
// Tail
// NonTailArrowFunctionFormals ::
......@@ -2406,9 +2407,30 @@ void Parser::AddArrowFunctionFormalParameters(
// VariableProxy
// Spread(VariableProxy)
//
// As we need to visit the parameters in left-to-right order, we recurse on
// the left-hand side of comma expressions.
// We need to visit the parameters in left-to-right order
//
// For the Nary case, we simply visit the parameters in a loop.
if (expr->IsNaryOperation()) {
NaryOperation* nary = expr->AsNaryOperation();
// The classifier has already run, so we know that the expression is a valid
// arrow function formals production.
DCHECK_EQ(nary->op(), Token::COMMA);
// Each op position is the end position of the *previous* expr, with the
// second (i.e. first "subsequent") op position being the end position of
// the first child expression.
Expression* next = nary->first();
for (size_t i = 0; i < nary->subsequent_length(); ++i) {
AddArrowFunctionFormalParameters(
parameters, next, nary->subsequent_op_position(i), CHECK_OK_VOID);
next = nary->subsequent(i);
}
AddArrowFunctionFormalParameters(parameters, next, end_pos, CHECK_OK_VOID);
return;
}
// For the binary case, we recurse on the left-hand side of binary comma
// expressions.
if (expr->IsBinaryOperation()) {
BinaryOperation* binop = expr->AsBinaryOperation();
// The classifier has already run, so we know that the expression is a valid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment