expression.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. auto Expression::MakeIndex(int line_num, const Expression* exp,
  163. const Expression* i) -> const Expression* {
  164. auto* e = new Expression();
  165. e->line_num = line_num;
  166. e->value = Index({.aggregate = exp, .offset = i});
  167. return e;
  168. }
  169. static void PrintOp(Operator op) {
  170. switch (op) {
  171. case Operator::Add:
  172. std::cout << "+";
  173. break;
  174. case Operator::Neg:
  175. case Operator::Sub:
  176. std::cout << "-";
  177. break;
  178. case Operator::Mul:
  179. case Operator::Deref:
  180. case Operator::Ptr:
  181. std::cout << "*";
  182. break;
  183. case Operator::Not:
  184. std::cout << "not";
  185. break;
  186. case Operator::And:
  187. std::cout << "and";
  188. break;
  189. case Operator::Or:
  190. std::cout << "or";
  191. break;
  192. case Operator::Eq:
  193. std::cout << "==";
  194. break;
  195. }
  196. }
  197. static void PrintFields(const std::vector<FieldInitializer>& fields) {
  198. int i = 0;
  199. for (auto iter = fields.begin(); iter != fields.end(); ++iter, ++i) {
  200. if (i != 0) {
  201. std::cout << ", ";
  202. }
  203. std::cout << iter->name << " = ";
  204. PrintExp(iter->expression.GetPointer());
  205. }
  206. }
  207. void PrintExp(const Expression* e) {
  208. switch (e->tag()) {
  209. case ExpressionKind::Index:
  210. PrintExp(e->GetIndex().aggregate);
  211. std::cout << "[";
  212. PrintExp(e->GetIndex().offset);
  213. std::cout << "]";
  214. break;
  215. case ExpressionKind::GetField:
  216. PrintExp(e->GetFieldAccess().aggregate.GetPointer());
  217. std::cout << ".";
  218. std::cout << e->GetFieldAccess().field;
  219. break;
  220. case ExpressionKind::Tuple:
  221. std::cout << "(";
  222. PrintFields(e->GetTuple().fields);
  223. std::cout << ")";
  224. break;
  225. case ExpressionKind::Integer:
  226. std::cout << e->GetInteger();
  227. break;
  228. case ExpressionKind::Boolean:
  229. std::cout << std::boolalpha;
  230. std::cout << e->GetBoolean();
  231. break;
  232. case ExpressionKind::PrimitiveOp: {
  233. std::cout << "(";
  234. PrimitiveOperator op = e->GetPrimitiveOperator();
  235. if (op.arguments->size() == 0) {
  236. PrintOp(op.op);
  237. } else if (op.arguments->size() == 1) {
  238. PrintOp(op.op);
  239. std::cout << " ";
  240. auto iter = op.arguments->begin();
  241. PrintExp(*iter);
  242. } else if (op.arguments->size() == 2) {
  243. auto iter = op.arguments->begin();
  244. PrintExp(*iter);
  245. std::cout << " ";
  246. PrintOp(op.op);
  247. std::cout << " ";
  248. ++iter;
  249. PrintExp(*iter);
  250. }
  251. std::cout << ")";
  252. break;
  253. }
  254. case ExpressionKind::Variable:
  255. std::cout << e->GetVariable().name;
  256. break;
  257. case ExpressionKind::PatternVariable:
  258. PrintExp(e->GetPatternVariable().type);
  259. std::cout << ": ";
  260. std::cout << e->GetPatternVariable().name;
  261. break;
  262. case ExpressionKind::Call:
  263. PrintExp(e->GetCall().function);
  264. if (e->GetCall().argument->tag() == ExpressionKind::Tuple) {
  265. PrintExp(e->GetCall().argument);
  266. } else {
  267. std::cout << "(";
  268. PrintExp(e->GetCall().argument);
  269. std::cout << ")";
  270. }
  271. break;
  272. case ExpressionKind::BoolT:
  273. std::cout << "Bool";
  274. break;
  275. case ExpressionKind::IntT:
  276. std::cout << "Int";
  277. break;
  278. case ExpressionKind::TypeT:
  279. std::cout << "Type";
  280. break;
  281. case ExpressionKind::AutoT:
  282. std::cout << "auto";
  283. break;
  284. case ExpressionKind::ContinuationT:
  285. std::cout << "Continuation";
  286. break;
  287. case ExpressionKind::FunctionT:
  288. std::cout << "fn ";
  289. PrintExp(e->GetFunctionType().parameter);
  290. std::cout << " -> ";
  291. PrintExp(e->GetFunctionType().return_type);
  292. break;
  293. }
  294. }
  295. } // namespace Carbon