expression.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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, std::string var,
  88. const Expression* type)
  89. -> const Expression* {
  90. auto* v = new Expression();
  91. v->line_num = line_num;
  92. v->value = BindingExpression({.name = std::move(var), .type = type});
  93. return v;
  94. }
  95. auto Expression::MakeIntLiteral(int line_num, int i) -> const Expression* {
  96. auto* e = new Expression();
  97. e->line_num = line_num;
  98. e->value = IntLiteral({.value = i});
  99. return e;
  100. }
  101. auto Expression::MakeBoolLiteral(int line_num, bool b) -> const Expression* {
  102. auto* e = new Expression();
  103. e->line_num = line_num;
  104. e->value = BoolLiteral({.value = b});
  105. return e;
  106. }
  107. auto Expression::MakePrimitiveOperatorExpression(
  108. int line_num, enum Operator op, std::vector<const Expression*> args)
  109. -> const Expression* {
  110. auto* e = new Expression();
  111. e->line_num = line_num;
  112. e->value =
  113. PrimitiveOperatorExpression({.op = op, .arguments = std::move(args)});
  114. return e;
  115. }
  116. auto Expression::MakeCallExpression(int line_num, const Expression* fun,
  117. const Expression* arg)
  118. -> const Expression* {
  119. auto* e = new Expression();
  120. e->line_num = line_num;
  121. e->value = CallExpression({.function = fun, .argument = arg});
  122. return e;
  123. }
  124. auto Expression::MakeFieldAccessExpression(int line_num, const Expression* exp,
  125. std::string field)
  126. -> const Expression* {
  127. auto* e = new Expression();
  128. e->line_num = line_num;
  129. e->value =
  130. FieldAccessExpression({.aggregate = exp, .field = std::move(field)});
  131. return e;
  132. }
  133. auto Expression::MakeTupleLiteral(int line_num,
  134. std::vector<FieldInitializer> args)
  135. -> const Expression* {
  136. auto* e = new Expression();
  137. e->line_num = line_num;
  138. int i = 0;
  139. bool seen_named_member = false;
  140. for (auto& arg : args) {
  141. if (arg.name == "") {
  142. if (seen_named_member) {
  143. std::cerr << line_num
  144. << ": positional members must come before named members"
  145. << std::endl;
  146. exit(-1);
  147. }
  148. arg.name = std::to_string(i);
  149. ++i;
  150. } else {
  151. seen_named_member = true;
  152. }
  153. }
  154. e->value = TupleLiteral({.fields = args});
  155. return e;
  156. }
  157. auto Expression::MakeIndexExpression(int line_num, const Expression* exp,
  158. const Expression* i) -> const Expression* {
  159. auto* e = new Expression();
  160. e->line_num = line_num;
  161. e->value = IndexExpression({.aggregate = exp, .offset = i});
  162. return e;
  163. }
  164. static void PrintOp(Operator op) {
  165. switch (op) {
  166. case Operator::Add:
  167. std::cout << "+";
  168. break;
  169. case Operator::Neg:
  170. case Operator::Sub:
  171. std::cout << "-";
  172. break;
  173. case Operator::Mul:
  174. case Operator::Deref:
  175. case Operator::Ptr:
  176. std::cout << "*";
  177. break;
  178. case Operator::Not:
  179. std::cout << "not";
  180. break;
  181. case Operator::And:
  182. std::cout << "and";
  183. break;
  184. case Operator::Or:
  185. std::cout << "or";
  186. break;
  187. case Operator::Eq:
  188. std::cout << "==";
  189. break;
  190. }
  191. }
  192. static void PrintFields(const std::vector<FieldInitializer>& fields) {
  193. int i = 0;
  194. for (auto iter = fields.begin(); iter != fields.end(); ++iter, ++i) {
  195. if (i != 0) {
  196. std::cout << ", ";
  197. }
  198. std::cout << iter->name << " = ";
  199. PrintExp(iter->expression);
  200. }
  201. }
  202. void PrintExp(const Expression* e) {
  203. switch (e->tag()) {
  204. case ExpressionKind::IndexExpression:
  205. PrintExp(e->GetIndexExpression().aggregate);
  206. std::cout << "[";
  207. PrintExp(e->GetIndexExpression().offset);
  208. std::cout << "]";
  209. break;
  210. case ExpressionKind::FieldAccessExpression:
  211. PrintExp(e->GetFieldAccessExpression().aggregate);
  212. std::cout << ".";
  213. std::cout << e->GetFieldAccessExpression().field;
  214. break;
  215. case ExpressionKind::TupleLiteral:
  216. std::cout << "(";
  217. PrintFields(e->GetTupleLiteral().fields);
  218. std::cout << ")";
  219. break;
  220. case ExpressionKind::IntLiteral:
  221. std::cout << e->GetIntLiteral();
  222. break;
  223. case ExpressionKind::BoolLiteral:
  224. std::cout << std::boolalpha;
  225. std::cout << e->GetBoolLiteral();
  226. break;
  227. case ExpressionKind::PrimitiveOperatorExpression: {
  228. std::cout << "(";
  229. PrimitiveOperatorExpression op = e->GetPrimitiveOperatorExpression();
  230. if (op.arguments.size() == 0) {
  231. PrintOp(op.op);
  232. } else if (op.arguments.size() == 1) {
  233. PrintOp(op.op);
  234. std::cout << " ";
  235. auto iter = op.arguments.begin();
  236. PrintExp(*iter);
  237. } else if (op.arguments.size() == 2) {
  238. auto iter = op.arguments.begin();
  239. PrintExp(*iter);
  240. std::cout << " ";
  241. PrintOp(op.op);
  242. std::cout << " ";
  243. ++iter;
  244. PrintExp(*iter);
  245. }
  246. std::cout << ")";
  247. break;
  248. }
  249. case ExpressionKind::IdentifierExpression:
  250. std::cout << e->GetIdentifierExpression().name;
  251. break;
  252. case ExpressionKind::BindingExpression:
  253. PrintExp(e->GetBindingExpression().type);
  254. std::cout << ": ";
  255. std::cout << e->GetBindingExpression().name;
  256. break;
  257. case ExpressionKind::CallExpression:
  258. PrintExp(e->GetCallExpression().function);
  259. if (e->GetCallExpression().argument->tag() ==
  260. ExpressionKind::TupleLiteral) {
  261. PrintExp(e->GetCallExpression().argument);
  262. } else {
  263. std::cout << "(";
  264. PrintExp(e->GetCallExpression().argument);
  265. std::cout << ")";
  266. }
  267. break;
  268. case ExpressionKind::BoolTypeLiteral:
  269. std::cout << "Bool";
  270. break;
  271. case ExpressionKind::IntTypeLiteral:
  272. std::cout << "Int";
  273. break;
  274. case ExpressionKind::TypeTypeLiteral:
  275. std::cout << "Type";
  276. break;
  277. case ExpressionKind::AutoTypeLiteral:
  278. std::cout << "auto";
  279. break;
  280. case ExpressionKind::ContinuationTypeLiteral:
  281. std::cout << "Continuation";
  282. break;
  283. case ExpressionKind::FunctionTypeLiteral:
  284. std::cout << "fn ";
  285. PrintExp(e->GetFunctionTypeLiteral().parameter);
  286. std::cout << " -> ";
  287. PrintExp(e->GetFunctionTypeLiteral().return_type);
  288. break;
  289. }
  290. }
  291. } // namespace Carbon