expression.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <map>
  6. #include <optional>
  7. #include "executable_semantics/common/arena.h"
  8. #include "executable_semantics/common/error.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/Support/Casting.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace Carbon {
  13. using llvm::cast;
  14. using llvm::isa;
  15. auto IntrinsicExpression::FindIntrinsic(std::string_view name,
  16. SourceLocation source_loc)
  17. -> Intrinsic {
  18. static const auto& intrinsic_map =
  19. *new std::map<std::string_view, Intrinsic>({{"print", Intrinsic::Print}});
  20. name.remove_prefix(std::strlen("__intrinsic_"));
  21. auto it = intrinsic_map.find(name);
  22. if (it == intrinsic_map.end()) {
  23. FATAL_COMPILATION_ERROR(source_loc) << "Unknown intrinsic '" << name << "'";
  24. }
  25. return it->second;
  26. }
  27. auto ExpressionFromParenContents(
  28. Nonnull<Arena*> arena, SourceLocation source_loc,
  29. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
  30. std::optional<Nonnull<Expression*>> single_term = paren_contents.SingleTerm();
  31. if (single_term.has_value()) {
  32. return *single_term;
  33. } else {
  34. return TupleExpressionFromParenContents(arena, source_loc, paren_contents);
  35. }
  36. }
  37. auto TupleExpressionFromParenContents(
  38. Nonnull<Arena*> arena, SourceLocation source_loc,
  39. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*> {
  40. return arena->New<TupleLiteral>(source_loc, paren_contents.elements);
  41. }
  42. Expression::~Expression() = default;
  43. auto ToString(Operator op) -> std::string_view {
  44. switch (op) {
  45. case Operator::Add:
  46. return "+";
  47. case Operator::Neg:
  48. case Operator::Sub:
  49. return "-";
  50. case Operator::Mul:
  51. case Operator::Deref:
  52. case Operator::Ptr:
  53. return "*";
  54. case Operator::Not:
  55. return "not";
  56. case Operator::And:
  57. return "and";
  58. case Operator::Or:
  59. return "or";
  60. case Operator::Eq:
  61. return "==";
  62. }
  63. }
  64. static void PrintFields(llvm::raw_ostream& out,
  65. const std::vector<FieldInitializer>& fields,
  66. std::string_view separator) {
  67. llvm::ListSeparator sep;
  68. for (const auto& field : fields) {
  69. out << sep << "." << field.name() << separator << field.expression();
  70. }
  71. }
  72. void Expression::Print(llvm::raw_ostream& out) const {
  73. switch (kind()) {
  74. case ExpressionKind::IndexExpression: {
  75. const auto& index = cast<IndexExpression>(*this);
  76. out << index.aggregate() << "[" << index.offset() << "]";
  77. break;
  78. }
  79. case ExpressionKind::FieldAccessExpression: {
  80. const auto& access = cast<FieldAccessExpression>(*this);
  81. out << access.aggregate() << "." << access.field();
  82. break;
  83. }
  84. case ExpressionKind::TupleLiteral: {
  85. out << "(";
  86. llvm::ListSeparator sep;
  87. for (Nonnull<const Expression*> field :
  88. cast<TupleLiteral>(*this).fields()) {
  89. out << sep << *field;
  90. }
  91. out << ")";
  92. break;
  93. }
  94. case ExpressionKind::StructLiteral:
  95. out << "{";
  96. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  97. out << "}";
  98. break;
  99. case ExpressionKind::StructTypeLiteral:
  100. out << "{";
  101. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  102. out << "}";
  103. break;
  104. case ExpressionKind::IntLiteral:
  105. out << cast<IntLiteral>(*this).value();
  106. break;
  107. case ExpressionKind::BoolLiteral:
  108. out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
  109. break;
  110. case ExpressionKind::PrimitiveOperatorExpression: {
  111. out << "(";
  112. const auto& op = cast<PrimitiveOperatorExpression>(*this);
  113. switch (op.arguments().size()) {
  114. case 0:
  115. out << ToString(op.op());
  116. break;
  117. case 1:
  118. out << ToString(op.op()) << " " << *op.arguments()[0];
  119. break;
  120. case 2:
  121. out << *op.arguments()[0] << " " << ToString(op.op()) << " "
  122. << *op.arguments()[1];
  123. break;
  124. default:
  125. FATAL() << "Unexpected argument count: " << op.arguments().size();
  126. }
  127. out << ")";
  128. break;
  129. }
  130. case ExpressionKind::IdentifierExpression:
  131. out << cast<IdentifierExpression>(*this).name();
  132. break;
  133. case ExpressionKind::CallExpression: {
  134. const auto& call = cast<CallExpression>(*this);
  135. out << call.function();
  136. if (isa<TupleLiteral>(call.argument())) {
  137. out << call.argument();
  138. } else {
  139. out << "(" << call.argument() << ")";
  140. }
  141. break;
  142. }
  143. case ExpressionKind::BoolTypeLiteral:
  144. out << "Bool";
  145. break;
  146. case ExpressionKind::IntTypeLiteral:
  147. out << "i32";
  148. break;
  149. case ExpressionKind::StringLiteral:
  150. out << "\"";
  151. out.write_escaped(cast<StringLiteral>(*this).value());
  152. out << "\"";
  153. break;
  154. case ExpressionKind::StringTypeLiteral:
  155. out << "String";
  156. break;
  157. case ExpressionKind::TypeTypeLiteral:
  158. out << "Type";
  159. break;
  160. case ExpressionKind::ContinuationTypeLiteral:
  161. out << "Continuation";
  162. break;
  163. case ExpressionKind::FunctionTypeLiteral: {
  164. const auto& fn = cast<FunctionTypeLiteral>(*this);
  165. out << "fn " << fn.parameter() << " -> " << fn.return_type();
  166. break;
  167. }
  168. case ExpressionKind::IntrinsicExpression:
  169. out << "intrinsic_expression(";
  170. switch (cast<IntrinsicExpression>(*this).intrinsic()) {
  171. case IntrinsicExpression::Intrinsic::Print:
  172. out << "print";
  173. }
  174. out << ")";
  175. break;
  176. case ExpressionKind::UnimplementedExpression: {
  177. const auto& unimplemented = cast<UnimplementedExpression>(*this);
  178. out << "UnimplementedExpression<" << unimplemented.label() << ">(";
  179. llvm::ListSeparator sep;
  180. for (Nonnull<const AstNode*> child : unimplemented.children()) {
  181. out << sep << *child;
  182. }
  183. out << ")";
  184. break;
  185. }
  186. }
  187. }
  188. } // namespace Carbon