expression.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. SourceLocation loc, const ParenContents<Expression>& paren_contents)
  15. -> Ptr<const Expression> {
  16. std::optional<Ptr<const Expression>> single_term =
  17. paren_contents.SingleTerm();
  18. if (single_term.has_value()) {
  19. return *single_term;
  20. } else {
  21. return TupleExpressionFromParenContents(loc, paren_contents);
  22. }
  23. }
  24. auto TupleExpressionFromParenContents(
  25. SourceLocation loc, const ParenContents<Expression>& paren_contents)
  26. -> Ptr<const Expression> {
  27. return global_arena->New<TupleLiteral>(
  28. loc, paren_contents.TupleElements<FieldInitializer>(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. llvm::ListSeparator sep;
  61. for (const auto& field : fields) {
  62. out << sep << field.name << " = " << *field.expression;
  63. }
  64. }
  65. void Expression::Print(llvm::raw_ostream& out) const {
  66. switch (Tag()) {
  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::IntLiteral:
  83. out << cast<IntLiteral>(*this).Val();
  84. break;
  85. case Expression::Kind::BoolLiteral:
  86. out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
  87. break;
  88. case Expression::Kind::PrimitiveOperatorExpression: {
  89. out << "(";
  90. PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
  91. if (op.Arguments().size() == 0) {
  92. PrintOp(out, op.Op());
  93. } else if (op.Arguments().size() == 1) {
  94. PrintOp(out, op.Op());
  95. out << " " << *op.Arguments()[0];
  96. } else if (op.Arguments().size() == 2) {
  97. out << *op.Arguments()[0] << " ";
  98. PrintOp(out, op.Op());
  99. out << " " << *op.Arguments()[1];
  100. }
  101. out << ")";
  102. break;
  103. }
  104. case Expression::Kind::IdentifierExpression:
  105. out << cast<IdentifierExpression>(*this).Name();
  106. break;
  107. case Expression::Kind::CallExpression: {
  108. const auto& call = cast<CallExpression>(*this);
  109. out << *call.Function();
  110. if (call.Argument()->Tag() == Expression::Kind::TupleLiteral) {
  111. out << *call.Argument();
  112. } else {
  113. out << "(" << *call.Argument() << ")";
  114. }
  115. break;
  116. }
  117. case Expression::Kind::BoolTypeLiteral:
  118. out << "Bool";
  119. break;
  120. case Expression::Kind::IntTypeLiteral:
  121. out << "i32";
  122. break;
  123. case Expression::Kind::StringLiteral:
  124. out << "\"";
  125. out.write_escaped(cast<StringLiteral>(*this).Val());
  126. out << "\"";
  127. break;
  128. case Expression::Kind::StringTypeLiteral:
  129. out << "String";
  130. break;
  131. case Expression::Kind::TypeTypeLiteral:
  132. out << "Type";
  133. break;
  134. case Expression::Kind::ContinuationTypeLiteral:
  135. out << "Continuation";
  136. break;
  137. case Expression::Kind::FunctionTypeLiteral: {
  138. const auto& fn = cast<FunctionTypeLiteral>(*this);
  139. out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
  140. break;
  141. }
  142. case Expression::Kind::IntrinsicExpression:
  143. out << "intrinsic_expression(";
  144. switch (cast<IntrinsicExpression>(*this).Intrinsic()) {
  145. case IntrinsicExpression::IntrinsicKind::Print:
  146. out << "print";
  147. }
  148. out << ")";
  149. }
  150. }
  151. } // namespace Carbon