AST.h 16 KB

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