main.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include "executable_semantics/syntax/parser.h"
  8. #include "executable_semantics/syntax/syntax_helpers.h"
  9. #include "executable_semantics/tracing_flag.h"
  10. #include "llvm/Support/CommandLine.h"
  11. extern FILE* yyin;
  12. int main(int argc, char* argv[]) {
  13. // yydebug = 1;
  14. using llvm::cl::desc;
  15. using llvm::cl::opt;
  16. opt<bool> quiet_option("quiet", desc("Disable tracing"));
  17. opt<std::string> input_filename(llvm::cl::Positional, desc("<input file>"));
  18. llvm::cl::ParseCommandLineOptions(argc, argv);
  19. if (input_filename.getNumOccurrences() > 0) {
  20. Carbon::input_filename = input_filename.c_str();
  21. yyin = fopen(input_filename.c_str(), "r");
  22. if (yyin == nullptr) {
  23. std::cerr << "Error opening '" << input_filename
  24. << "': " << strerror(errno) << std::endl;
  25. return 1;
  26. }
  27. }
  28. if (quiet_option) {
  29. Carbon::tracing_output = false;
  30. }
  31. // No AST yet
  32. std::optional<Carbon::AST> parsedInput = std::nullopt;
  33. // Parse and handle syntax errors
  34. auto syntaxErrorCode = yyparse(parsedInput);
  35. if (syntaxErrorCode != 0) {
  36. return syntaxErrorCode;
  37. }
  38. if (parsedInput == std::nullopt) {
  39. std::cerr << "Internal error: parser validated syntax yet didn't produce "
  40. "an AST.\n";
  41. return 1;
  42. }
  43. // Typecheck and run the parsed program.
  44. Carbon::ExecProgram(*parsedInput);
  45. }