expression.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. auto ExpressionFromParenContents(
  14. Nonnull<Arena*> arena, SourceLocation source_loc,
  15. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
  16. std::optional<Nonnull<Expression*>> single_term = paren_contents.SingleTerm();
  17. if (single_term.has_value()) {
  18. return *single_term;
  19. } else {
  20. return TupleExpressionFromParenContents(arena, source_loc, paren_contents);
  21. }
  22. }
  23. auto TupleExpressionFromParenContents(
  24. Nonnull<Arena*> arena, SourceLocation source_loc,
  25. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
  26. return arena->New<TupleLiteral>(
  27. source_loc, paren_contents.TupleElements<FieldInitializer>(source_loc));
  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. PrintFields(out, cast<TupleLiteral>(*this).Fields(), " = ");
  80. out << ")";
  81. break;
  82. case Expression::Kind::StructLiteral:
  83. out << "{";
  84. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  85. out << "}";
  86. break;
  87. case Expression::Kind::StructTypeLiteral:
  88. out << "{";
  89. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  90. out << "}";
  91. break;
  92. case Expression::Kind::IntLiteral:
  93. out << cast<IntLiteral>(*this).Val();
  94. break;
  95. case Expression::Kind::BoolLiteral:
  96. out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
  97. break;
  98. case Expression::Kind::PrimitiveOperatorExpression: {
  99. out << "(";
  100. PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
  101. if (op.Arguments().size() == 0) {
  102. PrintOp(out, op.Op());
  103. } else if (op.Arguments().size() == 1) {
  104. PrintOp(out, op.Op());
  105. out << " " << *op.Arguments()[0];
  106. } else if (op.Arguments().size() == 2) {
  107. out << *op.Arguments()[0] << " ";
  108. PrintOp(out, op.Op());
  109. out << " " << *op.Arguments()[1];
  110. }
  111. out << ")";
  112. break;
  113. }
  114. case Expression::Kind::IdentifierExpression:
  115. out << cast<IdentifierExpression>(*this).Name();
  116. break;
  117. case Expression::Kind::CallExpression: {
  118. const auto& call = cast<CallExpression>(*this);
  119. out << *call.Function();
  120. if (call.Argument()->kind() == Expression::Kind::TupleLiteral) {
  121. out << *call.Argument();
  122. } else {
  123. out << "(" << *call.Argument() << ")";
  124. }
  125. break;
  126. }
  127. case Expression::Kind::BoolTypeLiteral:
  128. out << "Bool";
  129. break;
  130. case Expression::Kind::IntTypeLiteral:
  131. out << "i32";
  132. break;
  133. case Expression::Kind::StringLiteral:
  134. out << "\"";
  135. out.write_escaped(cast<StringLiteral>(*this).Val());
  136. out << "\"";
  137. break;
  138. case Expression::Kind::StringTypeLiteral:
  139. out << "String";
  140. break;
  141. case Expression::Kind::TypeTypeLiteral:
  142. out << "Type";
  143. break;
  144. case Expression::Kind::ContinuationTypeLiteral:
  145. out << "Continuation";
  146. break;
  147. case Expression::Kind::FunctionTypeLiteral: {
  148. const auto& fn = cast<FunctionTypeLiteral>(*this);
  149. out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
  150. break;
  151. }
  152. case Expression::Kind::IntrinsicExpression:
  153. out << "intrinsic_expression(";
  154. switch (cast<IntrinsicExpression>(*this).Intrinsic()) {
  155. case IntrinsicExpression::IntrinsicKind::Print:
  156. out << "print";
  157. }
  158. out << ")";
  159. }
  160. }
  161. } // namespace Carbon