expression_or_field_list.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_or_field_list.h"
  5. namespace Carbon {
  6. auto MakeExp(Expression* exp) -> ExpOrFieldList* {
  7. auto e = new ExpOrFieldList();
  8. e->tag = ExpOrFieldListKind::Exp;
  9. e->u.exp = exp;
  10. return e;
  11. }
  12. auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
  13. -> ExpOrFieldList* {
  14. auto e = new ExpOrFieldList();
  15. e->tag = ExpOrFieldListKind::FieldList;
  16. e->u.fields = fields;
  17. return e;
  18. }
  19. auto MakeConsField(ExpOrFieldList* e1, ExpOrFieldList* e2) -> ExpOrFieldList* {
  20. auto fields = new std::list<std::pair<std::string, Expression*>>();
  21. switch (e1->tag) {
  22. case ExpOrFieldListKind::Exp:
  23. fields->push_back(std::make_pair("", e1->u.exp));
  24. break;
  25. case ExpOrFieldListKind::FieldList:
  26. for (auto& field : *e1->u.fields) {
  27. fields->push_back(field);
  28. }
  29. break;
  30. }
  31. switch (e2->tag) {
  32. case ExpOrFieldListKind::Exp:
  33. fields->push_back(std::make_pair("", e2->u.exp));
  34. break;
  35. case ExpOrFieldListKind::FieldList:
  36. for (auto& field : *e2->u.fields) {
  37. fields->push_back(field);
  38. }
  39. break;
  40. }
  41. return MakeFieldList(fields);
  42. }
  43. } // namespace Carbon