AST.cpp 11 KB

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