Commit 5bbe7992 authored by dslomov's avatar dslomov Committed by Commit bot

[destructuring] Implement basic binding destructuring infrastructure

This patch:
  - Refactors Parser::ParseVariableDeclarations
  - Introduces Parser::PatternMatcher class
  - Implements matching a single variable pattern
  - Implements rudimentary matching against object literal pattern
    as a proof of concept

R=arv@chromium.org,rossberg@chromium.org
BUG=v8:811
LOG=N

Review URL: https://codereview.chromium.org/1130623004

Cr-Commit-Position: refs/heads/master@{#28345}
parent 69489079
......@@ -958,6 +958,8 @@ source_set("v8_base") {
"src/optimizing-compile-dispatcher.h",
"src/ostreams.cc",
"src/ostreams.h",
"src/pattern-rewriter.cc",
"src/pattern-rewriter.h",
"src/parser.cc",
"src/parser.h",
"src/pending-compilation-error-handler.cc",
......
This diff is collapsed.
......@@ -869,6 +869,7 @@ class Parser : public ParserBase<ParserTraits> {
private:
friend class ParserTraits;
class PatternRewriter;
// Limit the allowed number of local variables in a function. The hard limit
// is that offsets computed by FullCodeGenerator::StackOperand and similar
......
This diff is collapsed.
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_PATTERN_MATCHER_H_
#define V8_PATTERN_MATCHER_H_
#include "src/ast.h"
#include "src/parser.h"
namespace v8 {
namespace internal {
class Parser::PatternRewriter : private AstVisitor {
public:
struct DeclarationDescriptor {
Parser* parser;
Scope* declaration_scope;
Scope* scope;
int initializer_position;
VariableMode mode;
ZoneList<const AstRawString*>* names;
bool is_const;
Block* block;
bool needs_init;
int pos;
Token::Value init_op;
};
explicit PatternRewriter(const DeclarationDescriptor* decl,
Expression* pattern)
: decl_(decl),
pattern_(pattern),
current_value_(nullptr),
ok_(nullptr),
nvars_(nullptr) {}
PatternRewriter()
: decl_(nullptr),
pattern_(nullptr),
current_value_(nullptr),
ok_(nullptr),
nvars_(nullptr) {}
bool IsSingleVariableBinding() const;
const AstRawString* SingleName() const;
void DeclareAndInitializeVariables(Expression* value, int* nvars, bool* ok);
private:
#define DECLARE_VISIT(type) void Visit##type(v8::internal::type* node) override;
// Visiting functions for AST nodes make this an AstVisitor.
AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT
virtual void Visit(AstNode* node) override;
void RecurseIntoSubpattern(AstNode* pattern, Expression* value) {
Expression* old_value = current_value_;
current_value_ = value;
pattern->Accept(this);
current_value_ = old_value;
}
AstNodeFactory* factory() const { return decl_->parser->factory(); }
AstValueFactory* ast_value_factory() const {
return decl_->parser->ast_value_factory();
}
bool inside_with() const { return decl_->parser->inside_with(); }
Zone* zone() const { return decl_->parser->zone(); }
const DeclarationDescriptor* decl_;
Expression* pattern_;
Expression* current_value_;
bool* ok_;
int* nvars_;
};
}
} // namespace v8::internal
#endif // V8_PATTERN_MATCHER_H_
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --harmony-destructuring
(function TestObjectLiteralPattern() {
var { x : x, y : y } = { x : 1, y : 2 };
assertEquals(1, x);
assertEquals(2, y);
var {z} = { z : 3 };
assertEquals(3, z);
}());
......@@ -798,6 +798,8 @@
'../../src/optimizing-compile-dispatcher.h',
'../../src/ostreams.cc',
'../../src/ostreams.h',
'../../src/pattern-rewriter.cc',
'../../src/pattern-rewriter.h',
'../../src/parser.cc',
'../../src/parser.h',
'../../src/pending-compilation-error-handler.cc',
......
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