expression.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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>(
  28. source_loc, paren_contents.TupleElements<FieldInitializer>(source_loc));
  29. }
  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 Expression::Kind::IndexExpression: {
  69. const auto& index = cast<IndexExpression>(*this);
  70. out << index.aggregate() << "[" << index.offset() << "]";
  71. break;
  72. }
  73. case Expression::Kind::FieldAccessExpression: {
  74. const auto& access = cast<FieldAccessExpression>(*this);
  75. out << access.aggregate() << "." << access.field();
  76. break;
  77. }
  78. case Expression::Kind::TupleLiteral:
  79. out << "(";
  80. PrintFields(out, cast<TupleLiteral>(*this).fields(), " = ");
  81. out << ")";
  82. break;
  83. case Expression::Kind::StructLiteral:
  84. out << "{";
  85. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  86. out << "}";
  87. break;
  88. case Expression::Kind::StructTypeLiteral:
  89. out << "{";
  90. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  91. out << "}";
  92. break;
  93. case Expression::Kind::IntLiteral:
  94. out << cast<IntLiteral>(*this).value();
  95. break;
  96. case Expression::Kind::BoolLiteral:
  97. out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
  98. break;
  99. case Expression::Kind::PrimitiveOperatorExpression: {
  100. out << "(";
  101. PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
  102. switch (op.arguments().size()) {
  103. case 0:
  104. PrintOp(out, op.op());
  105. break;
  106. case 1:
  107. PrintOp(out, op.op());
  108. out << " " << *op.arguments()[0];
  109. break;
  110. case 2:
  111. out << *op.arguments()[0] << " ";
  112. PrintOp(out, op.op());
  113. out << " " << *op.arguments()[1];
  114. break;
  115. default:
  116. FATAL() << "Unexpected argument count: " << op.arguments().size();
  117. }
  118. out << ")";
  119. break;
  120. }
  121. case Expression::Kind::IdentifierExpression:
  122. out << cast<IdentifierExpression>(*this).name();
  123. break;
  124. case Expression::Kind::CallExpression: {
  125. const auto& call = cast<CallExpression>(*this);
  126. out << call.function();
  127. if (isa<TupleLiteral>(call.argument())) {
  128. out << call.argument();
  129. } else {
  130. out << "(" << call.argument() << ")";
  131. }
  132. break;
  133. }
  134. case Expression::Kind::BoolTypeLiteral:
  135. out << "Bool";
  136. break;
  137. case Expression::Kind::IntTypeLiteral:
  138. out << "i32";
  139. break;
  140. case Expression::Kind::StringLiteral:
  141. out << "\"";
  142. out.write_escaped(cast<StringLiteral>(*this).value());
  143. out << "\"";
  144. break;
  145. case Expression::Kind::StringTypeLiteral:
  146. out << "String";
  147. break;
  148. case Expression::Kind::TypeTypeLiteral:
  149. out << "Type";
  150. break;
  151. case Expression::Kind::ContinuationTypeLiteral:
  152. out << "Continuation";
  153. break;
  154. case Expression::Kind::FunctionTypeLiteral: {
  155. const auto& fn = cast<FunctionTypeLiteral>(*this);
  156. out << "fn " << fn.parameter() << " -> " << fn.return_type();
  157. break;
  158. }
  159. case Expression::Kind::IntrinsicExpression:
  160. out << "intrinsic_expression(";
  161. switch (cast<IntrinsicExpression>(*this).intrinsic()) {
  162. case IntrinsicExpression::Intrinsic::Print:
  163. out << "print";
  164. }
  165. out << ")";
  166. }
  167. }
  168. } // namespace Carbon