AST.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/FlyString.h>
  29. #include <AK/HashMap.h>
  30. #include <AK/NonnullRefPtrVector.h>
  31. #include <AK/RefPtr.h>
  32. #include <AK/String.h>
  33. #include <AK/Vector.h>
  34. #include <LibJS/Forward.h>
  35. #include <LibJS/Runtime/PropertyName.h>
  36. #include <LibJS/Runtime/Value.h>
  37. namespace JS {
  38. class VariableDeclaration;
  39. template<class T, class... Args>
  40. static inline NonnullRefPtr<T>
  41. create_ast_node(Args&&... args)
  42. {
  43. return adopt(*new T(forward<Args>(args)...));
  44. }
  45. class ASTNode : public RefCounted<ASTNode> {
  46. public:
  47. virtual ~ASTNode() {}
  48. virtual const char* class_name() const = 0;
  49. virtual Value execute(Interpreter&) const = 0;
  50. virtual void dump(int indent) const;
  51. virtual bool is_identifier() const { return false; }
  52. virtual bool is_spread_expression() const { return false; }
  53. virtual bool is_member_expression() const { return false; }
  54. virtual bool is_scope_node() const { return false; }
  55. virtual bool is_program() const { return false; }
  56. virtual bool is_variable_declaration() const { return false; }
  57. virtual bool is_new_expression() const { return false; }
  58. protected:
  59. ASTNode() {}
  60. private:
  61. };
  62. class Statement : public ASTNode {
  63. };
  64. class ErrorStatement final : public Statement {
  65. public:
  66. Value execute(Interpreter&) const override { return js_undefined(); }
  67. const char* class_name() const override { return "ErrorStatement"; }
  68. };
  69. class ExpressionStatement final : public Statement {
  70. public:
  71. ExpressionStatement(NonnullRefPtr<Expression> expression)
  72. : m_expression(move(expression))
  73. {
  74. }
  75. Value execute(Interpreter&) const override;
  76. const char* class_name() const override { return "ExpressionStatement"; }
  77. virtual void dump(int indent) const override;
  78. private:
  79. NonnullRefPtr<Expression> m_expression;
  80. };
  81. class ScopeNode : public Statement {
  82. public:
  83. template<typename T, typename... Args>
  84. T& append(Args&&... args)
  85. {
  86. auto child = create_ast_node<T>(forward<Args>(args)...);
  87. m_children.append(move(child));
  88. return static_cast<T&>(m_children.last());
  89. }
  90. void append(NonnullRefPtr<Statement> child)
  91. {
  92. m_children.append(move(child));
  93. }
  94. const NonnullRefPtrVector<Statement>& children() const { return m_children; }
  95. virtual Value execute(Interpreter&) const override;
  96. virtual void dump(int indent) const override;
  97. void add_variables(NonnullRefPtrVector<VariableDeclaration>);
  98. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  99. protected:
  100. ScopeNode() {}
  101. private:
  102. virtual bool is_scope_node() const final { return true; }
  103. NonnullRefPtrVector<Statement> m_children;
  104. NonnullRefPtrVector<VariableDeclaration> m_variables;
  105. };
  106. class Program : public ScopeNode {
  107. public:
  108. Program() {}
  109. private:
  110. virtual bool is_program() const override { return true; }
  111. virtual const char* class_name() const override { return "Program"; }
  112. };
  113. class BlockStatement : public ScopeNode {
  114. public:
  115. BlockStatement() {}
  116. private:
  117. virtual const char* class_name() const override { return "BlockStatement"; }
  118. };
  119. class Expression : public ASTNode {
  120. public:
  121. virtual Reference to_reference(Interpreter&) const;
  122. };
  123. class Declaration : public Statement {
  124. };
  125. class FunctionNode {
  126. public:
  127. struct Parameter {
  128. FlyString name;
  129. RefPtr<Expression> default_value;
  130. };
  131. const FlyString& name() const { return m_name; }
  132. const Statement& body() const { return *m_body; }
  133. const Vector<Parameter>& parameters() const { return m_parameters; };
  134. protected:
  135. FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  136. : m_name(name)
  137. , m_body(move(body))
  138. , m_parameters(move(parameters))
  139. , m_variables(move(variables))
  140. {
  141. }
  142. void dump(int indent, const char* class_name) const;
  143. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  144. private:
  145. FlyString m_name;
  146. NonnullRefPtr<Statement> m_body;
  147. const Vector<Parameter> m_parameters;
  148. NonnullRefPtrVector<VariableDeclaration> m_variables;
  149. };
  150. class FunctionDeclaration final
  151. : public Declaration
  152. , public FunctionNode {
  153. public:
  154. static bool must_have_name() { return true; }
  155. FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  156. : FunctionNode(name, move(body), move(parameters), move(variables))
  157. {
  158. }
  159. virtual Value execute(Interpreter&) const override;
  160. virtual void dump(int indent) const override;
  161. private:
  162. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  163. };
  164. class FunctionExpression final : public Expression
  165. , public FunctionNode {
  166. public:
  167. static bool must_have_name() { return false; }
  168. FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  169. : FunctionNode(name, move(body), move(parameters), move(variables))
  170. {
  171. }
  172. virtual Value execute(Interpreter&) const override;
  173. virtual void dump(int indent) const override;
  174. private:
  175. virtual const char* class_name() const override { return "FunctionExpression"; }
  176. };
  177. class ErrorExpression final : public Expression {
  178. public:
  179. Value execute(Interpreter&) const override { return js_undefined(); }
  180. const char* class_name() const override { return "ErrorExpression"; }
  181. };
  182. class ReturnStatement : public Statement {
  183. public:
  184. explicit ReturnStatement(RefPtr<Expression> argument)
  185. : m_argument(move(argument))
  186. {
  187. }
  188. const Expression* argument() const { return m_argument; }
  189. virtual Value execute(Interpreter&) const override;
  190. virtual void dump(int indent) const override;
  191. private:
  192. virtual const char* class_name() const override { return "ReturnStatement"; }
  193. RefPtr<Expression> m_argument;
  194. };
  195. class IfStatement : public Statement {
  196. public:
  197. IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  198. : m_predicate(move(predicate))
  199. , m_consequent(move(consequent))
  200. , m_alternate(move(alternate))
  201. {
  202. }
  203. const Expression& predicate() const { return *m_predicate; }
  204. const Statement& consequent() const { return *m_consequent; }
  205. const Statement* alternate() const { return m_alternate; }
  206. virtual Value execute(Interpreter&) const override;
  207. virtual void dump(int indent) const override;
  208. private:
  209. virtual const char* class_name() const override { return "IfStatement"; }
  210. NonnullRefPtr<Expression> m_predicate;
  211. NonnullRefPtr<Statement> m_consequent;
  212. RefPtr<Statement> m_alternate;
  213. };
  214. class WhileStatement : public Statement {
  215. public:
  216. WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  217. : m_test(move(test))
  218. , m_body(move(body))
  219. {
  220. }
  221. const Expression& test() const { return *m_test; }
  222. const Statement& body() const { return *m_body; }
  223. virtual Value execute(Interpreter&) const override;
  224. virtual void dump(int indent) const override;
  225. private:
  226. virtual const char* class_name() const override { return "WhileStatement"; }
  227. NonnullRefPtr<Expression> m_test;
  228. NonnullRefPtr<Statement> m_body;
  229. };
  230. class DoWhileStatement : public Statement {
  231. public:
  232. DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  233. : m_test(move(test))
  234. , m_body(move(body))
  235. {
  236. }
  237. const Expression& test() const { return *m_test; }
  238. const Statement& body() const { return *m_body; }
  239. virtual Value execute(Interpreter&) const override;
  240. virtual void dump(int indent) const override;
  241. private:
  242. virtual const char* class_name() const override { return "DoWhileStatement"; }
  243. NonnullRefPtr<Expression> m_test;
  244. NonnullRefPtr<Statement> m_body;
  245. };
  246. class ForStatement : public Statement {
  247. public:
  248. ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  249. : m_init(move(init))
  250. , m_test(move(test))
  251. , m_update(move(update))
  252. , m_body(move(body))
  253. {
  254. }
  255. const ASTNode* init() const { return m_init; }
  256. const Expression* test() const { return m_test; }
  257. const Expression* update() const { return m_update; }
  258. const Statement& body() const { return *m_body; }
  259. virtual Value execute(Interpreter&) const override;
  260. virtual void dump(int indent) const override;
  261. private:
  262. virtual const char* class_name() const override { return "ForStatement"; }
  263. RefPtr<ASTNode> m_init;
  264. RefPtr<Expression> m_test;
  265. RefPtr<Expression> m_update;
  266. NonnullRefPtr<Statement> m_body;
  267. };
  268. enum class BinaryOp {
  269. Addition,
  270. Subtraction,
  271. Multiplication,
  272. Division,
  273. Modulo,
  274. Exponentiation,
  275. TypedEquals,
  276. TypedInequals,
  277. AbstractEquals,
  278. AbstractInequals,
  279. GreaterThan,
  280. GreaterThanEquals,
  281. LessThan,
  282. LessThanEquals,
  283. BitwiseAnd,
  284. BitwiseOr,
  285. BitwiseXor,
  286. LeftShift,
  287. RightShift,
  288. UnsignedRightShift,
  289. In,
  290. InstanceOf,
  291. };
  292. class BinaryExpression : public Expression {
  293. public:
  294. BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  295. : m_op(op)
  296. , m_lhs(move(lhs))
  297. , m_rhs(move(rhs))
  298. {
  299. }
  300. virtual Value execute(Interpreter&) const override;
  301. virtual void dump(int indent) const override;
  302. private:
  303. virtual const char* class_name() const override { return "BinaryExpression"; }
  304. BinaryOp m_op;
  305. NonnullRefPtr<Expression> m_lhs;
  306. NonnullRefPtr<Expression> m_rhs;
  307. };
  308. enum class LogicalOp {
  309. And,
  310. Or,
  311. NullishCoalescing,
  312. };
  313. class LogicalExpression : public Expression {
  314. public:
  315. LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<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 "LogicalExpression"; }
  325. LogicalOp m_op;
  326. NonnullRefPtr<Expression> m_lhs;
  327. NonnullRefPtr<Expression> m_rhs;
  328. };
  329. enum class UnaryOp {
  330. BitwiseNot,
  331. Not,
  332. Plus,
  333. Minus,
  334. Typeof,
  335. Void,
  336. Delete,
  337. };
  338. class UnaryExpression : public Expression {
  339. public:
  340. UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
  341. : m_op(op)
  342. , m_lhs(move(lhs))
  343. {
  344. }
  345. virtual Value execute(Interpreter&) const override;
  346. virtual void dump(int indent) const override;
  347. private:
  348. virtual const char* class_name() const override { return "UnaryExpression"; }
  349. UnaryOp m_op;
  350. NonnullRefPtr<Expression> m_lhs;
  351. };
  352. class SequenceExpression final : public Expression {
  353. public:
  354. SequenceExpression(NonnullRefPtrVector<Expression> expressions)
  355. : m_expressions(move(expressions))
  356. {
  357. }
  358. virtual void dump(int indent) const override;
  359. virtual Value execute(Interpreter&) const override;
  360. private:
  361. virtual const char* class_name() const override { return "SequenceExpression"; }
  362. NonnullRefPtrVector<Expression> m_expressions;
  363. };
  364. class Literal : public Expression {
  365. protected:
  366. explicit Literal() {}
  367. };
  368. class BooleanLiteral final : public Literal {
  369. public:
  370. explicit BooleanLiteral(bool value)
  371. : m_value(value)
  372. {
  373. }
  374. virtual Value execute(Interpreter&) const override;
  375. virtual void dump(int indent) const override;
  376. private:
  377. virtual const char* class_name() const override { return "BooleanLiteral"; }
  378. bool m_value { false };
  379. };
  380. class NumericLiteral final : public Literal {
  381. public:
  382. explicit NumericLiteral(double value)
  383. : m_value(value)
  384. {
  385. }
  386. virtual Value execute(Interpreter&) const override;
  387. virtual void dump(int indent) const override;
  388. private:
  389. virtual const char* class_name() const override { return "NumericLiteral"; }
  390. double m_value { 0 };
  391. };
  392. class StringLiteral final : public Literal {
  393. public:
  394. explicit StringLiteral(String value)
  395. : m_value(move(value))
  396. {
  397. }
  398. virtual Value execute(Interpreter&) const override;
  399. virtual void dump(int indent) const override;
  400. private:
  401. virtual const char* class_name() const override { return "StringLiteral"; }
  402. String m_value;
  403. };
  404. class NullLiteral final : public Literal {
  405. public:
  406. explicit NullLiteral() {}
  407. virtual Value execute(Interpreter&) const override;
  408. virtual void dump(int indent) const override;
  409. private:
  410. virtual const char* class_name() const override { return "NullLiteral"; }
  411. };
  412. class Identifier final : public Expression {
  413. public:
  414. explicit Identifier(const FlyString& string)
  415. : m_string(string)
  416. {
  417. }
  418. const FlyString& string() const { return m_string; }
  419. virtual Value execute(Interpreter&) const override;
  420. virtual void dump(int indent) const override;
  421. virtual bool is_identifier() const override { return true; }
  422. virtual Reference to_reference(Interpreter&) const override;
  423. private:
  424. virtual const char* class_name() const override { return "Identifier"; }
  425. FlyString m_string;
  426. };
  427. class SpreadExpression final : public Expression {
  428. public:
  429. explicit SpreadExpression(NonnullRefPtr<Expression> target)
  430. : m_target(target)
  431. {
  432. }
  433. virtual Value execute(Interpreter&) const override;
  434. virtual void dump(int indent) const override;
  435. virtual bool is_spread_expression() const override { return true; }
  436. private:
  437. virtual const char* class_name() const override { return "SpreadExpression"; }
  438. NonnullRefPtr<Expression> m_target;
  439. };
  440. class ThisExpression final : public Expression {
  441. public:
  442. virtual Value execute(Interpreter&) const override;
  443. virtual void dump(int indent) const override;
  444. private:
  445. virtual const char* class_name() const override { return "ThisExpression"; }
  446. };
  447. class CallExpression : public Expression {
  448. public:
  449. CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
  450. : m_callee(move(callee))
  451. , m_arguments(move(arguments))
  452. {
  453. }
  454. virtual Value execute(Interpreter&) const override;
  455. virtual void dump(int indent) const override;
  456. private:
  457. virtual const char* class_name() const override { return "CallExpression"; }
  458. struct ThisAndCallee {
  459. Value this_value;
  460. Value callee;
  461. };
  462. ThisAndCallee compute_this_and_callee(Interpreter&) const;
  463. NonnullRefPtr<Expression> m_callee;
  464. const NonnullRefPtrVector<Expression> m_arguments;
  465. };
  466. class NewExpression final : public CallExpression {
  467. public:
  468. NewExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
  469. : CallExpression(move(callee), move(arguments))
  470. {
  471. }
  472. private:
  473. virtual const char* class_name() const override { return "NewExpression"; }
  474. virtual bool is_new_expression() const override { return true; }
  475. };
  476. enum class AssignmentOp {
  477. Assignment,
  478. AdditionAssignment,
  479. SubtractionAssignment,
  480. MultiplicationAssignment,
  481. DivisionAssignment,
  482. LeftShiftAssignment,
  483. RightShiftAssignment,
  484. UnsignedRightShiftAssignment,
  485. };
  486. class AssignmentExpression : public Expression {
  487. public:
  488. AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  489. : m_op(op)
  490. , m_lhs(move(lhs))
  491. , m_rhs(move(rhs))
  492. {
  493. }
  494. virtual Value execute(Interpreter&) const override;
  495. virtual void dump(int indent) const override;
  496. private:
  497. virtual const char* class_name() const override { return "AssignmentExpression"; }
  498. AssignmentOp m_op;
  499. NonnullRefPtr<Expression> m_lhs;
  500. NonnullRefPtr<Expression> m_rhs;
  501. };
  502. enum class UpdateOp {
  503. Increment,
  504. Decrement,
  505. };
  506. class UpdateExpression : public Expression {
  507. public:
  508. UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  509. : m_op(op)
  510. , m_argument(move(argument))
  511. , m_prefixed(prefixed)
  512. {
  513. }
  514. virtual Value execute(Interpreter&) const override;
  515. virtual void dump(int indent) const override;
  516. private:
  517. virtual const char* class_name() const override { return "UpdateExpression"; }
  518. UpdateOp m_op;
  519. NonnullRefPtr<Expression> m_argument;
  520. bool m_prefixed;
  521. };
  522. enum class DeclarationKind {
  523. Var,
  524. Let,
  525. Const,
  526. };
  527. class VariableDeclarator final : public ASTNode {
  528. public:
  529. VariableDeclarator(NonnullRefPtr<Identifier> id, RefPtr<Expression> init)
  530. : m_id(move(id))
  531. , m_init(move(init))
  532. {
  533. }
  534. const Identifier& id() const { return m_id; }
  535. const Expression* init() const { return m_init; }
  536. virtual Value execute(Interpreter&) const override;
  537. virtual void dump(int indent) const override;
  538. private:
  539. virtual const char* class_name() const override { return "VariableDeclarator"; }
  540. NonnullRefPtr<Identifier> m_id;
  541. RefPtr<Expression> m_init;
  542. };
  543. class VariableDeclaration : public Declaration {
  544. public:
  545. VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  546. : m_declaration_kind(declaration_kind)
  547. , m_declarations(move(declarations))
  548. {
  549. }
  550. virtual bool is_variable_declaration() const override { return true; }
  551. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  552. virtual Value execute(Interpreter&) const override;
  553. virtual void dump(int indent) const override;
  554. const NonnullRefPtrVector<VariableDeclarator>& declarations() const { return m_declarations; }
  555. private:
  556. virtual const char* class_name() const override { return "VariableDeclaration"; }
  557. DeclarationKind m_declaration_kind;
  558. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  559. };
  560. class ObjectProperty final : public ASTNode {
  561. public:
  562. ObjectProperty(NonnullRefPtr<Expression> key, NonnullRefPtr<Expression> value)
  563. : m_key(move(key))
  564. , m_value(move(value))
  565. {
  566. }
  567. const Expression& key() const { return m_key; }
  568. const Expression& value() const { return m_value; }
  569. bool is_spread() const { return m_is_spread; }
  570. void set_is_spread() { m_is_spread = true; }
  571. virtual void dump(int indent) const override;
  572. virtual Value execute(Interpreter&) const override;
  573. private:
  574. virtual const char* class_name() const override { return "ObjectProperty"; }
  575. NonnullRefPtr<Expression> m_key;
  576. NonnullRefPtr<Expression> m_value;
  577. bool m_is_spread { false };
  578. };
  579. class ObjectExpression : public Expression {
  580. public:
  581. ObjectExpression(NonnullRefPtrVector<ObjectProperty> properties = {})
  582. : m_properties(move(properties))
  583. {
  584. }
  585. virtual Value execute(Interpreter&) const override;
  586. virtual void dump(int indent) const override;
  587. private:
  588. virtual const char* class_name() const override { return "ObjectExpression"; }
  589. NonnullRefPtrVector<ObjectProperty> m_properties;
  590. };
  591. class ArrayExpression : public Expression {
  592. public:
  593. ArrayExpression(Vector<RefPtr<Expression>> elements)
  594. : m_elements(move(elements))
  595. {
  596. }
  597. const Vector<RefPtr<Expression>>& elements() const { return m_elements; }
  598. virtual Value execute(Interpreter&) const override;
  599. virtual void dump(int indent) const override;
  600. private:
  601. virtual const char* class_name() const override { return "ArrayExpression"; }
  602. Vector<RefPtr<Expression>> m_elements;
  603. };
  604. class MemberExpression final : public Expression {
  605. public:
  606. MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  607. : m_object(move(object))
  608. , m_property(move(property))
  609. , m_computed(computed)
  610. {
  611. }
  612. virtual Value execute(Interpreter&) const override;
  613. virtual void dump(int indent) const override;
  614. virtual Reference to_reference(Interpreter&) const override;
  615. bool is_computed() const { return m_computed; }
  616. const Expression& object() const { return *m_object; }
  617. const Expression& property() const { return *m_property; }
  618. PropertyName computed_property_name(Interpreter&) const;
  619. String to_string_approximation() const;
  620. private:
  621. virtual bool is_member_expression() const override { return true; }
  622. virtual const char* class_name() const override { return "MemberExpression"; }
  623. NonnullRefPtr<Expression> m_object;
  624. NonnullRefPtr<Expression> m_property;
  625. bool m_computed { false };
  626. };
  627. class ConditionalExpression final : public Expression {
  628. public:
  629. ConditionalExpression(NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  630. : m_test(move(test))
  631. , m_consequent(move(consequent))
  632. , m_alternate(move(alternate))
  633. {
  634. }
  635. virtual void dump(int indent) const override;
  636. virtual Value execute(Interpreter&) const override;
  637. private:
  638. virtual const char* class_name() const override { return "ConditionalExpression"; }
  639. NonnullRefPtr<Expression> m_test;
  640. NonnullRefPtr<Expression> m_consequent;
  641. NonnullRefPtr<Expression> m_alternate;
  642. };
  643. class CatchClause final : public ASTNode {
  644. public:
  645. CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body)
  646. : m_parameter(parameter)
  647. , m_body(move(body))
  648. {
  649. }
  650. const FlyString& parameter() const { return m_parameter; }
  651. const BlockStatement& body() const { return m_body; }
  652. virtual void dump(int indent) const override;
  653. virtual Value execute(Interpreter&) const override;
  654. private:
  655. virtual const char* class_name() const override { return "CatchClause"; }
  656. FlyString m_parameter;
  657. NonnullRefPtr<BlockStatement> m_body;
  658. };
  659. class TryStatement final : public Statement {
  660. public:
  661. TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  662. : m_block(move(block))
  663. , m_handler(move(handler))
  664. , m_finalizer(move(finalizer))
  665. {
  666. }
  667. const BlockStatement& block() const { return m_block; }
  668. const CatchClause* handler() const { return m_handler; }
  669. const BlockStatement* finalizer() const { return m_finalizer; }
  670. virtual void dump(int indent) const override;
  671. virtual Value execute(Interpreter&) const override;
  672. private:
  673. virtual const char* class_name() const override { return "TryStatement"; }
  674. NonnullRefPtr<BlockStatement> m_block;
  675. RefPtr<CatchClause> m_handler;
  676. RefPtr<BlockStatement> m_finalizer;
  677. };
  678. class ThrowStatement final : public Statement {
  679. public:
  680. explicit ThrowStatement(NonnullRefPtr<Expression> argument)
  681. : m_argument(move(argument))
  682. {
  683. }
  684. const Expression& argument() const { return m_argument; }
  685. virtual void dump(int indent) const override;
  686. virtual Value execute(Interpreter&) const override;
  687. private:
  688. virtual const char* class_name() const override { return "ThrowStatement"; }
  689. NonnullRefPtr<Expression> m_argument;
  690. };
  691. class SwitchCase final : public ASTNode {
  692. public:
  693. SwitchCase(RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent)
  694. : m_test(move(test))
  695. , m_consequent(move(consequent))
  696. {
  697. }
  698. const Expression* test() const { return m_test; }
  699. const NonnullRefPtrVector<Statement>& consequent() const { return m_consequent; }
  700. virtual void dump(int indent) const override;
  701. virtual Value execute(Interpreter&) const override;
  702. private:
  703. virtual const char* class_name() const override { return "SwitchCase"; }
  704. RefPtr<Expression> m_test;
  705. NonnullRefPtrVector<Statement> m_consequent;
  706. };
  707. class SwitchStatement final : public Statement {
  708. public:
  709. SwitchStatement(NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases)
  710. : m_discriminant(move(discriminant))
  711. , m_cases(move(cases))
  712. {
  713. }
  714. virtual void dump(int indent) const override;
  715. virtual Value execute(Interpreter&) const override;
  716. private:
  717. virtual const char* class_name() const override { return "SwitchStatement"; }
  718. NonnullRefPtr<Expression> m_discriminant;
  719. NonnullRefPtrVector<SwitchCase> m_cases;
  720. };
  721. class BreakStatement final : public Statement {
  722. public:
  723. BreakStatement() {}
  724. virtual Value execute(Interpreter&) const override;
  725. private:
  726. virtual const char* class_name() const override { return "BreakStatement"; }
  727. };
  728. class ContinueStatement final : public Statement {
  729. public:
  730. ContinueStatement() {}
  731. virtual Value execute(Interpreter&) const override;
  732. private:
  733. virtual const char* class_name() const override { return "ContinueStatement"; }
  734. };
  735. class DebuggerStatement final : public Statement {
  736. public:
  737. DebuggerStatement() {}
  738. virtual Value execute(Interpreter&) const override;
  739. private:
  740. virtual const char* class_name() const override { return "DebuggerStatement"; }
  741. };
  742. }