AST.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 <AK/HashMap.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/AST.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/PrimitiveString.h>
  31. #include <LibJS/ScriptFunction.h>
  32. #include <LibJS/Value.h>
  33. #include <stdio.h>
  34. namespace JS {
  35. Value ScopeNode::execute(Interpreter& interpreter) const
  36. {
  37. return interpreter.run(*this);
  38. }
  39. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  40. {
  41. auto* function = interpreter.heap().allocate<ScriptFunction>(body(), parameters());
  42. interpreter.set_variable(m_name, function);
  43. return function;
  44. }
  45. Value ExpressionStatement::execute(Interpreter& interpreter) const
  46. {
  47. return m_expression->execute(interpreter);
  48. }
  49. Value CallExpression::execute(Interpreter& interpreter) const
  50. {
  51. auto callee = m_callee->execute(interpreter);
  52. ASSERT(callee.is_object());
  53. ASSERT(callee.as_object()->is_function());
  54. auto* function = static_cast<Function*>(callee.as_object());
  55. Vector<Value> argument_values;
  56. for (size_t i = 0; i < m_arguments.size(); ++i)
  57. argument_values.append(m_arguments[i].execute(interpreter));
  58. Value this_value = js_undefined();
  59. if (m_callee->is_member_expression())
  60. this_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter).to_object(interpreter.heap());
  61. if (!this_value.is_undefined())
  62. interpreter.push_this_value(this_value);
  63. auto result = function->call(interpreter, move(argument_values));
  64. if (!this_value.is_undefined())
  65. interpreter.pop_this_value();
  66. return result;
  67. }
  68. Value ReturnStatement::execute(Interpreter& interpreter) const
  69. {
  70. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  71. interpreter.do_return();
  72. return value;
  73. }
  74. Value IfStatement::execute(Interpreter& interpreter) const
  75. {
  76. auto predicate_result = m_predicate->execute(interpreter);
  77. if (predicate_result.to_boolean())
  78. return interpreter.run(*m_consequent);
  79. else
  80. return interpreter.run(*m_alternate);
  81. }
  82. Value WhileStatement::execute(Interpreter& interpreter) const
  83. {
  84. Value last_value = js_undefined();
  85. while (m_predicate->execute(interpreter).to_boolean()) {
  86. last_value = interpreter.run(*m_body);
  87. }
  88. return last_value;
  89. }
  90. Value ForStatement::execute(Interpreter& interpreter) const
  91. {
  92. OwnPtr<BlockStatement> wrapper;
  93. if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
  94. wrapper = make<BlockStatement>();
  95. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  96. }
  97. Value last_value = js_undefined();
  98. if (m_init)
  99. m_init->execute(interpreter);
  100. if (m_test) {
  101. while (m_test->execute(interpreter).to_boolean()) {
  102. last_value = interpreter.run(*m_body);
  103. if (m_update)
  104. m_update->execute(interpreter);
  105. }
  106. } else {
  107. while (true) {
  108. last_value = interpreter.run(*m_body);
  109. if (m_update)
  110. m_update->execute(interpreter);
  111. }
  112. }
  113. if (wrapper)
  114. interpreter.exit_scope(*wrapper);
  115. return last_value;
  116. }
  117. Value BinaryExpression::execute(Interpreter& interpreter) const
  118. {
  119. auto lhs_result = m_lhs->execute(interpreter);
  120. auto rhs_result = m_rhs->execute(interpreter);
  121. switch (m_op) {
  122. case BinaryOp::Plus:
  123. return add(lhs_result, rhs_result);
  124. case BinaryOp::Minus:
  125. return sub(lhs_result, rhs_result);
  126. case BinaryOp::Asterisk:
  127. return mul(lhs_result, rhs_result);
  128. case BinaryOp::Slash:
  129. return div(lhs_result, rhs_result);
  130. case BinaryOp::TypedEquals:
  131. return typed_eq(lhs_result, rhs_result);
  132. case BinaryOp::TypedInequals:
  133. return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
  134. case BinaryOp::GreaterThan:
  135. return greater_than(lhs_result, rhs_result);
  136. case BinaryOp::GreaterThanEquals:
  137. return greater_than_equals(lhs_result, rhs_result);
  138. case BinaryOp::LessThan:
  139. return less_than(lhs_result, rhs_result);
  140. case BinaryOp::LessThanEquals:
  141. return less_than_equals(lhs_result, rhs_result);
  142. case BinaryOp::BitwiseAnd:
  143. return bitwise_and(lhs_result, rhs_result);
  144. case BinaryOp::BitwiseOr:
  145. return bitwise_or(lhs_result, rhs_result);
  146. case BinaryOp::BitwiseXor:
  147. return bitwise_xor(lhs_result, rhs_result);
  148. case BinaryOp::LeftShift:
  149. return left_shift(lhs_result, rhs_result);
  150. case BinaryOp::RightShift:
  151. return right_shift(lhs_result, rhs_result);
  152. }
  153. ASSERT_NOT_REACHED();
  154. }
  155. Value LogicalExpression::execute(Interpreter& interpreter) const
  156. {
  157. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  158. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  159. switch (m_op) {
  160. case LogicalOp::And:
  161. return Value(lhs_result && rhs_result);
  162. case LogicalOp::Or:
  163. return Value(lhs_result || rhs_result);
  164. }
  165. ASSERT_NOT_REACHED();
  166. }
  167. Value UnaryExpression::execute(Interpreter& interpreter) const
  168. {
  169. auto lhs_result = m_lhs->execute(interpreter);
  170. switch (m_op) {
  171. case UnaryOp::BitwiseNot:
  172. return bitwise_not(lhs_result);
  173. case UnaryOp::Not:
  174. return Value(!lhs_result.to_boolean());
  175. }
  176. ASSERT_NOT_REACHED();
  177. }
  178. static void print_indent(int indent)
  179. {
  180. for (int i = 0; i < indent * 2; ++i)
  181. putchar(' ');
  182. }
  183. void ASTNode::dump(int indent) const
  184. {
  185. print_indent(indent);
  186. printf("%s\n", class_name());
  187. }
  188. void ScopeNode::dump(int indent) const
  189. {
  190. ASTNode::dump(indent);
  191. for (auto& child : children())
  192. child.dump(indent + 1);
  193. }
  194. void BinaryExpression::dump(int indent) const
  195. {
  196. const char* op_string = nullptr;
  197. switch (m_op) {
  198. case BinaryOp::Plus:
  199. op_string = "+";
  200. break;
  201. case BinaryOp::Minus:
  202. op_string = "-";
  203. break;
  204. case BinaryOp::Asterisk:
  205. op_string = "*";
  206. break;
  207. case BinaryOp::Slash:
  208. op_string = "/";
  209. break;
  210. case BinaryOp::TypedEquals:
  211. op_string = "===";
  212. break;
  213. case BinaryOp::TypedInequals:
  214. op_string = "!==";
  215. break;
  216. case BinaryOp::GreaterThan:
  217. op_string = ">";
  218. break;
  219. case BinaryOp::GreaterThanEquals:
  220. op_string = ">=";
  221. break;
  222. case BinaryOp::LessThan:
  223. op_string = "<";
  224. break;
  225. case BinaryOp::LessThanEquals:
  226. op_string = "<=";
  227. break;
  228. case BinaryOp::BitwiseAnd:
  229. op_string = "&";
  230. break;
  231. case BinaryOp::BitwiseOr:
  232. op_string = "|";
  233. break;
  234. case BinaryOp::BitwiseXor:
  235. op_string = "^";
  236. break;
  237. case BinaryOp::LeftShift:
  238. op_string = "<<";
  239. break;
  240. case BinaryOp::RightShift:
  241. op_string = ">>";
  242. break;
  243. }
  244. print_indent(indent);
  245. printf("%s\n", class_name());
  246. m_lhs->dump(indent + 1);
  247. print_indent(indent + 1);
  248. printf("%s\n", op_string);
  249. m_rhs->dump(indent + 1);
  250. }
  251. void LogicalExpression::dump(int indent) const
  252. {
  253. const char* op_string = nullptr;
  254. switch (m_op) {
  255. case LogicalOp::And:
  256. op_string = "&&";
  257. break;
  258. case LogicalOp::Or:
  259. op_string = "||";
  260. break;
  261. }
  262. print_indent(indent);
  263. printf("%s\n", class_name());
  264. m_lhs->dump(indent + 1);
  265. print_indent(indent + 1);
  266. printf("%s\n", op_string);
  267. m_rhs->dump(indent + 1);
  268. }
  269. void UnaryExpression::dump(int indent) const
  270. {
  271. const char* op_string = nullptr;
  272. switch (m_op) {
  273. case UnaryOp::BitwiseNot:
  274. op_string = "~";
  275. break;
  276. case UnaryOp::Not:
  277. op_string = "!";
  278. break;
  279. }
  280. print_indent(indent);
  281. printf("%s\n", class_name());
  282. print_indent(indent + 1);
  283. printf("%s\n", op_string);
  284. m_lhs->dump(indent + 1);
  285. }
  286. void CallExpression::dump(int indent) const
  287. {
  288. ASTNode::dump(indent);
  289. m_callee->dump(indent + 1);
  290. for (auto& argument : m_arguments)
  291. argument.dump(indent + 1);
  292. }
  293. void StringLiteral::dump(int indent) const
  294. {
  295. print_indent(indent);
  296. printf("StringLiteral \"%s\"\n", m_value.characters());
  297. }
  298. void NumericLiteral::dump(int indent) const
  299. {
  300. print_indent(indent);
  301. printf("NumericLiteral %g\n", m_value);
  302. }
  303. void BooleanLiteral::dump(int indent) const
  304. {
  305. print_indent(indent);
  306. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  307. }
  308. void FunctionDeclaration::dump(int indent) const
  309. {
  310. bool first_time = true;
  311. StringBuilder parameters_builder;
  312. for (const auto& parameter : m_parameters) {
  313. if (first_time)
  314. first_time = false;
  315. else
  316. parameters_builder.append(',');
  317. parameters_builder.append(parameter);
  318. }
  319. print_indent(indent);
  320. printf("%s '%s(%s)'\n", class_name(), name().characters(), parameters_builder.build().characters());
  321. body().dump(indent + 1);
  322. }
  323. void ReturnStatement::dump(int indent) const
  324. {
  325. ASTNode::dump(indent);
  326. if (argument())
  327. argument()->dump(indent + 1);
  328. }
  329. void IfStatement::dump(int indent) const
  330. {
  331. ASTNode::dump(indent);
  332. print_indent(indent);
  333. printf("If\n");
  334. predicate().dump(indent + 1);
  335. consequent().dump(indent + 1);
  336. print_indent(indent);
  337. printf("Else\n");
  338. alternate().dump(indent + 1);
  339. }
  340. void WhileStatement::dump(int indent) const
  341. {
  342. ASTNode::dump(indent);
  343. print_indent(indent);
  344. printf("While\n");
  345. predicate().dump(indent + 1);
  346. body().dump(indent + 1);
  347. }
  348. void ForStatement::dump(int indent) const
  349. {
  350. ASTNode::dump(indent);
  351. print_indent(indent);
  352. printf("For\n");
  353. if (init())
  354. init()->dump(indent + 1);
  355. if (test())
  356. test()->dump(indent + 1);
  357. if (update())
  358. update()->dump(indent + 1);
  359. body().dump(indent + 1);
  360. }
  361. Value Identifier::execute(Interpreter& interpreter) const
  362. {
  363. return interpreter.get_variable(string());
  364. }
  365. void Identifier::dump(int indent) const
  366. {
  367. print_indent(indent);
  368. printf("Identifier \"%s\"\n", m_string.characters());
  369. }
  370. Value AssignmentExpression::execute(Interpreter& interpreter) const
  371. {
  372. ASSERT(m_lhs->is_identifier());
  373. auto name = static_cast<const Identifier&>(*m_lhs).string();
  374. auto rhs_result = m_rhs->execute(interpreter);
  375. switch (m_op) {
  376. case AssignmentOp::Assignment:
  377. interpreter.set_variable(name, rhs_result);
  378. break;
  379. case AssignmentOp::AdditionAssignment:
  380. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  381. interpreter.set_variable(name, rhs_result);
  382. break;
  383. case AssignmentOp::SubtractionAssignment:
  384. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  385. interpreter.set_variable(name, rhs_result);
  386. break;
  387. case AssignmentOp::MultiplicationAssignment:
  388. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  389. interpreter.set_variable(name, rhs_result);
  390. break;
  391. case AssignmentOp::DivisionAssignment:
  392. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  393. interpreter.set_variable(name, rhs_result);
  394. break;
  395. }
  396. return rhs_result;
  397. }
  398. Value UpdateExpression::execute(Interpreter& interpreter) const
  399. {
  400. ASSERT(m_argument->is_identifier());
  401. auto name = static_cast<const Identifier&>(*m_argument).string();
  402. auto previous_value = interpreter.get_variable(name);
  403. ASSERT(previous_value.is_number());
  404. int op_result = 0;
  405. switch (m_op) {
  406. case UpdateOp::Increment:
  407. op_result = 1;
  408. break;
  409. case UpdateOp::Decrement:
  410. op_result = -1;
  411. break;
  412. }
  413. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  414. if (m_prefixed)
  415. return JS::Value(previous_value.as_double() + op_result);
  416. return previous_value;
  417. }
  418. void AssignmentExpression::dump(int indent) const
  419. {
  420. const char* op_string = nullptr;
  421. switch (m_op) {
  422. case AssignmentOp::Assignment:
  423. op_string = "=";
  424. break;
  425. case AssignmentOp::AdditionAssignment:
  426. op_string = "+=";
  427. break;
  428. case AssignmentOp::SubtractionAssignment:
  429. op_string = "-=";
  430. break;
  431. case AssignmentOp::MultiplicationAssignment:
  432. op_string = "*=";
  433. break;
  434. case AssignmentOp::DivisionAssignment:
  435. op_string = "/=";
  436. break;
  437. }
  438. ASTNode::dump(indent);
  439. print_indent(indent + 1);
  440. printf("%s\n", op_string);
  441. m_lhs->dump(indent + 1);
  442. m_rhs->dump(indent + 1);
  443. }
  444. void UpdateExpression::dump(int indent) const
  445. {
  446. const char* op_string = nullptr;
  447. switch (m_op) {
  448. case UpdateOp::Increment:
  449. op_string = "++";
  450. break;
  451. case UpdateOp::Decrement:
  452. op_string = "--";
  453. break;
  454. }
  455. ASTNode::dump(indent);
  456. print_indent(indent + 1);
  457. if (m_prefixed)
  458. printf("%s\n", op_string);
  459. m_argument->dump(indent + 1);
  460. if (!m_prefixed) {
  461. print_indent(indent + 1);
  462. printf("%s\n", op_string);
  463. }
  464. }
  465. Value VariableDeclaration::execute(Interpreter& interpreter) const
  466. {
  467. interpreter.declare_variable(name().string(), m_declaration_type);
  468. if (m_initializer) {
  469. auto initalizer_result = m_initializer->execute(interpreter);
  470. interpreter.set_variable(name().string(), initalizer_result);
  471. }
  472. return js_undefined();
  473. }
  474. void VariableDeclaration::dump(int indent) const
  475. {
  476. const char* declaration_type_string = nullptr;
  477. switch (m_declaration_type) {
  478. case DeclarationType::Let:
  479. declaration_type_string = "Let";
  480. break;
  481. case DeclarationType::Var:
  482. declaration_type_string = "Var";
  483. break;
  484. case DeclarationType::Const:
  485. declaration_type_string = "Const";
  486. break;
  487. }
  488. ASTNode::dump(indent);
  489. print_indent(indent + 1);
  490. printf("%s\n", declaration_type_string);
  491. m_name->dump(indent + 1);
  492. if (m_initializer)
  493. m_initializer->dump(indent + 1);
  494. }
  495. void ObjectExpression::dump(int indent) const
  496. {
  497. ASTNode::dump(indent);
  498. }
  499. void ExpressionStatement::dump(int indent) const
  500. {
  501. ASTNode::dump(indent);
  502. m_expression->dump(indent + 1);
  503. }
  504. Value ObjectExpression::execute(Interpreter& interpreter) const
  505. {
  506. return interpreter.heap().allocate<Object>();
  507. }
  508. void MemberExpression::dump(int indent) const
  509. {
  510. ASTNode::dump(indent);
  511. m_object->dump(indent + 1);
  512. m_property->dump(indent + 1);
  513. }
  514. Value MemberExpression::execute(Interpreter& interpreter) const
  515. {
  516. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  517. ASSERT(object_result.is_object());
  518. String property_name;
  519. if (m_property->is_identifier()) {
  520. property_name = static_cast<const Identifier&>(*m_property).string();
  521. } else {
  522. ASSERT_NOT_REACHED();
  523. }
  524. return object_result.as_object()->get(property_name);
  525. }
  526. Value StringLiteral::execute(Interpreter& interpreter) const
  527. {
  528. return js_string(interpreter.heap(), m_value);
  529. }
  530. Value NumericLiteral::execute(Interpreter&) const
  531. {
  532. return Value(m_value);
  533. }
  534. Value BooleanLiteral::execute(Interpreter&) const
  535. {
  536. return Value(m_value);
  537. }
  538. }