AST.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <LibJS/AST.h>
  27. #include <LibJS/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Value.h>
  30. #include <stdio.h>
  31. namespace JS {
  32. Value ScopeNode::execute(Interpreter& interpreter) const
  33. {
  34. return interpreter.run(*this);
  35. }
  36. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  37. {
  38. auto* function = interpreter.heap().allocate<Function>(name(), body());
  39. interpreter.set_variable(m_name, Value(function));
  40. return Value(function);
  41. }
  42. Value ExpressionStatement::execute(Interpreter& interpreter) const
  43. {
  44. return m_expression->execute(interpreter);
  45. }
  46. Value CallExpression::execute(Interpreter& interpreter) const
  47. {
  48. if (name() == "$gc") {
  49. interpreter.heap().collect_garbage();
  50. return js_undefined();
  51. }
  52. auto callee = interpreter.get_variable(name());
  53. ASSERT(callee.is_object());
  54. auto* callee_object = callee.as_object();
  55. ASSERT(callee_object->is_function());
  56. auto& function = static_cast<Function&>(*callee_object);
  57. return interpreter.run(function.body(), ScopeType::Function);
  58. }
  59. Value ReturnStatement::execute(Interpreter& interpreter) const
  60. {
  61. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  62. interpreter.do_return();
  63. return value;
  64. }
  65. Value IfStatement::execute(Interpreter& interpreter) const
  66. {
  67. auto predicate_result = m_predicate->execute(interpreter);
  68. if (predicate_result.to_boolean())
  69. return interpreter.run(*m_consequent);
  70. else
  71. return interpreter.run(*m_alternate);
  72. }
  73. Value WhileStatement::execute(Interpreter& interpreter) const
  74. {
  75. Value last_value = js_undefined();
  76. while (m_predicate->execute(interpreter).to_boolean()) {
  77. last_value = interpreter.run(*m_body);
  78. }
  79. return last_value;
  80. }
  81. Value add(Value lhs, Value rhs)
  82. {
  83. ASSERT(lhs.is_number());
  84. ASSERT(rhs.is_number());
  85. return Value(lhs.as_double() + rhs.as_double());
  86. }
  87. Value sub(Value lhs, Value rhs)
  88. {
  89. ASSERT(lhs.is_number());
  90. ASSERT(rhs.is_number());
  91. return Value(lhs.as_double() - rhs.as_double());
  92. }
  93. const Value typed_eq(const Value lhs, const Value rhs)
  94. {
  95. if (rhs.type() != lhs.type())
  96. return Value(false);
  97. switch (lhs.type()) {
  98. case Value::Type::Undefined:
  99. return Value(true);
  100. case Value::Type::Null:
  101. return Value(true);
  102. case Value::Type::Number:
  103. return Value(lhs.as_double() == rhs.as_double());
  104. case Value::Type::String:
  105. return Value(lhs.as_string() == rhs.as_string());
  106. case Value::Type::Boolean:
  107. return Value(lhs.as_bool() == rhs.as_bool());
  108. case Value::Type::Object:
  109. return Value(lhs.as_object() == rhs.as_object());
  110. }
  111. ASSERT_NOT_REACHED();
  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::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::LessThan:
  129. return less_than(lhs_result, rhs_result);
  130. case BinaryOp::BitwiseAnd:
  131. return bitwise_and(lhs_result, rhs_result);
  132. case BinaryOp::BitwiseOr:
  133. return bitwise_or(lhs_result, rhs_result);
  134. case BinaryOp::BitwiseXor:
  135. return bitwise_xor(lhs_result, rhs_result);
  136. case BinaryOp::LeftShift:
  137. return left_shift(lhs_result, rhs_result);
  138. case BinaryOp::RightShift:
  139. return right_shift(lhs_result, rhs_result);
  140. }
  141. ASSERT_NOT_REACHED();
  142. }
  143. Value LogicalExpression::execute(Interpreter& interpreter) const
  144. {
  145. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  146. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  147. switch (m_op) {
  148. case LogicalOp::And:
  149. return Value(lhs_result && rhs_result);
  150. case LogicalOp::Or:
  151. return Value(lhs_result || rhs_result);
  152. }
  153. ASSERT_NOT_REACHED();
  154. }
  155. Value UnaryExpression::execute(Interpreter& interpreter) const
  156. {
  157. auto lhs_result = m_lhs->execute(interpreter);
  158. switch (m_op) {
  159. case UnaryOp::BitNot:
  160. return bitwise_not(lhs_result);
  161. case UnaryOp::Not:
  162. return Value(!lhs_result.to_boolean());
  163. }
  164. ASSERT_NOT_REACHED();
  165. }
  166. static void print_indent(int indent)
  167. {
  168. for (int i = 0; i < indent * 2; ++i)
  169. putchar(' ');
  170. }
  171. void ASTNode::dump(int indent) const
  172. {
  173. print_indent(indent);
  174. printf("%s\n", class_name());
  175. }
  176. void ScopeNode::dump(int indent) const
  177. {
  178. ASTNode::dump(indent);
  179. for (auto& child : children())
  180. child.dump(indent + 1);
  181. }
  182. void BinaryExpression::dump(int indent) const
  183. {
  184. const char* op_string = nullptr;
  185. switch (m_op) {
  186. case BinaryOp::Plus:
  187. op_string = "+";
  188. break;
  189. case BinaryOp::Minus:
  190. op_string = "-";
  191. break;
  192. case BinaryOp::TypedEquals:
  193. op_string = "===";
  194. break;
  195. case BinaryOp::TypedInequals:
  196. op_string = "!==";
  197. break;
  198. case BinaryOp::GreaterThan:
  199. op_string = ">";
  200. break;
  201. case BinaryOp::LessThan:
  202. op_string = "<";
  203. break;
  204. case BinaryOp::BitwiseAnd:
  205. op_string = "&";
  206. break;
  207. case BinaryOp::BitwiseOr:
  208. op_string = "|";
  209. break;
  210. case BinaryOp::BitwiseXor:
  211. op_string = "^";
  212. break;
  213. case BinaryOp::LeftShift:
  214. op_string = "<<";
  215. break;
  216. case BinaryOp::RightShift:
  217. op_string = ">>";
  218. break;
  219. }
  220. print_indent(indent);
  221. printf("%s\n", class_name());
  222. m_lhs->dump(indent + 1);
  223. print_indent(indent + 1);
  224. printf("%s\n", op_string);
  225. m_rhs->dump(indent + 1);
  226. }
  227. void LogicalExpression::dump(int indent) const
  228. {
  229. const char* op_string = nullptr;
  230. switch (m_op) {
  231. case LogicalOp::And:
  232. op_string = "&&";
  233. break;
  234. case LogicalOp::Or:
  235. op_string = "||";
  236. break;
  237. }
  238. print_indent(indent);
  239. printf("%s\n", class_name());
  240. m_lhs->dump(indent + 1);
  241. print_indent(indent + 1);
  242. printf("%s\n", op_string);
  243. m_rhs->dump(indent + 1);
  244. }
  245. void UnaryExpression::dump(int indent) const
  246. {
  247. const char* op_string = nullptr;
  248. switch (m_op) {
  249. case UnaryOp::BitNot:
  250. op_string = "~";
  251. break;
  252. case UnaryOp::Not:
  253. op_string = "!";
  254. break;
  255. }
  256. print_indent(indent);
  257. printf("%s\n", class_name());
  258. print_indent(indent + 1);
  259. printf("%s\n", op_string);
  260. m_lhs->dump(indent + 1);
  261. }
  262. void CallExpression::dump(int indent) const
  263. {
  264. print_indent(indent);
  265. printf("%s '%s'\n", class_name(), name().characters());
  266. }
  267. void Literal::dump(int indent) const
  268. {
  269. print_indent(indent);
  270. printf("Literal _%s_\n", m_value.to_string().characters());
  271. }
  272. void FunctionDeclaration::dump(int indent) const
  273. {
  274. print_indent(indent);
  275. printf("%s '%s'\n", class_name(), name().characters());
  276. body().dump(indent + 1);
  277. }
  278. void ReturnStatement::dump(int indent) const
  279. {
  280. ASTNode::dump(indent);
  281. if (argument())
  282. argument()->dump(indent + 1);
  283. }
  284. void IfStatement::dump(int indent) const
  285. {
  286. ASTNode::dump(indent);
  287. print_indent(indent);
  288. printf("If\n");
  289. predicate().dump(indent + 1);
  290. consequent().dump(indent + 1);
  291. print_indent(indent);
  292. printf("Else\n");
  293. alternate().dump(indent + 1);
  294. }
  295. void WhileStatement::dump(int indent) const
  296. {
  297. ASTNode::dump(indent);
  298. print_indent(indent);
  299. printf("While\n");
  300. predicate().dump(indent + 1);
  301. body().dump(indent + 1);
  302. }
  303. Value Identifier::execute(Interpreter& interpreter) const
  304. {
  305. return interpreter.get_variable(string());
  306. }
  307. void Identifier::dump(int indent) const
  308. {
  309. print_indent(indent);
  310. printf("Identifier \"%s\"\n", m_string.characters());
  311. }
  312. Value AssignmentExpression::execute(Interpreter& interpreter) const
  313. {
  314. ASSERT(m_lhs->is_identifier());
  315. auto name = static_cast<const Identifier&>(*m_lhs).string();
  316. auto rhs_result = m_rhs->execute(interpreter);
  317. switch (m_op) {
  318. case AssignmentOp::Assign:
  319. interpreter.set_variable(name, rhs_result);
  320. break;
  321. }
  322. return rhs_result;
  323. }
  324. void AssignmentExpression::dump(int indent) const
  325. {
  326. const char* op_string = nullptr;
  327. switch (m_op) {
  328. case AssignmentOp::Assign:
  329. op_string = "=";
  330. break;
  331. }
  332. ASTNode::dump(indent);
  333. print_indent(indent + 1);
  334. printf("%s\n", op_string);
  335. m_lhs->dump(indent + 1);
  336. m_rhs->dump(indent + 1);
  337. }
  338. Value VariableDeclaration::execute(Interpreter& interpreter) const
  339. {
  340. interpreter.declare_variable(name().string(), m_declaration_type);
  341. if (m_initializer) {
  342. auto initalizer_result = m_initializer->execute(interpreter);
  343. interpreter.set_variable(name().string(), initalizer_result);
  344. }
  345. return js_undefined();
  346. }
  347. void VariableDeclaration::dump(int indent) const
  348. {
  349. const char* op_string = nullptr;
  350. switch (m_declaration_type) {
  351. case DeclarationType::Let:
  352. op_string = "Let";
  353. break;
  354. case DeclarationType::Var:
  355. op_string = "Var";
  356. break;
  357. }
  358. ASTNode::dump(indent);
  359. print_indent(indent + 1);
  360. printf("%s\n", op_string);
  361. m_name->dump(indent + 1);
  362. if (m_initializer)
  363. m_initializer->dump(indent + 1);
  364. }
  365. void ObjectExpression::dump(int indent) const
  366. {
  367. ASTNode::dump(indent);
  368. }
  369. void ExpressionStatement::dump(int indent) const
  370. {
  371. ASTNode::dump(indent);
  372. m_expression->dump(indent + 1);
  373. }
  374. Value ObjectExpression::execute(Interpreter& interpreter) const
  375. {
  376. return Value(interpreter.heap().allocate<Object>());
  377. }
  378. void MemberExpression::dump(int indent) const
  379. {
  380. ASTNode::dump(indent);
  381. m_object->dump(indent + 1);
  382. m_property->dump(indent + 1);
  383. }
  384. Value MemberExpression::execute(Interpreter& interpreter) const
  385. {
  386. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  387. ASSERT(object_result.is_object());
  388. String property_name;
  389. if (m_property->is_identifier()) {
  390. property_name = static_cast<const Identifier&>(*m_property).string();
  391. } else {
  392. ASSERT_NOT_REACHED();
  393. }
  394. return object_result.as_object()->get(property_name);
  395. }
  396. }