AST.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = new 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 add(Value lhs, Value rhs)
  58. {
  59. ASSERT(lhs.is_number());
  60. ASSERT(rhs.is_number());
  61. return Value(lhs.as_double() + rhs.as_double());
  62. }
  63. Value sub(Value lhs, Value rhs)
  64. {
  65. ASSERT(lhs.is_number());
  66. ASSERT(rhs.is_number());
  67. return Value(lhs.as_double() - rhs.as_double());
  68. }
  69. const Value typed_eq(const Value lhs, const Value rhs)
  70. {
  71. if (rhs.type() != lhs.type())
  72. return Value(false);
  73. switch (lhs.type()) {
  74. case Value::Type::Undefined:
  75. return Value(true);
  76. case Value::Type::Null:
  77. return Value(true);
  78. case Value::Type::Number:
  79. return Value(lhs.as_double() == rhs.as_double());
  80. case Value::Type::String:
  81. return Value(lhs.as_string() == rhs.as_string());
  82. case Value::Type::Boolean:
  83. return Value(lhs.as_bool() == rhs.as_bool());
  84. case Value::Type::Object:
  85. return Value(lhs.as_object() == rhs.as_object());
  86. }
  87. ASSERT_NOT_REACHED();
  88. }
  89. Value BinaryExpression::execute(Interpreter& interpreter) const
  90. {
  91. auto lhs_result = m_lhs->execute(interpreter);
  92. auto rhs_result = m_rhs->execute(interpreter);
  93. switch (m_op) {
  94. case BinaryOp::Plus:
  95. return add(lhs_result, rhs_result);
  96. case BinaryOp::Minus:
  97. return sub(lhs_result, rhs_result);
  98. case BinaryOp::TypedEquals:
  99. return typed_eq(lhs_result, rhs_result);
  100. }
  101. ASSERT_NOT_REACHED();
  102. }
  103. Value LogicalExpression::execute(Interpreter& interpreter) const
  104. {
  105. auto lhs_result = m_lhs->execute(interpreter).as_bool();
  106. if (m_op == LogicalOp::Not)
  107. return Value(!lhs_result);
  108. auto rhs_result = m_rhs->execute(interpreter).as_bool();
  109. switch (m_op) {
  110. case LogicalOp::And:
  111. return Value(lhs_result && rhs_result);
  112. case LogicalOp::Or:
  113. return Value(lhs_result || rhs_result);
  114. case LogicalOp::Not:
  115. ASSERT_NOT_REACHED();
  116. }
  117. ASSERT_NOT_REACHED();
  118. }
  119. static void print_indent(int indent)
  120. {
  121. for (int i = 0; i < indent * 2; ++i)
  122. putchar(' ');
  123. }
  124. void ASTNode::dump(int indent) const
  125. {
  126. print_indent(indent);
  127. printf("%s\n", class_name());
  128. }
  129. void ScopeNode::dump(int indent) const
  130. {
  131. ASTNode::dump(indent);
  132. for (auto& child : children())
  133. child.dump(indent + 1);
  134. }
  135. void BinaryExpression::dump(int indent) const
  136. {
  137. const char* op_string = nullptr;
  138. switch (m_op) {
  139. case BinaryOp::Plus:
  140. op_string = "+";
  141. break;
  142. case BinaryOp::Minus:
  143. op_string = "-";
  144. break;
  145. case BinaryOp::TypedEquals:
  146. op_string = "===";
  147. break;
  148. }
  149. print_indent(indent);
  150. printf("%s\n", class_name());
  151. m_lhs->dump(indent + 1);
  152. print_indent(indent + 1);
  153. printf("%s\n", op_string);
  154. m_rhs->dump(indent + 1);
  155. }
  156. void LogicalExpression::dump(int indent) const
  157. {
  158. const char* op_string = nullptr;
  159. switch (m_op) {
  160. case LogicalOp::And:
  161. op_string = "&&";
  162. break;
  163. case LogicalOp::Or:
  164. op_string = "||";
  165. break;
  166. case LogicalOp::Not:
  167. op_string = "!";
  168. print_indent(indent);
  169. printf("%s\n", class_name());
  170. print_indent(indent + 1);
  171. printf("%s\n", op_string);
  172. m_lhs->dump(indent + 1);
  173. return;
  174. }
  175. print_indent(indent);
  176. printf("%s\n", class_name());
  177. m_lhs->dump(indent + 1);
  178. print_indent(indent + 1);
  179. printf("%s\n", op_string);
  180. m_rhs->dump(indent + 1);
  181. }
  182. void CallExpression::dump(int indent) const
  183. {
  184. print_indent(indent);
  185. printf("%s '%s'\n", class_name(), name().characters());
  186. }
  187. void Literal::dump(int indent) const
  188. {
  189. print_indent(indent);
  190. if (m_value.is_object())
  191. ASSERT_NOT_REACHED();
  192. if (m_value.is_string())
  193. printf("%s\n", m_value.as_string()->characters());
  194. else
  195. printf("%s\n", m_value.to_string().characters());
  196. }
  197. void FunctionDeclaration::dump(int indent) const
  198. {
  199. print_indent(indent);
  200. printf("%s '%s'\n", class_name(), name().characters());
  201. body().dump(indent + 1);
  202. }
  203. void ReturnStatement::dump(int indent) const
  204. {
  205. ASTNode::dump(indent);
  206. argument().dump(indent + 1);
  207. }
  208. }