AST.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/ScriptFunction.h>
  32. #include <LibJS/Runtime/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. auto& call_frame = interpreter.push_call_frame();
  56. for (size_t i = 0; i < m_arguments.size(); ++i)
  57. call_frame.arguments.append(m_arguments[i].execute(interpreter));
  58. if (m_callee->is_member_expression())
  59. call_frame.this_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter).to_object(interpreter.heap());
  60. auto result = function->call(interpreter, call_frame.arguments);
  61. interpreter.pop_call_frame();
  62. return result;
  63. }
  64. Value ReturnStatement::execute(Interpreter& interpreter) const
  65. {
  66. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  67. interpreter.do_return();
  68. return value;
  69. }
  70. Value IfStatement::execute(Interpreter& interpreter) const
  71. {
  72. auto predicate_result = m_predicate->execute(interpreter);
  73. if (predicate_result.to_boolean())
  74. return interpreter.run(*m_consequent);
  75. else
  76. return interpreter.run(*m_alternate);
  77. }
  78. Value WhileStatement::execute(Interpreter& interpreter) const
  79. {
  80. Value last_value = js_undefined();
  81. while (m_predicate->execute(interpreter).to_boolean()) {
  82. last_value = interpreter.run(*m_body);
  83. }
  84. return last_value;
  85. }
  86. Value ForStatement::execute(Interpreter& interpreter) const
  87. {
  88. OwnPtr<BlockStatement> wrapper;
  89. if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
  90. wrapper = make<BlockStatement>();
  91. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  92. }
  93. Value last_value = js_undefined();
  94. if (m_init)
  95. m_init->execute(interpreter);
  96. if (m_test) {
  97. while (m_test->execute(interpreter).to_boolean()) {
  98. last_value = interpreter.run(*m_body);
  99. if (m_update)
  100. m_update->execute(interpreter);
  101. }
  102. } else {
  103. while (true) {
  104. last_value = interpreter.run(*m_body);
  105. if (m_update)
  106. m_update->execute(interpreter);
  107. }
  108. }
  109. if (wrapper)
  110. interpreter.exit_scope(*wrapper);
  111. return last_value;
  112. }
  113. Value BinaryExpression::execute(Interpreter& interpreter) const
  114. {
  115. auto lhs_result = m_lhs->execute(interpreter);
  116. auto rhs_result = m_rhs->execute(interpreter);
  117. switch (m_op) {
  118. case BinaryOp::Plus:
  119. return add(lhs_result, rhs_result);
  120. case BinaryOp::Minus:
  121. return sub(lhs_result, rhs_result);
  122. case BinaryOp::Asterisk:
  123. return mul(lhs_result, rhs_result);
  124. case BinaryOp::Slash:
  125. return div(lhs_result, rhs_result);
  126. case BinaryOp::TypedEquals:
  127. return typed_eq(lhs_result, rhs_result);
  128. case BinaryOp::TypedInequals:
  129. return Value(!typed_eq(lhs_result, rhs_result).as_bool());
  130. case BinaryOp::AbstractEquals:
  131. return eq(lhs_result, rhs_result);
  132. case BinaryOp::AbstractInequals:
  133. return Value(!eq(lhs_result, rhs_result).as_bool());
  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. case UnaryOp::Typeof:
  176. switch (lhs_result.type()) {
  177. case Value::Type::Undefined:
  178. return js_string(interpreter.heap(), "undefined");
  179. case Value::Type::Null:
  180. // yes, this is on purpose. yes, this is how javascript works.
  181. // yes, it's silly.
  182. return js_string(interpreter.heap(), "object");
  183. case Value::Type::Number:
  184. return js_string(interpreter.heap(), "number");
  185. case Value::Type::String:
  186. return js_string(interpreter.heap(), "string");
  187. case Value::Type::Object:
  188. return js_string(interpreter.heap(), "object");
  189. case Value::Type::Boolean:
  190. return js_string(interpreter.heap(), "boolean");
  191. }
  192. }
  193. ASSERT_NOT_REACHED();
  194. }
  195. static void print_indent(int indent)
  196. {
  197. for (int i = 0; i < indent * 2; ++i)
  198. putchar(' ');
  199. }
  200. void ASTNode::dump(int indent) const
  201. {
  202. print_indent(indent);
  203. printf("%s\n", class_name());
  204. }
  205. void ScopeNode::dump(int indent) const
  206. {
  207. ASTNode::dump(indent);
  208. for (auto& child : children())
  209. child.dump(indent + 1);
  210. }
  211. void BinaryExpression::dump(int indent) const
  212. {
  213. const char* op_string = nullptr;
  214. switch (m_op) {
  215. case BinaryOp::Plus:
  216. op_string = "+";
  217. break;
  218. case BinaryOp::Minus:
  219. op_string = "-";
  220. break;
  221. case BinaryOp::Asterisk:
  222. op_string = "*";
  223. break;
  224. case BinaryOp::Slash:
  225. op_string = "/";
  226. break;
  227. case BinaryOp::TypedEquals:
  228. op_string = "===";
  229. break;
  230. case BinaryOp::TypedInequals:
  231. op_string = "!==";
  232. break;
  233. case BinaryOp::AbstractEquals:
  234. op_string = "==";
  235. break;
  236. case BinaryOp::AbstractInequals:
  237. op_string = "!=";
  238. break;
  239. case BinaryOp::GreaterThan:
  240. op_string = ">";
  241. break;
  242. case BinaryOp::GreaterThanEquals:
  243. op_string = ">=";
  244. break;
  245. case BinaryOp::LessThan:
  246. op_string = "<";
  247. break;
  248. case BinaryOp::LessThanEquals:
  249. op_string = "<=";
  250. break;
  251. case BinaryOp::BitwiseAnd:
  252. op_string = "&";
  253. break;
  254. case BinaryOp::BitwiseOr:
  255. op_string = "|";
  256. break;
  257. case BinaryOp::BitwiseXor:
  258. op_string = "^";
  259. break;
  260. case BinaryOp::LeftShift:
  261. op_string = "<<";
  262. break;
  263. case BinaryOp::RightShift:
  264. op_string = ">>";
  265. break;
  266. }
  267. print_indent(indent);
  268. printf("%s\n", class_name());
  269. m_lhs->dump(indent + 1);
  270. print_indent(indent + 1);
  271. printf("%s\n", op_string);
  272. m_rhs->dump(indent + 1);
  273. }
  274. void LogicalExpression::dump(int indent) const
  275. {
  276. const char* op_string = nullptr;
  277. switch (m_op) {
  278. case LogicalOp::And:
  279. op_string = "&&";
  280. break;
  281. case LogicalOp::Or:
  282. op_string = "||";
  283. break;
  284. }
  285. print_indent(indent);
  286. printf("%s\n", class_name());
  287. m_lhs->dump(indent + 1);
  288. print_indent(indent + 1);
  289. printf("%s\n", op_string);
  290. m_rhs->dump(indent + 1);
  291. }
  292. void UnaryExpression::dump(int indent) const
  293. {
  294. const char* op_string = nullptr;
  295. switch (m_op) {
  296. case UnaryOp::BitwiseNot:
  297. op_string = "~";
  298. break;
  299. case UnaryOp::Not:
  300. op_string = "!";
  301. break;
  302. case UnaryOp::Typeof:
  303. op_string = "typeof ";
  304. break;
  305. }
  306. print_indent(indent);
  307. printf("%s\n", class_name());
  308. print_indent(indent + 1);
  309. printf("%s\n", op_string);
  310. m_lhs->dump(indent + 1);
  311. }
  312. void CallExpression::dump(int indent) const
  313. {
  314. ASTNode::dump(indent);
  315. m_callee->dump(indent + 1);
  316. for (auto& argument : m_arguments)
  317. argument.dump(indent + 1);
  318. }
  319. void StringLiteral::dump(int indent) const
  320. {
  321. print_indent(indent);
  322. printf("StringLiteral \"%s\"\n", m_value.characters());
  323. }
  324. void NumericLiteral::dump(int indent) const
  325. {
  326. print_indent(indent);
  327. printf("NumericLiteral %g\n", m_value);
  328. }
  329. void BooleanLiteral::dump(int indent) const
  330. {
  331. print_indent(indent);
  332. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  333. }
  334. void UndefinedLiteral::dump(int indent) const
  335. {
  336. print_indent(indent);
  337. printf("undefined\n");
  338. }
  339. void NullLiteral::dump(int indent) const
  340. {
  341. print_indent(indent);
  342. printf("null\n");
  343. }
  344. void FunctionDeclaration::dump(int indent) const
  345. {
  346. bool first_time = true;
  347. StringBuilder parameters_builder;
  348. for (const auto& parameter : m_parameters) {
  349. if (first_time)
  350. first_time = false;
  351. else
  352. parameters_builder.append(',');
  353. parameters_builder.append(parameter);
  354. }
  355. print_indent(indent);
  356. printf("%s '%s(%s)'\n", class_name(), name().characters(), parameters_builder.build().characters());
  357. body().dump(indent + 1);
  358. }
  359. void ReturnStatement::dump(int indent) const
  360. {
  361. ASTNode::dump(indent);
  362. if (argument())
  363. argument()->dump(indent + 1);
  364. }
  365. void IfStatement::dump(int indent) const
  366. {
  367. ASTNode::dump(indent);
  368. print_indent(indent);
  369. printf("If\n");
  370. predicate().dump(indent + 1);
  371. consequent().dump(indent + 1);
  372. print_indent(indent);
  373. printf("Else\n");
  374. alternate().dump(indent + 1);
  375. }
  376. void WhileStatement::dump(int indent) const
  377. {
  378. ASTNode::dump(indent);
  379. print_indent(indent);
  380. printf("While\n");
  381. predicate().dump(indent + 1);
  382. body().dump(indent + 1);
  383. }
  384. void ForStatement::dump(int indent) const
  385. {
  386. ASTNode::dump(indent);
  387. print_indent(indent);
  388. printf("For\n");
  389. if (init())
  390. init()->dump(indent + 1);
  391. if (test())
  392. test()->dump(indent + 1);
  393. if (update())
  394. update()->dump(indent + 1);
  395. body().dump(indent + 1);
  396. }
  397. Value Identifier::execute(Interpreter& interpreter) const
  398. {
  399. return interpreter.get_variable(string());
  400. }
  401. void Identifier::dump(int indent) const
  402. {
  403. print_indent(indent);
  404. printf("Identifier \"%s\"\n", m_string.characters());
  405. }
  406. Value AssignmentExpression::execute(Interpreter& interpreter) const
  407. {
  408. ASSERT(m_lhs->is_identifier());
  409. auto name = static_cast<const Identifier&>(*m_lhs).string();
  410. auto rhs_result = m_rhs->execute(interpreter);
  411. switch (m_op) {
  412. case AssignmentOp::Assignment:
  413. interpreter.set_variable(name, rhs_result);
  414. break;
  415. case AssignmentOp::AdditionAssignment:
  416. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  417. interpreter.set_variable(name, rhs_result);
  418. break;
  419. case AssignmentOp::SubtractionAssignment:
  420. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  421. interpreter.set_variable(name, rhs_result);
  422. break;
  423. case AssignmentOp::MultiplicationAssignment:
  424. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  425. interpreter.set_variable(name, rhs_result);
  426. break;
  427. case AssignmentOp::DivisionAssignment:
  428. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  429. interpreter.set_variable(name, rhs_result);
  430. break;
  431. }
  432. return rhs_result;
  433. }
  434. Value UpdateExpression::execute(Interpreter& interpreter) const
  435. {
  436. ASSERT(m_argument->is_identifier());
  437. auto name = static_cast<const Identifier&>(*m_argument).string();
  438. auto previous_value = interpreter.get_variable(name);
  439. ASSERT(previous_value.is_number());
  440. int op_result = 0;
  441. switch (m_op) {
  442. case UpdateOp::Increment:
  443. op_result = 1;
  444. break;
  445. case UpdateOp::Decrement:
  446. op_result = -1;
  447. break;
  448. }
  449. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  450. if (m_prefixed)
  451. return JS::Value(previous_value.as_double() + op_result);
  452. return previous_value;
  453. }
  454. void AssignmentExpression::dump(int indent) const
  455. {
  456. const char* op_string = nullptr;
  457. switch (m_op) {
  458. case AssignmentOp::Assignment:
  459. op_string = "=";
  460. break;
  461. case AssignmentOp::AdditionAssignment:
  462. op_string = "+=";
  463. break;
  464. case AssignmentOp::SubtractionAssignment:
  465. op_string = "-=";
  466. break;
  467. case AssignmentOp::MultiplicationAssignment:
  468. op_string = "*=";
  469. break;
  470. case AssignmentOp::DivisionAssignment:
  471. op_string = "/=";
  472. break;
  473. }
  474. ASTNode::dump(indent);
  475. print_indent(indent + 1);
  476. printf("%s\n", op_string);
  477. m_lhs->dump(indent + 1);
  478. m_rhs->dump(indent + 1);
  479. }
  480. void UpdateExpression::dump(int indent) const
  481. {
  482. const char* op_string = nullptr;
  483. switch (m_op) {
  484. case UpdateOp::Increment:
  485. op_string = "++";
  486. break;
  487. case UpdateOp::Decrement:
  488. op_string = "--";
  489. break;
  490. }
  491. ASTNode::dump(indent);
  492. print_indent(indent + 1);
  493. if (m_prefixed)
  494. printf("%s\n", op_string);
  495. m_argument->dump(indent + 1);
  496. if (!m_prefixed) {
  497. print_indent(indent + 1);
  498. printf("%s\n", op_string);
  499. }
  500. }
  501. Value VariableDeclaration::execute(Interpreter& interpreter) const
  502. {
  503. interpreter.declare_variable(name().string(), m_declaration_type);
  504. if (m_initializer) {
  505. auto initalizer_result = m_initializer->execute(interpreter);
  506. interpreter.set_variable(name().string(), initalizer_result, true);
  507. }
  508. return js_undefined();
  509. }
  510. void VariableDeclaration::dump(int indent) const
  511. {
  512. const char* declaration_type_string = nullptr;
  513. switch (m_declaration_type) {
  514. case DeclarationType::Let:
  515. declaration_type_string = "Let";
  516. break;
  517. case DeclarationType::Var:
  518. declaration_type_string = "Var";
  519. break;
  520. case DeclarationType::Const:
  521. declaration_type_string = "Const";
  522. break;
  523. }
  524. ASTNode::dump(indent);
  525. print_indent(indent + 1);
  526. printf("%s\n", declaration_type_string);
  527. m_name->dump(indent + 1);
  528. if (m_initializer)
  529. m_initializer->dump(indent + 1);
  530. }
  531. void ObjectExpression::dump(int indent) const
  532. {
  533. ASTNode::dump(indent);
  534. }
  535. void ExpressionStatement::dump(int indent) const
  536. {
  537. ASTNode::dump(indent);
  538. m_expression->dump(indent + 1);
  539. }
  540. Value ObjectExpression::execute(Interpreter& interpreter) const
  541. {
  542. return interpreter.heap().allocate<Object>();
  543. }
  544. void MemberExpression::dump(int indent) const
  545. {
  546. ASTNode::dump(indent);
  547. m_object->dump(indent + 1);
  548. m_property->dump(indent + 1);
  549. }
  550. Value MemberExpression::execute(Interpreter& interpreter) const
  551. {
  552. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  553. ASSERT(object_result.is_object());
  554. String property_name;
  555. if (m_property->is_identifier()) {
  556. property_name = static_cast<const Identifier&>(*m_property).string();
  557. } else {
  558. ASSERT_NOT_REACHED();
  559. }
  560. return object_result.as_object()->get(property_name);
  561. }
  562. Value StringLiteral::execute(Interpreter& interpreter) const
  563. {
  564. return js_string(interpreter.heap(), m_value);
  565. }
  566. Value NumericLiteral::execute(Interpreter&) const
  567. {
  568. return Value(m_value);
  569. }
  570. Value BooleanLiteral::execute(Interpreter&) const
  571. {
  572. return Value(m_value);
  573. }
  574. Value UndefinedLiteral::execute(Interpreter&) const
  575. {
  576. return js_undefined();
  577. }
  578. Value NullLiteral::execute(Interpreter&) const
  579. {
  580. return js_null();
  581. }
  582. }