expression.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/ast/expression.h"
  5. #include <optional>
  6. #include "executable_semantics/common/arena.h"
  7. #include "executable_semantics/common/error.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "llvm/Support/Casting.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon {
  12. using llvm::cast;
  13. using llvm::isa;
  14. auto ExpressionFromParenContents(
  15. Nonnull<Arena*> arena, SourceLocation source_loc,
  16. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
  17. std::optional<Nonnull<Expression*>> single_term = paren_contents.SingleTerm();
  18. if (single_term.has_value()) {
  19. return *single_term;
  20. } else {
  21. return TupleExpressionFromParenContents(arena, source_loc, paren_contents);
  22. }
  23. }
  24. auto TupleExpressionFromParenContents(
  25. Nonnull<Arena*> arena, SourceLocation source_loc,
  26. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*> {
  27. return arena->New<TupleLiteral>(source_loc, paren_contents.elements);
  28. }
  29. Expression::~Expression() = default;
  30. static void PrintOp(llvm::raw_ostream& out, Operator op) {
  31. switch (op) {
  32. case Operator::Add:
  33. out << "+";
  34. break;
  35. case Operator::Neg:
  36. case Operator::Sub:
  37. out << "-";
  38. break;
  39. case Operator::Mul:
  40. case Operator::Deref:
  41. case Operator::Ptr:
  42. out << "*";
  43. break;
  44. case Operator::Not:
  45. out << "not";
  46. break;
  47. case Operator::And:
  48. out << "and";
  49. break;
  50. case Operator::Or:
  51. out << "or";
  52. break;
  53. case Operator::Eq:
  54. out << "==";
  55. break;
  56. }
  57. }
  58. static void PrintFields(llvm::raw_ostream& out,
  59. const std::vector<FieldInitializer>& fields,
  60. std::string_view separator) {
  61. llvm::ListSeparator sep;
  62. for (const auto& field : fields) {
  63. out << sep << "." << field.name() << separator << field.expression();
  64. }
  65. }
  66. void Expression::Print(llvm::raw_ostream& out) const {
  67. switch (kind()) {
  68. case ExpressionKind::IndexExpression: {
  69. const auto& index = cast<IndexExpression>(*this);
  70. out << index.aggregate() << "[" << index.offset() << "]";
  71. break;
  72. }
  73. case ExpressionKind::FieldAccessExpression: {
  74. const auto& access = cast<FieldAccessExpression>(*this);
  75. out << access.aggregate() << "." << access.field();
  76. break;
  77. }
  78. case ExpressionKind::TupleLiteral: {
  79. out << "(";
  80. llvm::ListSeparator sep;
  81. for (Nonnull<const Expression*> field :
  82. cast<TupleLiteral>(*this).fields()) {
  83. out << sep << *field;
  84. }
  85. out << ")";
  86. break;
  87. }
  88. case ExpressionKind::StructLiteral:
  89. out << "{";
  90. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  91. out << "}";
  92. break;
  93. case ExpressionKind::StructTypeLiteral:
  94. out << "{";
  95. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  96. out << "}";
  97. break;
  98. case ExpressionKind::IntLiteral:
  99. out << cast<IntLiteral>(*this).value();
  100. break;
  101. case ExpressionKind::BoolLiteral:
  102. out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
  103. break;
  104. case ExpressionKind::PrimitiveOperatorExpression: {
  105. out << "(";
  106. const auto& op = cast<PrimitiveOperatorExpression>(*this);
  107. switch (op.arguments().size()) {
  108. case 0:
  109. PrintOp(out, op.op());
  110. break;
  111. case 1:
  112. PrintOp(out, op.op());
  113. out << " " << *op.arguments()[0];
  114. break;
  115. case 2:
  116. out << *op.arguments()[0] << " ";
  117. PrintOp(out, op.op());
  118. out << " " << *op.arguments()[1];
  119. break;
  120. default:
  121. FATAL() << "Unexpected argument count: " << op.arguments().size();
  122. }
  123. out << ")";
  124. break;
  125. }
  126. case ExpressionKind::IdentifierExpression:
  127. out << cast<IdentifierExpression>(*this).name();
  128. break;
  129. case ExpressionKind::CallExpression: {
  130. const auto& call = cast<CallExpression>(*this);
  131. out << call.function();
  132. if (isa<TupleLiteral>(call.argument())) {
  133. out << call.argument();
  134. } else {
  135. out << "(" << call.argument() << ")";
  136. }
  137. break;
  138. }
  139. case ExpressionKind::BoolTypeLiteral:
  140. out << "Bool";
  141. break;
  142. case ExpressionKind::IntTypeLiteral:
  143. out << "i32";
  144. break;
  145. case ExpressionKind::StringLiteral:
  146. out << "\"";
  147. out.write_escaped(cast<StringLiteral>(*this).value());
  148. out << "\"";
  149. break;
  150. case ExpressionKind::StringTypeLiteral:
  151. out << "String";
  152. break;
  153. case ExpressionKind::TypeTypeLiteral:
  154. out << "Type";
  155. break;
  156. case ExpressionKind::ContinuationTypeLiteral:
  157. out << "Continuation";
  158. break;
  159. case ExpressionKind::FunctionTypeLiteral: {
  160. const auto& fn = cast<FunctionTypeLiteral>(*this);
  161. out << "fn " << fn.parameter() << " -> " << fn.return_type();
  162. break;
  163. }
  164. case ExpressionKind::IntrinsicExpression:
  165. out << "intrinsic_expression(";
  166. switch (cast<IntrinsicExpression>(*this).intrinsic()) {
  167. case IntrinsicExpression::Intrinsic::Print:
  168. out << "print";
  169. }
  170. out << ")";
  171. }
  172. }
  173. } // namespace Carbon