expression.cpp 8.5 KB

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