AST.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 BinaryExpression::execute(Interpreter& interpreter) const
  94. {
  95. auto lhs_result = m_lhs->execute(interpreter);
  96. auto rhs_result = m_rhs->execute(interpreter);
  97. switch (m_op) {
  98. case BinaryOp::Plus:
  99. return add(lhs_result, rhs_result);
  100. case BinaryOp::Minus:
  101. return sub(lhs_result, rhs_result);
  102. case BinaryOp::Asterisk:
  103. return mul(lhs_result, rhs_result);
  104. case BinaryOp::Slash:
  105. return div(lhs_result, rhs_result);
  106. case BinaryOp::TypedEquals:
  107. return typed_eq(lhs_result, rhs_result);
  108. case BinaryOp::TypedInequals:
  109. return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
  110. case BinaryOp::GreaterThan:
  111. return greater_than(lhs_result, rhs_result);
  112. case BinaryOp::LessThan:
  113. return less_than(lhs_result, rhs_result);
  114. case BinaryOp::BitwiseAnd:
  115. return bitwise_and(lhs_result, rhs_result);
  116. case BinaryOp::BitwiseOr:
  117. return bitwise_or(lhs_result, rhs_result);
  118. case BinaryOp::BitwiseXor:
  119. return bitwise_xor(lhs_result, rhs_result);
  120. case BinaryOp::LeftShift:
  121. return left_shift(lhs_result, rhs_result);
  122. case BinaryOp::RightShift:
  123. return right_shift(lhs_result, rhs_result);
  124. }
  125. ASSERT_NOT_REACHED();
  126. }
  127. Value LogicalExpression::execute(Interpreter& interpreter) const
  128. {
  129. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  130. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  131. switch (m_op) {
  132. case LogicalOp::And:
  133. return Value(lhs_result && rhs_result);
  134. case LogicalOp::Or:
  135. return Value(lhs_result || rhs_result);
  136. }
  137. ASSERT_NOT_REACHED();
  138. }
  139. Value UnaryExpression::execute(Interpreter& interpreter) const
  140. {
  141. auto lhs_result = m_lhs->execute(interpreter);
  142. switch (m_op) {
  143. case UnaryOp::BitNot:
  144. return bitwise_not(lhs_result);
  145. case UnaryOp::Not:
  146. return Value(!lhs_result.to_boolean());
  147. }
  148. ASSERT_NOT_REACHED();
  149. }
  150. static void print_indent(int indent)
  151. {
  152. for (int i = 0; i < indent * 2; ++i)
  153. putchar(' ');
  154. }
  155. void ASTNode::dump(int indent) const
  156. {
  157. print_indent(indent);
  158. printf("%s\n", class_name());
  159. }
  160. void ScopeNode::dump(int indent) const
  161. {
  162. ASTNode::dump(indent);
  163. for (auto& child : children())
  164. child.dump(indent + 1);
  165. }
  166. void BinaryExpression::dump(int indent) const
  167. {
  168. const char* op_string = nullptr;
  169. switch (m_op) {
  170. case BinaryOp::Plus:
  171. op_string = "+";
  172. break;
  173. case BinaryOp::Minus:
  174. op_string = "-";
  175. break;
  176. case BinaryOp::Asterisk:
  177. op_string = "*";
  178. break;
  179. case BinaryOp::Slash:
  180. op_string = "/";
  181. break;
  182. case BinaryOp::TypedEquals:
  183. op_string = "===";
  184. break;
  185. case BinaryOp::TypedInequals:
  186. op_string = "!==";
  187. break;
  188. case BinaryOp::GreaterThan:
  189. op_string = ">";
  190. break;
  191. case BinaryOp::LessThan:
  192. op_string = "<";
  193. break;
  194. case BinaryOp::BitwiseAnd:
  195. op_string = "&";
  196. break;
  197. case BinaryOp::BitwiseOr:
  198. op_string = "|";
  199. break;
  200. case BinaryOp::BitwiseXor:
  201. op_string = "^";
  202. break;
  203. case BinaryOp::LeftShift:
  204. op_string = "<<";
  205. break;
  206. case BinaryOp::RightShift:
  207. op_string = ">>";
  208. break;
  209. }
  210. print_indent(indent);
  211. printf("%s\n", class_name());
  212. m_lhs->dump(indent + 1);
  213. print_indent(indent + 1);
  214. printf("%s\n", op_string);
  215. m_rhs->dump(indent + 1);
  216. }
  217. void LogicalExpression::dump(int indent) const
  218. {
  219. const char* op_string = nullptr;
  220. switch (m_op) {
  221. case LogicalOp::And:
  222. op_string = "&&";
  223. break;
  224. case LogicalOp::Or:
  225. op_string = "||";
  226. break;
  227. }
  228. print_indent(indent);
  229. printf("%s\n", class_name());
  230. m_lhs->dump(indent + 1);
  231. print_indent(indent + 1);
  232. printf("%s\n", op_string);
  233. m_rhs->dump(indent + 1);
  234. }
  235. void UnaryExpression::dump(int indent) const
  236. {
  237. const char* op_string = nullptr;
  238. switch (m_op) {
  239. case UnaryOp::BitNot:
  240. op_string = "~";
  241. break;
  242. case UnaryOp::Not:
  243. op_string = "!";
  244. break;
  245. }
  246. print_indent(indent);
  247. printf("%s\n", class_name());
  248. print_indent(indent + 1);
  249. printf("%s\n", op_string);
  250. m_lhs->dump(indent + 1);
  251. }
  252. void CallExpression::dump(int indent) const
  253. {
  254. print_indent(indent);
  255. printf("%s '%s'\n", class_name(), name().characters());
  256. }
  257. void StringLiteral::dump(int indent) const
  258. {
  259. print_indent(indent);
  260. printf("StringLiteral \"%s\"\n", m_value.characters());
  261. }
  262. void NumericLiteral::dump(int indent) const
  263. {
  264. print_indent(indent);
  265. printf("NumberLiteral %g\n", m_value);
  266. }
  267. void BooleanLiteral::dump(int indent) const
  268. {
  269. print_indent(indent);
  270. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  271. }
  272. void FunctionDeclaration::dump(int indent) const
  273. {
  274. bool first_time = true;
  275. StringBuilder parameters_builder;
  276. for (const auto& parameter : m_parameters) {
  277. if (first_time)
  278. first_time = false;
  279. else
  280. parameters_builder.append(',');
  281. parameters_builder.append(parameter);
  282. }
  283. print_indent(indent);
  284. printf("%s '%s(%s)'\n", class_name(), name().characters(), parameters_builder.build().characters());
  285. body().dump(indent + 1);
  286. }
  287. void ReturnStatement::dump(int indent) const
  288. {
  289. ASTNode::dump(indent);
  290. if (argument())
  291. argument()->dump(indent + 1);
  292. }
  293. void IfStatement::dump(int indent) const
  294. {
  295. ASTNode::dump(indent);
  296. print_indent(indent);
  297. printf("If\n");
  298. predicate().dump(indent + 1);
  299. consequent().dump(indent + 1);
  300. print_indent(indent);
  301. printf("Else\n");
  302. alternate().dump(indent + 1);
  303. }
  304. void WhileStatement::dump(int indent) const
  305. {
  306. ASTNode::dump(indent);
  307. print_indent(indent);
  308. printf("While\n");
  309. predicate().dump(indent + 1);
  310. body().dump(indent + 1);
  311. }
  312. Value Identifier::execute(Interpreter& interpreter) const
  313. {
  314. return interpreter.get_variable(string());
  315. }
  316. void Identifier::dump(int indent) const
  317. {
  318. print_indent(indent);
  319. printf("Identifier \"%s\"\n", m_string.characters());
  320. }
  321. Value AssignmentExpression::execute(Interpreter& interpreter) const
  322. {
  323. ASSERT(m_lhs->is_identifier());
  324. auto name = static_cast<const Identifier&>(*m_lhs).string();
  325. auto rhs_result = m_rhs->execute(interpreter);
  326. switch (m_op) {
  327. case AssignmentOp::Assign:
  328. interpreter.set_variable(name, rhs_result);
  329. break;
  330. }
  331. return rhs_result;
  332. }
  333. void AssignmentExpression::dump(int indent) const
  334. {
  335. const char* op_string = nullptr;
  336. switch (m_op) {
  337. case AssignmentOp::Assign:
  338. op_string = "=";
  339. break;
  340. }
  341. ASTNode::dump(indent);
  342. print_indent(indent + 1);
  343. printf("%s\n", op_string);
  344. m_lhs->dump(indent + 1);
  345. m_rhs->dump(indent + 1);
  346. }
  347. Value VariableDeclaration::execute(Interpreter& interpreter) const
  348. {
  349. interpreter.declare_variable(name().string(), m_declaration_type);
  350. if (m_initializer) {
  351. auto initalizer_result = m_initializer->execute(interpreter);
  352. interpreter.set_variable(name().string(), initalizer_result);
  353. }
  354. return js_undefined();
  355. }
  356. void VariableDeclaration::dump(int indent) const
  357. {
  358. const char* op_string = nullptr;
  359. switch (m_declaration_type) {
  360. case DeclarationType::Let:
  361. op_string = "Let";
  362. break;
  363. case DeclarationType::Var:
  364. op_string = "Var";
  365. break;
  366. }
  367. ASTNode::dump(indent);
  368. print_indent(indent + 1);
  369. printf("%s\n", op_string);
  370. m_name->dump(indent + 1);
  371. if (m_initializer)
  372. m_initializer->dump(indent + 1);
  373. }
  374. void ObjectExpression::dump(int indent) const
  375. {
  376. ASTNode::dump(indent);
  377. }
  378. void ExpressionStatement::dump(int indent) const
  379. {
  380. ASTNode::dump(indent);
  381. m_expression->dump(indent + 1);
  382. }
  383. Value ObjectExpression::execute(Interpreter& interpreter) const
  384. {
  385. return Value(interpreter.heap().allocate<Object>());
  386. }
  387. void MemberExpression::dump(int indent) const
  388. {
  389. ASTNode::dump(indent);
  390. m_object->dump(indent + 1);
  391. m_property->dump(indent + 1);
  392. }
  393. Value MemberExpression::execute(Interpreter& interpreter) const
  394. {
  395. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  396. ASSERT(object_result.is_object());
  397. String property_name;
  398. if (m_property->is_identifier()) {
  399. property_name = static_cast<const Identifier&>(*m_property).string();
  400. } else {
  401. ASSERT_NOT_REACHED();
  402. }
  403. return object_result.as_object()->get(property_name);
  404. }
  405. Value StringLiteral::execute(Interpreter& interpreter) const
  406. {
  407. return Value(js_string(interpreter.heap(), m_value));
  408. }
  409. Value NumericLiteral::execute(Interpreter&) const
  410. {
  411. return Value(m_value);
  412. }
  413. Value BooleanLiteral::execute(Interpreter&) const
  414. {
  415. return Value(m_value);
  416. }
  417. }