AST.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 UndefinedLiteral::dump(int indent) const
  309. {
  310. print_indent(indent);
  311. printf("undefined\n");
  312. }
  313. void NullLiteral::dump(int indent) const
  314. {
  315. print_indent(indent);
  316. printf("null\n");
  317. }
  318. void FunctionDeclaration::dump(int indent) const
  319. {
  320. bool first_time = true;
  321. StringBuilder parameters_builder;
  322. for (const auto& parameter : m_parameters) {
  323. if (first_time)
  324. first_time = false;
  325. else
  326. parameters_builder.append(',');
  327. parameters_builder.append(parameter);
  328. }
  329. print_indent(indent);
  330. printf("%s '%s(%s)'\n", class_name(), name().characters(), parameters_builder.build().characters());
  331. body().dump(indent + 1);
  332. }
  333. void ReturnStatement::dump(int indent) const
  334. {
  335. ASTNode::dump(indent);
  336. if (argument())
  337. argument()->dump(indent + 1);
  338. }
  339. void IfStatement::dump(int indent) const
  340. {
  341. ASTNode::dump(indent);
  342. print_indent(indent);
  343. printf("If\n");
  344. predicate().dump(indent + 1);
  345. consequent().dump(indent + 1);
  346. print_indent(indent);
  347. printf("Else\n");
  348. alternate().dump(indent + 1);
  349. }
  350. void WhileStatement::dump(int indent) const
  351. {
  352. ASTNode::dump(indent);
  353. print_indent(indent);
  354. printf("While\n");
  355. predicate().dump(indent + 1);
  356. body().dump(indent + 1);
  357. }
  358. void ForStatement::dump(int indent) const
  359. {
  360. ASTNode::dump(indent);
  361. print_indent(indent);
  362. printf("For\n");
  363. if (init())
  364. init()->dump(indent + 1);
  365. if (test())
  366. test()->dump(indent + 1);
  367. if (update())
  368. update()->dump(indent + 1);
  369. body().dump(indent + 1);
  370. }
  371. Value Identifier::execute(Interpreter& interpreter) const
  372. {
  373. return interpreter.get_variable(string());
  374. }
  375. void Identifier::dump(int indent) const
  376. {
  377. print_indent(indent);
  378. printf("Identifier \"%s\"\n", m_string.characters());
  379. }
  380. Value AssignmentExpression::execute(Interpreter& interpreter) const
  381. {
  382. ASSERT(m_lhs->is_identifier());
  383. auto name = static_cast<const Identifier&>(*m_lhs).string();
  384. auto rhs_result = m_rhs->execute(interpreter);
  385. switch (m_op) {
  386. case AssignmentOp::Assignment:
  387. interpreter.set_variable(name, rhs_result);
  388. break;
  389. case AssignmentOp::AdditionAssignment:
  390. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  391. interpreter.set_variable(name, rhs_result);
  392. break;
  393. case AssignmentOp::SubtractionAssignment:
  394. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  395. interpreter.set_variable(name, rhs_result);
  396. break;
  397. case AssignmentOp::MultiplicationAssignment:
  398. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  399. interpreter.set_variable(name, rhs_result);
  400. break;
  401. case AssignmentOp::DivisionAssignment:
  402. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  403. interpreter.set_variable(name, rhs_result);
  404. break;
  405. }
  406. return rhs_result;
  407. }
  408. Value UpdateExpression::execute(Interpreter& interpreter) const
  409. {
  410. ASSERT(m_argument->is_identifier());
  411. auto name = static_cast<const Identifier&>(*m_argument).string();
  412. auto previous_value = interpreter.get_variable(name);
  413. ASSERT(previous_value.is_number());
  414. int op_result = 0;
  415. switch (m_op) {
  416. case UpdateOp::Increment:
  417. op_result = 1;
  418. break;
  419. case UpdateOp::Decrement:
  420. op_result = -1;
  421. break;
  422. }
  423. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  424. if (m_prefixed)
  425. return JS::Value(previous_value.as_double() + op_result);
  426. return previous_value;
  427. }
  428. void AssignmentExpression::dump(int indent) const
  429. {
  430. const char* op_string = nullptr;
  431. switch (m_op) {
  432. case AssignmentOp::Assignment:
  433. op_string = "=";
  434. break;
  435. case AssignmentOp::AdditionAssignment:
  436. op_string = "+=";
  437. break;
  438. case AssignmentOp::SubtractionAssignment:
  439. op_string = "-=";
  440. break;
  441. case AssignmentOp::MultiplicationAssignment:
  442. op_string = "*=";
  443. break;
  444. case AssignmentOp::DivisionAssignment:
  445. op_string = "/=";
  446. break;
  447. }
  448. ASTNode::dump(indent);
  449. print_indent(indent + 1);
  450. printf("%s\n", op_string);
  451. m_lhs->dump(indent + 1);
  452. m_rhs->dump(indent + 1);
  453. }
  454. void UpdateExpression::dump(int indent) const
  455. {
  456. const char* op_string = nullptr;
  457. switch (m_op) {
  458. case UpdateOp::Increment:
  459. op_string = "++";
  460. break;
  461. case UpdateOp::Decrement:
  462. op_string = "--";
  463. break;
  464. }
  465. ASTNode::dump(indent);
  466. print_indent(indent + 1);
  467. if (m_prefixed)
  468. printf("%s\n", op_string);
  469. m_argument->dump(indent + 1);
  470. if (!m_prefixed) {
  471. print_indent(indent + 1);
  472. printf("%s\n", op_string);
  473. }
  474. }
  475. Value VariableDeclaration::execute(Interpreter& interpreter) const
  476. {
  477. interpreter.declare_variable(name().string(), m_declaration_type);
  478. if (m_initializer) {
  479. auto initalizer_result = m_initializer->execute(interpreter);
  480. interpreter.set_variable(name().string(), initalizer_result, true);
  481. }
  482. return js_undefined();
  483. }
  484. void VariableDeclaration::dump(int indent) const
  485. {
  486. const char* declaration_type_string = nullptr;
  487. switch (m_declaration_type) {
  488. case DeclarationType::Let:
  489. declaration_type_string = "Let";
  490. break;
  491. case DeclarationType::Var:
  492. declaration_type_string = "Var";
  493. break;
  494. case DeclarationType::Const:
  495. declaration_type_string = "Const";
  496. break;
  497. }
  498. ASTNode::dump(indent);
  499. print_indent(indent + 1);
  500. printf("%s\n", declaration_type_string);
  501. m_name->dump(indent + 1);
  502. if (m_initializer)
  503. m_initializer->dump(indent + 1);
  504. }
  505. void ObjectExpression::dump(int indent) const
  506. {
  507. ASTNode::dump(indent);
  508. }
  509. void ExpressionStatement::dump(int indent) const
  510. {
  511. ASTNode::dump(indent);
  512. m_expression->dump(indent + 1);
  513. }
  514. Value ObjectExpression::execute(Interpreter& interpreter) const
  515. {
  516. return interpreter.heap().allocate<Object>();
  517. }
  518. void MemberExpression::dump(int indent) const
  519. {
  520. ASTNode::dump(indent);
  521. m_object->dump(indent + 1);
  522. m_property->dump(indent + 1);
  523. }
  524. Value MemberExpression::execute(Interpreter& interpreter) const
  525. {
  526. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  527. ASSERT(object_result.is_object());
  528. String property_name;
  529. if (m_property->is_identifier()) {
  530. property_name = static_cast<const Identifier&>(*m_property).string();
  531. } else {
  532. ASSERT_NOT_REACHED();
  533. }
  534. return object_result.as_object()->get(property_name);
  535. }
  536. Value StringLiteral::execute(Interpreter& interpreter) const
  537. {
  538. return js_string(interpreter.heap(), m_value);
  539. }
  540. Value NumericLiteral::execute(Interpreter&) const
  541. {
  542. return Value(m_value);
  543. }
  544. Value BooleanLiteral::execute(Interpreter&) const
  545. {
  546. return Value(m_value);
  547. }
  548. Value UndefinedLiteral::execute(Interpreter&) const
  549. {
  550. return js_undefined();
  551. }
  552. Value NullLiteral::execute(Interpreter&) const
  553. {
  554. return js_null();
  555. }
  556. }