AST.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Value BinaryExpression::execute(Interpreter& interpreter) const
  70. {
  71. auto lhs_result = m_lhs->execute(interpreter);
  72. auto rhs_result = m_rhs->execute(interpreter);
  73. switch (m_op) {
  74. case BinaryOp::Plus:
  75. return add(lhs_result, rhs_result);
  76. case BinaryOp::Minus:
  77. return sub(lhs_result, rhs_result);
  78. }
  79. ASSERT_NOT_REACHED();
  80. }
  81. static void print_indent(int indent)
  82. {
  83. for (int i = 0; i < indent * 2; ++i)
  84. putchar(' ');
  85. }
  86. void ASTNode::dump(int indent) const
  87. {
  88. print_indent(indent);
  89. printf("%s\n", class_name());
  90. }
  91. void ScopeNode::dump(int indent) const
  92. {
  93. ASTNode::dump(indent);
  94. for (auto& child : children())
  95. child.dump(indent + 1);
  96. }
  97. void BinaryExpression::dump(int indent) const
  98. {
  99. ASTNode::dump(indent);
  100. m_lhs->dump(indent + 1);
  101. m_rhs->dump(indent + 1);
  102. }
  103. void CallExpression::dump(int indent) const
  104. {
  105. print_indent(indent);
  106. printf("%s '%s'\n", class_name(), name().characters());
  107. }
  108. void Literal::dump(int indent) const
  109. {
  110. print_indent(indent);
  111. printf("%d\n", (i32)m_value.as_double());
  112. }
  113. void FunctionDeclaration::dump(int indent) const
  114. {
  115. print_indent(indent);
  116. printf("%s '%s'\n", class_name(), name().characters());
  117. body().dump(indent + 1);
  118. }
  119. void ReturnStatement::dump(int indent) const
  120. {
  121. ASTNode::dump(indent);
  122. argument().dump(indent + 1);
  123. }
  124. }