expression.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. int line_num, const ParenContents<Expression>& paren_contents)
  15. -> const Expression* {
  16. std::optional<const Expression*> single_term = paren_contents.SingleTerm();
  17. if (single_term.has_value()) {
  18. return *single_term;
  19. } else {
  20. return TupleExpressionFromParenContents(line_num, paren_contents);
  21. }
  22. }
  23. auto TupleExpressionFromParenContents(
  24. int line_num, const ParenContents<Expression>& paren_contents)
  25. -> const Expression* {
  26. return global_arena->New<TupleLiteral>(
  27. line_num, paren_contents.TupleElements<FieldInitializer>(line_num));
  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. llvm::ListSeparator sep;
  60. for (const auto& field : fields) {
  61. out << sep << field.name << " = " << field.expression;
  62. }
  63. }
  64. void Expression::Print(llvm::raw_ostream& out) const {
  65. switch (Tag()) {
  66. case Expression::Kind::IndexExpression: {
  67. const auto& index = cast<IndexExpression>(*this);
  68. out << *index.Aggregate() << "[" << *index.Offset() << "]";
  69. break;
  70. }
  71. case Expression::Kind::FieldAccessExpression: {
  72. const auto& access = cast<FieldAccessExpression>(*this);
  73. out << *access.Aggregate() << "." << access.Field();
  74. break;
  75. }
  76. case Expression::Kind::TupleLiteral:
  77. out << "(";
  78. PrintFields(out, cast<TupleLiteral>(*this).Fields());
  79. out << ")";
  80. break;
  81. case Expression::Kind::IntLiteral:
  82. out << cast<IntLiteral>(*this).Val();
  83. break;
  84. case Expression::Kind::BoolLiteral:
  85. out << (cast<BoolLiteral>(*this).Val() ? "true" : "false");
  86. break;
  87. case Expression::Kind::PrimitiveOperatorExpression: {
  88. out << "(";
  89. PrimitiveOperatorExpression op = cast<PrimitiveOperatorExpression>(*this);
  90. if (op.Arguments().size() == 0) {
  91. PrintOp(out, op.Op());
  92. } else if (op.Arguments().size() == 1) {
  93. PrintOp(out, op.Op());
  94. out << " " << *op.Arguments()[0];
  95. } else if (op.Arguments().size() == 2) {
  96. out << *op.Arguments()[0] << " ";
  97. PrintOp(out, op.Op());
  98. out << " " << *op.Arguments()[1];
  99. }
  100. out << ")";
  101. break;
  102. }
  103. case Expression::Kind::IdentifierExpression:
  104. out << cast<IdentifierExpression>(*this).Name();
  105. break;
  106. case Expression::Kind::CallExpression: {
  107. const auto& call = cast<CallExpression>(*this);
  108. out << *call.Function();
  109. if (call.Argument()->Tag() == Expression::Kind::TupleLiteral) {
  110. out << *call.Argument();
  111. } else {
  112. out << "(" << *call.Argument() << ")";
  113. }
  114. break;
  115. }
  116. case Expression::Kind::BoolTypeLiteral:
  117. out << "Bool";
  118. break;
  119. case Expression::Kind::IntTypeLiteral:
  120. out << "i32";
  121. break;
  122. case Expression::Kind::TypeTypeLiteral:
  123. out << "Type";
  124. break;
  125. case Expression::Kind::ContinuationTypeLiteral:
  126. out << "Continuation";
  127. break;
  128. case Expression::Kind::FunctionTypeLiteral: {
  129. const auto& fn = cast<FunctionTypeLiteral>(*this);
  130. out << "fn " << *fn.Parameter() << " -> " << *fn.ReturnType();
  131. break;
  132. }
  133. }
  134. }
  135. } // namespace Carbon