AST.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullOwnPtrVector.h>
  28. #include <AK/String.h>
  29. #include <LibJS/Forward.h>
  30. #include <LibJS/Value.h>
  31. namespace JS {
  32. class ASTNode {
  33. public:
  34. virtual ~ASTNode() {}
  35. virtual const char* class_name() const = 0;
  36. virtual Value execute(Interpreter&) const = 0;
  37. virtual void dump(int indent) const;
  38. protected:
  39. ASTNode() {}
  40. private:
  41. };
  42. class ScopeNode : public ASTNode {
  43. public:
  44. template<typename T, typename... Args>
  45. T& append(Args&&... args)
  46. {
  47. auto child = make<T>(forward<Args>(args)...);
  48. m_children.append(move(child));
  49. return static_cast<T&>(m_children.last());
  50. }
  51. const NonnullOwnPtrVector<ASTNode>& children() const { return m_children; }
  52. virtual Value execute(Interpreter&) const override;
  53. virtual void dump(int indent) const override;
  54. protected:
  55. ScopeNode() {}
  56. private:
  57. NonnullOwnPtrVector<ASTNode> m_children;
  58. };
  59. class Program : public ScopeNode {
  60. public:
  61. Program() {}
  62. private:
  63. virtual const char* class_name() const override { return "Program"; }
  64. };
  65. class BlockStatement : public ScopeNode {
  66. public:
  67. BlockStatement() {}
  68. private:
  69. virtual const char* class_name() const override { return "BlockStatement"; }
  70. };
  71. class FunctionDeclaration : public ASTNode {
  72. public:
  73. FunctionDeclaration(String name, NonnullOwnPtr<ScopeNode> body)
  74. : m_name(move(name))
  75. , m_body(move(body))
  76. {
  77. }
  78. String name() const { return m_name; }
  79. const ScopeNode& body() const { return *m_body; }
  80. virtual Value execute(Interpreter&) const override;
  81. virtual void dump(int indent) const override;
  82. private:
  83. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  84. String m_name;
  85. NonnullOwnPtr<ScopeNode> m_body;
  86. };
  87. class Expression : public ASTNode {
  88. public:
  89. };
  90. class ReturnStatement : public ASTNode {
  91. public:
  92. explicit ReturnStatement(NonnullOwnPtr<Expression> argument)
  93. : m_argument(move(argument))
  94. {
  95. }
  96. const Expression& argument() const { return *m_argument; }
  97. virtual Value execute(Interpreter&) const override;
  98. virtual void dump(int indent) const override;
  99. private:
  100. virtual const char* class_name() const override { return "ReturnStatement"; }
  101. NonnullOwnPtr<Expression> m_argument;
  102. };
  103. enum class BinaryOp {
  104. Plus,
  105. Minus,
  106. };
  107. class BinaryExpression : public Expression {
  108. public:
  109. BinaryExpression(BinaryOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  110. : m_op(op)
  111. , m_lhs(move(lhs))
  112. , m_rhs(move(rhs))
  113. {
  114. }
  115. virtual Value execute(Interpreter&) const override;
  116. virtual void dump(int indent) const override;
  117. private:
  118. virtual const char* class_name() const override { return "BinaryExpression"; }
  119. BinaryOp m_op;
  120. NonnullOwnPtr<Expression> m_lhs;
  121. NonnullOwnPtr<Expression> m_rhs;
  122. };
  123. class Literal : public Expression {
  124. public:
  125. explicit Literal(Value value)
  126. : m_value(move(value))
  127. {
  128. }
  129. virtual Value execute(Interpreter&) const override { return m_value; }
  130. virtual void dump(int indent) const override;
  131. private:
  132. virtual const char* class_name() const override { return "Literal"; }
  133. Value m_value;
  134. };
  135. class CallExpression : public Expression {
  136. public:
  137. explicit CallExpression(String name)
  138. : m_name(move(name))
  139. {
  140. }
  141. virtual Value execute(Interpreter&) const override;
  142. virtual void dump(int indent) const override;
  143. const String& name() const { return m_name; }
  144. private:
  145. virtual const char* class_name() const override { return "CallExpression"; }
  146. String m_name;
  147. };
  148. }