expression.cpp 6.4 KB

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