syntax_helpers.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/syntax_helpers.h"
  5. #include <iostream>
  6. #include "executable_semantics/interpreter/interpreter.h"
  7. #include "executable_semantics/interpreter/typecheck.h"
  8. namespace Carbon {
  9. char* input_filename = nullptr;
  10. void PrintSyntaxError(char* error, int line_num) {
  11. std::cerr << input_filename << ":" << line_num << ": " << error << std::endl;
  12. exit(-1);
  13. }
  14. void ExecProgram(std::list<Declaration*>* fs) {
  15. std::cout << "********** source program **********" << std::endl;
  16. for (const auto& decl : *fs) {
  17. PrintDecl(decl);
  18. }
  19. std::cout << "********** type checking **********" << std::endl;
  20. state = new State(); // Compile-time state.
  21. std::pair<TypeEnv*, Env*> p = TopLevel(fs);
  22. TypeEnv* top = p.first;
  23. Env* ct_top = p.second;
  24. std::list<Declaration*> new_decls;
  25. for (const auto& i : *fs) {
  26. new_decls.push_back(TypeCheckDecl(i, top, ct_top));
  27. }
  28. std::cout << std::endl;
  29. std::cout << "********** type checking complete **********" << std::endl;
  30. for (const auto& decl : new_decls) {
  31. PrintDecl(decl);
  32. }
  33. std::cout << "********** starting execution **********" << std::endl;
  34. int result = InterpProgram(&new_decls);
  35. std::cout << "result: " << result << std::endl;
  36. }
  37. } // namespace Carbon