AST.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/OwnPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibJS/Forward.h>
  32. #include <LibJS/Value.h>
  33. namespace JS {
  34. class ASTNode {
  35. public:
  36. virtual ~ASTNode() {}
  37. virtual const char* class_name() const = 0;
  38. virtual Value execute(Interpreter&) const = 0;
  39. virtual void dump(int indent) const;
  40. virtual bool is_identifier() const { return false; }
  41. protected:
  42. ASTNode() {}
  43. private:
  44. };
  45. class Statement : public ASTNode {
  46. };
  47. class ErrorStatement final : public Statement {
  48. public:
  49. Value execute(Interpreter&) const { return js_undefined(); }
  50. const char* class_name() const override { return "ErrorStatement"; }
  51. };
  52. class ExpressionStatement final : public Statement {
  53. public:
  54. ExpressionStatement(NonnullOwnPtr<Expression> expression)
  55. : m_expression(move(expression))
  56. {
  57. }
  58. Value execute(Interpreter&) const override;
  59. const char* class_name() const override { return "ExpressionStatement"; }
  60. virtual void dump(int indent) const override;
  61. private:
  62. NonnullOwnPtr<Expression> m_expression;
  63. };
  64. class ScopeNode : public Statement {
  65. public:
  66. template<typename T, typename... Args>
  67. T& append(Args&&... args)
  68. {
  69. auto child = make<T>(forward<Args>(args)...);
  70. m_children.append(move(child));
  71. return static_cast<T&>(m_children.last());
  72. }
  73. void append(NonnullOwnPtr<Statement> child)
  74. {
  75. m_children.append(move(child));
  76. }
  77. const NonnullOwnPtrVector<Statement>& children() const { return m_children; }
  78. virtual Value execute(Interpreter&) const override;
  79. virtual void dump(int indent) const override;
  80. protected:
  81. ScopeNode() {}
  82. private:
  83. NonnullOwnPtrVector<Statement> m_children;
  84. };
  85. class Program : public ScopeNode {
  86. public:
  87. Program() {}
  88. private:
  89. virtual const char* class_name() const override { return "Program"; }
  90. };
  91. class BlockStatement : public ScopeNode {
  92. public:
  93. BlockStatement() {}
  94. private:
  95. virtual const char* class_name() const override { return "BlockStatement"; }
  96. };
  97. class FunctionDeclaration : public Statement {
  98. public:
  99. FunctionDeclaration(String name, NonnullOwnPtr<ScopeNode> body, Vector<String> parameters = {})
  100. : m_name(move(name))
  101. , m_body(move(body))
  102. , m_parameters(move(parameters))
  103. {
  104. }
  105. String name() const { return m_name; }
  106. const ScopeNode& body() const { return *m_body; }
  107. const Vector<String>& parameters() const { return m_parameters; };
  108. virtual Value execute(Interpreter&) const override;
  109. virtual void dump(int indent) const override;
  110. private:
  111. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  112. String m_name;
  113. NonnullOwnPtr<ScopeNode> m_body;
  114. const Vector<String> m_parameters;
  115. };
  116. class Expression : public ASTNode {
  117. public:
  118. };
  119. class ErrorExpression final : public Expression {
  120. public:
  121. Value execute(Interpreter&) const { return js_undefined(); }
  122. const char* class_name() const override { return "ErrorExpression"; }
  123. };
  124. class ReturnStatement : public Statement {
  125. public:
  126. explicit ReturnStatement(OwnPtr<Expression> argument)
  127. : m_argument(move(argument))
  128. {
  129. }
  130. const Expression* argument() const { return m_argument; }
  131. virtual Value execute(Interpreter&) const override;
  132. virtual void dump(int indent) const override;
  133. private:
  134. virtual const char* class_name() const override { return "ReturnStatement"; }
  135. OwnPtr<Expression> m_argument;
  136. };
  137. class IfStatement : public Statement {
  138. public:
  139. IfStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> consequent, NonnullOwnPtr<ScopeNode> alternate)
  140. : m_predicate(move(predicate))
  141. , m_consequent(move(consequent))
  142. , m_alternate(move(alternate))
  143. {
  144. }
  145. const Expression& predicate() const { return *m_predicate; }
  146. const ScopeNode& consequent() const { return *m_consequent; }
  147. const ScopeNode& alternate() const { return *m_alternate; }
  148. virtual Value execute(Interpreter&) const override;
  149. virtual void dump(int indent) const override;
  150. private:
  151. virtual const char* class_name() const override { return "IfStatement"; }
  152. NonnullOwnPtr<Expression> m_predicate;
  153. NonnullOwnPtr<ScopeNode> m_consequent;
  154. NonnullOwnPtr<ScopeNode> m_alternate;
  155. };
  156. class WhileStatement : public Statement {
  157. public:
  158. WhileStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> body)
  159. : m_predicate(move(predicate))
  160. , m_body(move(body))
  161. {
  162. }
  163. const Expression& predicate() const { return *m_predicate; }
  164. const ScopeNode& body() const { return *m_body; }
  165. virtual Value execute(Interpreter&) const override;
  166. virtual void dump(int indent) const override;
  167. private:
  168. virtual const char* class_name() const override { return "WhileStatement"; }
  169. NonnullOwnPtr<Expression> m_predicate;
  170. NonnullOwnPtr<ScopeNode> m_body;
  171. };
  172. enum class BinaryOp {
  173. Plus,
  174. Minus,
  175. Asterisk,
  176. Slash,
  177. TypedEquals,
  178. TypedInequals,
  179. GreaterThan,
  180. LessThan,
  181. BitwiseAnd,
  182. BitwiseOr,
  183. BitwiseXor,
  184. LeftShift,
  185. RightShift,
  186. };
  187. class BinaryExpression : public Expression {
  188. public:
  189. BinaryExpression(BinaryOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  190. : m_op(op)
  191. , m_lhs(move(lhs))
  192. , m_rhs(move(rhs))
  193. {
  194. }
  195. virtual Value execute(Interpreter&) const override;
  196. virtual void dump(int indent) const override;
  197. private:
  198. virtual const char* class_name() const override { return "BinaryExpression"; }
  199. BinaryOp m_op;
  200. NonnullOwnPtr<Expression> m_lhs;
  201. NonnullOwnPtr<Expression> m_rhs;
  202. };
  203. enum class LogicalOp {
  204. And,
  205. Or,
  206. };
  207. class LogicalExpression : public Expression {
  208. public:
  209. LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  210. : m_op(op)
  211. , m_lhs(move(lhs))
  212. , m_rhs(move(rhs))
  213. {
  214. }
  215. virtual Value execute(Interpreter&) const override;
  216. virtual void dump(int indent) const override;
  217. private:
  218. virtual const char* class_name() const override { return "LogicalExpression"; }
  219. LogicalOp m_op;
  220. NonnullOwnPtr<Expression> m_lhs;
  221. NonnullOwnPtr<Expression> m_rhs;
  222. };
  223. enum class UnaryOp {
  224. BitNot,
  225. Not,
  226. };
  227. class UnaryExpression : public Expression {
  228. public:
  229. UnaryExpression(UnaryOp op, NonnullOwnPtr<Expression> lhs)
  230. : m_op(op)
  231. , m_lhs(move(lhs))
  232. {
  233. }
  234. virtual Value execute(Interpreter&) const override;
  235. virtual void dump(int indent) const override;
  236. private:
  237. virtual const char* class_name() const override { return "UnaryExpression"; }
  238. UnaryOp m_op;
  239. NonnullOwnPtr<Expression> m_lhs;
  240. };
  241. class Literal : public Expression {
  242. protected:
  243. explicit Literal() {}
  244. };
  245. class BooleanLiteral final : public Literal {
  246. public:
  247. explicit BooleanLiteral(bool value)
  248. : m_value(value)
  249. {
  250. }
  251. virtual Value execute(Interpreter&) const override;
  252. virtual void dump(int indent) const override;
  253. private:
  254. virtual const char* class_name() const override { return "BooleanLiteral"; }
  255. bool m_value { false };
  256. };
  257. class NumericLiteral final : public Literal {
  258. public:
  259. explicit NumericLiteral(double value)
  260. : m_value(value)
  261. {
  262. }
  263. virtual Value execute(Interpreter&) const override;
  264. virtual void dump(int indent) const override;
  265. private:
  266. virtual const char* class_name() const override { return "NumericLiteral"; }
  267. double m_value { 0 };
  268. };
  269. class StringLiteral final : public Literal {
  270. public:
  271. explicit StringLiteral(String value)
  272. : m_value(move(value))
  273. {
  274. }
  275. virtual Value execute(Interpreter&) const override;
  276. virtual void dump(int indent) const override;
  277. private:
  278. virtual const char* class_name() const override { return "StringLiteral"; }
  279. String m_value;
  280. };
  281. class Identifier final : public Expression {
  282. public:
  283. explicit Identifier(String string)
  284. : m_string(move(string))
  285. {
  286. }
  287. const String& string() const { return m_string; }
  288. virtual Value execute(Interpreter&) const override;
  289. virtual void dump(int indent) const override;
  290. virtual bool is_identifier() const override { return true; }
  291. private:
  292. virtual const char* class_name() const override { return "Identifier"; }
  293. String m_string;
  294. };
  295. class CallExpression : public Expression {
  296. public:
  297. explicit CallExpression(String name, NonnullOwnPtrVector<Expression> arguments = {})
  298. : m_name(move(name))
  299. , m_arguments(move(arguments))
  300. {
  301. }
  302. virtual Value execute(Interpreter&) const override;
  303. virtual void dump(int indent) const override;
  304. const String& name() const { return m_name; }
  305. private:
  306. virtual const char* class_name() const override { return "CallExpression"; }
  307. String m_name;
  308. const NonnullOwnPtrVector<Expression> m_arguments;
  309. };
  310. enum class AssignmentOp {
  311. Assign,
  312. };
  313. class AssignmentExpression : public Expression {
  314. public:
  315. AssignmentExpression(AssignmentOp op, NonnullOwnPtr<ASTNode> lhs, NonnullOwnPtr<Expression> rhs)
  316. : m_op(op)
  317. , m_lhs(move(lhs))
  318. , m_rhs(move(rhs))
  319. {
  320. }
  321. virtual Value execute(Interpreter&) const override;
  322. virtual void dump(int indent) const override;
  323. private:
  324. virtual const char* class_name() const override { return "AssignmentExpression"; }
  325. AssignmentOp m_op;
  326. NonnullOwnPtr<ASTNode> m_lhs;
  327. NonnullOwnPtr<Expression> m_rhs;
  328. };
  329. enum class DeclarationType {
  330. Var,
  331. Let,
  332. };
  333. class VariableDeclaration : public Statement {
  334. public:
  335. VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
  336. : m_declaration_type(declaration_type)
  337. , m_name(move(name))
  338. , m_initializer(move(initializer))
  339. {
  340. }
  341. const Identifier& name() const { return *m_name; }
  342. virtual Value execute(Interpreter&) const override;
  343. virtual void dump(int indent) const override;
  344. private:
  345. virtual const char* class_name() const override { return "VariableDeclaration"; }
  346. DeclarationType m_declaration_type;
  347. NonnullOwnPtr<Identifier> m_name;
  348. OwnPtr<Expression> m_initializer;
  349. };
  350. class ObjectExpression : public Expression {
  351. public:
  352. ObjectExpression() {}
  353. virtual Value execute(Interpreter&) const override;
  354. virtual void dump(int indent) const override;
  355. private:
  356. virtual const char* class_name() const override { return "ObjectExpression"; }
  357. };
  358. class MemberExpression final : public Expression {
  359. public:
  360. MemberExpression(NonnullOwnPtr<Expression> object, NonnullOwnPtr<Expression> property)
  361. : m_object(move(object))
  362. , m_property(move(property))
  363. {
  364. }
  365. virtual Value execute(Interpreter&) const override;
  366. virtual void dump(int indent) const override;
  367. private:
  368. virtual const char* class_name() const override { return "MemberExpression"; }
  369. NonnullOwnPtr<Expression> m_object;
  370. NonnullOwnPtr<Expression> m_property;
  371. };
  372. }