expression.cpp 5.3 KB

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