AST.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/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. };
  47. class ErrorStatement final : public Statement {
  48. public:
  49. Value execute(Interpreter&) const { return js_undefined(); }
  50. const char* class_name() const override { return "ErrorStatement"; }
  51. };
  52. class ExpressionStatement final : public Statement {
  53. public:
  54. ExpressionStatement(NonnullOwnPtr<Expression> expression)
  55. : m_expression(move(expression))
  56. {
  57. }
  58. Value execute(Interpreter&) const override;
  59. const char* class_name() const override { return "ExpressionStatement"; }
  60. virtual void dump(int indent) const override;
  61. private:
  62. NonnullOwnPtr<Expression> m_expression;
  63. };
  64. class ScopeNode : public Statement {
  65. public:
  66. template<typename T, typename... Args>
  67. T& append(Args&&... args)
  68. {
  69. auto child = make<T>(forward<Args>(args)...);
  70. m_children.append(move(child));
  71. return static_cast<T&>(m_children.last());
  72. }
  73. void append(NonnullOwnPtr<Statement> child)
  74. {
  75. m_children.append(move(child));
  76. }
  77. const NonnullOwnPtrVector<Statement>& children() const { return m_children; }
  78. virtual Value execute(Interpreter&) const override;
  79. virtual void dump(int indent) const override;
  80. protected:
  81. ScopeNode() {}
  82. private:
  83. NonnullOwnPtrVector<Statement> m_children;
  84. };
  85. class Program : public ScopeNode {
  86. public:
  87. Program() {}
  88. private:
  89. virtual const char* class_name() const override { return "Program"; }
  90. };
  91. class BlockStatement : public ScopeNode {
  92. public:
  93. BlockStatement() {}
  94. private:
  95. virtual const char* class_name() const override { return "BlockStatement"; }
  96. };
  97. class FunctionDeclaration : public Statement {
  98. public:
  99. FunctionDeclaration(String name, NonnullOwnPtr<ScopeNode> body, Vector<String> parameters = {})
  100. : m_name(move(name))
  101. , m_body(move(body))
  102. , m_parameters(move(parameters))
  103. {
  104. }
  105. String name() const { return m_name; }
  106. const ScopeNode& body() const { return *m_body; }
  107. const Vector<String>& parameters() const { return m_parameters; };
  108. virtual Value execute(Interpreter&) const override;
  109. virtual void dump(int indent) const override;
  110. private:
  111. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  112. String m_name;
  113. NonnullOwnPtr<ScopeNode> m_body;
  114. const Vector<String> m_parameters;
  115. };
  116. class Expression : public ASTNode {
  117. public:
  118. };
  119. class ErrorExpression final : public Expression {
  120. public:
  121. Value execute(Interpreter&) const { return js_undefined(); }
  122. const char* class_name() const override { return "ErrorExpression"; }
  123. };
  124. class ReturnStatement : public Statement {
  125. public:
  126. explicit ReturnStatement(OwnPtr<Expression> argument)
  127. : m_argument(move(argument))
  128. {
  129. }
  130. const Expression* argument() const { return m_argument; }
  131. virtual Value execute(Interpreter&) const override;
  132. virtual void dump(int indent) const override;
  133. private:
  134. virtual const char* class_name() const override { return "ReturnStatement"; }
  135. OwnPtr<Expression> m_argument;
  136. };
  137. class IfStatement : public Statement {
  138. public:
  139. IfStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> consequent, NonnullOwnPtr<ScopeNode> alternate)
  140. : m_predicate(move(predicate))
  141. , m_consequent(move(consequent))
  142. , m_alternate(move(alternate))
  143. {
  144. }
  145. const Expression& predicate() const { return *m_predicate; }
  146. const ScopeNode& consequent() const { return *m_consequent; }
  147. const ScopeNode& alternate() const { return *m_alternate; }
  148. virtual Value execute(Interpreter&) const override;
  149. virtual void dump(int indent) const override;
  150. private:
  151. virtual const char* class_name() const override { return "IfStatement"; }
  152. NonnullOwnPtr<Expression> m_predicate;
  153. NonnullOwnPtr<ScopeNode> m_consequent;
  154. NonnullOwnPtr<ScopeNode> m_alternate;
  155. };
  156. class WhileStatement : public Statement {
  157. public:
  158. WhileStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> body)
  159. : m_predicate(move(predicate))
  160. , m_body(move(body))
  161. {
  162. }
  163. const Expression& predicate() const { return *m_predicate; }
  164. const ScopeNode& body() const { return *m_body; }
  165. virtual Value execute(Interpreter&) const override;
  166. virtual void dump(int indent) const override;
  167. private:
  168. virtual const char* class_name() const override { return "WhileStatement"; }
  169. NonnullOwnPtr<Expression> m_predicate;
  170. NonnullOwnPtr<ScopeNode> m_body;
  171. };
  172. class ForStatement : public Statement {
  173. public:
  174. ForStatement(OwnPtr<Statement> init, OwnPtr<Expression> test, OwnPtr<Expression> update, NonnullOwnPtr<ScopeNode> body)
  175. : m_init(move(init))
  176. , m_test(move(test))
  177. , m_update(move(update))
  178. , m_body(move(body))
  179. {
  180. }
  181. const Statement* init() const { return m_init; }
  182. const Expression* test() const { return m_test; }
  183. const Expression* update() const { return m_update; }
  184. const ScopeNode& body() const { return *m_body; }
  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 "ForStatement"; }
  189. OwnPtr<Statement> m_init;
  190. OwnPtr<Expression> m_test;
  191. OwnPtr<Expression> m_update;
  192. NonnullOwnPtr<ScopeNode> m_body;
  193. };
  194. enum class BinaryOp {
  195. Plus,
  196. Minus,
  197. Asterisk,
  198. Slash,
  199. TypedEquals,
  200. TypedInequals,
  201. GreaterThan,
  202. GreaterThanEquals,
  203. LessThan,
  204. LessThanEquals,
  205. BitwiseAnd,
  206. BitwiseOr,
  207. BitwiseXor,
  208. LeftShift,
  209. RightShift,
  210. };
  211. class BinaryExpression : public Expression {
  212. public:
  213. BinaryExpression(BinaryOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  214. : m_op(op)
  215. , m_lhs(move(lhs))
  216. , m_rhs(move(rhs))
  217. {
  218. }
  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 "BinaryExpression"; }
  223. BinaryOp m_op;
  224. NonnullOwnPtr<Expression> m_lhs;
  225. NonnullOwnPtr<Expression> m_rhs;
  226. };
  227. enum class LogicalOp {
  228. And,
  229. Or,
  230. };
  231. class LogicalExpression : public Expression {
  232. public:
  233. LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
  234. : m_op(op)
  235. , m_lhs(move(lhs))
  236. , m_rhs(move(rhs))
  237. {
  238. }
  239. virtual Value execute(Interpreter&) const override;
  240. virtual void dump(int indent) const override;
  241. private:
  242. virtual const char* class_name() const override { return "LogicalExpression"; }
  243. LogicalOp m_op;
  244. NonnullOwnPtr<Expression> m_lhs;
  245. NonnullOwnPtr<Expression> m_rhs;
  246. };
  247. enum class UnaryOp {
  248. BitNot,
  249. Not,
  250. };
  251. class UnaryExpression : public Expression {
  252. public:
  253. UnaryExpression(UnaryOp op, NonnullOwnPtr<Expression> lhs)
  254. : m_op(op)
  255. , m_lhs(move(lhs))
  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 "UnaryExpression"; }
  262. UnaryOp m_op;
  263. NonnullOwnPtr<Expression> m_lhs;
  264. };
  265. class Literal : public Expression {
  266. protected:
  267. explicit Literal() {}
  268. };
  269. class BooleanLiteral final : public Literal {
  270. public:
  271. explicit BooleanLiteral(bool value)
  272. : m_value(value)
  273. {
  274. }
  275. virtual Value execute(Interpreter&) const override;
  276. virtual void dump(int indent) const override;
  277. private:
  278. virtual const char* class_name() const override { return "BooleanLiteral"; }
  279. bool m_value { false };
  280. };
  281. class NumericLiteral final : public Literal {
  282. public:
  283. explicit NumericLiteral(double value)
  284. : m_value(value)
  285. {
  286. }
  287. virtual Value execute(Interpreter&) const override;
  288. virtual void dump(int indent) const override;
  289. private:
  290. virtual const char* class_name() const override { return "NumericLiteral"; }
  291. double m_value { 0 };
  292. };
  293. class StringLiteral final : public Literal {
  294. public:
  295. explicit StringLiteral(String value)
  296. : m_value(move(value))
  297. {
  298. }
  299. virtual Value execute(Interpreter&) const override;
  300. virtual void dump(int indent) const override;
  301. private:
  302. virtual const char* class_name() const override { return "StringLiteral"; }
  303. String m_value;
  304. };
  305. class Identifier final : public Expression {
  306. public:
  307. explicit Identifier(String string)
  308. : m_string(move(string))
  309. {
  310. }
  311. const String& string() const { return m_string; }
  312. virtual Value execute(Interpreter&) const override;
  313. virtual void dump(int indent) const override;
  314. virtual bool is_identifier() const override { return true; }
  315. private:
  316. virtual const char* class_name() const override { return "Identifier"; }
  317. String m_string;
  318. };
  319. class CallExpression : public Expression {
  320. public:
  321. explicit CallExpression(NonnullOwnPtr<Expression> callee, NonnullOwnPtrVector<Expression> arguments = {})
  322. : m_callee(move(callee))
  323. , m_arguments(move(arguments))
  324. {
  325. }
  326. virtual Value execute(Interpreter&) const override;
  327. virtual void dump(int indent) const override;
  328. private:
  329. virtual const char* class_name() const override { return "CallExpression"; }
  330. NonnullOwnPtr<Expression> m_callee;
  331. const NonnullOwnPtrVector<Expression> m_arguments;
  332. };
  333. enum class AssignmentOp {
  334. Assignment,
  335. AdditionAssignment,
  336. SubtractionAssignment,
  337. MultiplicationAssignment,
  338. DivisionAssignment,
  339. };
  340. class AssignmentExpression : public Expression {
  341. public:
  342. AssignmentExpression(AssignmentOp op, NonnullOwnPtr<ASTNode> lhs, NonnullOwnPtr<Expression> rhs)
  343. : m_op(op)
  344. , m_lhs(move(lhs))
  345. , m_rhs(move(rhs))
  346. {
  347. }
  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 "AssignmentExpression"; }
  352. AssignmentOp m_op;
  353. NonnullOwnPtr<ASTNode> m_lhs;
  354. NonnullOwnPtr<Expression> m_rhs;
  355. };
  356. enum class UpdateOp {
  357. Increment,
  358. Decrement,
  359. };
  360. class UpdateExpression : public Expression {
  361. public:
  362. UpdateExpression(UpdateOp op, NonnullOwnPtr<Expression> argument)
  363. : m_op(op)
  364. , m_argument(move(argument))
  365. {
  366. }
  367. virtual Value execute(Interpreter&) const override;
  368. virtual void dump(int indent) const override;
  369. private:
  370. virtual const char* class_name() const override { return "UpdateExpression"; }
  371. UpdateOp m_op;
  372. NonnullOwnPtr<Identifier> m_argument;
  373. };
  374. enum class DeclarationType {
  375. Var,
  376. Let,
  377. Const,
  378. };
  379. class VariableDeclaration : public Statement {
  380. public:
  381. VariableDeclaration(NonnullOwnPtr<Identifier> name, OwnPtr<Expression> initializer, DeclarationType declaration_type)
  382. : m_declaration_type(declaration_type)
  383. , m_name(move(name))
  384. , m_initializer(move(initializer))
  385. {
  386. }
  387. const Identifier& name() const { return *m_name; }
  388. virtual Value execute(Interpreter&) const override;
  389. virtual void dump(int indent) const override;
  390. private:
  391. virtual const char* class_name() const override { return "VariableDeclaration"; }
  392. DeclarationType m_declaration_type;
  393. NonnullOwnPtr<Identifier> m_name;
  394. OwnPtr<Expression> m_initializer;
  395. };
  396. class ObjectExpression : public Expression {
  397. public:
  398. ObjectExpression() {}
  399. virtual Value execute(Interpreter&) const override;
  400. virtual void dump(int indent) const override;
  401. private:
  402. virtual const char* class_name() const override { return "ObjectExpression"; }
  403. };
  404. class MemberExpression final : public Expression {
  405. public:
  406. MemberExpression(NonnullOwnPtr<Expression> object, NonnullOwnPtr<Expression> property)
  407. : m_object(move(object))
  408. , m_property(move(property))
  409. {
  410. }
  411. virtual Value execute(Interpreter&) const override;
  412. virtual void dump(int indent) const override;
  413. private:
  414. virtual const char* class_name() const override { return "MemberExpression"; }
  415. NonnullOwnPtr<Expression> m_object;
  416. NonnullOwnPtr<Expression> m_property;
  417. };
  418. }