AST.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullOwnPtrVector.h>
  28. #include <AK/OwnPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibJS/Forward.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace JS {
  34. class ASTNode {
  35. public:
  36. virtual ~ASTNode() {}
  37. virtual const char* class_name() const = 0;
  38. virtual Value execute(Interpreter&) const = 0;
  39. virtual void dump(int indent) const;
  40. virtual bool is_identifier() const { return false; }
  41. protected:
  42. ASTNode() {}
  43. private:
  44. };
  45. class Statement : public ASTNode {
  46. public:
  47. virtual bool is_variable_declaration() const { return false; }
  48. };
  49. class ErrorStatement final : public Statement {
  50. public:
  51. Value execute(Interpreter&) const override { return js_undefined(); }
  52. const char* class_name() const override { return "ErrorStatement"; }
  53. };
  54. class ExpressionStatement final : public Statement {
  55. public:
  56. ExpressionStatement(NonnullOwnPtr<Expression> expression)
  57. : m_expression(move(expression))
  58. {
  59. }
  60. Value execute(Interpreter&) const override;
  61. const char* class_name() const override { return "ExpressionStatement"; }
  62. virtual void dump(int indent) const override;
  63. private:
  64. NonnullOwnPtr<Expression> m_expression;
  65. };
  66. class ScopeNode : public Statement {
  67. public:
  68. template<typename T, typename... Args>
  69. T& append(Args&&... args)
  70. {
  71. auto child = make<T>(forward<Args>(args)...);
  72. m_children.append(move(child));
  73. return static_cast<T&>(m_children.last());
  74. }
  75. void append(NonnullOwnPtr<Statement> child)
  76. {
  77. m_children.append(move(child));
  78. }
  79. const NonnullOwnPtrVector<Statement>& children() const { return m_children; }
  80. virtual Value execute(Interpreter&) const override;
  81. virtual void dump(int indent) const override;
  82. protected:
  83. ScopeNode() {}
  84. private:
  85. NonnullOwnPtrVector<Statement> m_children;
  86. };
  87. class Program : public ScopeNode {
  88. public:
  89. Program() {}
  90. private:
  91. virtual const char* class_name() const override { return "Program"; }
  92. };
  93. class BlockStatement : public ScopeNode {
  94. public:
  95. BlockStatement() {}
  96. private:
  97. virtual const char* class_name() const override { return "BlockStatement"; }
  98. };
  99. class FunctionDeclaration : public Statement {
  100. public:
  101. FunctionDeclaration(String name, NonnullOwnPtr<ScopeNode> body, Vector<String> parameters = {})
  102. : m_name(move(name))
  103. , m_body(move(body))
  104. , m_parameters(move(parameters))
  105. {
  106. }
  107. String name() const { return m_name; }
  108. const ScopeNode& body() const { return *m_body; }
  109. const Vector<String>& parameters() const { return m_parameters; };
  110. virtual Value execute(Interpreter&) const override;
  111. virtual void dump(int indent) const override;
  112. private:
  113. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  114. String m_name;
  115. NonnullOwnPtr<ScopeNode> m_body;
  116. const Vector<String> m_parameters;
  117. };
  118. class Expression : public ASTNode {
  119. public:
  120. virtual bool is_member_expression() const { return false; }
  121. };
  122. class ErrorExpression final : public Expression {
  123. public:
  124. Value execute(Interpreter&) const override { return js_undefined(); }
  125. const char* class_name() const override { return "ErrorExpression"; }
  126. };
  127. class ReturnStatement : public Statement {
  128. public:
  129. explicit ReturnStatement(OwnPtr<Expression> argument)
  130. : m_argument(move(argument))
  131. {
  132. }
  133. const Expression* argument() const { return m_argument; }
  134. virtual Value execute(Interpreter&) const override;
  135. virtual void dump(int indent) const override;
  136. private:
  137. virtual const char* class_name() const override { return "ReturnStatement"; }
  138. OwnPtr<Expression> m_argument;
  139. };
  140. class IfStatement : public Statement {
  141. public:
  142. IfStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> consequent, NonnullOwnPtr<ScopeNode> alternate)
  143. : m_predicate(move(predicate))
  144. , m_consequent(move(consequent))
  145. , m_alternate(move(alternate))
  146. {
  147. }
  148. const Expression& predicate() const { return *m_predicate; }
  149. const ScopeNode& consequent() const { return *m_consequent; }
  150. const ScopeNode& alternate() const { return *m_alternate; }
  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 "IfStatement"; }
  155. NonnullOwnPtr<Expression> m_predicate;
  156. NonnullOwnPtr<ScopeNode> m_consequent;
  157. NonnullOwnPtr<ScopeNode> m_alternate;
  158. };
  159. class WhileStatement : public Statement {
  160. public:
  161. WhileStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> body)
  162. : m_predicate(move(predicate))
  163. , m_body(move(body))
  164. {
  165. }
  166. const Expression& predicate() const { return *m_predicate; }
  167. const ScopeNode& body() const { return *m_body; }
  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 "WhileStatement"; }
  172. NonnullOwnPtr<Expression> m_predicate;
  173. NonnullOwnPtr<ScopeNode> m_body;
  174. };
  175. class ForStatement : public Statement {
  176. public:
  177. ForStatement(OwnPtr<Statement> init, OwnPtr<Expression> test, OwnPtr<Expression> update, NonnullOwnPtr<ScopeNode> body)
  178. : m_init(move(init))
  179. , m_test(move(test))
  180. , m_update(move(update))
  181. , m_body(move(body))
  182. {
  183. }
  184. const Statement* init() const { return m_init; }
  185. const Expression* test() const { return m_test; }
  186. const Expression* update() const { return m_update; }
  187. const ScopeNode& body() const { return *m_body; }
  188. virtual Value execute(Interpreter&) const override;
  189. virtual void dump(int indent) const override;
  190. private:
  191. virtual const char* class_name() const override { return "ForStatement"; }
  192. OwnPtr<Statement> m_init;
  193. OwnPtr<Expression> m_test;
  194. OwnPtr<Expression> m_update;
  195. NonnullOwnPtr<ScopeNode> m_body;
  196. };
  197. enum class BinaryOp {
  198. Plus,
  199. Minus,
  200. Asterisk,
  201. Slash,
  202. TypedEquals,
  203. TypedInequals,
  204. AbstractEquals,
  205. AbstractInequals,
  206. GreaterThan,
  207. GreaterThanEquals,
  208. LessThan,
  209. LessThanEquals,
  210. BitwiseAnd,
  211. BitwiseOr,
  212. BitwiseXor,
  213. LeftShift,
  214. RightShift,
  215. };
  216. class BinaryExpression : public Expression {
  217. public:
  218. BinaryExpression(BinaryOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  219. : m_op(op)
  220. , m_lhs(move(lhs))
  221. , m_rhs(move(rhs))
  222. {
  223. }
  224. virtual Value execute(Interpreter&) const override;
  225. virtual void dump(int indent) const override;
  226. private:
  227. virtual const char* class_name() const override { return "BinaryExpression"; }
  228. BinaryOp m_op;
  229. NonnullOwnPtr<Expression> m_lhs;
  230. NonnullOwnPtr<Expression> m_rhs;
  231. };
  232. enum class LogicalOp {
  233. And,
  234. Or,
  235. };
  236. class LogicalExpression : public Expression {
  237. public:
  238. LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  239. : m_op(op)
  240. , m_lhs(move(lhs))
  241. , m_rhs(move(rhs))
  242. {
  243. }
  244. virtual Value execute(Interpreter&) const override;
  245. virtual void dump(int indent) const override;
  246. private:
  247. virtual const char* class_name() const override { return "LogicalExpression"; }
  248. LogicalOp m_op;
  249. NonnullOwnPtr<Expression> m_lhs;
  250. NonnullOwnPtr<Expression> m_rhs;
  251. };
  252. enum class UnaryOp {
  253. BitwiseNot,
  254. Not,
  255. Typeof,
  256. };
  257. class UnaryExpression : public Expression {
  258. public:
  259. UnaryExpression(UnaryOp op, NonnullOwnPtr<Expression> lhs)
  260. : m_op(op)
  261. , m_lhs(move(lhs))
  262. {
  263. }
  264. virtual Value execute(Interpreter&) const override;
  265. virtual void dump(int indent) const override;
  266. private:
  267. virtual const char* class_name() const override { return "UnaryExpression"; }
  268. UnaryOp m_op;
  269. NonnullOwnPtr<Expression> m_lhs;
  270. };
  271. class Literal : public Expression {
  272. protected:
  273. explicit Literal() {}
  274. };
  275. class BooleanLiteral final : public Literal {
  276. public:
  277. explicit BooleanLiteral(bool value)
  278. : m_value(value)
  279. {
  280. }
  281. virtual Value execute(Interpreter&) const override;
  282. virtual void dump(int indent) const override;
  283. private:
  284. virtual const char* class_name() const override { return "BooleanLiteral"; }
  285. bool m_value { false };
  286. };
  287. class NumericLiteral final : public Literal {
  288. public:
  289. explicit NumericLiteral(double value)
  290. : m_value(value)
  291. {
  292. }
  293. virtual Value execute(Interpreter&) const override;
  294. virtual void dump(int indent) const override;
  295. private:
  296. virtual const char* class_name() const override { return "NumericLiteral"; }
  297. double m_value { 0 };
  298. };
  299. class StringLiteral final : public Literal {
  300. public:
  301. explicit StringLiteral(String value)
  302. : m_value(move(value))
  303. {
  304. }
  305. virtual Value execute(Interpreter&) const override;
  306. virtual void dump(int indent) const override;
  307. private:
  308. virtual const char* class_name() const override { return "StringLiteral"; }
  309. String m_value;
  310. };
  311. class NullLiteral final : public Literal {
  312. public:
  313. explicit NullLiteral()
  314. {
  315. }
  316. virtual Value execute(Interpreter&) const override;
  317. virtual void dump(int indent) const override;
  318. private:
  319. virtual const char* class_name() const override { return "NullLiteral"; }
  320. String m_value;
  321. };
  322. class UndefinedLiteral final : public Literal {
  323. public:
  324. explicit UndefinedLiteral()
  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 "UndefinedLiteral"; }
  331. };
  332. class Identifier final : public Expression {
  333. public:
  334. explicit Identifier(String string)
  335. : m_string(move(string))
  336. {
  337. }
  338. const String& string() const { return m_string; }
  339. virtual Value execute(Interpreter&) const override;
  340. virtual void dump(int indent) const override;
  341. virtual bool is_identifier() const override { return true; }
  342. private:
  343. virtual const char* class_name() const override { return "Identifier"; }
  344. String m_string;
  345. };
  346. class CallExpression : public Expression {
  347. public:
  348. explicit CallExpression(NonnullOwnPtr<Expression> callee, NonnullOwnPtrVector<Expression> arguments = {})
  349. : m_callee(move(callee))
  350. , m_arguments(move(arguments))
  351. {
  352. }
  353. virtual Value execute(Interpreter&) const override;
  354. virtual void dump(int indent) const override;
  355. private:
  356. virtual const char* class_name() const override { return "CallExpression"; }
  357. NonnullOwnPtr<Expression> m_callee;
  358. const NonnullOwnPtrVector<Expression> m_arguments;
  359. };
  360. enum class AssignmentOp {
  361. Assignment,
  362. AdditionAssignment,
  363. SubtractionAssignment,
  364. MultiplicationAssignment,
  365. DivisionAssignment,
  366. };
  367. class AssignmentExpression : public Expression {
  368. public:
  369. AssignmentExpression(AssignmentOp op, NonnullOwnPtr<ASTNode> lhs, NonnullOwnPtr<Expression> rhs)
  370. : m_op(op)
  371. , m_lhs(move(lhs))
  372. , m_rhs(move(rhs))
  373. {
  374. }
  375. virtual Value execute(Interpreter&) const override;
  376. virtual void dump(int indent) const override;
  377. private:
  378. virtual const char* class_name() const override { return "AssignmentExpression"; }
  379. AssignmentOp m_op;
  380. NonnullOwnPtr<ASTNode> m_lhs;
  381. NonnullOwnPtr<Expression> m_rhs;
  382. };
  383. enum class UpdateOp {
  384. Increment,
  385. Decrement,
  386. };
  387. class UpdateExpression : public Expression {
  388. public:
  389. UpdateExpression(UpdateOp op, NonnullOwnPtr<Expression> argument, bool prefixed = false)
  390. : m_op(op)
  391. , m_argument(move(argument))
  392. , m_prefixed(prefixed)
  393. {
  394. }
  395. virtual Value execute(Interpreter&) const override;
  396. virtual void dump(int indent) const override;
  397. private:
  398. virtual const char* class_name() const override { return "UpdateExpression"; }
  399. UpdateOp m_op;
  400. NonnullOwnPtr<Identifier> m_argument;
  401. bool m_prefixed;
  402. };
  403. enum class DeclarationType {
  404. Var,
  405. Let,
  406. Const,
  407. };
  408. class VariableDeclaration : public Statement {
  409. public:
  410. VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
  411. : m_declaration_type(declaration_type)
  412. , m_name(move(name))
  413. , m_initializer(move(initializer))
  414. {
  415. }
  416. virtual bool is_variable_declaration() const override { return true; }
  417. const Identifier& name() const { return *m_name; }
  418. DeclarationType declaration_type() const { return m_declaration_type; }
  419. virtual Value execute(Interpreter&) const override;
  420. virtual void dump(int indent) const override;
  421. private:
  422. virtual const char* class_name() const override { return "VariableDeclaration"; }
  423. DeclarationType m_declaration_type;
  424. NonnullOwnPtr<Identifier> m_name;
  425. OwnPtr<Expression> m_initializer;
  426. };
  427. class ObjectExpression : public Expression {
  428. public:
  429. ObjectExpression() {}
  430. virtual Value execute(Interpreter&) const override;
  431. virtual void dump(int indent) const override;
  432. private:
  433. virtual const char* class_name() const override { return "ObjectExpression"; }
  434. };
  435. class MemberExpression final : public Expression {
  436. public:
  437. MemberExpression(NonnullOwnPtr<Expression> object, NonnullOwnPtr<Expression> property)
  438. : m_object(move(object))
  439. , m_property(move(property))
  440. {
  441. }
  442. virtual Value execute(Interpreter&) const override;
  443. virtual void dump(int indent) const override;
  444. const Expression& object() const { return *m_object; }
  445. private:
  446. virtual bool is_member_expression() const override { return true; }
  447. virtual const char* class_name() const override { return "MemberExpression"; }
  448. NonnullOwnPtr<Expression> m_object;
  449. NonnullOwnPtr<Expression> m_property;
  450. };
  451. }