AST.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. const FlyString& name() const { return m_name; }
  128. const Statement& body() const { return *m_body; }
  129. const Vector<FlyString>& parameters() const { return m_parameters; };
  130. protected:
  131. FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  132. : m_name(name)
  133. , m_body(move(body))
  134. , m_parameters(move(parameters))
  135. , m_variables(move(variables))
  136. {
  137. }
  138. void dump(int indent, const char* class_name) const;
  139. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  140. private:
  141. FlyString m_name;
  142. NonnullRefPtr<Statement> m_body;
  143. const Vector<FlyString> m_parameters;
  144. NonnullRefPtrVector<VariableDeclaration> m_variables;
  145. };
  146. class FunctionDeclaration final
  147. : public Declaration
  148. , public FunctionNode {
  149. public:
  150. static bool must_have_name() { return true; }
  151. FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  152. : FunctionNode(name, move(body), move(parameters), move(variables))
  153. {
  154. }
  155. virtual Value execute(Interpreter&) const override;
  156. virtual void dump(int indent) const override;
  157. private:
  158. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  159. };
  160. class FunctionExpression final : public Expression
  161. , public FunctionNode {
  162. public:
  163. static bool must_have_name() { return false; }
  164. FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters, NonnullRefPtrVector<VariableDeclaration> variables)
  165. : FunctionNode(name, move(body), move(parameters), move(variables))
  166. {
  167. }
  168. virtual Value execute(Interpreter&) const override;
  169. virtual void dump(int indent) const override;
  170. private:
  171. virtual const char* class_name() const override { return "FunctionExpression"; }
  172. };
  173. class ErrorExpression final : public Expression {
  174. public:
  175. Value execute(Interpreter&) const override { return js_undefined(); }
  176. const char* class_name() const override { return "ErrorExpression"; }
  177. };
  178. class ReturnStatement : public Statement {
  179. public:
  180. explicit ReturnStatement(RefPtr<Expression> argument)
  181. : m_argument(move(argument))
  182. {
  183. }
  184. const Expression* argument() const { return m_argument; }
  185. virtual Value execute(Interpreter&) const override;
  186. virtual void dump(int indent) const override;
  187. private:
  188. virtual const char* class_name() const override { return "ReturnStatement"; }
  189. RefPtr<Expression> m_argument;
  190. };
  191. class IfStatement : public Statement {
  192. public:
  193. IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  194. : m_predicate(move(predicate))
  195. , m_consequent(move(consequent))
  196. , m_alternate(move(alternate))
  197. {
  198. }
  199. const Expression& predicate() const { return *m_predicate; }
  200. const Statement& consequent() const { return *m_consequent; }
  201. const Statement* alternate() const { return m_alternate; }
  202. virtual Value execute(Interpreter&) const override;
  203. virtual void dump(int indent) const override;
  204. private:
  205. virtual const char* class_name() const override { return "IfStatement"; }
  206. NonnullRefPtr<Expression> m_predicate;
  207. NonnullRefPtr<Statement> m_consequent;
  208. RefPtr<Statement> m_alternate;
  209. };
  210. class WhileStatement : public Statement {
  211. public:
  212. WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  213. : m_test(move(test))
  214. , m_body(move(body))
  215. {
  216. }
  217. const Expression& test() const { return *m_test; }
  218. const Statement& body() const { return *m_body; }
  219. virtual Value execute(Interpreter&) const override;
  220. virtual void dump(int indent) const override;
  221. private:
  222. virtual const char* class_name() const override { return "WhileStatement"; }
  223. NonnullRefPtr<Expression> m_test;
  224. NonnullRefPtr<Statement> m_body;
  225. };
  226. class DoWhileStatement : public Statement {
  227. public:
  228. DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  229. : m_test(move(test))
  230. , m_body(move(body))
  231. {
  232. }
  233. const Expression& test() const { return *m_test; }
  234. const Statement& body() const { return *m_body; }
  235. virtual Value execute(Interpreter&) const override;
  236. virtual void dump(int indent) const override;
  237. private:
  238. virtual const char* class_name() const override { return "DoWhileStatement"; }
  239. NonnullRefPtr<Expression> m_test;
  240. NonnullRefPtr<Statement> m_body;
  241. };
  242. class ForStatement : public Statement {
  243. public:
  244. ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  245. : m_init(move(init))
  246. , m_test(move(test))
  247. , m_update(move(update))
  248. , m_body(move(body))
  249. {
  250. }
  251. const ASTNode* init() const { return m_init; }
  252. const Expression* test() const { return m_test; }
  253. const Expression* update() const { return m_update; }
  254. const Statement& body() const { return *m_body; }
  255. virtual Value execute(Interpreter&) const override;
  256. virtual void dump(int indent) const override;
  257. private:
  258. virtual const char* class_name() const override { return "ForStatement"; }
  259. RefPtr<ASTNode> m_init;
  260. RefPtr<Expression> m_test;
  261. RefPtr<Expression> m_update;
  262. NonnullRefPtr<Statement> m_body;
  263. };
  264. enum class BinaryOp {
  265. Addition,
  266. Subtraction,
  267. Multiplication,
  268. Division,
  269. Modulo,
  270. Exponentiation,
  271. TypedEquals,
  272. TypedInequals,
  273. AbstractEquals,
  274. AbstractInequals,
  275. GreaterThan,
  276. GreaterThanEquals,
  277. LessThan,
  278. LessThanEquals,
  279. BitwiseAnd,
  280. BitwiseOr,
  281. BitwiseXor,
  282. LeftShift,
  283. RightShift,
  284. UnsignedRightShift,
  285. In,
  286. InstanceOf,
  287. };
  288. class BinaryExpression : public Expression {
  289. public:
  290. BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  291. : m_op(op)
  292. , m_lhs(move(lhs))
  293. , m_rhs(move(rhs))
  294. {
  295. }
  296. virtual Value execute(Interpreter&) const override;
  297. virtual void dump(int indent) const override;
  298. private:
  299. virtual const char* class_name() const override { return "BinaryExpression"; }
  300. BinaryOp m_op;
  301. NonnullRefPtr<Expression> m_lhs;
  302. NonnullRefPtr<Expression> m_rhs;
  303. };
  304. enum class LogicalOp {
  305. And,
  306. Or,
  307. NullishCoalescing,
  308. };
  309. class LogicalExpression : public Expression {
  310. public:
  311. LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  312. : m_op(op)
  313. , m_lhs(move(lhs))
  314. , m_rhs(move(rhs))
  315. {
  316. }
  317. virtual Value execute(Interpreter&) const override;
  318. virtual void dump(int indent) const override;
  319. private:
  320. virtual const char* class_name() const override { return "LogicalExpression"; }
  321. LogicalOp m_op;
  322. NonnullRefPtr<Expression> m_lhs;
  323. NonnullRefPtr<Expression> m_rhs;
  324. };
  325. enum class UnaryOp {
  326. BitwiseNot,
  327. Not,
  328. Plus,
  329. Minus,
  330. Typeof,
  331. Void,
  332. Delete,
  333. };
  334. class UnaryExpression : public Expression {
  335. public:
  336. UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
  337. : m_op(op)
  338. , m_lhs(move(lhs))
  339. {
  340. }
  341. virtual Value execute(Interpreter&) const override;
  342. virtual void dump(int indent) const override;
  343. private:
  344. virtual const char* class_name() const override { return "UnaryExpression"; }
  345. UnaryOp m_op;
  346. NonnullRefPtr<Expression> m_lhs;
  347. };
  348. class SequenceExpression final : public Expression {
  349. public:
  350. SequenceExpression(NonnullRefPtrVector<Expression> expressions)
  351. : m_expressions(move(expressions))
  352. {
  353. }
  354. virtual void dump(int indent) const override;
  355. virtual Value execute(Interpreter&) const override;
  356. private:
  357. virtual const char* class_name() const override { return "SequenceExpression"; }
  358. NonnullRefPtrVector<Expression> m_expressions;
  359. };
  360. class Literal : public Expression {
  361. protected:
  362. explicit Literal() {}
  363. };
  364. class BooleanLiteral final : public Literal {
  365. public:
  366. explicit BooleanLiteral(bool value)
  367. : m_value(value)
  368. {
  369. }
  370. virtual Value execute(Interpreter&) const override;
  371. virtual void dump(int indent) const override;
  372. private:
  373. virtual const char* class_name() const override { return "BooleanLiteral"; }
  374. bool m_value { false };
  375. };
  376. class NumericLiteral final : public Literal {
  377. public:
  378. explicit NumericLiteral(double value)
  379. : m_value(value)
  380. {
  381. }
  382. virtual Value execute(Interpreter&) const override;
  383. virtual void dump(int indent) const override;
  384. private:
  385. virtual const char* class_name() const override { return "NumericLiteral"; }
  386. double m_value { 0 };
  387. };
  388. class StringLiteral final : public Literal {
  389. public:
  390. explicit StringLiteral(String value)
  391. : m_value(move(value))
  392. {
  393. }
  394. virtual Value execute(Interpreter&) const override;
  395. virtual void dump(int indent) const override;
  396. private:
  397. virtual const char* class_name() const override { return "StringLiteral"; }
  398. String m_value;
  399. };
  400. class NullLiteral final : public Literal {
  401. public:
  402. explicit NullLiteral() {}
  403. virtual Value execute(Interpreter&) const override;
  404. virtual void dump(int indent) const override;
  405. private:
  406. virtual const char* class_name() const override { return "NullLiteral"; }
  407. };
  408. class Identifier final : public Expression {
  409. public:
  410. explicit Identifier(const FlyString& string)
  411. : m_string(string)
  412. {
  413. }
  414. const FlyString& string() const { return m_string; }
  415. virtual Value execute(Interpreter&) const override;
  416. virtual void dump(int indent) const override;
  417. virtual bool is_identifier() const override { return true; }
  418. virtual Reference to_reference(Interpreter&) const override;
  419. private:
  420. virtual const char* class_name() const override { return "Identifier"; }
  421. FlyString m_string;
  422. };
  423. class SpreadExpression final : public Expression {
  424. public:
  425. explicit SpreadExpression(NonnullRefPtr<Expression> target)
  426. : m_target(target)
  427. {
  428. }
  429. virtual Value execute(Interpreter&) const override;
  430. virtual void dump(int indent) const override;
  431. virtual bool is_spread_expression() const override { return true; }
  432. private:
  433. virtual const char* class_name() const override { return "SpreadExpression"; }
  434. NonnullRefPtr<Expression> m_target;
  435. };
  436. class ThisExpression final : public Expression {
  437. public:
  438. virtual Value execute(Interpreter&) const override;
  439. virtual void dump(int indent) const override;
  440. private:
  441. virtual const char* class_name() const override { return "ThisExpression"; }
  442. };
  443. class CallExpression : public Expression {
  444. public:
  445. CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
  446. : m_callee(move(callee))
  447. , m_arguments(move(arguments))
  448. {
  449. }
  450. virtual Value execute(Interpreter&) const override;
  451. virtual void dump(int indent) const override;
  452. private:
  453. virtual const char* class_name() const override { return "CallExpression"; }
  454. struct ThisAndCallee {
  455. Value this_value;
  456. Value callee;
  457. };
  458. ThisAndCallee compute_this_and_callee(Interpreter&) const;
  459. NonnullRefPtr<Expression> m_callee;
  460. const NonnullRefPtrVector<Expression> m_arguments;
  461. };
  462. class NewExpression final : public CallExpression {
  463. public:
  464. NewExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
  465. : CallExpression(move(callee), move(arguments))
  466. {
  467. }
  468. private:
  469. virtual const char* class_name() const override { return "NewExpression"; }
  470. virtual bool is_new_expression() const override { return true; }
  471. };
  472. enum class AssignmentOp {
  473. Assignment,
  474. AdditionAssignment,
  475. SubtractionAssignment,
  476. MultiplicationAssignment,
  477. DivisionAssignment,
  478. LeftShiftAssignment,
  479. RightShiftAssignment,
  480. UnsignedRightShiftAssignment,
  481. };
  482. class AssignmentExpression : public Expression {
  483. public:
  484. AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  485. : m_op(op)
  486. , m_lhs(move(lhs))
  487. , m_rhs(move(rhs))
  488. {
  489. }
  490. virtual Value execute(Interpreter&) const override;
  491. virtual void dump(int indent) const override;
  492. private:
  493. virtual const char* class_name() const override { return "AssignmentExpression"; }
  494. AssignmentOp m_op;
  495. NonnullRefPtr<Expression> m_lhs;
  496. NonnullRefPtr<Expression> m_rhs;
  497. };
  498. enum class UpdateOp {
  499. Increment,
  500. Decrement,
  501. };
  502. class UpdateExpression : public Expression {
  503. public:
  504. UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  505. : m_op(op)
  506. , m_argument(move(argument))
  507. , m_prefixed(prefixed)
  508. {
  509. }
  510. virtual Value execute(Interpreter&) const override;
  511. virtual void dump(int indent) const override;
  512. private:
  513. virtual const char* class_name() const override { return "UpdateExpression"; }
  514. UpdateOp m_op;
  515. NonnullRefPtr<Expression> m_argument;
  516. bool m_prefixed;
  517. };
  518. enum class DeclarationKind {
  519. Var,
  520. Let,
  521. Const,
  522. };
  523. class VariableDeclarator final : public ASTNode {
  524. public:
  525. VariableDeclarator(NonnullRefPtr<Identifier> id, RefPtr<Expression> init)
  526. : m_id(move(id))
  527. , m_init(move(init))
  528. {
  529. }
  530. const Identifier& id() const { return m_id; }
  531. const Expression* init() const { return m_init; }
  532. virtual Value execute(Interpreter&) const override;
  533. virtual void dump(int indent) const override;
  534. private:
  535. virtual const char* class_name() const override { return "VariableDeclarator"; }
  536. NonnullRefPtr<Identifier> m_id;
  537. RefPtr<Expression> m_init;
  538. };
  539. class VariableDeclaration : public Declaration {
  540. public:
  541. VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  542. : m_declaration_kind(declaration_kind)
  543. , m_declarations(move(declarations))
  544. {
  545. }
  546. virtual bool is_variable_declaration() const override { return true; }
  547. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  548. virtual Value execute(Interpreter&) const override;
  549. virtual void dump(int indent) const override;
  550. const NonnullRefPtrVector<VariableDeclarator>& declarations() const { return m_declarations; }
  551. private:
  552. virtual const char* class_name() const override { return "VariableDeclaration"; }
  553. DeclarationKind m_declaration_kind;
  554. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  555. };
  556. class ObjectProperty final : public ASTNode {
  557. public:
  558. ObjectProperty(NonnullRefPtr<Expression> key, NonnullRefPtr<Expression> value)
  559. : m_key(move(key))
  560. , m_value(move(value))
  561. {
  562. }
  563. const Expression& key() const { return m_key; }
  564. const Expression& value() const { return m_value; }
  565. bool is_spread() const { return m_is_spread; }
  566. void set_is_spread() { m_is_spread = true; }
  567. virtual void dump(int indent) const override;
  568. virtual Value execute(Interpreter&) const override;
  569. private:
  570. virtual const char* class_name() const override { return "ObjectProperty"; }
  571. NonnullRefPtr<Expression> m_key;
  572. NonnullRefPtr<Expression> m_value;
  573. bool m_is_spread { false };
  574. };
  575. class ObjectExpression : public Expression {
  576. public:
  577. ObjectExpression(NonnullRefPtrVector<ObjectProperty> properties = {})
  578. : m_properties(move(properties))
  579. {
  580. }
  581. virtual Value execute(Interpreter&) const override;
  582. virtual void dump(int indent) const override;
  583. private:
  584. virtual const char* class_name() const override { return "ObjectExpression"; }
  585. NonnullRefPtrVector<ObjectProperty> m_properties;
  586. };
  587. class ArrayExpression : public Expression {
  588. public:
  589. ArrayExpression(Vector<RefPtr<Expression>> elements)
  590. : m_elements(move(elements))
  591. {
  592. }
  593. const Vector<RefPtr<Expression>>& elements() const { return m_elements; }
  594. virtual Value execute(Interpreter&) const override;
  595. virtual void dump(int indent) const override;
  596. private:
  597. virtual const char* class_name() const override { return "ArrayExpression"; }
  598. Vector<RefPtr<Expression>> m_elements;
  599. };
  600. class MemberExpression final : public Expression {
  601. public:
  602. MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  603. : m_object(move(object))
  604. , m_property(move(property))
  605. , m_computed(computed)
  606. {
  607. }
  608. virtual Value execute(Interpreter&) const override;
  609. virtual void dump(int indent) const override;
  610. virtual Reference to_reference(Interpreter&) const override;
  611. bool is_computed() const { return m_computed; }
  612. const Expression& object() const { return *m_object; }
  613. const Expression& property() const { return *m_property; }
  614. PropertyName computed_property_name(Interpreter&) const;
  615. String to_string_approximation() const;
  616. private:
  617. virtual bool is_member_expression() const override { return true; }
  618. virtual const char* class_name() const override { return "MemberExpression"; }
  619. NonnullRefPtr<Expression> m_object;
  620. NonnullRefPtr<Expression> m_property;
  621. bool m_computed { false };
  622. };
  623. class ConditionalExpression final : public Expression {
  624. public:
  625. ConditionalExpression(NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  626. : m_test(move(test))
  627. , m_consequent(move(consequent))
  628. , m_alternate(move(alternate))
  629. {
  630. }
  631. virtual void dump(int indent) const override;
  632. virtual Value execute(Interpreter&) const override;
  633. private:
  634. virtual const char* class_name() const override { return "ConditionalExpression"; }
  635. NonnullRefPtr<Expression> m_test;
  636. NonnullRefPtr<Expression> m_consequent;
  637. NonnullRefPtr<Expression> m_alternate;
  638. };
  639. class CatchClause final : public ASTNode {
  640. public:
  641. CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body)
  642. : m_parameter(parameter)
  643. , m_body(move(body))
  644. {
  645. }
  646. const FlyString& parameter() const { return m_parameter; }
  647. const BlockStatement& body() const { return m_body; }
  648. virtual void dump(int indent) const override;
  649. virtual Value execute(Interpreter&) const override;
  650. private:
  651. virtual const char* class_name() const override { return "CatchClause"; }
  652. FlyString m_parameter;
  653. NonnullRefPtr<BlockStatement> m_body;
  654. };
  655. class TryStatement final : public Statement {
  656. public:
  657. TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  658. : m_block(move(block))
  659. , m_handler(move(handler))
  660. , m_finalizer(move(finalizer))
  661. {
  662. }
  663. const BlockStatement& block() const { return m_block; }
  664. const CatchClause* handler() const { return m_handler; }
  665. const BlockStatement* finalizer() const { return m_finalizer; }
  666. virtual void dump(int indent) const override;
  667. virtual Value execute(Interpreter&) const override;
  668. private:
  669. virtual const char* class_name() const override { return "TryStatement"; }
  670. NonnullRefPtr<BlockStatement> m_block;
  671. RefPtr<CatchClause> m_handler;
  672. RefPtr<BlockStatement> m_finalizer;
  673. };
  674. class ThrowStatement final : public Statement {
  675. public:
  676. explicit ThrowStatement(NonnullRefPtr<Expression> argument)
  677. : m_argument(move(argument))
  678. {
  679. }
  680. const Expression& argument() const { return m_argument; }
  681. virtual void dump(int indent) const override;
  682. virtual Value execute(Interpreter&) const override;
  683. private:
  684. virtual const char* class_name() const override { return "ThrowStatement"; }
  685. NonnullRefPtr<Expression> m_argument;
  686. };
  687. class SwitchCase final : public ASTNode {
  688. public:
  689. SwitchCase(RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent)
  690. : m_test(move(test))
  691. , m_consequent(move(consequent))
  692. {
  693. }
  694. const Expression* test() const { return m_test; }
  695. const NonnullRefPtrVector<Statement>& consequent() const { return m_consequent; }
  696. virtual void dump(int indent) const override;
  697. virtual Value execute(Interpreter&) const override;
  698. private:
  699. virtual const char* class_name() const override { return "SwitchCase"; }
  700. RefPtr<Expression> m_test;
  701. NonnullRefPtrVector<Statement> m_consequent;
  702. };
  703. class SwitchStatement final : public Statement {
  704. public:
  705. SwitchStatement(NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases)
  706. : m_discriminant(move(discriminant))
  707. , m_cases(move(cases))
  708. {
  709. }
  710. virtual void dump(int indent) const override;
  711. virtual Value execute(Interpreter&) const override;
  712. private:
  713. virtual const char* class_name() const override { return "SwitchStatement"; }
  714. NonnullRefPtr<Expression> m_discriminant;
  715. NonnullRefPtrVector<SwitchCase> m_cases;
  716. };
  717. class BreakStatement final : public Statement {
  718. public:
  719. BreakStatement() {}
  720. virtual Value execute(Interpreter&) const override;
  721. private:
  722. virtual const char* class_name() const override { return "BreakStatement"; }
  723. };
  724. class ContinueStatement final : public Statement {
  725. public:
  726. ContinueStatement() {}
  727. virtual Value execute(Interpreter&) const override;
  728. private:
  729. virtual const char* class_name() const override { return "ContinueStatement"; }
  730. };
  731. }