function_definition.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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/function_definition.h"
  5. #include <iostream>
  6. namespace Carbon {
  7. auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
  8. Expression* param_pattern, Statement* body)
  9. -> struct FunctionDefinition* {
  10. auto* f = new struct FunctionDefinition();
  11. f->line_num = line_num;
  12. f->name = std::move(name);
  13. f->return_type = ret_type;
  14. f->param_pattern = param_pattern;
  15. f->body = body;
  16. return f;
  17. }
  18. void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
  19. std::cout << "fn " << f->name << " ";
  20. PrintExp(f->param_pattern);
  21. std::cout << " -> ";
  22. PrintExp(f->return_type);
  23. if (f->body) {
  24. std::cout << " {" << std::endl;
  25. PrintStatement(f->body, depth);
  26. std::cout << std::endl << "}" << std::endl;
  27. } else {
  28. std::cout << ";" << std::endl;
  29. }
  30. }
  31. void PrintFunDef(const FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
  32. } // namespace Carbon