expression.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 loc,
  15. const ParenContents<Expression>& paren_contents)
  16. -> Nonnull<const Expression*> {
  17. std::optional<Nonnull<const Expression*>> single_term =
  18. paren_contents.SingleTerm();
  19. if (single_term.has_value()) {
  20. return *single_term;
  21. } else {
  22. return TupleExpressionFromParenContents(arena, loc, paren_contents);
  23. }
  24. }
  25. auto TupleExpressionFromParenContents(
  26. Nonnull<Arena*> arena, SourceLocation loc,
  27. const ParenContents<Expression>& paren_contents)
  28. -> Nonnull<const Expression*> {
  29. return arena->New<TupleLiteral>(
  30. loc, paren_contents.TupleElements<FieldInitializer>(loc));
  31. }
  32. static void PrintOp(llvm::raw_ostream& out, Operator op) {
  33. switch (op) {
  34. case Operator::Add:
  35. out << "+";
  36. break;
  37. case Operator::Neg:
  38. case Operator::Sub:
  39. out << "-";
  40. break;
  41. case Operator::Mul:
  42. case Operator::Deref:
  43. case Operator::Ptr:
  44. out << "*";
  45. break;
  46. case Operator::Not:
  47. out << "not";
  48. break;
  49. case Operator::And:
  50. out << "and";
  51. break;
  52. case Operator::Or:
  53. out << "or";
  54. break;
  55. case Operator::Eq:
  56. out << "==";
  57. break;
  58. }
  59. }
  60. static void PrintFields(llvm::raw_ostream& out,
  61. const std::vector<FieldInitializer>& fields) {
  62. llvm::ListSeparator sep;
  63. for (const auto& field : fields) {
  64. out << sep << field.name << " = " << *field.expression;
  65. }
  66. }
  67. void Expression::Print(llvm::raw_ostream& out) const {
  68. switch (Tag()) {
  69. case Expression::Kind::IndexExpression: {
  70. const auto& index = cast<IndexExpression>(*this);
  71. out << *index.Aggregate() << "[" << *index.Offset() << "]";
  72. break;
  73. }
  74. case Expression::Kind::FieldAccessExpression: {
  75. const auto& access = cast<FieldAccessExpression>(*this);
  76. out << *access.Aggregate() << "." << access.Field();
  77. break;
  78. }
  79. case Expression::Kind::TupleLiteral:
  80. out << "(";
  81. PrintFields(out, cast<TupleLiteral>(*this).Fields());
  82. out << ")";
  83. break;
  84. case Expression::Kind::IntLiteral:
  85. out << cast<IntLiteral>(*this).Val();
  86. break;
  87. case Expression::Kind::BoolLiteral:
  88. out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
  89. break;
  90. case Expression::Kind::PrimitiveOperatorExpression: {
  91. out << "(";
  92. PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
  93. if (op.Arguments().size() == 0) {
  94. PrintOp(out, op.Op());
  95. } else if (op.Arguments().size() == 1) {
  96. PrintOp(out, op.Op());
  97. out << " " << *op.Arguments()[0];
  98. } else if (op.Arguments().size() == 2) {
  99. out << *op.Arguments()[0] << " ";
  100. PrintOp(out, op.Op());
  101. out << " " << *op.Arguments()[1];
  102. }
  103. out << ")";
  104. break;
  105. }
  106. case Expression::Kind::IdentifierExpression:
  107. out << cast<IdentifierExpression>(*this).Name();
  108. break;
  109. case Expression::Kind::CallExpression: {
  110. const auto& call = cast<CallExpression>(*this);
  111. out << *call.Function();
  112. if (call.Argument()->Tag() == Expression::Kind::TupleLiteral) {
  113. out << *call.Argument();
  114. } else {
  115. out << "(" << *call.Argument() << ")";
  116. }
  117. break;
  118. }
  119. case Expression::Kind::BoolTypeLiteral:
  120. out << "Bool";
  121. break;
  122. case Expression::Kind::IntTypeLiteral:
  123. out << "i32";
  124. break;
  125. case Expression::Kind::StringLiteral:
  126. out << "\"";
  127. out.write_escaped(cast<StringLiteral>(*this).Val());
  128. out << "\"";
  129. break;
  130. case Expression::Kind::StringTypeLiteral:
  131. out << "String";
  132. break;
  133. case Expression::Kind::TypeTypeLiteral:
  134. out << "Type";
  135. break;
  136. case Expression::Kind::ContinuationTypeLiteral:
  137. out << "Continuation";
  138. break;
  139. case Expression::Kind::FunctionTypeLiteral: {
  140. const auto& fn = cast<FunctionTypeLiteral>(*this);
  141. out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
  142. break;
  143. }
  144. case Expression::Kind::IntrinsicExpression:
  145. out << "intrinsic_expression(";
  146. switch (cast<IntrinsicExpression>(*this).Intrinsic()) {
  147. case IntrinsicExpression::IntrinsicKind::Print:
  148. out << "print";
  149. }
  150. out << ")";
  151. }
  152. }
  153. } // namespace Carbon