expression.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. auto MakeFunType(int line_num, Expression* param, Expression* ret)
  32. -> Expression* {
  33. auto* t = new Expression();
  34. t->tag = ExpressionKind::FunctionT;
  35. t->line_num = line_num;
  36. t->u.function_type.parameter = param;
  37. t->u.function_type.return_type = ret;
  38. return t;
  39. }
  40. auto MakeVar(int line_num, std::string var) -> Expression* {
  41. auto* v = new Expression();
  42. v->line_num = line_num;
  43. v->tag = ExpressionKind::Variable;
  44. v->u.variable.name = new std::string(std::move(var));
  45. return v;
  46. }
  47. auto MakeVarPat(int line_num, std::string var, Expression* type)
  48. -> Expression* {
  49. auto* v = new Expression();
  50. v->line_num = line_num;
  51. v->tag = ExpressionKind::PatternVariable;
  52. v->u.pattern_variable.name = new std::string(std::move(var));
  53. v->u.pattern_variable.type = type;
  54. return v;
  55. }
  56. auto MakeInt(int line_num, int i) -> Expression* {
  57. auto* e = new Expression();
  58. e->line_num = line_num;
  59. e->tag = ExpressionKind::Integer;
  60. e->u.integer = i;
  61. return e;
  62. }
  63. auto MakeBool(int line_num, bool b) -> Expression* {
  64. auto* e = new Expression();
  65. e->line_num = line_num;
  66. e->tag = ExpressionKind::Boolean;
  67. e->u.boolean = b;
  68. return e;
  69. }
  70. auto MakeOp(int line_num, enum Operator op, std::vector<Expression*>* args)
  71. -> Expression* {
  72. auto* e = new Expression();
  73. e->line_num = line_num;
  74. e->tag = ExpressionKind::PrimitiveOp;
  75. e->u.primitive_op.op = op;
  76. e->u.primitive_op.arguments = args;
  77. return e;
  78. }
  79. auto MakeUnOp(int line_num, enum Operator op, Expression* arg) -> Expression* {
  80. auto* e = new Expression();
  81. e->line_num = line_num;
  82. e->tag = ExpressionKind::PrimitiveOp;
  83. e->u.primitive_op.op = op;
  84. auto* args = new std::vector<Expression*>();
  85. args->push_back(arg);
  86. e->u.primitive_op.arguments = args;
  87. return e;
  88. }
  89. auto MakeBinOp(int line_num, enum Operator op, Expression* arg1,
  90. Expression* arg2) -> Expression* {
  91. auto* e = new Expression();
  92. e->line_num = line_num;
  93. e->tag = ExpressionKind::PrimitiveOp;
  94. e->u.primitive_op.op = op;
  95. auto* args = new std::vector<Expression*>();
  96. args->push_back(arg1);
  97. args->push_back(arg2);
  98. e->u.primitive_op.arguments = args;
  99. return e;
  100. }
  101. auto MakeCall(int line_num, Expression* fun, Expression* arg) -> Expression* {
  102. auto* e = new Expression();
  103. e->line_num = line_num;
  104. e->tag = ExpressionKind::Call;
  105. e->u.call.function = fun;
  106. e->u.call.argument = arg;
  107. return e;
  108. }
  109. auto MakeGetField(int line_num, Expression* exp, std::string field)
  110. -> Expression* {
  111. auto* e = new Expression();
  112. e->line_num = line_num;
  113. e->tag = ExpressionKind::GetField;
  114. e->u.get_field.aggregate = exp;
  115. e->u.get_field.field = new std::string(std::move(field));
  116. return e;
  117. }
  118. auto MakeTuple(int line_num,
  119. std::vector<std::pair<std::string, Expression*>>* args)
  120. -> Expression* {
  121. auto* e = new Expression();
  122. e->line_num = line_num;
  123. e->tag = ExpressionKind::Tuple;
  124. int i = 0;
  125. for (auto& arg : *args) {
  126. if (arg.first == "") {
  127. arg.first = std::to_string(i);
  128. ++i;
  129. }
  130. }
  131. e->u.tuple.fields = args;
  132. return e;
  133. }
  134. auto MakeIndex(int line_num, Expression* exp, Expression* i) -> Expression* {
  135. auto* e = new Expression();
  136. e->line_num = line_num;
  137. e->tag = ExpressionKind::Index;
  138. e->u.index.aggregate = exp;
  139. e->u.index.offset = i;
  140. return e;
  141. }
  142. static void PrintOp(Operator op) {
  143. switch (op) {
  144. case Operator::Neg:
  145. std::cout << "-";
  146. break;
  147. case Operator::Add:
  148. std::cout << "+";
  149. break;
  150. case Operator::Sub:
  151. std::cout << "-";
  152. break;
  153. case Operator::Not:
  154. std::cout << "!";
  155. break;
  156. case Operator::And:
  157. std::cout << "&&";
  158. break;
  159. case Operator::Or:
  160. std::cout << "||";
  161. break;
  162. case Operator::Eq:
  163. std::cout << "==";
  164. break;
  165. }
  166. }
  167. static void PrintFields(
  168. std::vector<std::pair<std::string, Expression*>>* fields) {
  169. int i = 0;
  170. for (auto iter = fields->begin(); iter != fields->end(); ++iter, ++i) {
  171. if (i != 0) {
  172. std::cout << ", ";
  173. }
  174. std::cout << iter->first << " = ";
  175. PrintExp(iter->second);
  176. }
  177. }
  178. void PrintExp(const Expression* e) {
  179. switch (e->tag) {
  180. case ExpressionKind::Index:
  181. PrintExp(e->u.index.aggregate);
  182. std::cout << "[";
  183. PrintExp(e->u.index.offset);
  184. std::cout << "]";
  185. break;
  186. case ExpressionKind::GetField:
  187. PrintExp(e->u.get_field.aggregate);
  188. std::cout << ".";
  189. std::cout << *e->u.get_field.field;
  190. break;
  191. case ExpressionKind::Tuple:
  192. std::cout << "(";
  193. PrintFields(e->u.tuple.fields);
  194. std::cout << ")";
  195. break;
  196. case ExpressionKind::Integer:
  197. std::cout << e->u.integer;
  198. break;
  199. case ExpressionKind::Boolean:
  200. std::cout << std::boolalpha;
  201. std::cout << e->u.boolean;
  202. break;
  203. case ExpressionKind::PrimitiveOp:
  204. std::cout << "(";
  205. if (e->u.primitive_op.arguments->size() == 0) {
  206. PrintOp(e->u.primitive_op.op);
  207. } else if (e->u.primitive_op.arguments->size() == 1) {
  208. PrintOp(e->u.primitive_op.op);
  209. std::cout << " ";
  210. auto iter = e->u.primitive_op.arguments->begin();
  211. PrintExp(*iter);
  212. } else if (e->u.primitive_op.arguments->size() == 2) {
  213. auto iter = e->u.primitive_op.arguments->begin();
  214. PrintExp(*iter);
  215. std::cout << " ";
  216. PrintOp(e->u.primitive_op.op);
  217. std::cout << " ";
  218. ++iter;
  219. PrintExp(*iter);
  220. }
  221. std::cout << ")";
  222. break;
  223. case ExpressionKind::Variable:
  224. std::cout << *e->u.variable.name;
  225. break;
  226. case ExpressionKind::PatternVariable:
  227. PrintExp(e->u.pattern_variable.type);
  228. std::cout << ": ";
  229. std::cout << *e->u.pattern_variable.name;
  230. break;
  231. case ExpressionKind::Call:
  232. PrintExp(e->u.call.function);
  233. if (e->u.call.argument->tag == ExpressionKind::Tuple) {
  234. PrintExp(e->u.call.argument);
  235. } else {
  236. std::cout << "(";
  237. PrintExp(e->u.call.argument);
  238. std::cout << ")";
  239. }
  240. break;
  241. case ExpressionKind::BoolT:
  242. std::cout << "Bool";
  243. break;
  244. case ExpressionKind::IntT:
  245. std::cout << "Int";
  246. break;
  247. case ExpressionKind::TypeT:
  248. std::cout << "Type";
  249. break;
  250. case ExpressionKind::AutoT:
  251. std::cout << "auto";
  252. break;
  253. case ExpressionKind::FunctionT:
  254. std::cout << "fn ";
  255. PrintExp(e->u.function_type.parameter);
  256. std::cout << " -> ";
  257. PrintExp(e->u.function_type.return_type);
  258. break;
  259. }
  260. }
  261. } // namespace Carbon