expression.cpp 8.0 KB

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