AST.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include <LibJS/AST.h>
  27. #include <LibJS/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Value.h>
  30. #include <stdio.h>
  31. namespace JS {
  32. Value ScopeNode::execute(Interpreter& interpreter) const
  33. {
  34. return interpreter.run(*this);
  35. }
  36. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  37. {
  38. auto* function = interpreter.heap().allocate<Function>(name(), body());
  39. interpreter.set_variable(m_name, Value(function));
  40. return Value(function);
  41. }
  42. Value ExpressionStatement::execute(Interpreter& interpreter) const
  43. {
  44. return m_expression->execute(interpreter);
  45. }
  46. Value CallExpression::execute(Interpreter& interpreter) const
  47. {
  48. if (name() == "$gc") {
  49. interpreter.heap().collect_garbage();
  50. return js_undefined();
  51. }
  52. auto callee = interpreter.get_variable(name());
  53. ASSERT(callee.is_object());
  54. auto* callee_object = callee.as_object();
  55. ASSERT(callee_object->is_function());
  56. auto& function = static_cast<Function&>(*callee_object);
  57. return interpreter.run(function.body(), ScopeType::Function);
  58. }
  59. Value ReturnStatement::execute(Interpreter& interpreter) const
  60. {
  61. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  62. interpreter.do_return();
  63. return value;
  64. }
  65. Value IfStatement::execute(Interpreter& interpreter) const
  66. {
  67. auto predicate_result = m_predicate->execute(interpreter);
  68. if (predicate_result.to_boolean())
  69. return interpreter.run(*m_consequent);
  70. else
  71. return interpreter.run(*m_alternate);
  72. }
  73. Value WhileStatement::execute(Interpreter& interpreter) const
  74. {
  75. Value last_value = js_undefined();
  76. while (m_predicate->execute(interpreter).to_boolean()) {
  77. last_value = interpreter.run(*m_body);
  78. }
  79. return last_value;
  80. }
  81. Value BinaryExpression::execute(Interpreter& interpreter) const
  82. {
  83. auto lhs_result = m_lhs->execute(interpreter);
  84. auto rhs_result = m_rhs->execute(interpreter);
  85. switch (m_op) {
  86. case BinaryOp::Plus:
  87. return add(lhs_result, rhs_result);
  88. case BinaryOp::Minus:
  89. return sub(lhs_result, rhs_result);
  90. case BinaryOp::TypedEquals:
  91. return typed_eq(lhs_result, rhs_result);
  92. case BinaryOp::TypedInequals:
  93. return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
  94. case BinaryOp::GreaterThan:
  95. return greater_than(lhs_result, rhs_result);
  96. case BinaryOp::LessThan:
  97. return less_than(lhs_result, rhs_result);
  98. case BinaryOp::BitwiseAnd:
  99. return bitwise_and(lhs_result, rhs_result);
  100. case BinaryOp::BitwiseOr:
  101. return bitwise_or(lhs_result, rhs_result);
  102. case BinaryOp::BitwiseXor:
  103. return bitwise_xor(lhs_result, rhs_result);
  104. case BinaryOp::LeftShift:
  105. return left_shift(lhs_result, rhs_result);
  106. case BinaryOp::RightShift:
  107. return right_shift(lhs_result, rhs_result);
  108. }
  109. ASSERT_NOT_REACHED();
  110. }
  111. Value LogicalExpression::execute(Interpreter& interpreter) const
  112. {
  113. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  114. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  115. switch (m_op) {
  116. case LogicalOp::And:
  117. return Value(lhs_result && rhs_result);
  118. case LogicalOp::Or:
  119. return Value(lhs_result || rhs_result);
  120. }
  121. ASSERT_NOT_REACHED();
  122. }
  123. Value UnaryExpression::execute(Interpreter& interpreter) const
  124. {
  125. auto lhs_result = m_lhs->execute(interpreter);
  126. switch (m_op) {
  127. case UnaryOp::BitNot:
  128. return bitwise_not(lhs_result);
  129. case UnaryOp::Not:
  130. return Value(!lhs_result.to_boolean());
  131. }
  132. ASSERT_NOT_REACHED();
  133. }
  134. static void print_indent(int indent)
  135. {
  136. for (int i = 0; i < indent * 2; ++i)
  137. putchar(' ');
  138. }
  139. void ASTNode::dump(int indent) const
  140. {
  141. print_indent(indent);
  142. printf("%s\n", class_name());
  143. }
  144. void ScopeNode::dump(int indent) const
  145. {
  146. ASTNode::dump(indent);
  147. for (auto& child : children())
  148. child.dump(indent + 1);
  149. }
  150. void BinaryExpression::dump(int indent) const
  151. {
  152. const char* op_string = nullptr;
  153. switch (m_op) {
  154. case BinaryOp::Plus:
  155. op_string = "+";
  156. break;
  157. case BinaryOp::Minus:
  158. op_string = "-";
  159. break;
  160. case BinaryOp::TypedEquals:
  161. op_string = "===";
  162. break;
  163. case BinaryOp::TypedInequals:
  164. op_string = "!==";
  165. break;
  166. case BinaryOp::GreaterThan:
  167. op_string = ">";
  168. break;
  169. case BinaryOp::LessThan:
  170. op_string = "<";
  171. break;
  172. case BinaryOp::BitwiseAnd:
  173. op_string = "&";
  174. break;
  175. case BinaryOp::BitwiseOr:
  176. op_string = "|";
  177. break;
  178. case BinaryOp::BitwiseXor:
  179. op_string = "^";
  180. break;
  181. case BinaryOp::LeftShift:
  182. op_string = "<<";
  183. break;
  184. case BinaryOp::RightShift:
  185. op_string = ">>";
  186. break;
  187. }
  188. print_indent(indent);
  189. printf("%s\n", class_name());
  190. m_lhs->dump(indent + 1);
  191. print_indent(indent + 1);
  192. printf("%s\n", op_string);
  193. m_rhs->dump(indent + 1);
  194. }
  195. void LogicalExpression::dump(int indent) const
  196. {
  197. const char* op_string = nullptr;
  198. switch (m_op) {
  199. case LogicalOp::And:
  200. op_string = "&&";
  201. break;
  202. case LogicalOp::Or:
  203. op_string = "||";
  204. break;
  205. }
  206. print_indent(indent);
  207. printf("%s\n", class_name());
  208. m_lhs->dump(indent + 1);
  209. print_indent(indent + 1);
  210. printf("%s\n", op_string);
  211. m_rhs->dump(indent + 1);
  212. }
  213. void UnaryExpression::dump(int indent) const
  214. {
  215. const char* op_string = nullptr;
  216. switch (m_op) {
  217. case UnaryOp::BitNot:
  218. op_string = "~";
  219. break;
  220. case UnaryOp::Not:
  221. op_string = "!";
  222. break;
  223. }
  224. print_indent(indent);
  225. printf("%s\n", class_name());
  226. print_indent(indent + 1);
  227. printf("%s\n", op_string);
  228. m_lhs->dump(indent + 1);
  229. }
  230. void CallExpression::dump(int indent) const
  231. {
  232. print_indent(indent);
  233. printf("%s '%s'\n", class_name(), name().characters());
  234. }
  235. void Literal::dump(int indent) const
  236. {
  237. print_indent(indent);
  238. printf("Literal _%s_\n", m_value.to_string().characters());
  239. }
  240. void FunctionDeclaration::dump(int indent) const
  241. {
  242. print_indent(indent);
  243. printf("%s '%s'\n", class_name(), name().characters());
  244. body().dump(indent + 1);
  245. }
  246. void ReturnStatement::dump(int indent) const
  247. {
  248. ASTNode::dump(indent);
  249. if (argument())
  250. argument()->dump(indent + 1);
  251. }
  252. void IfStatement::dump(int indent) const
  253. {
  254. ASTNode::dump(indent);
  255. print_indent(indent);
  256. printf("If\n");
  257. predicate().dump(indent + 1);
  258. consequent().dump(indent + 1);
  259. print_indent(indent);
  260. printf("Else\n");
  261. alternate().dump(indent + 1);
  262. }
  263. void WhileStatement::dump(int indent) const
  264. {
  265. ASTNode::dump(indent);
  266. print_indent(indent);
  267. printf("While\n");
  268. predicate().dump(indent + 1);
  269. body().dump(indent + 1);
  270. }
  271. Value Identifier::execute(Interpreter& interpreter) const
  272. {
  273. return interpreter.get_variable(string());
  274. }
  275. void Identifier::dump(int indent) const
  276. {
  277. print_indent(indent);
  278. printf("Identifier \"%s\"\n", m_string.characters());
  279. }
  280. Value AssignmentExpression::execute(Interpreter& interpreter) const
  281. {
  282. ASSERT(m_lhs->is_identifier());
  283. auto name = static_cast<const Identifier&>(*m_lhs).string();
  284. auto rhs_result = m_rhs->execute(interpreter);
  285. switch (m_op) {
  286. case AssignmentOp::Assign:
  287. interpreter.set_variable(name, rhs_result);
  288. break;
  289. }
  290. return rhs_result;
  291. }
  292. void AssignmentExpression::dump(int indent) const
  293. {
  294. const char* op_string = nullptr;
  295. switch (m_op) {
  296. case AssignmentOp::Assign:
  297. op_string = "=";
  298. break;
  299. }
  300. ASTNode::dump(indent);
  301. print_indent(indent + 1);
  302. printf("%s\n", op_string);
  303. m_lhs->dump(indent + 1);
  304. m_rhs->dump(indent + 1);
  305. }
  306. Value VariableDeclaration::execute(Interpreter& interpreter) const
  307. {
  308. interpreter.declare_variable(name().string(), m_declaration_type);
  309. if (m_initializer) {
  310. auto initalizer_result = m_initializer->execute(interpreter);
  311. interpreter.set_variable(name().string(), initalizer_result);
  312. }
  313. return js_undefined();
  314. }
  315. void VariableDeclaration::dump(int indent) const
  316. {
  317. const char* op_string = nullptr;
  318. switch (m_declaration_type) {
  319. case DeclarationType::Let:
  320. op_string = "Let";
  321. break;
  322. case DeclarationType::Var:
  323. op_string = "Var";
  324. break;
  325. }
  326. ASTNode::dump(indent);
  327. print_indent(indent + 1);
  328. printf("%s\n", op_string);
  329. m_name->dump(indent + 1);
  330. if (m_initializer)
  331. m_initializer->dump(indent + 1);
  332. }
  333. void ObjectExpression::dump(int indent) const
  334. {
  335. ASTNode::dump(indent);
  336. }
  337. void ExpressionStatement::dump(int indent) const
  338. {
  339. ASTNode::dump(indent);
  340. m_expression->dump(indent + 1);
  341. }
  342. Value ObjectExpression::execute(Interpreter& interpreter) const
  343. {
  344. return Value(interpreter.heap().allocate<Object>());
  345. }
  346. void MemberExpression::dump(int indent) const
  347. {
  348. ASTNode::dump(indent);
  349. m_object->dump(indent + 1);
  350. m_property->dump(indent + 1);
  351. }
  352. Value MemberExpression::execute(Interpreter& interpreter) const
  353. {
  354. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  355. ASSERT(object_result.is_object());
  356. String property_name;
  357. if (m_property->is_identifier()) {
  358. property_name = static_cast<const Identifier&>(*m_property).string();
  359. } else {
  360. ASSERT_NOT_REACHED();
  361. }
  362. return object_result.as_object()->get(property_name);
  363. }
  364. }