syntax_helpers.cpp 1.5 KB

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