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