AST.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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, function);
  44. return 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. auto callee = interpreter.get_variable(name());
  53. ASSERT(callee.is_object());
  54. auto* callee_object = callee.as_object();
  55. Vector<Argument> passed_arguments;
  56. for (size_t i = 0; i < m_arguments.size(); ++i) {
  57. String name;
  58. if (callee_object->is_function())
  59. name = static_cast<Function&>(*callee_object).parameters()[i];
  60. auto value = m_arguments[i].execute(interpreter);
  61. dbg() << name << ": " << value;
  62. passed_arguments.append({ move(name), move(value) });
  63. }
  64. if (callee_object->is_function())
  65. return interpreter.run(static_cast<Function&>(*callee_object).body(), move(passed_arguments), ScopeType::Function);
  66. if (callee_object->is_native_function()) {
  67. return static_cast<NativeFunction&>(*callee_object).native_function()(interpreter, move(passed_arguments));
  68. }
  69. ASSERT_NOT_REACHED();
  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. for (auto& argument : m_arguments)
  287. argument.dump(indent + 1);
  288. }
  289. void StringLiteral::dump(int indent) const
  290. {
  291. print_indent(indent);
  292. printf("StringLiteral \"%s\"\n", m_value.characters());
  293. }
  294. void NumericLiteral::dump(int indent) const
  295. {
  296. print_indent(indent);
  297. printf("NumericLiteral %g\n", m_value);
  298. }
  299. void BooleanLiteral::dump(int indent) const
  300. {
  301. print_indent(indent);
  302. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  303. }
  304. void FunctionDeclaration::dump(int indent) const
  305. {
  306. bool first_time = true;
  307. StringBuilder parameters_builder;
  308. for (const auto& parameter : m_parameters) {
  309. if (first_time)
  310. first_time = false;
  311. else
  312. parameters_builder.append(',');
  313. parameters_builder.append(parameter);
  314. }
  315. print_indent(indent);
  316. printf("%s '%s(%s)'\n", class_name(), name().characters(), parameters_builder.build().characters());
  317. body().dump(indent + 1);
  318. }
  319. void ReturnStatement::dump(int indent) const
  320. {
  321. ASTNode::dump(indent);
  322. if (argument())
  323. argument()->dump(indent + 1);
  324. }
  325. void IfStatement::dump(int indent) const
  326. {
  327. ASTNode::dump(indent);
  328. print_indent(indent);
  329. printf("If\n");
  330. predicate().dump(indent + 1);
  331. consequent().dump(indent + 1);
  332. print_indent(indent);
  333. printf("Else\n");
  334. alternate().dump(indent + 1);
  335. }
  336. void WhileStatement::dump(int indent) const
  337. {
  338. ASTNode::dump(indent);
  339. print_indent(indent);
  340. printf("While\n");
  341. predicate().dump(indent + 1);
  342. body().dump(indent + 1);
  343. }
  344. void ForStatement::dump(int indent) const
  345. {
  346. ASTNode::dump(indent);
  347. print_indent(indent);
  348. printf("For\n");
  349. if (init())
  350. init()->dump(indent + 1);
  351. if (test())
  352. test()->dump(indent + 1);
  353. if (update())
  354. update()->dump(indent + 1);
  355. body().dump(indent + 1);
  356. }
  357. Value Identifier::execute(Interpreter& interpreter) const
  358. {
  359. return interpreter.get_variable(string());
  360. }
  361. void Identifier::dump(int indent) const
  362. {
  363. print_indent(indent);
  364. printf("Identifier \"%s\"\n", m_string.characters());
  365. }
  366. Value AssignmentExpression::execute(Interpreter& interpreter) const
  367. {
  368. ASSERT(m_lhs->is_identifier());
  369. auto name = static_cast<const Identifier&>(*m_lhs).string();
  370. auto rhs_result = m_rhs->execute(interpreter);
  371. switch (m_op) {
  372. case AssignmentOp::Assignment:
  373. interpreter.set_variable(name, rhs_result);
  374. break;
  375. case AssignmentOp::AdditionAssignment:
  376. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  377. interpreter.set_variable(name, rhs_result);
  378. break;
  379. case AssignmentOp::SubtractionAssignment:
  380. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  381. interpreter.set_variable(name, rhs_result);
  382. break;
  383. case AssignmentOp::MultiplicationAssignment:
  384. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  385. interpreter.set_variable(name, rhs_result);
  386. break;
  387. case AssignmentOp::DivisionAssignment:
  388. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  389. interpreter.set_variable(name, rhs_result);
  390. break;
  391. }
  392. return rhs_result;
  393. }
  394. Value UpdateExpression::execute(Interpreter& interpreter) const
  395. {
  396. ASSERT(m_argument->is_identifier());
  397. auto name = static_cast<const Identifier&>(*m_argument).string();
  398. auto previous_value = interpreter.get_variable(name);
  399. ASSERT(previous_value.is_number());
  400. switch (m_op) {
  401. case UpdateOp::Increment:
  402. interpreter.set_variable(name, Value(previous_value.as_double() + 1));
  403. break;
  404. case UpdateOp::Decrement:
  405. interpreter.set_variable(name, Value(previous_value.as_double() - 1));
  406. break;
  407. }
  408. return previous_value;
  409. }
  410. void AssignmentExpression::dump(int indent) const
  411. {
  412. const char* op_string = nullptr;
  413. switch (m_op) {
  414. case AssignmentOp::Assignment:
  415. op_string = "=";
  416. break;
  417. case AssignmentOp::AdditionAssignment:
  418. op_string = "+=";
  419. break;
  420. case AssignmentOp::SubtractionAssignment:
  421. op_string = "-=";
  422. break;
  423. case AssignmentOp::MultiplicationAssignment:
  424. op_string = "*=";
  425. break;
  426. case AssignmentOp::DivisionAssignment:
  427. op_string = "/=";
  428. break;
  429. }
  430. ASTNode::dump(indent);
  431. print_indent(indent + 1);
  432. printf("%s\n", op_string);
  433. m_lhs->dump(indent + 1);
  434. m_rhs->dump(indent + 1);
  435. }
  436. void UpdateExpression::dump(int indent) const
  437. {
  438. const char* op_string = nullptr;
  439. switch (m_op) {
  440. case UpdateOp::Increment:
  441. op_string = "++";
  442. break;
  443. case UpdateOp::Decrement:
  444. op_string = "--";
  445. break;
  446. }
  447. ASTNode::dump(indent);
  448. print_indent(indent + 1);
  449. printf("%s\n", op_string);
  450. m_argument->dump(indent + 1);
  451. }
  452. Value VariableDeclaration::execute(Interpreter& interpreter) const
  453. {
  454. interpreter.declare_variable(name().string(), m_declaration_type);
  455. if (m_initializer) {
  456. auto initalizer_result = m_initializer->execute(interpreter);
  457. interpreter.set_variable(name().string(), initalizer_result);
  458. }
  459. return js_undefined();
  460. }
  461. void VariableDeclaration::dump(int indent) const
  462. {
  463. const char* declaration_type_string = nullptr;
  464. switch (m_declaration_type) {
  465. case DeclarationType::Let:
  466. declaration_type_string = "Let";
  467. break;
  468. case DeclarationType::Var:
  469. declaration_type_string = "Var";
  470. break;
  471. case DeclarationType::Const:
  472. declaration_type_string = "Const";
  473. break;
  474. }
  475. ASTNode::dump(indent);
  476. print_indent(indent + 1);
  477. printf("%s\n", declaration_type_string);
  478. m_name->dump(indent + 1);
  479. if (m_initializer)
  480. m_initializer->dump(indent + 1);
  481. }
  482. void ObjectExpression::dump(int indent) const
  483. {
  484. ASTNode::dump(indent);
  485. }
  486. void ExpressionStatement::dump(int indent) const
  487. {
  488. ASTNode::dump(indent);
  489. m_expression->dump(indent + 1);
  490. }
  491. Value ObjectExpression::execute(Interpreter& interpreter) const
  492. {
  493. return interpreter.heap().allocate<Object>();
  494. }
  495. void MemberExpression::dump(int indent) const
  496. {
  497. ASTNode::dump(indent);
  498. m_object->dump(indent + 1);
  499. m_property->dump(indent + 1);
  500. }
  501. Value MemberExpression::execute(Interpreter& interpreter) const
  502. {
  503. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  504. ASSERT(object_result.is_object());
  505. String property_name;
  506. if (m_property->is_identifier()) {
  507. property_name = static_cast<const Identifier&>(*m_property).string();
  508. } else {
  509. ASSERT_NOT_REACHED();
  510. }
  511. return object_result.as_object()->get(property_name);
  512. }
  513. Value StringLiteral::execute(Interpreter& interpreter) const
  514. {
  515. return js_string(interpreter.heap(), m_value);
  516. }
  517. Value NumericLiteral::execute(Interpreter&) const
  518. {
  519. return Value(m_value);
  520. }
  521. Value BooleanLiteral::execute(Interpreter&) const
  522. {
  523. return Value(m_value);
  524. }
  525. }