|
|
@@ -2,6 +2,8 @@
|
|
|
// Exceptions. See /LICENSE for license information.
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
|
+#include "executable_semantics/main.h"
|
|
|
+
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#include <cstdio>
|
|
|
@@ -16,21 +18,15 @@
|
|
|
#include "executable_semantics/interpreter/exec_program.h"
|
|
|
#include "executable_semantics/syntax/parse.h"
|
|
|
#include "executable_semantics/syntax/prelude.h"
|
|
|
-#include "llvm/ADT/SmallString.h"
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
-#include "llvm/Support/ErrorOr.h"
|
|
|
-#include "llvm/Support/FileSystem.h"
|
|
|
#include "llvm/Support/InitLLVM.h"
|
|
|
-#include "llvm/Support/Path.h"
|
|
|
-#include "llvm/Support/Program.h"
|
|
|
|
|
|
-// Prints an error message and returns error code value.
|
|
|
-auto PrintError(const Carbon::Error& error) -> int {
|
|
|
- llvm::errs() << error.message() << "\n";
|
|
|
- return EXIT_FAILURE;
|
|
|
-}
|
|
|
+namespace Carbon {
|
|
|
+
|
|
|
+namespace cl = llvm::cl;
|
|
|
|
|
|
-auto main(int argc, char* argv[]) -> int {
|
|
|
+static auto Main(llvm::StringRef default_prelude_file, int argc, char* argv[])
|
|
|
+ -> ErrorOr<Success> {
|
|
|
llvm::setBugReportMsg(
|
|
|
"Please report issues to "
|
|
|
"https://github.com/carbon-language/carbon-lang/issues and include the "
|
|
|
@@ -41,48 +37,33 @@ auto main(int argc, char* argv[]) -> int {
|
|
|
// is piped to stdout.
|
|
|
llvm::errs().tie(&llvm::outs());
|
|
|
|
|
|
- using llvm::cl::desc;
|
|
|
- using llvm::cl::opt;
|
|
|
- opt<bool> trace_option("trace", desc("Enable tracing"));
|
|
|
- opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
|
|
|
- llvm::cl::Required);
|
|
|
+ cl::opt<bool> trace_option("trace", cl::desc("Enable tracing"));
|
|
|
+ cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
|
|
|
+ cl::Required);
|
|
|
|
|
|
// Find the path of the executable if possible and use that as a relative root
|
|
|
- // for finding the prelude. FIXME: Currently, this assumes a Bazel-like
|
|
|
- // runfiles tree rather than any kind of installation tree.
|
|
|
- llvm::SmallString<256> exe_path(argv[0]);
|
|
|
- if (!llvm::sys::fs::exists(exe_path)) {
|
|
|
- // Try to lookup the program name in the `PATH`.
|
|
|
- if (llvm::ErrorOr<std::string> path =
|
|
|
- llvm::sys::findProgramByName(exe_path)) {
|
|
|
- exe_path = *path;
|
|
|
- }
|
|
|
- }
|
|
|
- // If we have a valid path, find the parent directory. Otherwise, just use an
|
|
|
- // empty string to get the current working directory.
|
|
|
- if (llvm::sys::fs::exists(exe_path)) {
|
|
|
- llvm::sys::path::remove_filename(exe_path);
|
|
|
- } else {
|
|
|
- exe_path = "";
|
|
|
- }
|
|
|
- llvm::SmallString<256> prelude_path = exe_path;
|
|
|
- llvm::sys::path::append(prelude_path, "data/prelude.carbon");
|
|
|
- opt<std::string> prelude_file_name("prelude", desc("<prelude file>"),
|
|
|
- llvm::cl::init(prelude_path.str().str()));
|
|
|
+ cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
|
|
|
+ cl::init(default_prelude_file.str()));
|
|
|
+ cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
|
- llvm::cl::ParseCommandLineOptions(argc, argv);
|
|
|
-
|
|
|
- Carbon::Arena arena;
|
|
|
- Carbon::ErrorOr<Carbon::AST> ast =
|
|
|
- Carbon::Parse(&arena, input_file_name, trace_option);
|
|
|
- if (!ast.ok()) {
|
|
|
- return PrintError(ast.error());
|
|
|
- }
|
|
|
- AddPrelude(prelude_file_name, &arena, &ast->declarations);
|
|
|
+ Arena arena;
|
|
|
+ ASSIGN_OR_RETURN(AST ast, Parse(&arena, input_file_name, trace_option));
|
|
|
+ AddPrelude(prelude_file_name, &arena, &ast.declarations);
|
|
|
|
|
|
// Typecheck and run the parsed program.
|
|
|
- Carbon::ErrorOr<int> result = Carbon::ExecProgram(&arena, *ast, trace_option);
|
|
|
- if (!result.ok()) {
|
|
|
- return PrintError(result.error());
|
|
|
+ ASSIGN_OR_RETURN(int unused_return_code,
|
|
|
+ ExecProgram(&arena, ast, trace_option));
|
|
|
+ (void)unused_return_code;
|
|
|
+ return Success();
|
|
|
+}
|
|
|
+
|
|
|
+auto ExecutableSemanticsMain(llvm::StringRef default_prelude_file, int argc,
|
|
|
+ char** argv) -> int {
|
|
|
+ if (auto result = Main(default_prelude_file, argc, argv); !result.ok()) {
|
|
|
+ llvm::errs() << result.error().message() << "\n";
|
|
|
+ return EXIT_FAILURE;
|
|
|
}
|
|
|
+ return EXIT_SUCCESS;
|
|
|
}
|
|
|
+
|
|
|
+} // namespace Carbon
|