expression.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <iostream>
  6. namespace Carbon {
  7. auto Expression::GetIdentifierExpression() const
  8. -> const IdentifierExpression& {
  9. return std::get<IdentifierExpression>(value);
  10. }
  11. auto Expression::GetFieldAccessExpression() const
  12. -> const FieldAccessExpression& {
  13. return std::get<FieldAccessExpression>(value);
  14. }
  15. auto Expression::GetIndexExpression() const -> const IndexExpression& {
  16. return std::get<IndexExpression>(value);
  17. }
  18. auto Expression::GetPatternVariableExpression() const
  19. -> const PatternVariableExpression& {
  20. return std::get<PatternVariableExpression>(value);
  21. }
  22. auto Expression::GetIntLiteral() const -> int {
  23. return std::get<IntLiteral>(value).value;
  24. }
  25. auto Expression::GetBoolLiteral() const -> bool {
  26. return std::get<BoolLiteral>(value).value;
  27. }
  28. auto Expression::GetTupleLiteral() const -> const TupleLiteral& {
  29. return std::get<TupleLiteral>(value);
  30. }
  31. auto Expression::GetPrimitiveOperatorExpression() const
  32. -> const PrimitiveOperatorExpression& {
  33. return std::get<PrimitiveOperatorExpression>(value);
  34. }
  35. auto Expression::GetCallExpression() const -> const CallExpression& {
  36. return std::get<CallExpression>(value);
  37. }
  38. auto Expression::GetFunctionTypeLiteral() const -> const FunctionTypeLiteral& {
  39. return std::get<FunctionTypeLiteral>(value);
  40. }
  41. auto Expression::MakeTypeTypeLiteral(int line_num) -> const Expression* {
  42. auto* t = new Expression();
  43. t->line_num = line_num;
  44. t->value = TypeTypeLiteral();
  45. return t;
  46. }
  47. auto Expression::MakeIntTypeLiteral(int line_num) -> const Expression* {
  48. auto* t = new Expression();
  49. t->line_num = line_num;
  50. t->value = IntTypeLiteral();
  51. return t;
  52. }
  53. auto Expression::MakeBoolTypeLiteral(int line_num) -> const Expression* {
  54. auto* t = new Expression();
  55. t->line_num = line_num;
  56. t->value = BoolTypeLiteral();
  57. return t;
  58. }
  59. auto Expression::MakeAutoTypeLiteral(int line_num) -> const Expression* {
  60. auto* t = new Expression();
  61. t->line_num = line_num;
  62. t->value = AutoTypeLiteral();
  63. return t;
  64. }
  65. // Returns a Continuation type AST node at the given source location.
  66. auto Expression::MakeContinuationTypeLiteral(int line_num)
  67. -> const Expression* {
  68. auto* type = new Expression();
  69. type->line_num = line_num;
  70. type->value = ContinuationTypeLiteral();
  71. return type;
  72. }
  73. auto Expression::MakeFunctionTypeLiteral(int line_num, const Expression* param,
  74. const Expression* ret)
  75. -> const Expression* {
  76. auto* t = new Expression();
  77. t->line_num = line_num;
  78. t->value = FunctionTypeLiteral({.parameter = param, .return_type = ret});
  79. return t;
  80. }
  81. auto Expression::MakeIdentifierExpression(int line_num, std::string var)
  82. -> const Expression* {
  83. auto* v = new Expression();
  84. v->line_num = line_num;
  85. v->value = IdentifierExpression({.name = std::move(var)});
  86. return v;
  87. }
  88. auto Expression::MakePatternVariableExpression(int line_num, std::string var,
  89. const Expression* type)
  90. -> const Expression* {
  91. auto* v = new Expression();
  92. v->line_num = line_num;
  93. v->value = PatternVariableExpression({.name = std::move(var), .type = type});
  94. return v;
  95. }
  96. auto Expression::MakeIntLiteral(int line_num, int i) -> const Expression* {
  97. auto* e = new Expression();
  98. e->line_num = line_num;
  99. e->value = IntLiteral({.value = i});
  100. return e;
  101. }
  102. auto Expression::MakeBoolLiteral(int line_num, bool b) -> const Expression* {
  103. auto* e = new Expression();
  104. e->line_num = line_num;
  105. e->value = BoolLiteral({.value = b});
  106. return e;
  107. }
  108. auto Expression::MakePrimitiveOperatorExpression(
  109. int line_num, enum Operator op, std::vector<const Expression*> args)
  110. -> const Expression* {
  111. auto* e = new Expression();
  112. e->line_num = line_num;
  113. e->value =
  114. PrimitiveOperatorExpression({.op = op, .arguments = std::move(args)});
  115. return e;
  116. }
  117. auto Expression::MakeCallExpression(int line_num, const Expression* fun,
  118. const Expression* arg)
  119. -> const Expression* {
  120. auto* e = new Expression();
  121. e->line_num = line_num;
  122. e->value = CallExpression({.function = fun, .argument = arg});
  123. return e;
  124. }
  125. auto Expression::MakeFieldAccessExpression(int line_num, const Expression* exp,
  126. std::string field)
  127. -> const Expression* {
  128. auto* e = new Expression();
  129. e->line_num = line_num;
  130. e->value =
  131. FieldAccessExpression({.aggregate = exp, .field = std::move(field)});
  132. return e;
  133. }
  134. auto Expression::MakeTupleLiteral(int line_num,
  135. std::vector<FieldInitializer> args)
  136. -> const Expression* {
  137. auto* e = new Expression();
  138. e->line_num = line_num;
  139. int i = 0;
  140. bool seen_named_member = false;
  141. for (auto& arg : args) {
  142. if (arg.name == "") {
  143. if (seen_named_member) {
  144. std::cerr << line_num
  145. << ": positional members must come before named members"
  146. << std::endl;
  147. exit(-1);
  148. }
  149. arg.name = std::to_string(i);
  150. ++i;
  151. } else {
  152. seen_named_member = true;
  153. }
  154. }
  155. e->value = TupleLiteral({.fields = args});
  156. return e;
  157. }
  158. auto Expression::MakeIndexExpression(int line_num, const Expression* exp,
  159. const Expression* i) -> const Expression* {
  160. auto* e = new Expression();
  161. e->line_num = line_num;
  162. e->value = IndexExpression({.aggregate = exp, .offset = i});
  163. return e;
  164. }
  165. static void PrintOp(Operator op) {
  166. switch (op) {
  167. case Operator::Add:
  168. std::cout << "+";
  169. break;
  170. case Operator::Neg:
  171. case Operator::Sub:
  172. std::cout << "-";
  173. break;
  174. case Operator::Mul:
  175. case Operator::Deref:
  176. case Operator::Ptr:
  177. std::cout << "*";
  178. break;
  179. case Operator::Not:
  180. std::cout << "not";
  181. break;
  182. case Operator::And:
  183. std::cout << "and";
  184. break;
  185. case Operator::Or:
  186. std::cout << "or";
  187. break;
  188. case Operator::Eq:
  189. std::cout << "==";
  190. break;
  191. }
  192. }
  193. static void PrintFields(const std::vector<FieldInitializer>& fields) {
  194. int i = 0;
  195. for (auto iter = fields.begin(); iter != fields.end(); ++iter, ++i) {
  196. if (i != 0) {
  197. std::cout << ", ";
  198. }
  199. std::cout << iter->name << " = ";
  200. PrintExp(iter->expression);
  201. }
  202. }
  203. void PrintExp(const Expression* e) {
  204. switch (e->tag()) {
  205. case ExpressionKind::IndexExpression:
  206. PrintExp(e->GetIndexExpression().aggregate);
  207. std::cout << "[";
  208. PrintExp(e->GetIndexExpression().offset);
  209. std::cout << "]";
  210. break;
  211. case ExpressionKind::FieldAccessExpression:
  212. PrintExp(e->GetFieldAccessExpression().aggregate);
  213. std::cout << ".";
  214. std::cout << e->GetFieldAccessExpression().field;
  215. break;
  216. case ExpressionKind::TupleLiteral:
  217. std::cout << "(";
  218. PrintFields(e->GetTupleLiteral().fields);
  219. std::cout << ")";
  220. break;
  221. case ExpressionKind::IntLiteral:
  222. std::cout << e->GetIntLiteral();
  223. break;
  224. case ExpressionKind::BoolLiteral:
  225. std::cout << std::boolalpha;
  226. std::cout << e->GetBoolLiteral();
  227. break;
  228. case ExpressionKind::PrimitiveOperatorExpression: {
  229. std::cout << "(";
  230. PrimitiveOperatorExpression op = e->GetPrimitiveOperatorExpression();
  231. if (op.arguments.size() == 0) {
  232. PrintOp(op.op);
  233. } else if (op.arguments.size() == 1) {
  234. PrintOp(op.op);
  235. std::cout << " ";
  236. auto iter = op.arguments.begin();
  237. PrintExp(*iter);
  238. } else if (op.arguments.size() == 2) {
  239. auto iter = op.arguments.begin();
  240. PrintExp(*iter);
  241. std::cout << " ";
  242. PrintOp(op.op);
  243. std::cout << " ";
  244. ++iter;
  245. PrintExp(*iter);
  246. }
  247. std::cout << ")";
  248. break;
  249. }
  250. case ExpressionKind::IdentifierExpression:
  251. std::cout << e->GetIdentifierExpression().name;
  252. break;
  253. case ExpressionKind::PatternVariableExpression:
  254. PrintExp(e->GetPatternVariableExpression().type);
  255. std::cout << ": ";
  256. std::cout << e->GetPatternVariableExpression().name;
  257. break;
  258. case ExpressionKind::CallExpression:
  259. PrintExp(e->GetCallExpression().function);
  260. if (e->GetCallExpression().argument->tag() ==
  261. ExpressionKind::TupleLiteral) {
  262. PrintExp(e->GetCallExpression().argument);
  263. } else {
  264. std::cout << "(";
  265. PrintExp(e->GetCallExpression().argument);
  266. std::cout << ")";
  267. }
  268. break;
  269. case ExpressionKind::BoolTypeLiteral:
  270. std::cout << "Bool";
  271. break;
  272. case ExpressionKind::IntTypeLiteral:
  273. std::cout << "Int";
  274. break;
  275. case ExpressionKind::TypeTypeLiteral:
  276. std::cout << "Type";
  277. break;
  278. case ExpressionKind::AutoTypeLiteral:
  279. std::cout << "auto";
  280. break;
  281. case ExpressionKind::ContinuationTypeLiteral:
  282. std::cout << "Continuation";
  283. break;
  284. case ExpressionKind::FunctionTypeLiteral:
  285. std::cout << "fn ";
  286. PrintExp(e->GetFunctionTypeLiteral().parameter);
  287. std::cout << " -> ";
  288. PrintExp(e->GetFunctionTypeLiteral().return_type);
  289. break;
  290. }
  291. }
  292. } // namespace Carbon