expression.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. for (auto& arg : *args) {
  135. if (arg.first == "") {
  136. arg.first = std::to_string(i);
  137. ++i;
  138. }
  139. }
  140. e->u.tuple.fields = args;
  141. return e;
  142. }
  143. // Create an AST node for an empty tuple.
  144. // TODO(geoffromer): remove this and rewrite its callers to use
  145. // `MakeTuple(line_num, {})`, once that works.
  146. auto MakeUnit(int line_num) -> const Expression* {
  147. auto* unit = new Expression();
  148. unit->line_num = line_num;
  149. unit->tag = ExpressionKind::Tuple;
  150. auto* args = new std::vector<std::pair<std::string, const Expression*>>();
  151. unit->u.tuple.fields = args;
  152. return unit;
  153. }
  154. auto MakeIndex(int line_num, const Expression* exp, const Expression* i)
  155. -> const Expression* {
  156. auto* e = new Expression();
  157. e->line_num = line_num;
  158. e->tag = ExpressionKind::Index;
  159. e->u.index.aggregate = exp;
  160. e->u.index.offset = i;
  161. return e;
  162. }
  163. static void PrintOp(Operator op) {
  164. switch (op) {
  165. case Operator::Neg:
  166. std::cout << "-";
  167. break;
  168. case Operator::Add:
  169. std::cout << "+";
  170. break;
  171. case Operator::Sub:
  172. std::cout << "-";
  173. break;
  174. case Operator::Not:
  175. std::cout << "not";
  176. break;
  177. case Operator::And:
  178. std::cout << "and";
  179. break;
  180. case Operator::Or:
  181. std::cout << "or";
  182. break;
  183. case Operator::Eq:
  184. std::cout << "==";
  185. break;
  186. }
  187. }
  188. static void PrintFields(
  189. std::vector<std::pair<std::string, const Expression*>>* fields) {
  190. int i = 0;
  191. for (auto iter = fields->begin(); iter != fields->end(); ++iter, ++i) {
  192. if (i != 0) {
  193. std::cout << ", ";
  194. }
  195. std::cout << iter->first << " = ";
  196. PrintExp(iter->second);
  197. }
  198. }
  199. void PrintExp(const Expression* e) {
  200. switch (e->tag) {
  201. case ExpressionKind::Index:
  202. PrintExp(e->u.index.aggregate);
  203. std::cout << "[";
  204. PrintExp(e->u.index.offset);
  205. std::cout << "]";
  206. break;
  207. case ExpressionKind::GetField:
  208. PrintExp(e->u.get_field.aggregate);
  209. std::cout << ".";
  210. std::cout << *e->u.get_field.field;
  211. break;
  212. case ExpressionKind::Tuple:
  213. std::cout << "(";
  214. PrintFields(e->u.tuple.fields);
  215. std::cout << ")";
  216. break;
  217. case ExpressionKind::Integer:
  218. std::cout << e->u.integer;
  219. break;
  220. case ExpressionKind::Boolean:
  221. std::cout << std::boolalpha;
  222. std::cout << e->u.boolean;
  223. break;
  224. case ExpressionKind::PrimitiveOp:
  225. std::cout << "(";
  226. if (e->u.primitive_op.arguments->size() == 0) {
  227. PrintOp(e->u.primitive_op.op);
  228. } else if (e->u.primitive_op.arguments->size() == 1) {
  229. PrintOp(e->u.primitive_op.op);
  230. std::cout << " ";
  231. auto iter = e->u.primitive_op.arguments->begin();
  232. PrintExp(*iter);
  233. } else if (e->u.primitive_op.arguments->size() == 2) {
  234. auto iter = e->u.primitive_op.arguments->begin();
  235. PrintExp(*iter);
  236. std::cout << " ";
  237. PrintOp(e->u.primitive_op.op);
  238. std::cout << " ";
  239. ++iter;
  240. PrintExp(*iter);
  241. }
  242. std::cout << ")";
  243. break;
  244. case ExpressionKind::Variable:
  245. std::cout << *e->u.variable.name;
  246. break;
  247. case ExpressionKind::PatternVariable:
  248. PrintExp(e->u.pattern_variable.type);
  249. std::cout << ": ";
  250. std::cout << *e->u.pattern_variable.name;
  251. break;
  252. case ExpressionKind::Call:
  253. PrintExp(e->u.call.function);
  254. if (e->u.call.argument->tag == ExpressionKind::Tuple) {
  255. PrintExp(e->u.call.argument);
  256. } else {
  257. std::cout << "(";
  258. PrintExp(e->u.call.argument);
  259. std::cout << ")";
  260. }
  261. break;
  262. case ExpressionKind::BoolT:
  263. std::cout << "Bool";
  264. break;
  265. case ExpressionKind::IntT:
  266. std::cout << "Int";
  267. break;
  268. case ExpressionKind::TypeT:
  269. std::cout << "Type";
  270. break;
  271. case ExpressionKind::AutoT:
  272. std::cout << "auto";
  273. break;
  274. case ExpressionKind::ContinuationT:
  275. std::cout << "Continuation";
  276. break;
  277. case ExpressionKind::FunctionT:
  278. std::cout << "fn ";
  279. PrintExp(e->u.function_type.parameter);
  280. std::cout << " -> ";
  281. PrintExp(e->u.function_type.return_type);
  282. break;
  283. }
  284. }
  285. } // namespace Carbon