| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  | #include <AK/FlyString.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-21 02:29:00 +02:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  | #include <AK/NonnullRefPtrVector.h>
 | 
					
						
							|  |  |  | #include <AK/RefPtr.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-12 19:22:13 +08:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:20:30 +01:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  | 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> { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							|  |  |  |     virtual ~ASTNode() {} | 
					
						
							|  |  |  |     virtual const char* class_name() const = 0; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const = 0; | 
					
						
							|  |  |  |     virtual void dump(int indent) const; | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  |     virtual bool is_identifier() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-03-19 17:39:13 +01:00
										 |  |  |     virtual bool is_member_expression() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     virtual bool is_scope_node() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-03-23 19:08:32 +01:00
										 |  |  |     virtual bool is_variable_declaration() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-03-28 16:33:52 +01:00
										 |  |  |     virtual bool is_new_expression() const { return false; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     ASTNode() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  | class Statement : public ASTNode { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ErrorStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-15 23:33:38 +02:00
										 |  |  |     Value execute(Interpreter&) const override { return js_undefined(); } | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  |     const char* class_name() const override { return "ErrorStatement"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ExpressionStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     ExpressionStatement(NonnullRefPtr<Expression> expression) | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  |         : m_expression(move(expression)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     const char* class_name() const override { return "ExpressionStatement"; } | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_expression; | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ScopeNode : public Statement { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							|  |  |  |     template<typename T, typename... Args> | 
					
						
							|  |  |  |     T& append(Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-19 11:02:20 +01:00
										 |  |  |         auto child = create_ast_node<T>(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |         m_children.append(move(child)); | 
					
						
							|  |  |  |         return static_cast<T&>(m_children.last()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     void append(NonnullRefPtr<Statement> child) | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_children.append(move(child)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     const NonnullRefPtrVector<Statement>& children() const { return m_children; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     ScopeNode() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     virtual bool is_scope_node() const final { return true; } | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtrVector<Statement> m_children; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Program : public ScopeNode { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     Program() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "Program"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BlockStatement : public ScopeNode { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     BlockStatement() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "BlockStatement"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  | class Expression : public ASTNode { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:32:10 +02:00
										 |  |  | class Declaration : public Statement { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  | class FunctionNode { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     const FlyString& name() const { return m_name; } | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     const Statement& body() const { return *m_body; } | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     const Vector<FlyString>& parameters() const { return m_parameters; }; | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {}) | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |         : m_name(name) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |         , m_body(move(body)) | 
					
						
							| 
									
										
										
										
											2020-03-12 19:22:13 +08:00
										 |  |  |         , m_parameters(move(parameters)) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  |     void dump(int indent, const char* class_name) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     FlyString m_name; | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     NonnullRefPtr<Statement> m_body; | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     const Vector<FlyString> m_parameters; | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunctionDeclaration final | 
					
						
							| 
									
										
										
										
											2020-04-04 21:32:10 +02:00
										 |  |  |     : public Declaration | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  |     , public FunctionNode { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-19 11:52:56 +01:00
										 |  |  |     static bool must_have_name() { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     FunctionDeclaration(String name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {}) | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  |         : FunctionNode(move(name), move(body), move(parameters)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "FunctionDeclaration"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  | class FunctionExpression final : public Expression | 
					
						
							|  |  |  |     , public FunctionNode { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-19 11:52:56 +01:00
										 |  |  |     static bool must_have_name() { return false; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {}) | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |         : FunctionNode(name, move(body), move(parameters)) | 
					
						
							| 
									
										
										
										
											2020-03-19 11:12:08 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "FunctionExpression"; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  | class ErrorExpression final : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-15 23:33:38 +02:00
										 |  |  |     Value execute(Interpreter&) const override { return js_undefined(); } | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  |     const char* class_name() const override { return "ErrorExpression"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReturnStatement : public Statement { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     explicit ReturnStatement(RefPtr<Expression> argument) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |         : m_argument(move(argument)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  |     const Expression* argument() const { return m_argument; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ReturnStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     RefPtr<Expression> m_argument; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  | class IfStatement : public Statement { | 
					
						
							| 
									
										
										
										
											2020-03-08 07:58:58 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate) | 
					
						
							| 
									
										
										
										
											2020-03-08 07:58:58 +02:00
										 |  |  |         : m_predicate(move(predicate)) | 
					
						
							|  |  |  |         , m_consequent(move(consequent)) | 
					
						
							|  |  |  |         , m_alternate(move(alternate)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Expression& predicate() const { return *m_predicate; } | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     const Statement& consequent() const { return *m_consequent; } | 
					
						
							|  |  |  |     const Statement* alternate() const { return m_alternate; } | 
					
						
							| 
									
										
										
										
											2020-03-08 07:58:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "IfStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_predicate; | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     NonnullRefPtr<Statement> m_consequent; | 
					
						
							|  |  |  |     RefPtr<Statement> m_alternate; | 
					
						
							| 
									
										
										
										
											2020-03-08 07:58:58 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 19:27:43 +01:00
										 |  |  | class WhileStatement : public Statement { | 
					
						
							| 
									
										
										
										
											2020-03-09 03:22:21 +08:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body) | 
					
						
							| 
									
										
										
										
											2020-04-04 21:21:19 +02:00
										 |  |  |         : m_test(move(test)) | 
					
						
							| 
									
										
										
										
											2020-03-09 03:22:21 +08:00
										 |  |  |         , m_body(move(body)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:21:19 +02:00
										 |  |  |     const Expression& test() const { return *m_test; } | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     const Statement& body() const { return *m_body; } | 
					
						
							| 
									
										
										
										
											2020-03-09 03:22:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "WhileStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:21:19 +02:00
										 |  |  |     NonnullRefPtr<Expression> m_test; | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     NonnullRefPtr<Statement> m_body; | 
					
						
							| 
									
										
										
										
											2020-03-09 03:22:21 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:29:23 +02:00
										 |  |  | class DoWhileStatement : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body) | 
					
						
							| 
									
										
										
										
											2020-04-04 21:29:23 +02:00
										 |  |  |         : m_test(move(test)) | 
					
						
							|  |  |  |         , m_body(move(body)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Expression& test() const { return *m_test; } | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     const Statement& body() const { return *m_body; } | 
					
						
							| 
									
										
										
										
											2020-04-04 21:29:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "DoWhileStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_test; | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     NonnullRefPtr<Statement> m_body; | 
					
						
							| 
									
										
										
										
											2020-04-04 21:29:23 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 23:12:12 +11:00
										 |  |  | class ForStatement : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body) | 
					
						
							| 
									
										
										
										
											2020-03-12 23:12:12 +11:00
										 |  |  |         : m_init(move(init)) | 
					
						
							|  |  |  |         , m_test(move(test)) | 
					
						
							|  |  |  |         , m_update(move(update)) | 
					
						
							|  |  |  |         , m_body(move(body)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 19:08:32 +01:00
										 |  |  |     const ASTNode* init() const { return m_init; } | 
					
						
							| 
									
										
										
										
											2020-03-12 23:12:12 +11:00
										 |  |  |     const Expression* test() const { return m_test; } | 
					
						
							|  |  |  |     const Expression* update() const { return m_update; } | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     const Statement& body() const { return *m_body; } | 
					
						
							| 
									
										
										
										
											2020-03-12 23:12:12 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ForStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 19:08:32 +01:00
										 |  |  |     RefPtr<ASTNode> m_init; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     RefPtr<Expression> m_test; | 
					
						
							|  |  |  |     RefPtr<Expression> m_update; | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     NonnullRefPtr<Statement> m_body; | 
					
						
							| 
									
										
										
										
											2020-03-12 23:12:12 +11:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | enum class BinaryOp { | 
					
						
							| 
									
										
										
										
											2020-04-05 12:56:53 +01:00
										 |  |  |     Addition, | 
					
						
							|  |  |  |     Subtraction, | 
					
						
							|  |  |  |     Multiplication, | 
					
						
							|  |  |  |     Division, | 
					
						
							| 
									
										
										
										
											2020-04-04 21:17:34 +02:00
										 |  |  |     Modulo, | 
					
						
							| 
									
										
										
										
											2020-04-05 13:40:00 +01:00
										 |  |  |     Exponentiation, | 
					
						
							| 
									
										
										
										
											2020-03-08 07:53:02 +02:00
										 |  |  |     TypedEquals, | 
					
						
							| 
									
										
										
										
											2020-03-08 23:27:18 +02:00
										 |  |  |     TypedInequals, | 
					
						
							| 
									
										
										
										
											2020-03-16 00:23:38 +02:00
										 |  |  |     AbstractEquals, | 
					
						
							|  |  |  |     AbstractInequals, | 
					
						
							| 
									
										
										
										
											2020-03-10 11:35:05 +01:00
										 |  |  |     GreaterThan, | 
					
						
							| 
									
										
										
										
											2020-03-12 23:07:08 +11:00
										 |  |  |     GreaterThanEquals, | 
					
						
							| 
									
										
										
										
											2020-03-10 11:35:05 +01:00
										 |  |  |     LessThan, | 
					
						
							| 
									
										
										
										
											2020-03-12 23:07:08 +11:00
										 |  |  |     LessThanEquals, | 
					
						
							| 
									
										
										
										
											2020-03-10 11:35:05 +01:00
										 |  |  |     BitwiseAnd, | 
					
						
							|  |  |  |     BitwiseOr, | 
					
						
							|  |  |  |     BitwiseXor, | 
					
						
							|  |  |  |     LeftShift, | 
					
						
							|  |  |  |     RightShift, | 
					
						
							| 
									
										
										
										
											2020-03-28 16:56:54 +01:00
										 |  |  |     InstanceOf, | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BinaryExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |         : m_op(op) | 
					
						
							|  |  |  |         , m_lhs(move(lhs)) | 
					
						
							|  |  |  |         , m_rhs(move(rhs)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "BinaryExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BinaryOp m_op; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_lhs; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_rhs; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 07:55:44 +02:00
										 |  |  | enum class LogicalOp { | 
					
						
							|  |  |  |     And, | 
					
						
							|  |  |  |     Or, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LogicalExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs) | 
					
						
							| 
									
										
										
										
											2020-03-08 07:55:44 +02:00
										 |  |  |         : m_op(op) | 
					
						
							|  |  |  |         , m_lhs(move(lhs)) | 
					
						
							|  |  |  |         , m_rhs(move(rhs)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "LogicalExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     LogicalOp m_op; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_lhs; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_rhs; | 
					
						
							| 
									
										
										
										
											2020-03-08 07:55:44 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 23:27:18 +02:00
										 |  |  | enum class UnaryOp { | 
					
						
							| 
									
										
										
										
											2020-03-14 20:43:35 +02:00
										 |  |  |     BitwiseNot, | 
					
						
							| 
									
										
										
										
											2020-03-09 19:04:44 +02:00
										 |  |  |     Not, | 
					
						
							| 
									
										
										
										
											2020-04-02 17:58:39 +01:00
										 |  |  |     Plus, | 
					
						
							|  |  |  |     Minus, | 
					
						
							| 
									
										
										
										
											2020-03-18 06:33:32 +11:00
										 |  |  |     Typeof, | 
					
						
							| 
									
										
										
										
											2020-03-08 23:27:18 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UnaryExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs) | 
					
						
							| 
									
										
										
										
											2020-03-08 23:27:18 +02:00
										 |  |  |         : m_op(op) | 
					
						
							|  |  |  |         , m_lhs(move(lhs)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "UnaryExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     UnaryOp m_op; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_lhs; | 
					
						
							| 
									
										
										
										
											2020-03-08 23:27:18 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | class Literal : public Expression { | 
					
						
							| 
									
										
										
										
											2020-03-12 12:19:11 +01:00
										 |  |  | protected: | 
					
						
							|  |  |  |     explicit Literal() {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BooleanLiteral final : public Literal { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     explicit BooleanLiteral(bool value) | 
					
						
							|  |  |  |         : m_value(value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "BooleanLiteral"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool m_value { false }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NumericLiteral final : public Literal { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-12 12:19:11 +01:00
										 |  |  |     explicit NumericLiteral(double value) | 
					
						
							|  |  |  |         : m_value(value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "NumericLiteral"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     double m_value { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StringLiteral final : public Literal { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     explicit StringLiteral(String value) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |         : m_value(move(value)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 12:19:11 +01:00
										 |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-12 12:19:11 +01:00
										 |  |  |     virtual const char* class_name() const override { return "StringLiteral"; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 12:19:11 +01:00
										 |  |  |     String m_value; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 23:32:34 +02:00
										 |  |  | class NullLiteral final : public Literal { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     explicit NullLiteral() {} | 
					
						
							| 
									
										
										
										
											2020-03-15 23:32:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "NullLiteral"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 11:48:49 +01:00
										 |  |  | class Identifier final : public Expression { | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     explicit Identifier(const FlyString& string) | 
					
						
							|  |  |  |         : m_string(string) | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     const FlyString& string() const { return m_string; } | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual bool is_identifier() const override { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "Identifier"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     FlyString m_string; | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | class CallExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-28 16:33:52 +01:00
										 |  |  |     CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {}) | 
					
						
							| 
									
										
										
										
											2020-03-12 23:02:41 +01:00
										 |  |  |         : m_callee(move(callee)) | 
					
						
							| 
									
										
										
										
											2020-03-12 19:22:13 +08:00
										 |  |  |         , m_arguments(move(arguments)) | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "CallExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 18:51:27 +02:00
										 |  |  |     struct ThisAndCallee { | 
					
						
							|  |  |  |         Value this_value; | 
					
						
							|  |  |  |         Value callee; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ThisAndCallee compute_this_and_callee(Interpreter&) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_callee; | 
					
						
							|  |  |  |     const NonnullRefPtrVector<Expression> m_arguments; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 16:33:52 +01:00
										 |  |  | class NewExpression final : public CallExpression { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     NewExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {}) | 
					
						
							|  |  |  |         : CallExpression(move(callee), move(arguments)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-29 13:09:54 +02:00
										 |  |  |     virtual const char* class_name() const override { return "NewExpression"; } | 
					
						
							| 
									
										
										
										
											2020-03-28 16:33:52 +01:00
										 |  |  |     virtual bool is_new_expression() const override { return true; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | enum class AssignmentOp { | 
					
						
							| 
									
										
										
										
											2020-03-12 13:54:56 +01:00
										 |  |  |     Assignment, | 
					
						
							|  |  |  |     AdditionAssignment, | 
					
						
							|  |  |  |     SubtractionAssignment, | 
					
						
							|  |  |  |     MultiplicationAssignment, | 
					
						
							|  |  |  |     DivisionAssignment, | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AssignmentExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     AssignmentExpression(AssignmentOp op, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs) | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  |         : m_op(op) | 
					
						
							|  |  |  |         , m_lhs(move(lhs)) | 
					
						
							|  |  |  |         , m_rhs(move(rhs)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "AssignmentExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AssignmentOp m_op; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<ASTNode> m_lhs; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_rhs; | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 13:45:45 +02:00
										 |  |  | enum class UpdateOp { | 
					
						
							|  |  |  |     Increment, | 
					
						
							|  |  |  |     Decrement, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UpdateExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false) | 
					
						
							| 
									
										
										
										
											2020-03-12 13:45:45 +02:00
										 |  |  |         : m_op(op) | 
					
						
							|  |  |  |         , m_argument(move(argument)) | 
					
						
							| 
									
										
										
										
											2020-03-14 20:44:57 +02:00
										 |  |  |         , m_prefixed(prefixed) | 
					
						
							| 
									
										
										
										
											2020-03-12 13:45:45 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "UpdateExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     UpdateOp m_op; | 
					
						
							| 
									
										
										
										
											2020-04-05 11:11:07 +02:00
										 |  |  |     NonnullRefPtr<Expression> m_argument; | 
					
						
							| 
									
										
										
										
											2020-03-14 20:44:57 +02:00
										 |  |  |     bool m_prefixed; | 
					
						
							| 
									
										
										
										
											2020-03-12 13:45:45 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  | enum class DeclarationType { | 
					
						
							|  |  |  |     Var, | 
					
						
							|  |  |  |     Let, | 
					
						
							| 
									
										
										
										
											2020-03-12 14:24:34 +02:00
										 |  |  |     Const, | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:46:25 +02:00
										 |  |  | class VariableDeclarator final : public ASTNode { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     VariableDeclarator(NonnullRefPtr<Identifier> id, RefPtr<Expression> init) | 
					
						
							|  |  |  |         : m_id(move(id)) | 
					
						
							|  |  |  |         , m_init(move(init)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Identifier& id() const { return m_id; } | 
					
						
							|  |  |  |     const Expression* init() const { return m_init; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "VariableDeclarator"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Identifier> m_id; | 
					
						
							|  |  |  |     RefPtr<Expression> m_init; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:32:10 +02:00
										 |  |  | class VariableDeclaration : public Declaration { | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-04 21:46:25 +02:00
										 |  |  |     VariableDeclaration(DeclarationType declaration_type, NonnullRefPtrVector<VariableDeclarator> declarations) | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  |         : m_declaration_type(declaration_type) | 
					
						
							| 
									
										
										
										
											2020-04-04 21:46:25 +02:00
										 |  |  |         , m_declarations(move(declarations)) | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:56:49 +02:00
										 |  |  |     virtual bool is_variable_declaration() const override { return true; } | 
					
						
							|  |  |  |     DeclarationType declaration_type() const { return m_declaration_type; } | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "VariableDeclaration"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  |     DeclarationType m_declaration_type; | 
					
						
							| 
									
										
										
										
											2020-04-04 21:46:25 +02:00
										 |  |  |     NonnullRefPtrVector<VariableDeclarator> m_declarations; | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:31 +01:00
										 |  |  | class ObjectExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     ObjectExpression(HashMap<FlyString, NonnullRefPtr<Expression>> properties = {}) | 
					
						
							| 
									
										
										
										
											2020-03-21 13:05:43 +01:00
										 |  |  |         : m_properties(move(properties)) | 
					
						
							| 
									
										
										
										
											2020-03-21 02:29:00 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ObjectExpression"; } | 
					
						
							| 
									
										
										
										
											2020-03-21 02:29:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     HashMap<FlyString, NonnullRefPtr<Expression>> m_properties; | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:31 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 20:29:57 +01:00
										 |  |  | class ArrayExpression : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     ArrayExpression(NonnullRefPtrVector<Expression> elements) | 
					
						
							|  |  |  |         : m_elements(move(elements)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const NonnullRefPtrVector<Expression>& elements() const { return m_elements; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ArrayExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtrVector<Expression> m_elements; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  | class MemberExpression final : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-20 20:51:03 +01:00
										 |  |  |     MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false) | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  |         : m_object(move(object)) | 
					
						
							|  |  |  |         , m_property(move(property)) | 
					
						
							| 
									
										
										
										
											2020-03-20 20:51:03 +01:00
										 |  |  |         , m_computed(computed) | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 20:51:03 +01:00
										 |  |  |     bool is_computed() const { return m_computed; } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  |     const Expression& object() const { return *m_object; } | 
					
						
							| 
									
										
										
										
											2020-03-19 17:39:13 +01:00
										 |  |  |     const Expression& property() const { return *m_property; } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     FlyString computed_property_name(Interpreter&) const; | 
					
						
							| 
									
										
										
										
											2020-03-20 20:51:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  |     virtual bool is_member_expression() const override { return true; } | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  |     virtual const char* class_name() const override { return "MemberExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<Expression> m_object; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_property; | 
					
						
							| 
									
										
										
										
											2020-03-20 20:51:03 +01:00
										 |  |  |     bool m_computed { false }; | 
					
						
							| 
									
										
										
										
											2020-03-11 18:58:19 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 12:14:28 +02:00
										 |  |  | class ConditionalExpression final : public Expression { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     ConditionalExpression(NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate) | 
					
						
							|  |  |  |         : m_test(move(test)) | 
					
						
							|  |  |  |         , m_consequent(move(consequent)) | 
					
						
							|  |  |  |         , m_alternate(move(alternate)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ConditionalExpression"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_test; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_consequent; | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_alternate; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 14:03:55 +01:00
										 |  |  | class CatchClause final : public ASTNode { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body) | 
					
						
							|  |  |  |         : m_parameter(parameter) | 
					
						
							|  |  |  |         , m_body(move(body)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const FlyString& parameter() const { return m_parameter; } | 
					
						
							|  |  |  |     const BlockStatement& body() const { return m_body; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "CatchClause"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     FlyString m_parameter; | 
					
						
							|  |  |  |     NonnullRefPtr<BlockStatement> m_body; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TryStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer) | 
					
						
							|  |  |  |         : m_block(move(block)) | 
					
						
							|  |  |  |         , m_handler(move(handler)) | 
					
						
							|  |  |  |         , m_finalizer(move(finalizer)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const BlockStatement& block() const { return m_block; } | 
					
						
							|  |  |  |     const CatchClause* handler() const { return m_handler; } | 
					
						
							|  |  |  |     const BlockStatement* finalizer() const { return m_finalizer; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "TryStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<BlockStatement> m_block; | 
					
						
							|  |  |  |     RefPtr<CatchClause> m_handler; | 
					
						
							|  |  |  |     RefPtr<BlockStatement> m_finalizer; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  | class ThrowStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     explicit ThrowStatement(NonnullRefPtr<Expression> argument) | 
					
						
							|  |  |  |         : m_argument(move(argument)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Expression& argument() const { return m_argument; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ThrowStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_argument; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-29 13:09:54 +02:00
										 |  |  | class SwitchCase final : public ASTNode { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     SwitchCase(RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent) | 
					
						
							|  |  |  |         : m_test(move(test)) | 
					
						
							|  |  |  |         , m_consequent(move(consequent)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-29 14:34:25 +02:00
										 |  |  |     const Expression* test() const { return m_test; } | 
					
						
							|  |  |  |     const NonnullRefPtrVector<Statement>& consequent() const { return m_consequent; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-29 13:09:54 +02:00
										 |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "SwitchCase"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     RefPtr<Expression> m_test; | 
					
						
							|  |  |  |     NonnullRefPtrVector<Statement> m_consequent; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SwitchStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     SwitchStatement(NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases) | 
					
						
							|  |  |  |         : m_discriminant(move(discriminant)) | 
					
						
							|  |  |  |         , m_cases(move(cases)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void dump(int indent) const override; | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "SwitchStatement"; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Expression> m_discriminant; | 
					
						
							|  |  |  |     NonnullRefPtrVector<SwitchCase> m_cases; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BreakStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     BreakStatement() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "BreakStatement"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:22:42 +02:00
										 |  |  | class ContinueStatement final : public Statement { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     ContinueStatement() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Value execute(Interpreter&) const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual const char* class_name() const override { return "ContinueStatement"; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | } |