AST.cpp 16 KB

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