AST.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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/NativeFunction.h>
  32. #include <LibJS/PrimitiveString.h>
  33. #include <LibJS/Value.h>
  34. #include <stdio.h>
  35. namespace JS {
  36. Value ScopeNode::execute(Interpreter& interpreter) const
  37. {
  38. return interpreter.run(*this);
  39. }
  40. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  41. {
  42. auto* function = interpreter.heap().allocate<Function>(name(), body(), parameters());
  43. interpreter.set_variable(m_name, Value(function));
  44. return Value(function);
  45. }
  46. Value ExpressionStatement::execute(Interpreter& interpreter) const
  47. {
  48. return m_expression->execute(interpreter);
  49. }
  50. Value CallExpression::execute(Interpreter& interpreter) const
  51. {
  52. if (name() == "$gc") {
  53. interpreter.heap().collect_garbage();
  54. return js_undefined();
  55. }
  56. auto callee = interpreter.get_variable(name());
  57. ASSERT(callee.is_object());
  58. auto* callee_object = callee.as_object();
  59. Vector<Argument> passed_arguments;
  60. for (size_t i = 0; i < m_arguments.size(); ++i) {
  61. String name;
  62. if (callee_object->is_function())
  63. name = static_cast<Function&>(*callee_object).parameters()[i];
  64. auto value = m_arguments[i].execute(interpreter);
  65. dbg() << name << ": " << value;
  66. passed_arguments.append({ move(name), move(value) });
  67. }
  68. if (callee_object->is_function())
  69. return interpreter.run(static_cast<Function&>(*callee_object).body(), move(passed_arguments), ScopeType::Function);
  70. if (callee_object->is_native_function()) {
  71. return static_cast<NativeFunction&>(*callee_object).native_function()(move(passed_arguments));
  72. }
  73. ASSERT_NOT_REACHED();
  74. }
  75. Value ReturnStatement::execute(Interpreter& interpreter) const
  76. {
  77. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  78. interpreter.do_return();
  79. return value;
  80. }
  81. Value IfStatement::execute(Interpreter& interpreter) const
  82. {
  83. auto predicate_result = m_predicate->execute(interpreter);
  84. if (predicate_result.to_boolean())
  85. return interpreter.run(*m_consequent);
  86. else
  87. return interpreter.run(*m_alternate);
  88. }
  89. Value WhileStatement::execute(Interpreter& interpreter) const
  90. {
  91. Value last_value = js_undefined();
  92. while (m_predicate->execute(interpreter).to_boolean()) {
  93. last_value = interpreter.run(*m_body);
  94. }
  95. return last_value;
  96. }
  97. Value ForStatement::execute(Interpreter& interpreter) const
  98. {
  99. Value last_value = js_undefined();
  100. if (m_init)
  101. m_init->execute(interpreter);
  102. if (m_test) {
  103. while (m_test->execute(interpreter).to_boolean()) {
  104. last_value = interpreter.run(*m_body);
  105. if (m_update)
  106. m_update->execute(interpreter);
  107. }
  108. } else {
  109. while (true) {
  110. last_value = interpreter.run(*m_body);
  111. if (m_update)
  112. m_update->execute(interpreter);
  113. }
  114. }
  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::BitNot:
  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::BitNot:
  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. print_indent(indent);
  289. printf("%s '%s'\n", class_name(), name().characters());
  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. switch (m_op) {
  405. case UpdateOp::Increment:
  406. interpreter.set_variable(name, Value(previous_value.as_double() + 1));
  407. break;
  408. case UpdateOp::Decrement:
  409. interpreter.set_variable(name, Value(previous_value.as_double() - 1));
  410. break;
  411. }
  412. return previous_value;
  413. }
  414. void AssignmentExpression::dump(int indent) const
  415. {
  416. const char* op_string = nullptr;
  417. switch (m_op) {
  418. case AssignmentOp::Assignment:
  419. op_string = "=";
  420. break;
  421. case AssignmentOp::AdditionAssignment:
  422. op_string = "+=";
  423. break;
  424. case AssignmentOp::SubtractionAssignment:
  425. op_string = "-=";
  426. break;
  427. case AssignmentOp::MultiplicationAssignment:
  428. op_string = "*=";
  429. break;
  430. case AssignmentOp::DivisionAssignment:
  431. op_string = "/=";
  432. break;
  433. }
  434. ASTNode::dump(indent);
  435. print_indent(indent + 1);
  436. printf("%s\n", op_string);
  437. m_lhs->dump(indent + 1);
  438. m_rhs->dump(indent + 1);
  439. }
  440. void UpdateExpression::dump(int indent) const
  441. {
  442. const char* op_string = nullptr;
  443. switch (m_op) {
  444. case UpdateOp::Increment:
  445. op_string = "++";
  446. break;
  447. case UpdateOp::Decrement:
  448. op_string = "--";
  449. break;
  450. }
  451. ASTNode::dump(indent);
  452. print_indent(indent + 1);
  453. printf("%s\n", op_string);
  454. m_argument->dump(indent + 1);
  455. }
  456. Value VariableDeclaration::execute(Interpreter& interpreter) const
  457. {
  458. interpreter.declare_variable(name().string(), m_declaration_type);
  459. if (m_initializer) {
  460. auto initalizer_result = m_initializer->execute(interpreter);
  461. interpreter.set_variable(name().string(), initalizer_result);
  462. }
  463. return js_undefined();
  464. }
  465. void VariableDeclaration::dump(int indent) const
  466. {
  467. const char* declaration_type_string = nullptr;
  468. switch (m_declaration_type) {
  469. case DeclarationType::Let:
  470. declaration_type_string = "Let";
  471. break;
  472. case DeclarationType::Var:
  473. declaration_type_string = "Var";
  474. break;
  475. case DeclarationType::Const:
  476. declaration_type_string = "Const";
  477. break;
  478. }
  479. ASTNode::dump(indent);
  480. print_indent(indent + 1);
  481. printf("%s\n", declaration_type_string);
  482. m_name->dump(indent + 1);
  483. if (m_initializer)
  484. m_initializer->dump(indent + 1);
  485. }
  486. void ObjectExpression::dump(int indent) const
  487. {
  488. ASTNode::dump(indent);
  489. }
  490. void ExpressionStatement::dump(int indent) const
  491. {
  492. ASTNode::dump(indent);
  493. m_expression->dump(indent + 1);
  494. }
  495. Value ObjectExpression::execute(Interpreter& interpreter) const
  496. {
  497. return Value(interpreter.heap().allocate<Object>());
  498. }
  499. void MemberExpression::dump(int indent) const
  500. {
  501. ASTNode::dump(indent);
  502. m_object->dump(indent + 1);
  503. m_property->dump(indent + 1);
  504. }
  505. Value MemberExpression::execute(Interpreter& interpreter) const
  506. {
  507. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  508. ASSERT(object_result.is_object());
  509. String property_name;
  510. if (m_property->is_identifier()) {
  511. property_name = static_cast<const Identifier&>(*m_property).string();
  512. } else {
  513. ASSERT_NOT_REACHED();
  514. }
  515. return object_result.as_object()->get(property_name);
  516. }
  517. Value StringLiteral::execute(Interpreter& interpreter) const
  518. {
  519. return Value(js_string(interpreter.heap(), m_value));
  520. }
  521. Value NumericLiteral::execute(Interpreter&) const
  522. {
  523. return Value(m_value);
  524. }
  525. Value BooleanLiteral::execute(Interpreter&) const
  526. {
  527. return Value(m_value);
  528. }
  529. }