expression.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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::GetBindingExpression() const -> const BindingExpression& {
  19. return std::get<BindingExpression>(value);
  20. }
  21. auto Expression::GetIntLiteral() const -> int {
  22. return std::get<IntLiteral>(value).value;
  23. }
  24. auto Expression::GetBoolLiteral() const -> bool {
  25. return std::get<BoolLiteral>(value).value;
  26. }
  27. auto Expression::GetTupleLiteral() const -> const TupleLiteral& {
  28. return std::get<TupleLiteral>(value);
  29. }
  30. auto Expression::GetPrimitiveOperatorExpression() const
  31. -> const PrimitiveOperatorExpression& {
  32. return std::get<PrimitiveOperatorExpression>(value);
  33. }
  34. auto Expression::GetCallExpression() const -> const CallExpression& {
  35. return std::get<CallExpression>(value);
  36. }
  37. auto Expression::GetFunctionTypeLiteral() const -> const FunctionTypeLiteral& {
  38. return std::get<FunctionTypeLiteral>(value);
  39. }
  40. auto Expression::MakeTypeTypeLiteral(int line_num) -> const Expression* {
  41. auto* t = new Expression();
  42. t->line_num = line_num;
  43. t->value = TypeTypeLiteral();
  44. return t;
  45. }
  46. auto Expression::MakeIntTypeLiteral(int line_num) -> const Expression* {
  47. auto* t = new Expression();
  48. t->line_num = line_num;
  49. t->value = IntTypeLiteral();
  50. return t;
  51. }
  52. auto Expression::MakeBoolTypeLiteral(int line_num) -> const Expression* {
  53. auto* t = new Expression();
  54. t->line_num = line_num;
  55. t->value = BoolTypeLiteral();
  56. return t;
  57. }
  58. auto Expression::MakeAutoTypeLiteral(int line_num) -> const Expression* {
  59. auto* t = new Expression();
  60. t->line_num = line_num;
  61. t->value = AutoTypeLiteral();
  62. return t;
  63. }
  64. // Returns a Continuation type AST node at the given source location.
  65. auto Expression::MakeContinuationTypeLiteral(int line_num)
  66. -> const Expression* {
  67. auto* type = new Expression();
  68. type->line_num = line_num;
  69. type->value = ContinuationTypeLiteral();
  70. return type;
  71. }
  72. auto Expression::MakeFunctionTypeLiteral(int line_num, const Expression* param,
  73. const Expression* ret)
  74. -> const Expression* {
  75. auto* t = new Expression();
  76. t->line_num = line_num;
  77. t->value = FunctionTypeLiteral({.parameter = param, .return_type = ret});
  78. return t;
  79. }
  80. auto Expression::MakeIdentifierExpression(int line_num, std::string var)
  81. -> const Expression* {
  82. auto* v = new Expression();
  83. v->line_num = line_num;
  84. v->value = IdentifierExpression({.name = std::move(var)});
  85. return v;
  86. }
  87. auto Expression::MakeBindingExpression(int line_num,
  88. std::optional<std::string> var,
  89. const Expression* type)
  90. -> const Expression* {
  91. auto* v = new Expression();
  92. v->line_num = line_num;
  93. v->value = BindingExpression({.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::BindingExpression: {
  254. const BindingExpression& binding = e->GetBindingExpression();
  255. if (binding.name.has_value()) {
  256. std::cout << *binding.name;
  257. } else {
  258. std::cout << "_";
  259. }
  260. std::cout << ": ";
  261. PrintExp(e->GetBindingExpression().type);
  262. break;
  263. }
  264. case ExpressionKind::CallExpression:
  265. PrintExp(e->GetCallExpression().function);
  266. if (e->GetCallExpression().argument->tag() ==
  267. ExpressionKind::TupleLiteral) {
  268. PrintExp(e->GetCallExpression().argument);
  269. } else {
  270. std::cout << "(";
  271. PrintExp(e->GetCallExpression().argument);
  272. std::cout << ")";
  273. }
  274. break;
  275. case ExpressionKind::BoolTypeLiteral:
  276. std::cout << "Bool";
  277. break;
  278. case ExpressionKind::IntTypeLiteral:
  279. std::cout << "Int";
  280. break;
  281. case ExpressionKind::TypeTypeLiteral:
  282. std::cout << "Type";
  283. break;
  284. case ExpressionKind::AutoTypeLiteral:
  285. std::cout << "auto";
  286. break;
  287. case ExpressionKind::ContinuationTypeLiteral:
  288. std::cout << "Continuation";
  289. break;
  290. case ExpressionKind::FunctionTypeLiteral:
  291. std::cout << "fn ";
  292. PrintExp(e->GetFunctionTypeLiteral().parameter);
  293. std::cout << " -> ";
  294. PrintExp(e->GetFunctionTypeLiteral().return_type);
  295. break;
  296. }
  297. }
  298. } // namespace Carbon