AST.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.global_object().put(m_name, Value(function));
  40. return Value(function);
  41. }
  42. Value CallExpression::execute(Interpreter& interpreter) const
  43. {
  44. auto callee = interpreter.global_object().get(name());
  45. ASSERT(callee.is_object());
  46. auto* callee_object = callee.as_object();
  47. ASSERT(callee_object->is_function());
  48. auto& function = static_cast<Function&>(*callee_object);
  49. return interpreter.run(function.body());
  50. }
  51. Value ReturnStatement::execute(Interpreter& interpreter) const
  52. {
  53. auto value = argument().execute(interpreter);
  54. interpreter.do_return();
  55. return value;
  56. }
  57. Value IfStatement::execute(Interpreter& interpreter) const
  58. {
  59. auto predicate_result = m_predicate->execute(interpreter);
  60. if (predicate_result.as_bool())
  61. return interpreter.run(*m_consequent);
  62. else
  63. return interpreter.run(*m_alternate);
  64. }
  65. Value WhileStatement::execute(Interpreter& interpreter) const
  66. {
  67. Value last_value = js_undefined();
  68. while (m_predicate->execute(interpreter).as_bool()) {
  69. last_value = interpreter.run(*m_body);
  70. }
  71. return last_value;
  72. }
  73. Value add(Value lhs, Value rhs)
  74. {
  75. ASSERT(lhs.is_number());
  76. ASSERT(rhs.is_number());
  77. return Value(lhs.as_double() + rhs.as_double());
  78. }
  79. Value sub(Value lhs, Value rhs)
  80. {
  81. ASSERT(lhs.is_number());
  82. ASSERT(rhs.is_number());
  83. return Value(lhs.as_double() - rhs.as_double());
  84. }
  85. const Value typed_eq(const Value lhs, const Value rhs)
  86. {
  87. if (rhs.type() != lhs.type())
  88. return Value(false);
  89. switch (lhs.type()) {
  90. case Value::Type::Undefined:
  91. return Value(true);
  92. case Value::Type::Null:
  93. return Value(true);
  94. case Value::Type::Number:
  95. return Value(lhs.as_double() == rhs.as_double());
  96. case Value::Type::String:
  97. return Value(lhs.as_string() == rhs.as_string());
  98. case Value::Type::Boolean:
  99. return Value(lhs.as_bool() == rhs.as_bool());
  100. case Value::Type::Object:
  101. return Value(lhs.as_object() == rhs.as_object());
  102. }
  103. ASSERT_NOT_REACHED();
  104. }
  105. Value BinaryExpression::execute(Interpreter& interpreter) const
  106. {
  107. auto lhs_result = m_lhs->execute(interpreter);
  108. auto rhs_result = m_rhs->execute(interpreter);
  109. switch (m_op) {
  110. case BinaryOp::Plus:
  111. return add(lhs_result, rhs_result);
  112. case BinaryOp::Minus:
  113. return sub(lhs_result, rhs_result);
  114. case BinaryOp::TypedEquals:
  115. return typed_eq(lhs_result, rhs_result);
  116. }
  117. ASSERT_NOT_REACHED();
  118. }
  119. Value LogicalExpression::execute(Interpreter& interpreter) const
  120. {
  121. auto lhs_result = m_lhs->execute(interpreter).as_bool();
  122. if (m_op == LogicalOp::Not)
  123. return Value(!lhs_result);
  124. auto rhs_result = m_rhs->execute(interpreter).as_bool();
  125. switch (m_op) {
  126. case LogicalOp::And:
  127. return Value(lhs_result && rhs_result);
  128. case LogicalOp::Or:
  129. return Value(lhs_result || rhs_result);
  130. case LogicalOp::Not:
  131. ASSERT_NOT_REACHED();
  132. }
  133. ASSERT_NOT_REACHED();
  134. }
  135. static void print_indent(int indent)
  136. {
  137. for (int i = 0; i < indent * 2; ++i)
  138. putchar(' ');
  139. }
  140. void ASTNode::dump(int indent) const
  141. {
  142. print_indent(indent);
  143. printf("%s\n", class_name());
  144. }
  145. void ScopeNode::dump(int indent) const
  146. {
  147. ASTNode::dump(indent);
  148. for (auto& child : children())
  149. child.dump(indent + 1);
  150. }
  151. void BinaryExpression::dump(int indent) const
  152. {
  153. const char* op_string = nullptr;
  154. switch (m_op) {
  155. case BinaryOp::Plus:
  156. op_string = "+";
  157. break;
  158. case BinaryOp::Minus:
  159. op_string = "-";
  160. break;
  161. case BinaryOp::TypedEquals:
  162. op_string = "===";
  163. break;
  164. }
  165. print_indent(indent);
  166. printf("%s\n", class_name());
  167. m_lhs->dump(indent + 1);
  168. print_indent(indent + 1);
  169. printf("%s\n", op_string);
  170. m_rhs->dump(indent + 1);
  171. }
  172. void LogicalExpression::dump(int indent) const
  173. {
  174. const char* op_string = nullptr;
  175. switch (m_op) {
  176. case LogicalOp::And:
  177. op_string = "&&";
  178. break;
  179. case LogicalOp::Or:
  180. op_string = "||";
  181. break;
  182. case LogicalOp::Not:
  183. op_string = "!";
  184. print_indent(indent);
  185. printf("%s\n", class_name());
  186. print_indent(indent + 1);
  187. printf("%s\n", op_string);
  188. m_lhs->dump(indent + 1);
  189. return;
  190. }
  191. print_indent(indent);
  192. printf("%s\n", class_name());
  193. m_lhs->dump(indent + 1);
  194. print_indent(indent + 1);
  195. printf("%s\n", op_string);
  196. m_rhs->dump(indent + 1);
  197. }
  198. void CallExpression::dump(int indent) const
  199. {
  200. print_indent(indent);
  201. printf("%s '%s'\n", class_name(), name().characters());
  202. }
  203. void Literal::dump(int indent) const
  204. {
  205. print_indent(indent);
  206. if (m_value.is_object())
  207. ASSERT_NOT_REACHED();
  208. if (m_value.is_string())
  209. printf("%s\n", m_value.as_string()->characters());
  210. else
  211. printf("%s\n", m_value.to_string().characters());
  212. }
  213. void FunctionDeclaration::dump(int indent) const
  214. {
  215. print_indent(indent);
  216. printf("%s '%s'\n", class_name(), name().characters());
  217. body().dump(indent + 1);
  218. }
  219. void ReturnStatement::dump(int indent) const
  220. {
  221. ASTNode::dump(indent);
  222. argument().dump(indent + 1);
  223. }
  224. void IfStatement::dump(int indent) const
  225. {
  226. ASTNode::dump(indent);
  227. print_indent(indent);
  228. printf("If\n");
  229. predicate().dump(indent + 1);
  230. consequent().dump(indent + 1);
  231. print_indent(indent);
  232. printf("Else\n");
  233. alternate().dump(indent + 1);
  234. }
  235. void WhileStatement::dump(int indent) const
  236. {
  237. ASTNode::dump(indent);
  238. print_indent(indent);
  239. printf("While\n");
  240. predicate().dump(indent + 1);
  241. body().dump(indent + 1);
  242. }
  243. }