expression.cpp 9.2 KB

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