AST.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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/FlyString.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/NonnullRefPtrVector.h>
  30. #include <AK/RefPtr.h>
  31. #include <AK/String.h>
  32. #include <AK/Vector.h>
  33. #include <LibJS/Forward.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. template<class T, class... Args>
  37. static inline NonnullRefPtr<T>
  38. create_ast_node(Args&&... args)
  39. {
  40. return adopt(*new T(forward<Args>(args)...));
  41. }
  42. class ASTNode : public RefCounted<ASTNode> {
  43. public:
  44. virtual ~ASTNode() {}
  45. virtual const char* class_name() const = 0;
  46. virtual Value execute(Interpreter&) const = 0;
  47. virtual void dump(int indent) const;
  48. virtual bool is_identifier() const { return false; }
  49. virtual bool is_member_expression() const { return false; }
  50. virtual bool is_scope_node() const { return false; }
  51. virtual bool is_variable_declaration() const { return false; }
  52. protected:
  53. ASTNode() {}
  54. private:
  55. };
  56. class Statement : public ASTNode {
  57. };
  58. class ErrorStatement final : public Statement {
  59. public:
  60. Value execute(Interpreter&) const override { return js_undefined(); }
  61. const char* class_name() const override { return "ErrorStatement"; }
  62. };
  63. class ExpressionStatement final : public Statement {
  64. public:
  65. ExpressionStatement(NonnullRefPtr<Expression> expression)
  66. : m_expression(move(expression))
  67. {
  68. }
  69. Value execute(Interpreter&) const override;
  70. const char* class_name() const override { return "ExpressionStatement"; }
  71. virtual void dump(int indent) const override;
  72. private:
  73. NonnullRefPtr<Expression> m_expression;
  74. };
  75. class ScopeNode : public Statement {
  76. public:
  77. template<typename T, typename... Args>
  78. T& append(Args&&... args)
  79. {
  80. auto child = create_ast_node<T>(forward<Args>(args)...);
  81. m_children.append(move(child));
  82. return static_cast<T&>(m_children.last());
  83. }
  84. void append(NonnullRefPtr<Statement> child)
  85. {
  86. m_children.append(move(child));
  87. }
  88. const NonnullRefPtrVector<Statement>& children() const { return m_children; }
  89. virtual Value execute(Interpreter&) const override;
  90. virtual void dump(int indent) const override;
  91. protected:
  92. ScopeNode() {}
  93. private:
  94. virtual bool is_scope_node() const final { return true; }
  95. NonnullRefPtrVector<Statement> m_children;
  96. };
  97. class Program : public ScopeNode {
  98. public:
  99. Program() {}
  100. private:
  101. virtual const char* class_name() const override { return "Program"; }
  102. };
  103. class BlockStatement : public ScopeNode {
  104. public:
  105. BlockStatement() {}
  106. private:
  107. virtual const char* class_name() const override { return "BlockStatement"; }
  108. };
  109. class Expression : public ASTNode {
  110. };
  111. class FunctionNode {
  112. public:
  113. const FlyString& name() const { return m_name; }
  114. const ScopeNode& body() const { return *m_body; }
  115. const Vector<FlyString>& parameters() const { return m_parameters; };
  116. protected:
  117. FunctionNode(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
  118. : m_name(name)
  119. , m_body(move(body))
  120. , m_parameters(move(parameters))
  121. {
  122. }
  123. void dump(int indent, const char* class_name) const;
  124. private:
  125. FlyString m_name;
  126. NonnullRefPtr<ScopeNode> m_body;
  127. const Vector<FlyString> m_parameters;
  128. };
  129. class FunctionDeclaration final
  130. : public Statement
  131. , public FunctionNode {
  132. public:
  133. static bool must_have_name() { return true; }
  134. FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
  135. : FunctionNode(move(name), move(body), move(parameters))
  136. {
  137. }
  138. virtual Value execute(Interpreter&) const override;
  139. virtual void dump(int indent) const override;
  140. private:
  141. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  142. };
  143. class FunctionExpression final : public Expression
  144. , public FunctionNode {
  145. public:
  146. static bool must_have_name() { return false; }
  147. FunctionExpression(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {})
  148. : FunctionNode(name, move(body), move(parameters))
  149. {
  150. }
  151. virtual Value execute(Interpreter&) const override;
  152. virtual void dump(int indent) const override;
  153. private:
  154. virtual const char* class_name() const override { return "FunctionExpression"; }
  155. };
  156. class ErrorExpression final : public Expression {
  157. public:
  158. Value execute(Interpreter&) const override { return js_undefined(); }
  159. const char* class_name() const override { return "ErrorExpression"; }
  160. };
  161. class ReturnStatement : public Statement {
  162. public:
  163. explicit ReturnStatement(RefPtr<Expression> argument)
  164. : m_argument(move(argument))
  165. {
  166. }
  167. const Expression* argument() const { return m_argument; }
  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 "ReturnStatement"; }
  172. RefPtr<Expression> m_argument;
  173. };
  174. class IfStatement : public Statement {
  175. public:
  176. IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  177. : m_predicate(move(predicate))
  178. , m_consequent(move(consequent))
  179. , m_alternate(move(alternate))
  180. {
  181. }
  182. const Expression& predicate() const { return *m_predicate; }
  183. const Statement& consequent() const { return *m_consequent; }
  184. const Statement* alternate() const { return m_alternate; }
  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 "IfStatement"; }
  189. NonnullRefPtr<Expression> m_predicate;
  190. NonnullRefPtr<Statement> m_consequent;
  191. RefPtr<Statement> m_alternate;
  192. };
  193. class WhileStatement : public Statement {
  194. public:
  195. WhileStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<ScopeNode> body)
  196. : m_predicate(move(predicate))
  197. , m_body(move(body))
  198. {
  199. }
  200. const Expression& predicate() const { return *m_predicate; }
  201. const ScopeNode& body() const { return *m_body; }
  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 "WhileStatement"; }
  206. NonnullRefPtr<Expression> m_predicate;
  207. NonnullRefPtr<ScopeNode> m_body;
  208. };
  209. class ForStatement : public Statement {
  210. public:
  211. ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<ScopeNode> body)
  212. : m_init(move(init))
  213. , m_test(move(test))
  214. , m_update(move(update))
  215. , m_body(move(body))
  216. {
  217. }
  218. const ASTNode* init() const { return m_init; }
  219. const Expression* test() const { return m_test; }
  220. const Expression* update() const { return m_update; }
  221. const ScopeNode& body() const { return *m_body; }
  222. virtual Value execute(Interpreter&) const override;
  223. virtual void dump(int indent) const override;
  224. private:
  225. virtual const char* class_name() const override { return "ForStatement"; }
  226. RefPtr<ASTNode> m_init;
  227. RefPtr<Expression> m_test;
  228. RefPtr<Expression> m_update;
  229. NonnullRefPtr<ScopeNode> m_body;
  230. };
  231. enum class BinaryOp {
  232. Plus,
  233. Minus,
  234. Asterisk,
  235. Slash,
  236. TypedEquals,
  237. TypedInequals,
  238. AbstractEquals,
  239. AbstractInequals,
  240. GreaterThan,
  241. GreaterThanEquals,
  242. LessThan,
  243. LessThanEquals,
  244. BitwiseAnd,
  245. BitwiseOr,
  246. BitwiseXor,
  247. LeftShift,
  248. RightShift,
  249. };
  250. class BinaryExpression : public Expression {
  251. public:
  252. BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  253. : m_op(op)
  254. , m_lhs(move(lhs))
  255. , m_rhs(move(rhs))
  256. {
  257. }
  258. virtual Value execute(Interpreter&) const override;
  259. virtual void dump(int indent) const override;
  260. private:
  261. virtual const char* class_name() const override { return "BinaryExpression"; }
  262. BinaryOp m_op;
  263. NonnullRefPtr<Expression> m_lhs;
  264. NonnullRefPtr<Expression> m_rhs;
  265. };
  266. enum class LogicalOp {
  267. And,
  268. Or,
  269. };
  270. class LogicalExpression : public Expression {
  271. public:
  272. LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  273. : m_op(op)
  274. , m_lhs(move(lhs))
  275. , m_rhs(move(rhs))
  276. {
  277. }
  278. virtual Value execute(Interpreter&) const override;
  279. virtual void dump(int indent) const override;
  280. private:
  281. virtual const char* class_name() const override { return "LogicalExpression"; }
  282. LogicalOp m_op;
  283. NonnullRefPtr<Expression> m_lhs;
  284. NonnullRefPtr<Expression> m_rhs;
  285. };
  286. enum class UnaryOp {
  287. BitwiseNot,
  288. Not,
  289. Typeof,
  290. };
  291. class UnaryExpression : public Expression {
  292. public:
  293. UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
  294. : m_op(op)
  295. , m_lhs(move(lhs))
  296. {
  297. }
  298. virtual Value execute(Interpreter&) const override;
  299. virtual void dump(int indent) const override;
  300. private:
  301. virtual const char* class_name() const override { return "UnaryExpression"; }
  302. UnaryOp m_op;
  303. NonnullRefPtr<Expression> m_lhs;
  304. };
  305. class Literal : public Expression {
  306. protected:
  307. explicit Literal() {}
  308. };
  309. class BooleanLiteral final : public Literal {
  310. public:
  311. explicit BooleanLiteral(bool value)
  312. : m_value(value)
  313. {
  314. }
  315. virtual Value execute(Interpreter&) const override;
  316. virtual void dump(int indent) const override;
  317. private:
  318. virtual const char* class_name() const override { return "BooleanLiteral"; }
  319. bool m_value { false };
  320. };
  321. class NumericLiteral final : public Literal {
  322. public:
  323. explicit NumericLiteral(double value)
  324. : m_value(value)
  325. {
  326. }
  327. virtual Value execute(Interpreter&) const override;
  328. virtual void dump(int indent) const override;
  329. private:
  330. virtual const char* class_name() const override { return "NumericLiteral"; }
  331. double m_value { 0 };
  332. };
  333. class StringLiteral final : public Literal {
  334. public:
  335. explicit StringLiteral(String value)
  336. : m_value(move(value))
  337. {
  338. }
  339. virtual Value execute(Interpreter&) const override;
  340. virtual void dump(int indent) const override;
  341. private:
  342. virtual const char* class_name() const override { return "StringLiteral"; }
  343. String m_value;
  344. };
  345. class NullLiteral final : public Literal {
  346. public:
  347. explicit NullLiteral() {}
  348. virtual Value execute(Interpreter&) const override;
  349. virtual void dump(int indent) const override;
  350. private:
  351. virtual const char* class_name() const override { return "NullLiteral"; }
  352. };
  353. class UndefinedLiteral final : public Literal {
  354. public:
  355. explicit UndefinedLiteral()
  356. {
  357. }
  358. virtual Value execute(Interpreter&) const override;
  359. virtual void dump(int indent) const override;
  360. private:
  361. virtual const char* class_name() const override { return "UndefinedLiteral"; }
  362. };
  363. class Identifier final : public Expression {
  364. public:
  365. explicit Identifier(const FlyString& string)
  366. : m_string(string)
  367. {
  368. }
  369. const FlyString& string() const { return m_string; }
  370. virtual Value execute(Interpreter&) const override;
  371. virtual void dump(int indent) const override;
  372. virtual bool is_identifier() const override { return true; }
  373. private:
  374. virtual const char* class_name() const override { return "Identifier"; }
  375. FlyString m_string;
  376. };
  377. class CallExpression : public Expression {
  378. public:
  379. explicit CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
  380. : m_callee(move(callee))
  381. , m_arguments(move(arguments))
  382. {
  383. }
  384. virtual Value execute(Interpreter&) const override;
  385. virtual void dump(int indent) const override;
  386. private:
  387. virtual const char* class_name() const override { return "CallExpression"; }
  388. NonnullRefPtr<Expression> m_callee;
  389. const NonnullRefPtrVector<Expression> m_arguments;
  390. };
  391. enum class AssignmentOp {
  392. Assignment,
  393. AdditionAssignment,
  394. SubtractionAssignment,
  395. MultiplicationAssignment,
  396. DivisionAssignment,
  397. };
  398. class AssignmentExpression : public Expression {
  399. public:
  400. AssignmentExpression(AssignmentOp op, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs)
  401. : m_op(op)
  402. , m_lhs(move(lhs))
  403. , m_rhs(move(rhs))
  404. {
  405. }
  406. virtual Value execute(Interpreter&) const override;
  407. virtual void dump(int indent) const override;
  408. private:
  409. virtual const char* class_name() const override { return "AssignmentExpression"; }
  410. AssignmentOp m_op;
  411. NonnullRefPtr<ASTNode> m_lhs;
  412. NonnullRefPtr<Expression> m_rhs;
  413. };
  414. enum class UpdateOp {
  415. Increment,
  416. Decrement,
  417. };
  418. class UpdateExpression : public Expression {
  419. public:
  420. UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  421. : m_op(op)
  422. , m_argument(move(argument))
  423. , m_prefixed(prefixed)
  424. {
  425. }
  426. virtual Value execute(Interpreter&) const override;
  427. virtual void dump(int indent) const override;
  428. private:
  429. virtual const char* class_name() const override { return "UpdateExpression"; }
  430. UpdateOp m_op;
  431. NonnullRefPtr<Identifier> m_argument;
  432. bool m_prefixed;
  433. };
  434. enum class DeclarationType {
  435. Var,
  436. Let,
  437. Const,
  438. };
  439. class VariableDeclaration : public Statement {
  440. public:
  441. VariableDeclaration(NonnullRefPtr<Identifier> name, RefPtr<Expression> initializer, DeclarationType declaration_type)
  442. : m_declaration_type(declaration_type)
  443. , m_name(move(name))
  444. , m_initializer(move(initializer))
  445. {
  446. }
  447. virtual bool is_variable_declaration() const override { return true; }
  448. const Identifier& name() const { return *m_name; }
  449. DeclarationType declaration_type() const { return m_declaration_type; }
  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 "VariableDeclaration"; }
  454. DeclarationType m_declaration_type;
  455. NonnullRefPtr<Identifier> m_name;
  456. RefPtr<Expression> m_initializer;
  457. };
  458. class ObjectExpression : public Expression {
  459. public:
  460. ObjectExpression(HashMap<FlyString, NonnullRefPtr<Expression>> properties = {})
  461. : m_properties(move(properties))
  462. {
  463. }
  464. virtual Value execute(Interpreter&) const override;
  465. virtual void dump(int indent) const override;
  466. private:
  467. virtual const char* class_name() const override { return "ObjectExpression"; }
  468. HashMap<FlyString, NonnullRefPtr<Expression>> m_properties;
  469. };
  470. class ArrayExpression : public Expression {
  471. public:
  472. ArrayExpression(NonnullRefPtrVector<Expression> elements)
  473. : m_elements(move(elements))
  474. {
  475. }
  476. const NonnullRefPtrVector<Expression>& elements() const { return m_elements; }
  477. virtual Value execute(Interpreter&) const override;
  478. virtual void dump(int indent) const override;
  479. private:
  480. virtual const char* class_name() const override { return "ArrayExpression"; }
  481. NonnullRefPtrVector<Expression> m_elements;
  482. };
  483. class MemberExpression final : public Expression {
  484. public:
  485. MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  486. : m_object(move(object))
  487. , m_property(move(property))
  488. , m_computed(computed)
  489. {
  490. }
  491. virtual Value execute(Interpreter&) const override;
  492. virtual void dump(int indent) const override;
  493. bool is_computed() const { return m_computed; }
  494. const Expression& object() const { return *m_object; }
  495. const Expression& property() const { return *m_property; }
  496. FlyString computed_property_name(Interpreter&) const;
  497. private:
  498. virtual bool is_member_expression() const override { return true; }
  499. virtual const char* class_name() const override { return "MemberExpression"; }
  500. NonnullRefPtr<Expression> m_object;
  501. NonnullRefPtr<Expression> m_property;
  502. bool m_computed { false };
  503. };
  504. class CatchClause final : public ASTNode {
  505. public:
  506. CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body)
  507. : m_parameter(parameter)
  508. , m_body(move(body))
  509. {
  510. }
  511. const FlyString& parameter() const { return m_parameter; }
  512. const BlockStatement& body() const { return m_body; }
  513. virtual void dump(int indent) const override;
  514. virtual Value execute(Interpreter&) const override;
  515. private:
  516. virtual const char* class_name() const override { return "CatchClause"; }
  517. FlyString m_parameter;
  518. NonnullRefPtr<BlockStatement> m_body;
  519. };
  520. class TryStatement final : public Statement {
  521. public:
  522. TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  523. : m_block(move(block))
  524. , m_handler(move(handler))
  525. , m_finalizer(move(finalizer))
  526. {
  527. }
  528. const BlockStatement& block() const { return m_block; }
  529. const CatchClause* handler() const { return m_handler; }
  530. const BlockStatement* finalizer() const { return m_finalizer; }
  531. virtual void dump(int indent) const override;
  532. virtual Value execute(Interpreter&) const override;
  533. private:
  534. virtual const char* class_name() const override { return "TryStatement"; }
  535. NonnullRefPtr<BlockStatement> m_block;
  536. RefPtr<CatchClause> m_handler;
  537. RefPtr<BlockStatement> m_finalizer;
  538. };
  539. }