expression.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. auto ToString(Operator op) -> std::string_view {
  31. switch (op) {
  32. case Operator::Add:
  33. return "+";
  34. case Operator::Neg:
  35. case Operator::Sub:
  36. return "-";
  37. case Operator::Mul:
  38. case Operator::Deref:
  39. case Operator::Ptr:
  40. return "*";
  41. case Operator::Not:
  42. return "not";
  43. case Operator::And:
  44. return "and";
  45. case Operator::Or:
  46. return "or";
  47. case Operator::Eq:
  48. return "==";
  49. }
  50. }
  51. static void PrintFields(llvm::raw_ostream& out,
  52. const std::vector<FieldInitializer>& fields,
  53. std::string_view separator) {
  54. llvm::ListSeparator sep;
  55. for (const auto& field : fields) {
  56. out << sep << "." << field.name() << separator << field.expression();
  57. }
  58. }
  59. void Expression::Print(llvm::raw_ostream& out) const {
  60. switch (kind()) {
  61. case ExpressionKind::IndexExpression: {
  62. const auto& index = cast<IndexExpression>(*this);
  63. out << index.aggregate() << "[" << index.offset() << "]";
  64. break;
  65. }
  66. case ExpressionKind::FieldAccessExpression: {
  67. const auto& access = cast<FieldAccessExpression>(*this);
  68. out << access.aggregate() << "." << access.field();
  69. break;
  70. }
  71. case ExpressionKind::TupleLiteral: {
  72. out << "(";
  73. llvm::ListSeparator sep;
  74. for (Nonnull<const Expression*> field :
  75. cast<TupleLiteral>(*this).fields()) {
  76. out << sep << *field;
  77. }
  78. out << ")";
  79. break;
  80. }
  81. case ExpressionKind::StructLiteral:
  82. out << "{";
  83. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  84. out << "}";
  85. break;
  86. case ExpressionKind::StructTypeLiteral:
  87. out << "{";
  88. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  89. out << "}";
  90. break;
  91. case ExpressionKind::IntLiteral:
  92. out << cast<IntLiteral>(*this).value();
  93. break;
  94. case ExpressionKind::BoolLiteral:
  95. out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
  96. break;
  97. case ExpressionKind::PrimitiveOperatorExpression: {
  98. out << "(";
  99. const auto& op = cast<PrimitiveOperatorExpression>(*this);
  100. switch (op.arguments().size()) {
  101. case 0:
  102. out << ToString(op.op());
  103. break;
  104. case 1:
  105. out << ToString(op.op()) << " " << *op.arguments()[0];
  106. break;
  107. case 2:
  108. out << *op.arguments()[0] << " " << ToString(op.op()) << " "
  109. << *op.arguments()[1];
  110. break;
  111. default:
  112. FATAL() << "Unexpected argument count: " << op.arguments().size();
  113. }
  114. out << ")";
  115. break;
  116. }
  117. case ExpressionKind::IdentifierExpression:
  118. out << cast<IdentifierExpression>(*this).name();
  119. break;
  120. case ExpressionKind::CallExpression: {
  121. const auto& call = cast<CallExpression>(*this);
  122. out << call.function();
  123. if (isa<TupleLiteral>(call.argument())) {
  124. out << call.argument();
  125. } else {
  126. out << "(" << call.argument() << ")";
  127. }
  128. break;
  129. }
  130. case ExpressionKind::BoolTypeLiteral:
  131. out << "Bool";
  132. break;
  133. case ExpressionKind::IntTypeLiteral:
  134. out << "i32";
  135. break;
  136. case ExpressionKind::StringLiteral:
  137. out << "\"";
  138. out.write_escaped(cast<StringLiteral>(*this).value());
  139. out << "\"";
  140. break;
  141. case ExpressionKind::StringTypeLiteral:
  142. out << "String";
  143. break;
  144. case ExpressionKind::TypeTypeLiteral:
  145. out << "Type";
  146. break;
  147. case ExpressionKind::ContinuationTypeLiteral:
  148. out << "Continuation";
  149. break;
  150. case ExpressionKind::FunctionTypeLiteral: {
  151. const auto& fn = cast<FunctionTypeLiteral>(*this);
  152. out << "fn " << fn.parameter() << " -> " << fn.return_type();
  153. break;
  154. }
  155. case ExpressionKind::IntrinsicExpression:
  156. out << "intrinsic_expression(";
  157. switch (cast<IntrinsicExpression>(*this).intrinsic()) {
  158. case IntrinsicExpression::Intrinsic::Print:
  159. out << "print";
  160. }
  161. out << ")";
  162. }
  163. }
  164. } // namespace Carbon