expression.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <cassert>
  6. #include <iostream>
  7. namespace Carbon {
  8. auto Expression::GetVariable() const -> const Variable& {
  9. return std::get<Variable>(value);
  10. }
  11. auto Expression::GetFieldAccess() const -> const FieldAccess& {
  12. return std::get<FieldAccess>(value);
  13. }
  14. auto Expression::GetIndex() const -> const Index& {
  15. return std::get<Index>(value);
  16. }
  17. auto Expression::GetPatternVariable() const -> const PatternVariable& {
  18. return std::get<PatternVariable>(value);
  19. }
  20. auto Expression::GetInteger() const -> int {
  21. return std::get<IntLiteral>(value).value;
  22. }
  23. auto Expression::GetBoolean() const -> bool {
  24. return std::get<BoolLiteral>(value).value;
  25. }
  26. auto Expression::GetTuple() const -> const Tuple& {
  27. return std::get<Tuple>(value);
  28. }
  29. auto Expression::GetPrimitiveOperator() const -> const PrimitiveOperator& {
  30. return std::get<PrimitiveOperator>(value);
  31. }
  32. auto Expression::GetCall() const -> const Call& {
  33. return std::get<Call>(value);
  34. }
  35. auto Expression::GetFunctionType() const -> const FunctionType& {
  36. return std::get<FunctionType>(value);
  37. }
  38. auto Expression::MakeTypeType(int line_num) -> const Expression* {
  39. auto* t = new Expression();
  40. t->line_num = line_num;
  41. t->value = TypeT();
  42. return t;
  43. }
  44. auto Expression::MakeIntType(int line_num) -> const Expression* {
  45. auto* t = new Expression();
  46. t->line_num = line_num;
  47. t->value = IntT();
  48. return t;
  49. }
  50. auto Expression::MakeBoolType(int line_num) -> const Expression* {
  51. auto* t = new Expression();
  52. t->line_num = line_num;
  53. t->value = BoolT();
  54. return t;
  55. }
  56. auto Expression::MakeAutoType(int line_num) -> const Expression* {
  57. auto* t = new Expression();
  58. t->line_num = line_num;
  59. t->value = AutoT();
  60. return t;
  61. }
  62. // Returns a Continuation type AST node at the given source location.
  63. auto Expression::MakeContinuationType(int line_num) -> const Expression* {
  64. auto* type = new Expression();
  65. type->line_num = line_num;
  66. type->value = ContinuationT();
  67. return type;
  68. }
  69. auto Expression::MakeFunType(int line_num, const Expression* param,
  70. const Expression* ret) -> const Expression* {
  71. auto* t = new Expression();
  72. t->line_num = line_num;
  73. t->value = FunctionType({.parameter = param, .return_type = ret});
  74. return t;
  75. }
  76. auto Expression::MakeVar(int line_num, std::string var) -> const Expression* {
  77. auto* v = new Expression();
  78. v->line_num = line_num;
  79. v->value = Variable({.name = std::move(var)});
  80. return v;
  81. }
  82. auto Expression::MakeVarPat(int line_num, std::string var,
  83. const Expression* type) -> const Expression* {
  84. auto* v = new Expression();
  85. v->line_num = line_num;
  86. v->value = PatternVariable({.name = std::move(var), .type = type});
  87. return v;
  88. }
  89. auto Expression::MakeInt(int line_num, int i) -> const Expression* {
  90. auto* e = new Expression();
  91. e->line_num = line_num;
  92. e->value = IntLiteral({.value = i});
  93. return e;
  94. }
  95. auto Expression::MakeBool(int line_num, bool b) -> const Expression* {
  96. auto* e = new Expression();
  97. e->line_num = line_num;
  98. e->value = BoolLiteral({.value = b});
  99. return e;
  100. }
  101. auto Expression::MakeOp(int line_num, enum Operator op,
  102. std::vector<const Expression*>* args)
  103. -> const Expression* {
  104. auto* e = new Expression();
  105. e->line_num = line_num;
  106. e->value = PrimitiveOperator({.op = op, .arguments = args});
  107. return e;
  108. }
  109. auto Expression::MakeUnOp(int line_num, enum Operator op, const Expression* arg)
  110. -> const Expression* {
  111. auto* e = new Expression();
  112. e->line_num = line_num;
  113. e->value = PrimitiveOperator(
  114. {.op = op, .arguments = new std::vector<const Expression*>{arg}});
  115. return e;
  116. }
  117. auto Expression::MakeBinOp(int line_num, enum Operator op,
  118. const Expression* arg1, const Expression* arg2)
  119. -> const Expression* {
  120. auto* e = new Expression();
  121. e->line_num = line_num;
  122. e->value = PrimitiveOperator(
  123. {.op = op, .arguments = new std::vector<const Expression*>{arg1, arg2}});
  124. return e;
  125. }
  126. auto Expression::MakeCall(int line_num, const Expression* fun,
  127. const Expression* arg) -> const Expression* {
  128. auto* e = new Expression();
  129. e->line_num = line_num;
  130. e->value = Call({.function = fun, .argument = arg});
  131. return e;
  132. }
  133. auto Expression::MakeGetField(int line_num, const Expression* exp,
  134. std::string field) -> const Expression* {
  135. auto* e = new Expression();
  136. e->line_num = line_num;
  137. e->value = FieldAccess({.aggregate = exp, .field = std::move(field)});
  138. return e;
  139. }
  140. auto Expression::MakeTuple(int line_num, std::vector<FieldInitializer>* args)
  141. -> const Expression* {
  142. auto* e = new Expression();
  143. e->line_num = line_num;
  144. int i = 0;
  145. bool seen_named_member = false;
  146. for (auto& arg : *args) {
  147. if (arg.name == "") {
  148. if (seen_named_member) {
  149. std::cerr << line_num
  150. << ": positional members must come before named members"
  151. << std::endl;
  152. exit(-1);
  153. }
  154. arg.name = std::to_string(i);
  155. ++i;
  156. } else {
  157. seen_named_member = true;
  158. }
  159. }
  160. e->value = Tuple({.fields = args});
  161. return e;
  162. }
  163. // Create an AST node for an empty tuple.
  164. // TODO(geoffromer): remove this and rewrite its callers to use
  165. // `MakeTuple(line_num, {})`, once that works.
  166. auto Expression::MakeUnit(int line_num) -> const Expression* {
  167. auto* unit = new Expression();
  168. unit->line_num = line_num;
  169. auto* args = new std::vector<FieldInitializer>();
  170. unit->value = Tuple({.fields = args});
  171. return unit;
  172. }
  173. auto Expression::MakeIndex(int line_num, const Expression* exp,
  174. const Expression* i) -> const Expression* {
  175. auto* e = new Expression();
  176. e->line_num = line_num;
  177. e->value = Index({.aggregate = exp, .offset = i});
  178. return e;
  179. }
  180. static void PrintOp(Operator op) {
  181. switch (op) {
  182. case Operator::Add:
  183. std::cout << "+";
  184. break;
  185. case Operator::Neg:
  186. case Operator::Sub:
  187. std::cout << "-";
  188. break;
  189. case Operator::Mul:
  190. case Operator::Deref:
  191. case Operator::Ptr:
  192. std::cout << "*";
  193. break;
  194. case Operator::Not:
  195. std::cout << "not";
  196. break;
  197. case Operator::And:
  198. std::cout << "and";
  199. break;
  200. case Operator::Or:
  201. std::cout << "or";
  202. break;
  203. case Operator::Eq:
  204. std::cout << "==";
  205. break;
  206. }
  207. }
  208. static void PrintFields(std::vector<FieldInitializer>* fields) {
  209. int i = 0;
  210. for (auto iter = fields->begin(); iter != fields->end(); ++iter, ++i) {
  211. if (i != 0) {
  212. std::cout << ", ";
  213. }
  214. std::cout << iter->name << " = ";
  215. PrintExp(iter->expression);
  216. }
  217. }
  218. void PrintExp(const Expression* e) {
  219. switch (e->tag()) {
  220. case ExpressionKind::Index:
  221. PrintExp(e->GetIndex().aggregate);
  222. std::cout << "[";
  223. PrintExp(e->GetIndex().offset);
  224. std::cout << "]";
  225. break;
  226. case ExpressionKind::GetField:
  227. PrintExp(e->GetFieldAccess().aggregate);
  228. std::cout << ".";
  229. std::cout << e->GetFieldAccess().field;
  230. break;
  231. case ExpressionKind::Tuple:
  232. std::cout << "(";
  233. PrintFields(e->GetTuple().fields);
  234. std::cout << ")";
  235. break;
  236. case ExpressionKind::Integer:
  237. std::cout << e->GetInteger();
  238. break;
  239. case ExpressionKind::Boolean:
  240. std::cout << std::boolalpha;
  241. std::cout << e->GetBoolean();
  242. break;
  243. case ExpressionKind::PrimitiveOp: {
  244. std::cout << "(";
  245. PrimitiveOperator op = e->GetPrimitiveOperator();
  246. if (op.arguments->size() == 0) {
  247. PrintOp(op.op);
  248. } else if (op.arguments->size() == 1) {
  249. PrintOp(op.op);
  250. std::cout << " ";
  251. auto iter = op.arguments->begin();
  252. PrintExp(*iter);
  253. } else if (op.arguments->size() == 2) {
  254. auto iter = op.arguments->begin();
  255. PrintExp(*iter);
  256. std::cout << " ";
  257. PrintOp(op.op);
  258. std::cout << " ";
  259. ++iter;
  260. PrintExp(*iter);
  261. }
  262. std::cout << ")";
  263. break;
  264. }
  265. case ExpressionKind::Variable:
  266. std::cout << e->GetVariable().name;
  267. break;
  268. case ExpressionKind::PatternVariable:
  269. PrintExp(e->GetPatternVariable().type);
  270. std::cout << ": ";
  271. std::cout << e->GetPatternVariable().name;
  272. break;
  273. case ExpressionKind::Call:
  274. PrintExp(e->GetCall().function);
  275. if (e->GetCall().argument->tag() == ExpressionKind::Tuple) {
  276. PrintExp(e->GetCall().argument);
  277. } else {
  278. std::cout << "(";
  279. PrintExp(e->GetCall().argument);
  280. std::cout << ")";
  281. }
  282. break;
  283. case ExpressionKind::BoolT:
  284. std::cout << "Bool";
  285. break;
  286. case ExpressionKind::IntT:
  287. std::cout << "Int";
  288. break;
  289. case ExpressionKind::TypeT:
  290. std::cout << "Type";
  291. break;
  292. case ExpressionKind::AutoT:
  293. std::cout << "auto";
  294. break;
  295. case ExpressionKind::ContinuationT:
  296. std::cout << "Continuation";
  297. break;
  298. case ExpressionKind::FunctionT:
  299. std::cout << "fn ";
  300. PrintExp(e->GetFunctionType().parameter);
  301. std::cout << " -> ";
  302. PrintExp(e->GetFunctionType().return_type);
  303. break;
  304. }
  305. }
  306. } // namespace Carbon