From 38dfd046332fd0b72930181bc220b3ca8a69ee46 Mon Sep 17 00:00:00 2001 From: Emanuele Torre Date: Wed, 8 Apr 2020 11:59:18 +0200 Subject: [PATCH] LibJS: rename JS::DeclarationType => JS::DeclarationKind Many other parsers call it with this name. Also Type can be confusing in this context since the DeclarationType is not the type (number, string, etc.) of the variables that are being declared by the VariableDeclaration. --- Libraries/LibJS/AST.cpp | 22 +++++++++++----------- Libraries/LibJS/AST.h | 10 +++++----- Libraries/LibJS/Forward.h | 2 +- Libraries/LibJS/Interpreter.cpp | 26 +++++++++++++------------- Libraries/LibJS/Interpreter.h | 4 ++-- Libraries/LibJS/Parser.cpp | 10 +++++----- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 8ff7de1512..a749d772ef 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -202,7 +202,7 @@ Value ForStatement::execute(Interpreter& interpreter) const { RefPtr wrapper; - if (m_init && m_init->is_variable_declaration() && static_cast(m_init.ptr())->declaration_type() != DeclarationType::Var) { + if (m_init && m_init->is_variable_declaration() && static_cast(m_init.ptr())->declaration_kind() != DeclarationKind::Var) { wrapper = create_ast_node(); interpreter.enter_scope(*wrapper, {}, ScopeType::Block); } @@ -799,7 +799,7 @@ void UpdateExpression::dump(int indent) const Value VariableDeclaration::execute(Interpreter& interpreter) const { for (auto& declarator : m_declarations) { - interpreter.declare_variable(declarator.id().string(), m_declaration_type); + interpreter.declare_variable(declarator.id().string(), m_declaration_kind); if (auto* init = declarator.init()) { auto initalizer_result = init->execute(interpreter); if (interpreter.exception()) @@ -818,22 +818,22 @@ Value VariableDeclarator::execute(Interpreter&) const void VariableDeclaration::dump(int indent) const { - const char* declaration_type_string = nullptr; - switch (m_declaration_type) { - case DeclarationType::Let: - declaration_type_string = "Let"; + const char* declaration_kind_string = nullptr; + switch (m_declaration_kind) { + case DeclarationKind::Let: + declaration_kind_string = "Let"; break; - case DeclarationType::Var: - declaration_type_string = "Var"; + case DeclarationKind::Var: + declaration_kind_string = "Var"; break; - case DeclarationType::Const: - declaration_type_string = "Const"; + case DeclarationKind::Const: + declaration_kind_string = "Const"; break; } ASTNode::dump(indent); print_indent(indent + 1); - printf("%s\n", declaration_type_string); + printf("%s\n", declaration_kind_string); for (auto& declarator : m_declarations) declarator.dump(indent + 1); diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 1f58b4c4c0..dc2b398055 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -591,7 +591,7 @@ private: bool m_prefixed; }; -enum class DeclarationType { +enum class DeclarationKind { Var, Let, Const, @@ -620,14 +620,14 @@ private: class VariableDeclaration : public Declaration { public: - VariableDeclaration(DeclarationType declaration_type, NonnullRefPtrVector declarations) - : m_declaration_type(declaration_type) + VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector declarations) + : m_declaration_kind(declaration_kind) , m_declarations(move(declarations)) { } virtual bool is_variable_declaration() const override { return true; } - DeclarationType declaration_type() const { return m_declaration_type; } + DeclarationKind declaration_kind() const { return m_declaration_kind; } virtual Value execute(Interpreter&) const override; virtual void dump(int indent) const override; @@ -635,7 +635,7 @@ public: private: virtual const char* class_name() const override { return "VariableDeclaration"; } - DeclarationType m_declaration_type; + DeclarationKind m_declaration_kind; NonnullRefPtrVector m_declarations; }; diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index eafd2f09d2..c301e3bd0e 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -52,7 +52,7 @@ class ScopeNode; class Shape; class Statement; class Value; -enum class DeclarationType; +enum class DeclarationKind; struct Argument; diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index dc50045f71..1485fcbf65 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -90,11 +90,11 @@ Value Interpreter::run(const Statement& statement, ArgumentVector arguments, Sco void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type) { - HashMap scope_variables_with_declaration_type; + HashMap scope_variables_with_declaration_kind; for (auto& argument : arguments) { - scope_variables_with_declaration_type.set(argument.name, { argument.value, DeclarationType::Var }); + scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var }); } - m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_type) }); + m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_kind) }); } void Interpreter::exit_scope(const ScopeNode& scope_node) @@ -110,29 +110,29 @@ void Interpreter::exit_scope(const ScopeNode& scope_node) m_unwind_until = ScopeType::None; } -void Interpreter::declare_variable(const FlyString& name, DeclarationType declaration_type) +void Interpreter::declare_variable(const FlyString& name, DeclarationKind declaration_kind) { - switch (declaration_type) { - case DeclarationType::Var: + switch (declaration_kind) { + case DeclarationKind::Var: for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { auto& scope = m_scope_stack.at(i); if (scope.type == ScopeType::Function) { - if (scope.variables.get(name).has_value() && scope.variables.get(name).value().declaration_type != DeclarationType::Var) + if (scope.variables.get(name).has_value() && scope.variables.get(name).value().declaration_kind != DeclarationKind::Var) ASSERT_NOT_REACHED(); - scope.variables.set(move(name), { js_undefined(), declaration_type }); + scope.variables.set(move(name), { js_undefined(), declaration_kind }); return; } } global_object().put(move(name), js_undefined()); break; - case DeclarationType::Let: - case DeclarationType::Const: + case DeclarationKind::Let: + case DeclarationKind::Const: if (m_scope_stack.last().variables.get(name).has_value()) ASSERT_NOT_REACHED(); - m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_type }); + m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_kind }); break; } } @@ -144,10 +144,10 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as auto possible_match = scope.variables.get(name); if (possible_match.has_value()) { - if (!first_assignment && possible_match.value().declaration_type == DeclarationType::Const) + if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) ASSERT_NOT_REACHED(); - scope.variables.set(move(name), { move(value), possible_match.value().declaration_type }); + scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind }); return; } } diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 3e54132184..b11f2dc94d 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -48,7 +48,7 @@ enum class ScopeType { struct Variable { Value value; - DeclarationType declaration_type; + DeclarationKind declaration_kind; }; struct ScopeFrame { @@ -95,7 +95,7 @@ public: Optional get_variable(const FlyString& name); void set_variable(const FlyString& name, Value, bool first_assignment = false); - void declare_variable(const FlyString& name, DeclarationType); + void declare_variable(const FlyString& name, DeclarationKind); void gather_roots(Badge, HashTable&); diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 408f573e8d..eb0b679fb3 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -658,19 +658,19 @@ NonnullRefPtr Parser::parse_function_node() NonnullRefPtr Parser::parse_variable_declaration() { - DeclarationType declaration_type; + DeclarationKind declaration_kind; switch (m_parser_state.m_current_token.type()) { case TokenType::Var: - declaration_type = DeclarationType::Var; + declaration_kind = DeclarationKind::Var; consume(TokenType::Var); break; case TokenType::Let: - declaration_type = DeclarationType::Let; + declaration_kind = DeclarationKind::Let; consume(TokenType::Let); break; case TokenType::Const: - declaration_type = DeclarationType::Const; + declaration_kind = DeclarationKind::Const; consume(TokenType::Const); break; default: @@ -692,7 +692,7 @@ NonnullRefPtr Parser::parse_variable_declaration() } break; } - return create_ast_node(declaration_type, move(declarations)); + return create_ast_node(declaration_kind, move(declarations)); } NonnullRefPtr Parser::parse_throw_statement()