1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibJS: Make the AST reference-counted

This allows function objects to outlive the original parsed program
without their ScopeNode disappearing.
This commit is contained in:
Andreas Kling 2020-03-18 11:23:53 +01:00
parent d98fbd192e
commit ddd69e3660
8 changed files with 138 additions and 130 deletions

View file

@ -100,10 +100,10 @@ Value WhileStatement::execute(Interpreter& interpreter) const
Value ForStatement::execute(Interpreter& interpreter) const
{
OwnPtr<BlockStatement> wrapper;
RefPtr<BlockStatement> wrapper;
if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
wrapper = make<BlockStatement>();
wrapper = create_ast_node<BlockStatement>();
interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
}

View file

@ -26,8 +26,8 @@
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <AK/OwnPtr.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
@ -35,7 +35,14 @@
namespace JS {
class ASTNode {
template<class T, class... Args>
static inline NonnullRefPtr<T>
create_ast_node(Args&&... args)
{
return adopt(*new T(forward<Args>(args)...));
}
class ASTNode : public RefCounted<ASTNode> {
public:
virtual ~ASTNode() {}
virtual const char* class_name() const = 0;
@ -62,7 +69,7 @@ public:
class ExpressionStatement final : public Statement {
public:
ExpressionStatement(NonnullOwnPtr<Expression> expression)
ExpressionStatement(NonnullRefPtr<Expression> expression)
: m_expression(move(expression))
{
}
@ -72,7 +79,7 @@ public:
virtual void dump(int indent) const override;
private:
NonnullOwnPtr<Expression> m_expression;
NonnullRefPtr<Expression> m_expression;
};
class ScopeNode : public Statement {
@ -84,12 +91,12 @@ public:
m_children.append(move(child));
return static_cast<T&>(m_children.last());
}
void append(NonnullOwnPtr<Statement> child)
void append(NonnullRefPtr<Statement> child)
{
m_children.append(move(child));
}
const NonnullOwnPtrVector<Statement>& children() const { return m_children; }
const NonnullRefPtrVector<Statement>& children() const { return m_children; }
virtual Value execute(Interpreter&) const override;
virtual void dump(int indent) const override;
@ -97,7 +104,7 @@ protected:
ScopeNode() {}
private:
NonnullOwnPtrVector<Statement> m_children;
NonnullRefPtrVector<Statement> m_children;
};
class Program : public ScopeNode {
@ -118,7 +125,7 @@ private:
class FunctionDeclaration : public Statement {
public:
FunctionDeclaration(String name, NonnullOwnPtr<ScopeNode> body, Vector<String> parameters = {})
FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {})
: m_name(move(name))
, m_body(move(body))
, m_parameters(move(parameters))
@ -136,7 +143,7 @@ private:
virtual const char* class_name() const override { return "FunctionDeclaration"; }
String m_name;
NonnullOwnPtr<ScopeNode> m_body;
NonnullRefPtr<ScopeNode> m_body;
const Vector<String> m_parameters;
};
@ -153,7 +160,7 @@ public:
class ReturnStatement : public Statement {
public:
explicit ReturnStatement(OwnPtr<Expression> argument)
explicit ReturnStatement(RefPtr<Expression> argument)
: m_argument(move(argument))
{
}
@ -166,12 +173,12 @@ public:
private:
virtual const char* class_name() const override { return "ReturnStatement"; }
OwnPtr<Expression> m_argument;
RefPtr<Expression> m_argument;
};
class IfStatement : public Statement {
public:
IfStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> consequent, NonnullOwnPtr<ScopeNode> alternate)
IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<ScopeNode> consequent, NonnullRefPtr<ScopeNode> alternate)
: m_predicate(move(predicate))
, m_consequent(move(consequent))
, m_alternate(move(alternate))
@ -188,14 +195,14 @@ public:
private:
virtual const char* class_name() const override { return "IfStatement"; }
NonnullOwnPtr<Expression> m_predicate;
NonnullOwnPtr<ScopeNode> m_consequent;
NonnullOwnPtr<ScopeNode> m_alternate;
NonnullRefPtr<Expression> m_predicate;
NonnullRefPtr<ScopeNode> m_consequent;
NonnullRefPtr<ScopeNode> m_alternate;
};
class WhileStatement : public Statement {
public:
WhileStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> body)
WhileStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<ScopeNode> body)
: m_predicate(move(predicate))
, m_body(move(body))
{
@ -210,13 +217,13 @@ public:
private:
virtual const char* class_name() const override { return "WhileStatement"; }
NonnullOwnPtr<Expression> m_predicate;
NonnullOwnPtr<ScopeNode> m_body;
NonnullRefPtr<Expression> m_predicate;
NonnullRefPtr<ScopeNode> m_body;
};
class ForStatement : public Statement {
public:
ForStatement(OwnPtr<Statement> init, OwnPtr<Expression> test, OwnPtr<Expression> update, NonnullOwnPtr<ScopeNode> body)
ForStatement(RefPtr<Statement> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<ScopeNode> body)
: m_init(move(init))
, m_test(move(test))
, m_update(move(update))
@ -235,10 +242,10 @@ public:
private:
virtual const char* class_name() const override { return "ForStatement"; }
OwnPtr<Statement> m_init;
OwnPtr<Expression> m_test;
OwnPtr<Expression> m_update;
NonnullOwnPtr<ScopeNode> m_body;
RefPtr<Statement> m_init;
RefPtr<Expression> m_test;
RefPtr<Expression> m_update;
NonnullRefPtr<ScopeNode> m_body;
};
enum class BinaryOp {
@ -263,7 +270,7 @@ enum class BinaryOp {
class BinaryExpression : public Expression {
public:
BinaryExpression(BinaryOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
: m_op(op)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
@ -277,8 +284,8 @@ private:
virtual const char* class_name() const override { return "BinaryExpression"; }
BinaryOp m_op;
NonnullOwnPtr<Expression> m_lhs;
NonnullOwnPtr<Expression> m_rhs;
NonnullRefPtr<Expression> m_lhs;
NonnullRefPtr<Expression> m_rhs;
};
enum class LogicalOp {
@ -288,7 +295,7 @@ enum class LogicalOp {
class LogicalExpression : public Expression {
public:
LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
: m_op(op)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
@ -302,8 +309,8 @@ private:
virtual const char* class_name() const override { return "LogicalExpression"; }
LogicalOp m_op;
NonnullOwnPtr<Expression> m_lhs;
NonnullOwnPtr<Expression> m_rhs;
NonnullRefPtr<Expression> m_lhs;
NonnullRefPtr<Expression> m_rhs;
};
enum class UnaryOp {
@ -314,7 +321,7 @@ enum class UnaryOp {
class UnaryExpression : public Expression {
public:
UnaryExpression(UnaryOp op, NonnullOwnPtr<Expression> lhs)
UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
: m_op(op)
, m_lhs(move(lhs))
{
@ -327,7 +334,7 @@ private:
virtual const char* class_name() const override { return "UnaryExpression"; }
UnaryOp m_op;
NonnullOwnPtr<Expression> m_lhs;
NonnullRefPtr<Expression> m_lhs;
};
class Literal : public Expression {
@ -432,7 +439,7 @@ private:
class CallExpression : public Expression {
public:
explicit CallExpression(NonnullOwnPtr<Expression> callee, NonnullOwnPtrVector<Expression> arguments = {})
explicit CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
: m_callee(move(callee))
, m_arguments(move(arguments))
{
@ -444,8 +451,8 @@ public:
private:
virtual const char* class_name() const override { return "CallExpression"; }
NonnullOwnPtr<Expression> m_callee;
const NonnullOwnPtrVector<Expression> m_arguments;
NonnullRefPtr<Expression> m_callee;
const NonnullRefPtrVector<Expression> m_arguments;
};
enum class AssignmentOp {
@ -458,7 +465,7 @@ enum class AssignmentOp {
class AssignmentExpression : public Expression {
public:
AssignmentExpression(AssignmentOp op, NonnullOwnPtr<ASTNode> lhs, NonnullOwnPtr<Expression> rhs)
AssignmentExpression(AssignmentOp op, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs)
: m_op(op)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
@ -472,8 +479,8 @@ private:
virtual const char* class_name() const override { return "AssignmentExpression"; }
AssignmentOp m_op;
NonnullOwnPtr<ASTNode> m_lhs;
NonnullOwnPtr<Expression> m_rhs;
NonnullRefPtr<ASTNode> m_lhs;
NonnullRefPtr<Expression> m_rhs;
};
enum class UpdateOp {
@ -483,7 +490,7 @@ enum class UpdateOp {
class UpdateExpression : public Expression {
public:
UpdateExpression(UpdateOp op, NonnullOwnPtr<Expression> argument, bool prefixed = false)
UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
: m_op(op)
, m_argument(move(argument))
, m_prefixed(prefixed)
@ -497,7 +504,7 @@ private:
virtual const char* class_name() const override { return "UpdateExpression"; }
UpdateOp m_op;
NonnullOwnPtr<Identifier> m_argument;
NonnullRefPtr<Identifier> m_argument;
bool m_prefixed;
};
@ -509,7 +516,7 @@ enum class DeclarationType {
class VariableDeclaration : public Statement {
public:
VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
VariableDeclaration(NonnullRefPtr<Identifier> name, RefPtr<Expression> initializer, DeclarationType declaration_type)
: m_declaration_type(declaration_type)
, m_name(move(name))
, m_initializer(move(initializer))
@ -527,8 +534,8 @@ private:
virtual const char* class_name() const override { return "VariableDeclaration"; }
DeclarationType m_declaration_type;
NonnullOwnPtr<Identifier> m_name;
OwnPtr<Expression> m_initializer;
NonnullRefPtr<Identifier> m_name;
RefPtr<Expression> m_initializer;
};
class ObjectExpression : public Expression {
@ -544,7 +551,7 @@ private:
class MemberExpression final : public Expression {
public:
MemberExpression(NonnullOwnPtr<Expression> object, NonnullOwnPtr<Expression> property)
MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property)
: m_object(move(object))
, m_property(move(property))
{
@ -559,8 +566,8 @@ private:
virtual bool is_member_expression() const override { return true; }
virtual const char* class_name() const override { return "MemberExpression"; }
NonnullOwnPtr<Expression> m_object;
NonnullOwnPtr<Expression> m_property;
NonnullRefPtr<Expression> m_object;
NonnullRefPtr<Expression> m_property;
};
}

View file

@ -72,7 +72,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, Vector<Argument> argu
void Interpreter::exit_scope(const ScopeNode& scope_node)
{
while (&m_scope_stack.last().scope_node != &scope_node)
while (m_scope_stack.last().scope_node.ptr() != &scope_node)
m_scope_stack.take_last();
}

View file

@ -47,7 +47,7 @@ struct Variable {
struct ScopeFrame {
ScopeType type;
const ScopeNode& scope_node;
NonnullRefPtr<ScopeNode> scope_node;
HashMap<String, Variable> variables;
};

View file

@ -163,9 +163,9 @@ Associativity Parser::operator_associativity(TokenType type) const
}
}
NonnullOwnPtr<Program> Parser::parse_program()
NonnullRefPtr<Program> Parser::parse_program()
{
auto program = make<Program>();
auto program = adopt(*new Program);
while (!done()) {
if (match(TokenType::Semicolon)) {
consume();
@ -179,10 +179,10 @@ NonnullOwnPtr<Program> Parser::parse_program()
return program;
}
NonnullOwnPtr<Statement> Parser::parse_statement()
NonnullRefPtr<Statement> Parser::parse_statement()
{
if (match_expression()) {
return make<JS::ExpressionStatement>(parse_expression(0));
return adopt(*new ExpressionStatement(parse_expression(0)));
}
switch (m_current_token.type()) {
@ -202,11 +202,11 @@ NonnullOwnPtr<Statement> Parser::parse_statement()
m_has_errors = true;
expected("statement (missing switch case)");
consume();
return make<ErrorStatement>();
return create_ast_node<ErrorStatement>();
}
}
NonnullOwnPtr<Expression> Parser::parse_primary_expression()
NonnullRefPtr<Expression> Parser::parse_primary_expression()
{
switch (m_current_token.type()) {
case TokenType::ParenOpen: {
@ -216,64 +216,64 @@ NonnullOwnPtr<Expression> Parser::parse_primary_expression()
return expression;
}
case TokenType::Identifier:
return make<Identifier>(consume().value());
return create_ast_node<Identifier>(consume().value());
case TokenType::NumericLiteral:
return make<NumericLiteral>(consume().double_value());
return create_ast_node<NumericLiteral>(consume().double_value());
case TokenType::BoolLiteral:
return make<BooleanLiteral>(consume().bool_value());
return create_ast_node<BooleanLiteral>(consume().bool_value());
case TokenType::StringLiteral:
return make<StringLiteral>(consume().string_value());
return create_ast_node<StringLiteral>(consume().string_value());
case TokenType::NullLiteral:
consume();
return make<NullLiteral>();
return create_ast_node<NullLiteral>();
case TokenType::UndefinedLiteral:
consume();
return make<UndefinedLiteral>();
return create_ast_node<UndefinedLiteral>();
case TokenType::CurlyOpen:
return parse_object_expression();
default:
m_has_errors = true;
expected("primary expression (missing switch case)");
consume();
return make<ErrorExpression>();
return create_ast_node<ErrorExpression>();
}
}
NonnullOwnPtr<Expression> Parser::parse_unary_prefixed_expression()
NonnullRefPtr<Expression> Parser::parse_unary_prefixed_expression()
{
switch (m_current_token.type()) {
case TokenType::PlusPlus:
consume();
return make<UpdateExpression>(UpdateOp::Increment, parse_primary_expression(), true);
return create_ast_node<UpdateExpression>(UpdateOp::Increment, parse_primary_expression(), true);
case TokenType::MinusMinus:
consume();
return make<UpdateExpression>(UpdateOp::Decrement, parse_primary_expression(), true);
return create_ast_node<UpdateExpression>(UpdateOp::Decrement, parse_primary_expression(), true);
case TokenType::ExclamationMark:
consume();
return make<UnaryExpression>(UnaryOp::Not, parse_primary_expression());
return create_ast_node<UnaryExpression>(UnaryOp::Not, parse_primary_expression());
case TokenType::Tilde:
consume();
return make<UnaryExpression>(UnaryOp::BitwiseNot, parse_primary_expression());
return create_ast_node<UnaryExpression>(UnaryOp::BitwiseNot, parse_primary_expression());
case TokenType::Typeof:
consume();
return make<UnaryExpression>(UnaryOp::Typeof, parse_primary_expression());
return create_ast_node<UnaryExpression>(UnaryOp::Typeof, parse_primary_expression());
default:
m_has_errors = true;
expected("primary expression (missing switch case)");
consume();
return make<ErrorExpression>();
return create_ast_node<ErrorExpression>();
}
}
NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
{
// FIXME: Parse actual object expression
consume(TokenType::CurlyOpen);
consume(TokenType::CurlyClose);
return make<ObjectExpression>();
return create_ast_node<ObjectExpression>();
}
NonnullOwnPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity)
NonnullRefPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity)
{
if (match_unary_prefixed_expression())
return parse_unary_prefixed_expression();
@ -292,90 +292,90 @@ NonnullOwnPtr<Expression> Parser::parse_expression(int min_precedence, Associati
return expression;
}
NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expression> lhs, int min_precedence, Associativity associativity)
NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expression> lhs, int min_precedence, Associativity associativity)
{
switch (m_current_token.type()) {
case TokenType::Plus:
consume();
return make<BinaryExpression>(BinaryOp::Plus, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::Plus, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::PlusEquals:
consume();
return make<AssignmentExpression>(AssignmentOp::AdditionAssignment, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<AssignmentExpression>(AssignmentOp::AdditionAssignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Minus:
consume();
return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::MinusEquals:
consume();
return make<AssignmentExpression>(AssignmentOp::SubtractionAssignment, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<AssignmentExpression>(AssignmentOp::SubtractionAssignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Asterisk:
consume();
return make<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::AsteriskEquals:
consume();
return make<AssignmentExpression>(AssignmentOp::MultiplicationAssignment, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<AssignmentExpression>(AssignmentOp::MultiplicationAssignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Slash:
consume();
return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::SlashEquals:
consume();
return make<AssignmentExpression>(AssignmentOp::DivisionAssignment, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<AssignmentExpression>(AssignmentOp::DivisionAssignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::GreaterThan:
consume();
return make<BinaryExpression>(BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::GreaterThanEquals:
consume();
return make<BinaryExpression>(BinaryOp::GreaterThanEquals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::GreaterThanEquals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::LessThan:
consume();
return make<BinaryExpression>(BinaryOp::LessThan, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::LessThan, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::LessThanEquals:
consume();
return make<BinaryExpression>(BinaryOp::LessThanEquals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::LessThanEquals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::EqualsEqualsEquals:
consume();
return make<BinaryExpression>(BinaryOp::TypedEquals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::TypedEquals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::ExclamationMarkEqualsEquals:
consume();
return make<BinaryExpression>(BinaryOp::TypedInequals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::TypedInequals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::EqualsEquals:
consume();
return make<BinaryExpression>(BinaryOp::AbstractEquals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::AbstractEquals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::ExclamationMarkEquals:
consume();
return make<BinaryExpression>(BinaryOp::AbstractInequals, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<BinaryExpression>(BinaryOp::AbstractInequals, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::ParenOpen:
return parse_call_expression(move(lhs));
case TokenType::Equals:
consume();
return make<AssignmentExpression>(AssignmentOp::Assignment, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<AssignmentExpression>(AssignmentOp::Assignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::Period:
consume();
return make<MemberExpression>(move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<MemberExpression>(move(lhs), parse_expression(min_precedence, associativity));
case TokenType::PlusPlus:
consume();
return make<UpdateExpression>(UpdateOp::Increment, move(lhs));
return create_ast_node<UpdateExpression>(UpdateOp::Increment, move(lhs));
case TokenType::MinusMinus:
consume();
return make<UpdateExpression>(UpdateOp::Decrement, move(lhs));
return create_ast_node<UpdateExpression>(UpdateOp::Decrement, move(lhs));
case TokenType::DoubleAmpersand:
consume();
return make<LogicalExpression>(LogicalOp::And, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<LogicalExpression>(LogicalOp::And, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::DoublePipe:
consume();
return make<LogicalExpression>(LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity));
return create_ast_node<LogicalExpression>(LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity));
default:
m_has_errors = true;
expected("secondary expression (missing switch case)");
consume();
return make<ErrorExpression>();
return create_ast_node<ErrorExpression>();
}
}
NonnullOwnPtr<CallExpression> Parser::parse_call_expression(NonnullOwnPtr<Expression> lhs)
NonnullRefPtr<CallExpression> Parser::parse_call_expression(NonnullRefPtr<Expression> lhs)
{
consume(TokenType::ParenOpen);
NonnullOwnPtrVector<Expression> arguments;
NonnullRefPtrVector<Expression> arguments;
while (match_expression()) {
arguments.append(parse_expression(0));
@ -386,21 +386,21 @@ NonnullOwnPtr<CallExpression> Parser::parse_call_expression(NonnullOwnPtr<Expres
consume(TokenType::ParenClose);
return make<CallExpression>(move(lhs), move(arguments));
return create_ast_node<CallExpression>(move(lhs), move(arguments));
}
NonnullOwnPtr<ReturnStatement> Parser::parse_return_statement()
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
{
consume(TokenType::Return);
if (match_expression()) {
return make<ReturnStatement>(parse_expression(0));
return create_ast_node<ReturnStatement>(parse_expression(0));
}
return make<ReturnStatement>(nullptr);
return create_ast_node<ReturnStatement>(nullptr);
}
NonnullOwnPtr<BlockStatement> Parser::parse_block_statement()
NonnullRefPtr<BlockStatement> Parser::parse_block_statement()
{
auto block = make<BlockStatement>();
auto block = create_ast_node<BlockStatement>();
consume(TokenType::CurlyOpen);
while (!done() && !match(TokenType::CurlyClose)) {
if (match(TokenType::Semicolon)) {
@ -416,7 +416,7 @@ NonnullOwnPtr<BlockStatement> Parser::parse_block_statement()
return block;
}
NonnullOwnPtr<FunctionDeclaration> Parser::parse_function_declaration()
NonnullRefPtr<FunctionDeclaration> Parser::parse_function_declaration()
{
consume(TokenType::Function);
auto name = consume(TokenType::Identifier).value();
@ -432,10 +432,10 @@ NonnullOwnPtr<FunctionDeclaration> Parser::parse_function_declaration()
}
consume(TokenType::ParenClose);
auto body = parse_block_statement();
return make<FunctionDeclaration>(name, move(body), move(parameters));
return create_ast_node<FunctionDeclaration>(name, move(body), move(parameters));
}
NonnullOwnPtr<VariableDeclaration> Parser::parse_variable_declaration()
NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration()
{
DeclarationType declaration_type;
@ -456,21 +456,21 @@ NonnullOwnPtr<VariableDeclaration> Parser::parse_variable_declaration()
ASSERT_NOT_REACHED();
}
auto name = consume(TokenType::Identifier).value();
OwnPtr<Expression> initializer;
RefPtr<Expression> initializer;
if (match(TokenType::Equals)) {
consume();
initializer = parse_expression(0);
}
return make<VariableDeclaration>(make<Identifier>(name), move(initializer), declaration_type);
return create_ast_node<VariableDeclaration>(create_ast_node<Identifier>(name), move(initializer), declaration_type);
}
NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
NonnullRefPtr<ForStatement> Parser::parse_for_statement()
{
consume(TokenType::For);
consume(TokenType::ParenOpen);
OwnPtr<Statement> init = nullptr;
RefPtr<Statement> init;
switch (m_current_token.type()) {
case TokenType::Semicolon:
break;
@ -481,7 +481,7 @@ NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
consume(TokenType::Semicolon);
OwnPtr<Expression> test = nullptr;
RefPtr<Expression> test;
switch (m_current_token.type()) {
case TokenType::Semicolon:
break;
@ -492,7 +492,7 @@ NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
consume(TokenType::Semicolon);
OwnPtr<Expression> update = nullptr;
RefPtr<Expression> update;
switch (m_current_token.type()) {
case TokenType::Semicolon:
break;
@ -505,7 +505,7 @@ NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
auto body = parse_block_statement();
return make<ForStatement>(move(init), move(test), move(update), move(body));
return create_ast_node<ForStatement>(move(init), move(test), move(update), move(body));
}
bool Parser::match(TokenType type) const

View file

@ -28,7 +28,7 @@
#include "AST.h"
#include "Lexer.h"
#include <AK/NonnullOwnPtr.h>
#include <AK/NonnullRefPtr.h>
namespace JS {
@ -41,21 +41,21 @@ class Parser {
public:
explicit Parser(Lexer lexer);
NonnullOwnPtr<Program> parse_program();
NonnullRefPtr<Program> parse_program();
NonnullOwnPtr<Statement> parse_statement();
NonnullOwnPtr<BlockStatement> parse_block_statement();
NonnullOwnPtr<ReturnStatement> parse_return_statement();
NonnullOwnPtr<FunctionDeclaration> parse_function_declaration();
NonnullOwnPtr<VariableDeclaration> parse_variable_declaration();
NonnullOwnPtr<ForStatement> parse_for_statement();
NonnullRefPtr<Statement> parse_statement();
NonnullRefPtr<BlockStatement> parse_block_statement();
NonnullRefPtr<ReturnStatement> parse_return_statement();
NonnullRefPtr<FunctionDeclaration> parse_function_declaration();
NonnullRefPtr<VariableDeclaration> parse_variable_declaration();
NonnullRefPtr<ForStatement> parse_for_statement();
NonnullOwnPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
NonnullOwnPtr<Expression> parse_primary_expression();
NonnullOwnPtr<Expression> parse_unary_prefixed_expression();
NonnullOwnPtr<ObjectExpression> parse_object_expression();
NonnullOwnPtr<Expression> parse_secondary_expression(NonnullOwnPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
NonnullOwnPtr<CallExpression> parse_call_expression(NonnullOwnPtr<Expression>);
NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
NonnullRefPtr<Expression> parse_primary_expression();
NonnullRefPtr<Expression> parse_unary_prefixed_expression();
NonnullRefPtr<ObjectExpression> parse_object_expression();
NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
NonnullRefPtr<CallExpression> parse_call_expression(NonnullRefPtr<Expression>);
bool has_errors() const { return m_has_errors; }

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ScriptFunction.h>
#include <LibJS/Runtime/Value.h>

View file

@ -44,7 +44,7 @@ private:
virtual bool is_script_function() const final { return true; }
virtual const char* class_name() const override { return "ScriptFunction"; }
const ScopeNode& m_body;
NonnullRefPtr<ScopeNode> m_body;
const Vector<String> m_parameters;
};